你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【STM32电机培训】——5、任务3电机状态切换、正反转

[复制链接]
七哥 发布时间:2018-9-11 02:02
本帖最后由 toofree 于 2018-9-14 08:46 编辑
9 j; ~  {' M1 h; Q7 S1 g& g* j& ^* d3 A' w' g. L1 e1 O
【STM32电机培训】——5、任务3电机状态切换、正反转" b) f  J, a6 i
: W5 h/ a- e0 \0 e
+ \" t7 h: }7 h0 Y+ x" S) e

: r. J% t. @7 i! h任务目标:
& d3 x( k  e( \$ {9 R
) Z9 C/ _3 ~. m) R5 |6 m

& R1 }. N$ i8 p4 }# c' u2 n& P  u 01.jpg
  v( r! V4 ?0 h" m( `  t+ z; @0 I0 N, x8 C
* `8 D0 A* v1 |- t5 F1 \
. V& u2 C( n7 U; h
# T( }4 F9 s) n3 L- V
02.jpg 4 \  K% C! x7 |! X/ r" A: L
4 A# g- B4 _/ B. U& P. m$ f
6 ]7 M& ]' D4 V; ^# a
03.jpg
6 L. |( _+ s7 F6 y& f: t2 I, T
  r  Q2 X9 A/ u% c

6 I  a# @. n' s: n' W1 q% m  V0 p" i0 {; Q; H' @

$ }+ H( E" E- i( w1 z3 q 04.jpg
4 D) @- C& [" L6 S8 x0 l* O9 c0 c3 o- i9 T

+ j& l. b$ Z  \) f7 [& C0 p) h  T4 }: ~& G
* u6 l; ^' g1 N2 D; Q8 d
本实验源程序基于上一贴,做了适当修改。- g7 ?1 r7 S  {, F* Z8 p4 v) t1 X
【STM32电机培训】——4、任务2电机设定程序修改PI参数' A, |- m8 L: L9 H9 z! r+ P
https://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=617433&fromuid=3072973 Z. G- o* @) ~+ E' T- [% b
(出处: 意法半导体STM32/STM8技术社区)/ N) \' ], P! c& w

7 E, C% m3 M0 x  P- i/ b# d/ d( L2 A4 r
/ E0 Q* A2 `# C( c$ F! D
新增包含了头文件“"SystemNDriveParams.h",新增了外部变量声明“extern STM_Handle_t STM[NBR_OF_MOTORS]”,电机正、反转速度设定做成了宏定义“INVERT”、“NONINVT”。
5 Q, d! ?9 J/ K9 A$ N6 o& S$ ] 10.jpg , L6 E0 \! g8 x2 @% J

+ Q: L9 E! s: F% G: `$ ?
" W: C' d( s9 Q1 P+ m" E: [. s! ]
用户主程序,可以分为5块。初始状态设定、出错处理(详见注释)、周期性正反转切换、时基控制(定时0.1s)、LED闪灯灯。现在唯一有疑问的就是,这个“错误状态”反馈怎么玩,反馈给谁?对于本实验的意义,是想知道详细出错信息吗?
2 k* s, v" A) I5 }& {# P
11.jpg
9 J8 ]8 h* Y% P6 X, Q7 Y* t3 k. e! J2 H5 w! b0 k3 h6 p# q# k0 T

, j* ~4 X6 ~" Y! f2 Z下载程序到开发套件中。打开Workbench界面,并与目标板套件连接% ^! k5 u* u. D& d& ^
19.jpg
. D  a8 Z5 ?- J5 S+ v' {/ v! x9 x/ ~! M! W- l
5 j8 q; m3 W4 G1 \8 x. ~  W7 g
“监控”窗口下,在打开“Plotter”绘图窗口。观察电机速度状态变化% x  e: C( K( r' b8 w9 u
20.jpg 0 k) t$ U$ X8 V6 f4 v* a# ?

4 @  \7 k4 S7 }; H$ d
2 M" V, J3 q/ C! R  r, C$ J' c
局部放大,可以看到电机运行速度反馈丢失出错、清除错误、重启电机、重新建立速度反馈,正常工作。9 m6 N  n# x0 Y1 {
21.jpg ( d, d: v/ m" _" b
% H" ?$ B5 q7 p3 D* ^4 Q

+ d& g1 o, ?5 E# ?Workbench界面观察电机状态变化,一共出现过5种状态:Run、Idle、Start、Fault Over、Unknow,但是Unknow时间太短,未能抓图捕获
' \" A4 Z; U) \" S/ C: H
& Y4 Q9 o4 [+ ]1 G7 Z( v

, R+ [* H. b6 Q. \. c& E/ dRun状态8 _! Q8 }$ C+ O/ _2 o9 L" L. C
22.jpg
4 t0 |2 J% w" B4 _7 v4 e1 I
. z+ [  Y2 O1 [0 g9 C" c
. e1 H, r0 ^, Y+ Y6 B
Idle状态
0 S: D# t5 t9 P' I* L& i 24.jpg
3 B! X1 |, H7 S) D
+ {) X8 p( z  e& _. V8 Z& J! y' s
Start状态
4 H+ `" h  @% ~ 23.jpg $ [9 @2 q; l3 |$ G# g

