本帖最后由 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
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
4 \ K% C! x7 |! X/ r" A: L
4 A# g- B4 _/ B. U& P. m$ f
6 ]7 M& ]' D4 V; ^# a
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
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$ ]
, 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
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& ^
. 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
0 k) t$ U$ X8 V6 f4 v* a# ?
4 @ \7 k4 S7 }; H$ d2 M" V, J3 q/ C! R r, C$ J' c
局部放大,可以看到电机运行速度反馈丢失出错、清除错误、重启电机、重新建立速度反馈,正常工作。9 m6 N n# x0 Y1 {
( 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
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
3 B! X1 |, H7 S) D+ {) X8 p( z e& _. V8 Z& J! y' s
Start状态
4 H+ `" h @% ~
$ [9 @2 q; l3 |$ G# g
& C; ?! m" [+ d v- V! |1 }9 SFault Over状态3 i% t4 ?3 W3 S* I4 g5 @5 ~
/ {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
- typedef enum
8 Z( F8 e2 E- d2 S; M - {( P( p* p2 Q# e% W) B+ T
- ICLWAIT = 12, /*!< Persistent state, the system is waiting for ICL + C1 V: z6 W) D, @* F% J: V5 _
- deactivation. Is not possible to run the motor if
8 S4 X& B, s- W - ICL is active. Until the ICL is active the state is 2 [* Y- |6 N2 \" H9 u" h
- forced to ICLWAIT, when ICL become inactive the state
1 ?+ L- x }( E. T& }' F - is moved to IDLE */* J& D: Z& L5 x, M% n
- IDLE = 0, /*!< Persistent state, following state can be IDLE_START
4 N$ C# M3 z- {( s5 R( h7 u - if a start motor command has been given or & h. l* i& i3 e) @
- IDLE_ALIGNMENT if a start alignment command has been
! C' h( @. N4 ]8 ], G* u - given */
4 n4 Y" H+ H$ A4 f8 M - IDLE_ALIGNMENT = 1, /*!< "Pass-through" state containg the code to be executed - ~) ]7 L2 H" _8 j% z/ F
- only once after encoder alignment command.
7 C; Q$ O/ k/ V. l - Next states can be ALIGN_CHARGE_BOOT_CAP or
& Y8 @- ^% x6 {1 u8 P6 y- p - ALIGN_OFFSET_CALIB according the configuration. It 8 `: V. b1 X3 V( P3 g$ m* X1 w
- can also be ANY_STOP if a stop motor command has been
. p8 S5 r3 A7 w" T7 E, t$ k - given. */. b# }8 p. I6 \) f
- ALIGN_CHARGE_BOOT_CAP = 13,/*!< Persistent state where the gate driver boot 5 }$ j: ?, g Z% k0 \
- capacitors will be charged. Next states will be , F: F3 B' f: b' e% Y9 _5 k
- ALIGN_OFFSET_CALIB. It can also be ANY_STOP if a stop
) `/ i$ h6 u# b9 n - motor command has been given. */) V" e5 B( v {0 ~, x" P
- ALIGN_OFFSET_CALIB = 14,/*!< Persistent state where the offset of motor currents
7 h: [& `; Q, C3 w5 g+ f8 H - measurements will be calibrated. Next state will be
, ~+ \2 b0 P* P! s, F7 h - ALIGN_CLEAR. It can also be ANY_STOP if a stop motor
! u" t+ X. j- d2 ?& | - command has been given. */0 t7 {' j9 w. _ E- j: H4 j" u& }. K
- ALIGN_CLEAR = 15, /*!< "Pass-through" state in which object is cleared and
' H$ P8 x* J) y7 P - set for the startup.
# e0 y6 {6 [7 e2 c - Next state will be ALIGNMENT. It can also be ANY_STOP
3 u7 w4 L% l$ {( G2 C - if a stop motor command has been given. */: L: n9 O5 ^5 N* N8 M# w
- ALIGNMENT = 2, /*!< Persistent state in which the encoder are properly
1 W+ `; B1 s; V - aligned to set mechanical angle, following state can
& Z4 F( ?* |0 l! J- l& L, q5 U - only be ANY_STOP */+ m. ^; C: M1 W- ]6 J$ H4 S2 m8 \
- IDLE_START = 3, /*!< "Pass-through" state containg the code to be executed
- }9 t+ t$ Y' o. W - only once after start motor command. 3 v7 z% H4 F$ y1 @
- Next states can be CHARGE_BOOT_CAP or OFFSET_CALIB
7 g* `! h7 r$ R8 O/ x; _3 g - according the configuration. It can also be ANY_STOP 5 n* R% P: ?; z4 g% g
- if a stop motor command has been given. */
. A" n; ^) B( V# \+ @9 t, z - CHARGE_BOOT_CAP = 16, /*!< Persistent state where the gate driver boot : m v8 }- l) h; B6 d4 ]( V
- capacitors will be charged. Next states will be
$ n, D& H: F7 K# ^$ N - OFFSET_CALIB. It can also be ANY_STOP if a stop motor : G: `3 Y9 v3 J* i# [" A, O* J4 o
- command has been given. */+ ^9 k4 _$ Q+ z8 u% @8 g
- OFFSET_CALIB = 17, /*!< Persistent state where the offset of motor currents - B1 n$ r$ h8 C0 j4 _& a7 R
- measurements will be calibrated. Next state will be
& a2 y: n8 V/ k' v: v- A - CLEAR. It can also be ANY_STOP if a stop motor
. W2 M+ ]' e5 E - command has been given. */
! s! O! K2 t0 ^, S - CLEAR = 18, /*!< "Pass-through" state in which object is cleared and
, y9 ?- ]7 y! p2 `. K& u - set for the startup.4 R, ?9 p: V7 [/ r2 O
- Next state will be START. It can also be ANY_STOP if
P W" K6 [5 p3 u - a stop motor command has been given. */ J1 _3 l# ]( `' H* [. I
- START = 4, /*!< Persistent state where the motor start-up is intended $ _; G; \) O( P/ h6 Y1 T
- to be executed. The following state is normally " D- H' s+ _8 r/ O5 t
- START_RUN as soon as first validated speed is : R# W# z' {( O! v+ }0 ?( p
- detected. Another possible following state is 8 S+ l0 D! ^' ?$ G3 @9 j
- ANY_STOP if a stop motor command has been executed */
. @; K4 t; [0 s( u( w: I; q3 b - START_RUN = 5, /*!< "Pass-through" state, the code to be executed only 9 N+ r. e# Q' G. F# m: t5 L5 g
- once between START and RUN states it’s intended to be ) K- y$ c) F& j; y- W2 c
- here executed. Following state is normally RUN but
6 e$ s3 ]* x0 _! K - it can also be ANY_STOP if a stop motor command has $ r6 I4 Q+ k% ^( O7 j
- been given */
! H- d( C8 J( b/ M8 V - RUN = 6, /*!< Persistent state with running motor. The following . p* d5 h# l) |- \
- state is normally ANY_STOP when a stop motor command 3 H, x# v6 K6 [$ f9 m
- has been executed */
% ~/ b& S3 M% ^8 ~. c- ~ - ANY_STOP = 7, /*!< "Pass-through" state, the code to be executed only
7 u6 N) R+ U4 ], H+ k; n - once between any state and STOP it’s intended to be 6 {4 g2 z9 F! q* E8 x! d
- here executed. Following state is normally STOP */0 `; r7 P4 W: d4 J7 `, s
- STOP = 8, /*!< Persistent state. Following state is normally 0 T: |. K) A& @) }% {0 M
- STOP_IDLE as soon as conditions for moving state
* F+ ^3 ?! w& t: C% Q - machine are detected */: Y- x: X( `# ?
- STOP_IDLE = 9, /*!< "Pass-through" state, the code to be executed only8 R5 @- ]0 g) h% ]- ^' `1 c
- once between STOP and IDLE it’s intended to be here
& e$ `$ v4 l3 d$ c! x - executed. Following state is normally IDLE */; K) Y: [. {4 g$ K0 Y8 w$ z$ P2 ~3 T
- FAULT_NOW = 10, /*!< Persistent state, the state machine can be moved from
7 V& k2 G6 T1 `! a) g; [8 l - any condition directly to this state by
9 c2 N6 `2 t$ b F! D/ g3 F - STM_FaultProcessing method. This method also manage
3 f! r& R1 |& g. m" N; T' R0 R - the passage to the only allowed following state that
. X+ v9 d$ ?+ q1 {+ A7 ] T. w - is FAULT_OVER */# z: O9 I3 F' S7 p2 Q2 _# r" h* y
- FAULT_OVER = 11 /*!< Persistent state where the application is intended to6 P5 B6 F2 Y! I5 Q( T
- stay when the fault conditions disappeared. Following
" ^# U0 y7 s' J; M - state is normally STOP_IDLE, state machine is moved as 9 H7 p; ^+ B5 v" {
- soon as the user has acknowledged the fault condition.
+ k/ r4 t) U4 E. o) U - */! @1 M6 p& a: R6 H* G
- } 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)
|
多谢捧场
羡慕你的HP3457和吉时利2000