本帖最后由 toofree 于 2018-9-14 08:46 编辑 * r: ^3 B# n) d6 j4 B( Z& c
* S# H; z$ j" ~
【STM32电机培训】——5、任务3电机状态切换、正反转0 j7 {: k R9 ^" D; `8 `
* F0 ^- L; F( S: J! r; {! T( D+ }1 i" a3 o* e" b
' ]' R% n4 a& k# o' C) v任务目标:9 g4 W- \- d8 p) @; j
" g+ w' K3 E2 y a) Y5 [4 ]! t
/ A1 ^( k# o3 m0 ~6 N
* o0 Z! E6 [$ j" t- A8 S. e! w u4 p
7 ~. I4 Z' V& G+ `) P$ d8 v
+ s1 B, @& k3 }: X( A3 `2 K# \3 O2 x, Q, [
, A; F' r( [ J% W4 z
6 |% `0 B* d9 M( B6 U, C" s0 ?4 Q( u: ]( ^1 b5 ?
0 |2 q4 ^7 ?. T: ^
5 A" s& m2 E) ~: z/ J7 Q
5 q6 F. b' k2 l. j- S, {) o5 p
: t% ^* L8 X6 m4 C/ v' P9 S. i, ^5 ]" F9 L+ W
3 ]# x1 R% [: |
6 O" h* O" E* u, X k1 ?5 [. L' {8 p6 m
! I: O5 @3 G* |- i, E* v$ q/ H. G6 K. p( H5 p
, E3 \' Y4 O# }- ~0 F4 c* H
1 n0 K& l: Z0 k' t( e, ]本实验源程序基于上一贴,做了适当修改。9 _$ Z2 z( Q8 A" y4 I1 n3 s. o
【STM32电机培训】——4、任务2电机设定程序修改PI参数 W4 O& |4 f* X* R# [9 l
https://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=617433&fromuid=307297
& U7 d3 S& b2 s3 j' s% E(出处: 意法半导体STM32/STM8技术社区)
9 U7 u9 Y: c- @1 t
& _3 [5 G5 d P+ x; J1 A. _
~6 @3 j8 H" }2 ?- m I6 N; e" p6 N* s
新增包含了头文件“"SystemNDriveParams.h",新增了外部变量声明“extern STM_Handle_t STM[NBR_OF_MOTORS]”,电机正、反转速度设定做成了宏定义“INVERT”、“NONINVT”。
% S6 ]6 f% J2 G0 L {
9 J" Q% b: ]5 e2 t, h
- [+ c3 r1 T3 I- v7 d
! j0 E& J, w, j: d- \: s用户主程序,可以分为5块。初始状态设定、出错处理(详见注释)、周期性正反转切换、时基控制(定时0.1s)、LED闪灯灯。现在唯一有疑问的就是,这个“错误状态”反馈怎么玩,反馈给谁?对于本实验的意义,是想知道详细出错信息吗?: P5 V0 d A3 P) n; j8 p% D/ q0 b
( m; ?6 C- j1 a5 L) _- a6 t% E& d
2 o7 \) k/ M" @* J下载程序到开发套件中。打开Workbench界面,并与目标板套件连接
, i) q. R6 [* e' g1 C
& a! a( [' d) K$ z" x- s7 q* o4 w7 z; h$ O ?; K
0 O9 x) [( i/ a( |
在“监控”窗口下,在打开“Plotter”绘图窗口。观察电机速度状态变化: m+ [! O/ R1 |$ A3 e
( |- a0 | e7 A: ~, O
8 U+ B: V! N5 N X, q( b5 Z' N
+ v0 a+ q! \/ \/ w4 Z5 V k
局部放大,可以看到电机运行速度反馈丢失出错、清除错误、重启电机、重新建立速度反馈,正常工作。7 d3 Y' h& l. x$ E! [6 W
) R; K/ }$ l: Y& C9 u) O9 {
+ U# V6 [6 M( w/ _; J
2 c* F9 s% v$ G, N+ h1 b& G在Workbench界面观察电机状态变化,一共出现过5种状态:Run、Idle、Start、Fault Over、Unknow,但是Unknow时间太短,未能抓图捕获。
) s2 b& P- V- e" o; j% y
& I9 ?2 Y0 E$ k& E3 j, L9 x( p- y, U
Run状态
( z/ J* O) m# M" u* N
" o3 X* H$ l, r2 y% h
8 s$ @: O4 G* d' v* J9 P, Z3 |% N- E- w$ V
Idle状态
- x) N4 V2 G- N: O3 t+ b- g
- [ r; Q. Y1 \9 p
/ b* u% k. t& z0 G2 e. Z1 lStart状态
8 j/ Q( k- e, y4 e
& D& f7 J6 C6 \/ [" }, D3 x; {9 M
( J$ D7 J" f# f; {! m/ p( HFault Over状态
# I, n2 z" O! }3 v! ?: U% g
, d* ^5 k6 U. m8 L3 Y. t$ u6 H1 p. F! l5 c% o$ ?5 N) I+ c
0 A3 d' V& q* T* D* I7 E( b2 d- e5 ?
从State_t的枚举类型定义来看,状态还有好多种。如果要搞明白,需要进一步研究。4 A Y" [, F9 I4 _4 ?- q
- typedef enum7 b3 X0 ~5 D8 R( U1 F- f k8 G
- {) b, ]" u5 r) k$ W7 ^5 T& j% E
- ICLWAIT = 12, /*!< Persistent state, the system is waiting for ICL
0 g v8 A6 z" u0 `& [ - deactivation. Is not possible to run the motor if ( M1 {. x) R: J" }6 u
- ICL is active. Until the ICL is active the state is : G' D; ]/ K( Y& ^6 ?2 @
- forced to ICLWAIT, when ICL become inactive the state ' D, z1 L% U4 e8 P' X. w! |% g
- is moved to IDLE */- g9 t) Y' z1 Z( n8 q' U
- IDLE = 0, /*!< Persistent state, following state can be IDLE_START
/ B5 c. M$ Y, l' t' ?$ q3 A6 ^ - if a start motor command has been given or
: L( D+ v/ C! n) g/ x K' ?2 s, d - IDLE_ALIGNMENT if a start alignment command has been
4 x# @; F c( o( K - given */
' F0 `' d& I& U$ x2 w - IDLE_ALIGNMENT = 1, /*!< "Pass-through" state containg the code to be executed / w5 t+ `4 {: i' P3 l4 J& k
- only once after encoder alignment command.
+ n( D( V. X- T3 r6 S" m0 ]5 r - Next states can be ALIGN_CHARGE_BOOT_CAP or
; a6 U8 R( t( Q( d2 I# n - ALIGN_OFFSET_CALIB according the configuration. It % g/ `" \! D# T
- can also be ANY_STOP if a stop motor command has been 5 A# K6 a) D1 {1 d8 I
- given. */
6 }+ h5 ]4 w% w- |5 B: } - ALIGN_CHARGE_BOOT_CAP = 13,/*!< Persistent state where the gate driver boot $ z. c) B6 {+ ^( T! H6 E$ E
- capacitors will be charged. Next states will be + N. |) `3 t' e( y( U
- ALIGN_OFFSET_CALIB. It can also be ANY_STOP if a stop - X8 g* @: j: Q; P! Q& \
- motor command has been given. */
. a7 B ]1 e3 n( s% j# J - ALIGN_OFFSET_CALIB = 14,/*!< Persistent state where the offset of motor currents
. H4 M, U8 q$ ` I - measurements will be calibrated. Next state will be 5 Y( g- y, p' H- V: L6 d$ ~2 t
- ALIGN_CLEAR. It can also be ANY_STOP if a stop motor 5 p$ K9 j5 N# z" G9 F& |% [
- command has been given. */5 a/ B) m# x }& a! d! K9 v1 ?
- ALIGN_CLEAR = 15, /*!< "Pass-through" state in which object is cleared and
- f8 E/ U8 \% ]* N9 v; _/ K - set for the startup.
" e+ k' \, d* L, Z( s2 [' a+ n - Next state will be ALIGNMENT. It can also be ANY_STOP
+ _& f& _8 t+ ]! G - if a stop motor command has been given. */6 M1 t- c* F) V0 @6 y
- ALIGNMENT = 2, /*!< Persistent state in which the encoder are properly
& y) U5 R+ C, _6 F - aligned to set mechanical angle, following state can
7 k8 U, o& v1 y2 z: E7 y - only be ANY_STOP */ f5 c1 q& m- e9 K
- IDLE_START = 3, /*!< "Pass-through" state containg the code to be executed
% w9 W8 u, w2 `7 p% J; b! ^ - only once after start motor command. `( k9 M7 W7 `4 P: ~
- Next states can be CHARGE_BOOT_CAP or OFFSET_CALIB
' ^: b" c6 D6 k" M - according the configuration. It can also be ANY_STOP / V9 F. t+ X$ Y" E/ Z' p- c, ^( m
- if a stop motor command has been given. */8 U" K8 W/ l3 S
- CHARGE_BOOT_CAP = 16, /*!< Persistent state where the gate driver boot 2 L, A+ o- m8 N' f# l8 z! C
- capacitors will be charged. Next states will be , E ]7 G7 P$ c+ u# D W) z
- OFFSET_CALIB. It can also be ANY_STOP if a stop motor
7 L3 g8 k8 I) g7 f7 L @! A - command has been given. */# K& _. q0 g+ D$ Y( r3 ?
- OFFSET_CALIB = 17, /*!< Persistent state where the offset of motor currents 8 k0 s3 u& j5 F6 s3 o
- measurements will be calibrated. Next state will be
7 w" L }) I/ ?5 R' }8 b1 I" t1 U - CLEAR. It can also be ANY_STOP if a stop motor
% U" C# z* O w# U. N - command has been given. */% }( ]- K! W/ c
- CLEAR = 18, /*!< "Pass-through" state in which object is cleared and 4 \7 O5 h* w( n3 e& |# l9 K
- set for the startup.
* N* A" h: w. N3 D1 g4 ` - Next state will be START. It can also be ANY_STOP if $ X, h7 l9 H$ O' r& Z
- a stop motor command has been given. */ q+ q* X/ y% y. N3 a
- START = 4, /*!< Persistent state where the motor start-up is intended
7 I( P% m: f$ A! E7 A - to be executed. The following state is normally
0 s+ J8 j& C3 v7 J0 e- G. ` - START_RUN as soon as first validated speed is
+ b# L& p( I# ? a5 b2 Y# [" X - detected. Another possible following state is
2 E; I' w3 y* _1 G! ?( x - ANY_STOP if a stop motor command has been executed */7 x% z/ N- g ~/ C! t& M2 f
- START_RUN = 5, /*!< "Pass-through" state, the code to be executed only # `8 a. r4 K" t" E: d" d
- once between START and RUN states it’s intended to be 6 @' b O. Z6 x3 d
- here executed. Following state is normally RUN but ! O+ n% y# N; S: w
- it can also be ANY_STOP if a stop motor command has
4 T8 {4 t$ {# ?- j. ^: }/ E - been given */. V+ Q; E1 f. S1 x
- RUN = 6, /*!< Persistent state with running motor. The following ; M% g& g& e. T* u) \ b+ g
- state is normally ANY_STOP when a stop motor command $ t/ ]7 C8 K& `, s7 ~( [1 v$ g4 O9 y
- has been executed */0 L) S. ^6 Q5 F, |) D4 N" }
- ANY_STOP = 7, /*!< "Pass-through" state, the code to be executed only 1 |" j2 D$ `1 h
- once between any state and STOP it’s intended to be
7 e4 N/ K" `: [. l; w' z, C - here executed. Following state is normally STOP */
) w6 P" u* [( \. Q/ M! _ - STOP = 8, /*!< Persistent state. Following state is normally |, p- R, ] ^6 c. }
- STOP_IDLE as soon as conditions for moving state
3 D; I1 \/ {9 {& ]$ y. ^ - machine are detected */0 t9 O6 v/ O" B) X/ f5 R3 Z- R
- STOP_IDLE = 9, /*!< "Pass-through" state, the code to be executed only5 m- b. L+ p% w. T, c6 z
- once between STOP and IDLE it’s intended to be here
0 A, {' V. l* s. w4 Z - executed. Following state is normally IDLE */, O0 p' g, J" b+ ~. t
- FAULT_NOW = 10, /*!< Persistent state, the state machine can be moved from
% D: A# q4 p M% Y# Q3 q5 R - any condition directly to this state by
X3 U4 p2 z+ a `6 T- `( n' W2 x, P - STM_FaultProcessing method. This method also manage
! I! k. m. C4 c - the passage to the only allowed following state that
1 K1 y7 @( a4 Z& _. }# ?) x. L - is FAULT_OVER */+ Y, T( u2 |/ g7 v! v
- FAULT_OVER = 11 /*!< Persistent state where the application is intended to
9 o/ f- Q" R+ h, z3 C - stay when the fault conditions disappeared. Following
+ P1 R( Y* E' t) R+ B) x% L - state is normally STOP_IDLE, state machine is moved as , K6 d9 ^' c; Y
- soon as the user has acknowledged the fault condition.
7 J- V$ R0 O* o, Z# @ - */5 F# p* c5 R/ V; }, \ D
- } State_t;
% X/ W+ W/ y, K
复制代码
- K* P- A$ s% |( q
/ ?* k% _7 k* n- N4 e* C9 M
7 n5 G5 y. c4 ]0 c! h0 T6 h好了,本贴任务3完成。
6 ^0 k, W0 l0 V/ j% p' c1 M1 a& p% }9 L1 x5 H4 t
按照惯例,程序附上:; Q9 L3 X. ]! u; Y% `
P-NUCLEO-IHM001_ zhengfan.rar
(8.99 MB, 下载次数: 60)
|
多谢捧场
羡慕你的HP3457和吉时利2000