& C; ?! m" [+ d  v- V! |1 }9 SFault Over状态3 i% t4 ?3 W3 S* I4 g5 @5 ~
25.jpg
/ {0 X2 X' b" c* ]0 M. w
% |1 a( b2 s# A) {% |

9 ?+ W! y7 Y$ ?8 T从State_t的枚举类型定义来看,状态还有好多种。如果要搞明白,需要进一步研究。  j) U$ `2 S) k6 E: c0 k1 Q- i
  1. typedef enum
    8 Z( F8 e2 E- d2 S; M
  2. {( P( p* p2 Q# e% W) B+ T
  3. ICLWAIT = 12,         /*!< Persistent state, the system is waiting for ICL + C1 V: z6 W) D, @* F% J: V5 _
  4.                            deactivation. Is not possible to run the motor if
    8 S4 X& B, s- W
  5.                            ICL is active. Until the ICL is active the state is 2 [* Y- |6 N2 \" H9 u" h
  6.                            forced to ICLWAIT, when ICL become inactive the state
    1 ?+ L- x  }( E. T& }' F
  7.                            is moved to IDLE */* J& D: Z& L5 x, M% n
  8. IDLE = 0,             /*!< Persistent state, following state can be IDLE_START
    4 N$ C# M3 z- {( s5 R( h7 u
  9.                            if a start motor command has been given or & h. l* i& i3 e) @
  10.                            IDLE_ALIGNMENT if a start alignment command has been
    ! C' h( @. N4 ]8 ], G* u
  11.                            given */
    4 n4 Y" H+ H$ A4 f8 M
  12. IDLE_ALIGNMENT = 1,   /*!< "Pass-through" state containg the code to be executed - ~) ]7 L2 H" _8 j% z/ F
  13.                            only once after encoder alignment command.
    7 C; Q$ O/ k/ V. l
  14.                            Next states can be ALIGN_CHARGE_BOOT_CAP or
    & Y8 @- ^% x6 {1 u8 P6 y- p
  15.                            ALIGN_OFFSET_CALIB according the configuration. It 8 `: V. b1 X3 V( P3 g$ m* X1 w
  16.                            can also be ANY_STOP if a stop motor command has been
    . p8 S5 r3 A7 w" T7 E, t$ k
  17.                            given. */. b# }8 p. I6 \) f
  18. ALIGN_CHARGE_BOOT_CAP = 13,/*!< Persistent state where the gate driver boot 5 }$ j: ?, g  Z% k0 \
  19.                            capacitors will be charged. Next states will be , F: F3 B' f: b' e% Y9 _5 k
  20.                            ALIGN_OFFSET_CALIB. It can also be ANY_STOP if a stop
    ) `/ i$ h6 u# b9 n
  21.                            motor command has been given. */) V" e5 B( v  {0 ~, x" P
  22. ALIGN_OFFSET_CALIB = 14,/*!< Persistent state where the offset of motor currents
    7 h: [& `; Q, C3 w5 g+ f8 H
  23.                            measurements will be calibrated. Next state will be
    , ~+ \2 b0 P* P! s, F7 h
  24.                            ALIGN_CLEAR. It can also be ANY_STOP if a stop motor
    ! u" t+ X. j- d2 ?& |
  25.                            command has been given. */0 t7 {' j9 w. _  E- j: H4 j" u& }. K
  26. ALIGN_CLEAR = 15,     /*!< "Pass-through" state in which object is cleared and
    ' H$ P8 x* J) y7 P
  27.                            set for the startup.
    # e0 y6 {6 [7 e2 c
  28.                            Next state will be ALIGNMENT. It can also be ANY_STOP
    3 u7 w4 L% l$ {( G2 C
  29.                            if a stop motor command has been given. */: L: n9 O5 ^5 N* N8 M# w
  30. ALIGNMENT = 2,        /*!< Persistent state in which the encoder are properly
    1 W+ `; B1 s; V
  31.                            aligned to set mechanical angle, following state can
    & Z4 F( ?* |0 l! J- l& L, q5 U
  32.                            only be ANY_STOP */+ m. ^; C: M1 W- ]6 J$ H4 S2 m8 \
  33. IDLE_START = 3,       /*!< "Pass-through" state containg the code to be executed
    - }9 t+ t$ Y' o. W
  34.                            only once after start motor command. 3 v7 z% H4 F$ y1 @
  35.                            Next states can be CHARGE_BOOT_CAP or OFFSET_CALIB
    7 g* `! h7 r$ R8 O/ x; _3 g
  36.                            according the configuration. It can also be ANY_STOP 5 n* R% P: ?; z4 g% g
  37.                            if a stop motor command has been given. */
    . A" n; ^) B( V# \+ @9 t, z
  38. CHARGE_BOOT_CAP = 16, /*!< Persistent state where the gate driver boot : m  v8 }- l) h; B6 d4 ]( V
  39.                            capacitors will be charged. Next states will be
    $ n, D& H: F7 K# ^$ N
  40.                            OFFSET_CALIB. It can also be ANY_STOP if a stop motor : G: `3 Y9 v3 J* i# [" A, O* J4 o
  41.                            command has been given. */+ ^9 k4 _$ Q+ z8 u% @8 g
  42. OFFSET_CALIB = 17,    /*!< Persistent state where the offset of motor currents - B1 n$ r$ h8 C0 j4 _& a7 R
  43.                            measurements will be calibrated. Next state will be
    & a2 y: n8 V/ k' v: v- A
  44.                            CLEAR. It can also be ANY_STOP if a stop motor
    . W2 M+ ]' e5 E
  45.                            command has been given. */
    ! s! O! K2 t0 ^, S
  46. CLEAR = 18,           /*!< "Pass-through" state in which object is cleared and
    , y9 ?- ]7 y! p2 `. K& u
  47.                            set for the startup.4 R, ?9 p: V7 [/ r2 O
  48.                            Next state will be START. It can also be ANY_STOP if
      P  W" K6 [5 p3 u
  49.                            a stop motor command has been given. */  J1 _3 l# ]( `' H* [. I
  50. START = 4,            /*!< Persistent state where the motor start-up is intended $ _; G; \) O( P/ h6 Y1 T
  51.                            to be executed. The following state is normally " D- H' s+ _8 r/ O5 t
  52.                            START_RUN as soon as first validated speed is : R# W# z' {( O! v+ }0 ?( p
  53.                            detected. Another possible following state is 8 S+ l0 D! ^' ?$ G3 @9 j
  54.                            ANY_STOP if a stop motor command has been executed */
    . @; K4 t; [0 s( u( w: I; q3 b
  55. START_RUN = 5,        /*!< "Pass-through" state, the code to be executed only 9 N+ r. e# Q' G. F# m: t5 L5 g
  56.                            once between START and RUN states it’s intended to be ) K- y$ c) F& j; y- W2 c
  57.                            here executed. Following state is normally  RUN but
    6 e$ s3 ]* x0 _! K
  58.                            it can also be ANY_STOP  if a stop motor command has $ r6 I4 Q+ k% ^( O7 j
  59.                            been given */
    ! H- d( C8 J( b/ M8 V
  60. RUN = 6,              /*!< Persistent state with running motor. The following . p* d5 h# l) |- \
  61.                            state is normally ANY_STOP when a stop motor command 3 H, x# v6 K6 [$ f9 m
  62.                            has been executed */
    % ~/ b& S3 M% ^8 ~. c- ~
  63. ANY_STOP = 7,         /*!< "Pass-through" state, the code to be executed only
    7 u6 N) R+ U4 ], H+ k; n
  64.                            once between any state and STOP it’s intended to be 6 {4 g2 z9 F! q* E8 x! d
  65.                            here executed. Following state is normally STOP */0 `; r7 P4 W: d4 J7 `, s
  66. STOP = 8,             /*!< Persistent state. Following state is normally 0 T: |. K) A& @) }% {0 M
  67.                            STOP_IDLE as soon as conditions for moving state
    * F+ ^3 ?! w& t: C% Q
  68.                            machine are detected */: Y- x: X( `# ?
  69. STOP_IDLE = 9,        /*!< "Pass-through" state, the code to be executed only8 R5 @- ]0 g) h% ]- ^' `1 c
  70.                            once between STOP and IDLE it’s intended to be here
    & e$ `$ v4 l3 d$ c! x
  71.                            executed. Following state is normally IDLE */; K) Y: [. {4 g$ K0 Y8 w$ z$ P2 ~3 T
  72. FAULT_NOW = 10,       /*!< Persistent state, the state machine can be moved from
    7 V& k2 G6 T1 `! a) g; [8 l
  73.                            any condition directly to this state by
    9 c2 N6 `2 t$ b  F! D/ g3 F
  74.                            STM_FaultProcessing method. This method also manage
    3 f! r& R1 |& g. m" N; T' R0 R
  75.                            the passage to the only allowed following state that
    . X+ v9 d$ ?+ q1 {+ A7 ]  T. w
  76.                            is FAULT_OVER */# z: O9 I3 F' S7 p2 Q2 _# r" h* y
  77. FAULT_OVER = 11       /*!< Persistent state where the application is intended to6 P5 B6 F2 Y! I5 Q( T
  78.                           stay when the fault conditions disappeared. Following
    " ^# U0 y7 s' J; M
  79.                           state is normally STOP_IDLE, state machine is moved as 9 H7 p; ^+ B5 v" {
  80.                           soon as the user has acknowledged the fault condition.
    + k/ r4 t) U4 E. o) U
  81.                       */! @1 M6 p& a: R6 H* G
  82. } State_t;  , o; E" }5 \* r
复制代码

" L4 X' y+ C5 W
( ?/ j( O5 E6 e7 G6 O
3 J$ I) P' s2 x  A9 i
好了,本贴任务3完成。
" g* T/ v, d5 q& B. a% K) L  _% h9 m1 T$ v" ^" g1 ?
按照惯例,程序附上:
$ P- }; a/ b, E1 d+ F P-NUCLEO-IHM001_ zhengfan.rar (8.99 MB, 下载次数: 58)
收藏 1 评论3 发布时间:2018-9-11 02:02

举报

3个回答
ersha4877 回答时间:2018-9-11 09:46:04
正好有块板子,等着慢慢学习
七哥 回答时间:2018-9-11 10:09:58
ersha4877 发表于 2018-9-11 09:46
) c+ {) }8 c; G正好有块板子,等着慢慢学习

5 E$ ?; ^3 I" ]多谢捧场
2 G& R& P0 ~6 g( z羡慕你的HP3457和吉时利2000
STM1024 回答时间:2018-9-11 13:08:26
我也想知道,这个肯定是采用的串口通讯,但是格式具体是怎样的,ST似乎并没有公布,在代码中看不到

所属标签

相似分享

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版