本帖最后由 toofree 于 2018-9-14 08:46 编辑 * W3 G, ]' q3 Q! H" A4 Y
- z5 M/ U! {. X! ?& b
【STM32电机培训】——5、任务3电机状态切换、正反转
. k5 N6 S7 g+ s+ ~% P: L0 H
) X" p! z7 ~, L
/ L% P7 r$ v0 `3 M0 R0 k! U0 h3 {6 g: i# X! ~
任务目标:. [5 a# N1 y6 m
8 O$ _. l" O! Y7 b0 Z9 Q- ^7 C2 o8 }" Z
1 n; W2 [# L0 I3 l9 h9 I! v
4 r9 u9 t3 G6 v/ R' X
* N! q$ D3 @9 l8 ]; ~+ y
" {1 ~ {' n1 r3 M) ^
9 H4 _. q$ A) q- u
2 ~# M" I9 A; z- d
, ]5 w. \) [6 w( E& H* x( C
; [( a6 \+ s B7 R7 w# t5 C t9 M. {; V5 \* ^. t3 Q
! U5 ?7 y: ~- p4 H% F
% E+ S. K2 R( N% k+ W
9 m, ~ S+ l5 O: B0 e2 _6 G0 W: H: M' u, R2 E2 Z# W! F' `) _: F$ J
4 |/ N* ]4 e" m$ o
+ A. Q$ Y. A& c& R
# H6 u! O: H. K) l8 A, N2 h7 E# E, A
% K0 Y6 R! D7 _. |! ?
1 W, y- m" x9 {6 B1 ~! o3 q/ ]本实验源程序基于上一贴,做了适当修改。
8 o3 r3 i L( _% Q8 l【STM32电机培训】——4、任务2电机设定程序修改PI参数
# k0 r: m; |! B* z, ]https://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=617433&fromuid=307297
' A/ j. l. l1 ?$ y(出处: 意法半导体STM32/STM8技术社区)
5 h5 p. E: w( B, V$ X8 |6 V& G5 a- F2 N; z' \9 s/ }) D$ j
6 q5 K5 T3 j' [& K: ]: O
- ]0 J$ X( A: t: M
新增包含了头文件“"SystemNDriveParams.h",新增了外部变量声明“extern STM_Handle_t STM[NBR_OF_MOTORS]”,电机正、反转速度设定做成了宏定义“INVERT”、“NONINVT”。, P( c& I6 ]8 p6 {# i- q0 e
2 q6 \5 ~. H* g/ t* H- s4 `
1 [, K8 [- ^, ~& f, U2 L. B
! [7 _+ @# F4 _+ z2 m9 C用户主程序,可以分为5块。初始状态设定、出错处理(详见注释)、周期性正反转切换、时基控制(定时0.1s)、LED闪灯灯。现在唯一有疑问的就是,这个“错误状态”反馈怎么玩,反馈给谁?对于本实验的意义,是想知道详细出错信息吗?, I9 W2 ]% q% G7 r" h8 _1 C2 N
1 ~) R( Q3 y6 ~. q, M
6 G( P0 a! o3 h' D4 K- z2 g
9 [! z* I2 y9 M' n! P E
下载程序到开发套件中。打开Workbench界面,并与目标板套件连接* {$ Y* W- Z6 P8 K3 Q* E% d9 g
! `4 x* @' {0 ]& \& k3 }7 `
" V) j( b" i6 G6 `3 R
9 G, S* M5 U6 \; `$ A p6 C9 t% V+ V) f在“监控”窗口下,在打开“Plotter”绘图窗口。观察电机速度状态变化
8 S3 n2 |: S3 t
: l& W) G: Y6 I7 H6 A: [# }
" W9 r- N& x. V/ ]: y4 [
9 c K9 L. ? a1 S( S* H局部放大,可以看到电机运行速度反馈丢失出错、清除错误、重启电机、重新建立速度反馈,正常工作。+ l9 I+ W5 J$ t6 t, b* P: J6 H5 A
5 [$ E/ W2 B4 s0 B# f) V, d- F1 ?
* {% `6 k, [( ?
$ Y" m/ f' d+ }! O在Workbench界面观察电机状态变化,一共出现过5种状态:Run、Idle、Start、Fault Over、Unknow,但是Unknow时间太短,未能抓图捕获。2 i3 M$ l0 G9 d) v
& C: ^" {# D8 Q9 b
9 Y; v7 d& A3 t9 k0 Q1 NRun状态
) Y' q" |4 K/ B. `8 @! D [
' D9 a! l9 u: h/ U" b. }3 W+ f& }7 o5 E3 b( n: e
1 q. `7 {" ?) H# ?Idle状态# t! m- ], T- ?# K0 O8 l) B
9 u1 \/ N$ }3 w$ x1 r- h7 D
7 j# g @1 y& g2 n D
Start状态
+ ]" _2 i1 k) Y; I, Y4 j2 \
- \" J! I$ x8 `6 v+ \3 b
% B3 J' u" L: J, p0 t* W" j
Fault Over状态# `6 ^+ S6 i' {: C6 q+ L: _/ [
6 Z5 N0 \6 m: H; R1 l& b% Q
1 E" q, L4 u2 r, v/ E& U! _ e; E& A) j( \+ i
从State_t的枚举类型定义来看,状态还有好多种。如果要搞明白,需要进一步研究。
1 L2 y* @+ s6 o' p) s9 D- typedef enum+ V$ p& p5 e/ _
- {
. W& h0 _. h& T - ICLWAIT = 12, /*!< Persistent state, the system is waiting for ICL
/ i$ R4 C4 f: y& x- s" c+ P4 z - deactivation. Is not possible to run the motor if
) _, E4 l' \( l+ C - ICL is active. Until the ICL is active the state is - ~2 C* u; S& U$ E: O1 ^3 H
- forced to ICLWAIT, when ICL become inactive the state
% T3 H$ u. @! d$ l8 O, c - is moved to IDLE */- g( Q4 _7 A/ ~7 f9 _
- IDLE = 0, /*!< Persistent state, following state can be IDLE_START 0 u0 F g. T# n( R/ B" v$ A+ x$ z
- if a start motor command has been given or ( }! R. {. X" r% u( f3 p
- IDLE_ALIGNMENT if a start alignment command has been
r9 `) Z7 ~" ^0 n - given */' |- {0 \* D/ `9 o5 j& t: a" G. |
- IDLE_ALIGNMENT = 1, /*!< "Pass-through" state containg the code to be executed
8 L' z- T5 V5 u6 O% P1 q6 @3 | - only once after encoder alignment command.
# B# g7 `/ D* F( l - Next states can be ALIGN_CHARGE_BOOT_CAP or
0 U: y- _: i' E3 T) W - ALIGN_OFFSET_CALIB according the configuration. It : y8 ?% i- l( I: t6 B' j
- can also be ANY_STOP if a stop motor command has been ; _, e7 D Z, \" P
- given. */
5 x& ~/ h- _; a0 q - ALIGN_CHARGE_BOOT_CAP = 13,/*!< Persistent state where the gate driver boot
4 Q$ [4 B6 E5 E; u0 M. R: s% G - capacitors will be charged. Next states will be
# o2 @: q5 x% K/ B" V - ALIGN_OFFSET_CALIB. It can also be ANY_STOP if a stop 4 X: U* G3 ^$ V _
- motor command has been given. */2 z$ q: _' V) h4 t
- ALIGN_OFFSET_CALIB = 14,/*!< Persistent state where the offset of motor currents ' K- x( L1 ^8 w! E2 b( o I
- measurements will be calibrated. Next state will be * D. P' h7 k, `$ A) c9 S
- ALIGN_CLEAR. It can also be ANY_STOP if a stop motor
( B& S, `- t- w$ s - command has been given. */7 q3 }6 [ m2 |* L# M. t6 a+ }
- ALIGN_CLEAR = 15, /*!< "Pass-through" state in which object is cleared and
3 h' S" R8 R- Z5 g! ~7 ` - set for the startup." @& b* V' }- v, [1 |
- Next state will be ALIGNMENT. It can also be ANY_STOP ( g# g4 n5 l& @9 f, F6 A1 o* I
- if a stop motor command has been given. */
) V; X) O, v- E; t: E - ALIGNMENT = 2, /*!< Persistent state in which the encoder are properly
" F [7 p3 |" ], e' X - aligned to set mechanical angle, following state can * r, B: j/ ?: E L0 B
- only be ANY_STOP */
4 A f) q [! u& J - IDLE_START = 3, /*!< "Pass-through" state containg the code to be executed
4 u* {- k& g! w5 e - only once after start motor command.
. f7 `3 U8 C G! T - Next states can be CHARGE_BOOT_CAP or OFFSET_CALIB
, @6 F# ~2 J" H" b$ h - according the configuration. It can also be ANY_STOP 4 D6 W+ g/ m& w) ]6 \6 L
- if a stop motor command has been given. */! n" B) ]& M B3 J' R5 g. V( E
- CHARGE_BOOT_CAP = 16, /*!< Persistent state where the gate driver boot ! n. \/ P* _$ {: E5 Q7 z1 K
- capacitors will be charged. Next states will be
/ V8 G3 g. D; t( v$ f, j - OFFSET_CALIB. It can also be ANY_STOP if a stop motor
7 K. M& b# }% W. l+ Z - command has been given. */
# Q1 X0 i% F4 k1 ^& A7 W5 | - OFFSET_CALIB = 17, /*!< Persistent state where the offset of motor currents 5 U; H4 Y# t% H0 l6 `
- measurements will be calibrated. Next state will be
! s( D s% F! L8 u1 t+ j& y - CLEAR. It can also be ANY_STOP if a stop motor 2 r7 A+ I6 g) W0 }* h
- command has been given. */
7 r( H2 s- D2 H - CLEAR = 18, /*!< "Pass-through" state in which object is cleared and 5 k/ V- H* j- v% I* Z) m
- set for the startup.! z+ C# ~" c, x8 R) a: d9 G2 y% i$ h
- Next state will be START. It can also be ANY_STOP if 1 g8 C! F5 p6 p; q' I. o) [
- a stop motor command has been given. */
/ U& S& n1 |- D8 Z - START = 4, /*!< Persistent state where the motor start-up is intended
8 \( @ C2 b+ ^ - to be executed. The following state is normally : W3 E" u2 Z+ U0 c; M
- START_RUN as soon as first validated speed is : F+ ]3 T. @; X) S' k
- detected. Another possible following state is
0 H3 }- H5 ?/ l! X, O - ANY_STOP if a stop motor command has been executed */8 j j1 m% r5 T& @3 O/ J3 R
- START_RUN = 5, /*!< "Pass-through" state, the code to be executed only
9 C0 o0 ?& C. }5 k5 i( j4 f" ` - once between START and RUN states it’s intended to be
& d* m, J5 A1 s# v8 e1 Z - here executed. Following state is normally RUN but
9 D/ x1 t- n2 @6 u - it can also be ANY_STOP if a stop motor command has % a: g( F- ^6 A! L
- been given */9 b* R8 d6 R( K0 S- s9 B b: [
- RUN = 6, /*!< Persistent state with running motor. The following 2 k# ]5 b6 }2 e% q3 K; ~
- state is normally ANY_STOP when a stop motor command
9 I* J8 i1 E0 d' Y9 b - has been executed */2 M0 S: T% d' t& [$ }$ ^: E' {
- ANY_STOP = 7, /*!< "Pass-through" state, the code to be executed only
8 s0 [* ]1 N5 f0 F, S: T - once between any state and STOP it’s intended to be
! ?7 B1 Q: U9 d9 Y+ @ - here executed. Following state is normally STOP */+ `+ E) k% H' _0 w
- STOP = 8, /*!< Persistent state. Following state is normally
" ]$ d6 [ J# i9 n - STOP_IDLE as soon as conditions for moving state
9 L5 q% r% K& A6 a1 o% ]8 N$ R) q8 g - machine are detected */. E/ ~9 ]& n, K+ C* a0 T" @
- STOP_IDLE = 9, /*!< "Pass-through" state, the code to be executed only
4 g( T! x9 ]) O% y# X0 ]8 G' [( v - once between STOP and IDLE it’s intended to be here ' F9 W/ B! @; p" x5 l
- executed. Following state is normally IDLE */. j2 s- V: B/ }% J1 S- O' W8 H/ o
- FAULT_NOW = 10, /*!< Persistent state, the state machine can be moved from% T( M2 q" Z6 i+ s+ _
- any condition directly to this state by 5 Y) s1 f; F. m* h# @7 l
- STM_FaultProcessing method. This method also manage # C, R: ~( W9 q8 ]% E9 j! J
- the passage to the only allowed following state that ! q, n% x% f6 Q7 D D
- is FAULT_OVER */
. b7 `& ?9 a1 J - FAULT_OVER = 11 /*!< Persistent state where the application is intended to1 I& Y' [4 [/ B$ w. }7 r9 D9 S: i
- stay when the fault conditions disappeared. Following . a- x( x8 H$ \" d9 y
- state is normally STOP_IDLE, state machine is moved as . a+ f7 `+ A; p5 S( I
- soon as the user has acknowledged the fault condition. j2 s4 Y! b5 @
- */ k) f w# g( q6 a+ F! P8 D) V1 s
- } State_t;
" V8 W! P( n& H+ Y' H
复制代码
( I! K" Z- n. z- C+ { z0 h: J& Q# F# ?1 e$ L
. z' b2 n' N" P5 K7 Z9 H( `9 [好了,本贴任务3完成。
# L! k d3 F- F t) k+ a' @' n6 Q
' h: d1 X- ^2 i5 X. }, O9 k按照惯例,程序附上:
2 u* {/ u- \7 b3 Y1 P
P-NUCLEO-IHM001_ zhengfan.rar
(8.99 MB, 下载次数: 60)
|
多谢捧场
羡慕你的HP3457和吉时利2000