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

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

[复制链接]
七哥 发布时间:2018-9-11 02:02
本帖最后由 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 R
0 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
01.jpg 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
02.jpg , ]5 w. \) [6 w( E& H* x( C

; [( a6 \+ s  B7 R7 w
# t5 C  t9 M. {; V5 \* ^. t3 Q
03.jpg
! 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
04.jpg + A. Q$ Y. A& c& R

# H6 u! O: H. K) l
8 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
10.jpg 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
11.jpg 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
19.jpg ! `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 20.jpg
: 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
21.jpg 5 [$ E/ W2 B4 s0 B# f) V, d- F1 ?

* {% `6 k, [( ?

$ Y" m/ f' d+ }! OWorkbench界面观察电机状态变化,一共出现过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  [ 22.jpg
' 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
24.jpg 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 \ 23.jpg - \" J! I$ x8 `6 v+ \3 b
% B3 J' u" L: J, p0 t* W" j
Fault Over状态# `6 ^+ S6 i' {: C6 q+ L: _/ [
25.jpg 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
  1. typedef enum+ V$ p& p5 e/ _
  2. {
    . W& h0 _. h& T
  3. ICLWAIT = 12,         /*!< Persistent state, the system is waiting for ICL
    / i$ R4 C4 f: y& x- s" c+ P4 z
  4.                            deactivation. Is not possible to run the motor if
    ) _, E4 l' \( l+ C
  5.                            ICL is active. Until the ICL is active the state is - ~2 C* u; S& U$ E: O1 ^3 H
  6.                            forced to ICLWAIT, when ICL become inactive the state
    % T3 H$ u. @! d$ l8 O, c
  7.                            is moved to IDLE */- g( Q4 _7 A/ ~7 f9 _
  8. IDLE = 0,             /*!< Persistent state, following state can be IDLE_START 0 u0 F  g. T# n( R/ B" v$ A+ x$ z
  9.                            if a start motor command has been given or ( }! R. {. X" r% u( f3 p
  10.                            IDLE_ALIGNMENT if a start alignment command has been
      r9 `) Z7 ~" ^0 n
  11.                            given */' |- {0 \* D/ `9 o5 j& t: a" G. |
  12. IDLE_ALIGNMENT = 1,   /*!< "Pass-through" state containg the code to be executed
    8 L' z- T5 V5 u6 O% P1 q6 @3 |
  13.                            only once after encoder alignment command.
    # B# g7 `/ D* F( l
  14.                            Next states can be ALIGN_CHARGE_BOOT_CAP or
    0 U: y- _: i' E3 T) W
  15.                            ALIGN_OFFSET_CALIB according the configuration. It : y8 ?% i- l( I: t6 B' j
  16.                            can also be ANY_STOP if a stop motor command has been ; _, e7 D  Z, \" P
  17.                            given. */
    5 x& ~/ h- _; a0 q
  18. ALIGN_CHARGE_BOOT_CAP = 13,/*!< Persistent state where the gate driver boot
    4 Q$ [4 B6 E5 E; u0 M. R: s% G
  19.                            capacitors will be charged. Next states will be
    # o2 @: q5 x% K/ B" V
  20.                            ALIGN_OFFSET_CALIB. It can also be ANY_STOP if a stop 4 X: U* G3 ^$ V  _
  21.                            motor command has been given. */2 z$ q: _' V) h4 t
  22. ALIGN_OFFSET_CALIB = 14,/*!< Persistent state where the offset of motor currents ' K- x( L1 ^8 w! E2 b( o  I
  23.                            measurements will be calibrated. Next state will be * D. P' h7 k, `$ A) c9 S
  24.                            ALIGN_CLEAR. It can also be ANY_STOP if a stop motor
    ( B& S, `- t- w$ s
  25.                            command has been given. */7 q3 }6 [  m2 |* L# M. t6 a+ }
  26. ALIGN_CLEAR = 15,     /*!< "Pass-through" state in which object is cleared and
    3 h' S" R8 R- Z5 g! ~7 `
  27.                            set for the startup." @& b* V' }- v, [1 |
  28.                            Next state will be ALIGNMENT. It can also be ANY_STOP ( g# g4 n5 l& @9 f, F6 A1 o* I
  29.                            if a stop motor command has been given. */
    ) V; X) O, v- E; t: E
  30. ALIGNMENT = 2,        /*!< Persistent state in which the encoder are properly
    " F  [7 p3 |" ], e' X
  31.                            aligned to set mechanical angle, following state can * r, B: j/ ?: E  L0 B
  32.                            only be ANY_STOP */
    4 A  f) q  [! u& J
  33. IDLE_START = 3,       /*!< "Pass-through" state containg the code to be executed
    4 u* {- k& g! w5 e
  34.                            only once after start motor command.
    . f7 `3 U8 C  G! T
  35.                            Next states can be CHARGE_BOOT_CAP or OFFSET_CALIB
    , @6 F# ~2 J" H" b$ h
  36.                            according the configuration. It can also be ANY_STOP 4 D6 W+ g/ m& w) ]6 \6 L
  37.                            if a stop motor command has been given. */! n" B) ]& M  B3 J' R5 g. V( E
  38. CHARGE_BOOT_CAP = 16, /*!< Persistent state where the gate driver boot ! n. \/ P* _$ {: E5 Q7 z1 K
  39.                            capacitors will be charged. Next states will be
    / V8 G3 g. D; t( v$ f, j
  40.                            OFFSET_CALIB. It can also be ANY_STOP if a stop motor
    7 K. M& b# }% W. l+ Z
  41.                            command has been given. */
    # Q1 X0 i% F4 k1 ^& A7 W5 |
  42. OFFSET_CALIB = 17,    /*!< Persistent state where the offset of motor currents 5 U; H4 Y# t% H0 l6 `
  43.                            measurements will be calibrated. Next state will be
    ! s( D  s% F! L8 u1 t+ j& y
  44.                            CLEAR. It can also be ANY_STOP if a stop motor 2 r7 A+ I6 g) W0 }* h
  45.                            command has been given. */
    7 r( H2 s- D2 H
  46. CLEAR = 18,           /*!< "Pass-through" state in which object is cleared and 5 k/ V- H* j- v% I* Z) m
  47.                            set for the startup.! z+ C# ~" c, x8 R) a: d9 G2 y% i$ h
  48.                            Next state will be START. It can also be ANY_STOP if 1 g8 C! F5 p6 p; q' I. o) [
  49.                            a stop motor command has been given. */
    / U& S& n1 |- D8 Z
  50. START = 4,            /*!< Persistent state where the motor start-up is intended
    8 \( @  C2 b+ ^
  51.                            to be executed. The following state is normally : W3 E" u2 Z+ U0 c; M
  52.                            START_RUN as soon as first validated speed is : F+ ]3 T. @; X) S' k
  53.                            detected. Another possible following state is
    0 H3 }- H5 ?/ l! X, O
  54.                            ANY_STOP if a stop motor command has been executed */8 j  j1 m% r5 T& @3 O/ J3 R
  55. START_RUN = 5,        /*!< "Pass-through" state, the code to be executed only
    9 C0 o0 ?& C. }5 k5 i( j4 f" `
  56.                            once between START and RUN states it’s intended to be
    & d* m, J5 A1 s# v8 e1 Z
  57.                            here executed. Following state is normally  RUN but
    9 D/ x1 t- n2 @6 u
  58.                            it can also be ANY_STOP  if a stop motor command has % a: g( F- ^6 A! L
  59.                            been given */9 b* R8 d6 R( K0 S- s9 B  b: [
  60. RUN = 6,              /*!< Persistent state with running motor. The following 2 k# ]5 b6 }2 e% q3 K; ~
  61.                            state is normally ANY_STOP when a stop motor command
    9 I* J8 i1 E0 d' Y9 b
  62.                            has been executed */2 M0 S: T% d' t& [$ }$ ^: E' {
  63. ANY_STOP = 7,         /*!< "Pass-through" state, the code to be executed only
    8 s0 [* ]1 N5 f0 F, S: T
  64.                            once between any state and STOP it’s intended to be
    ! ?7 B1 Q: U9 d9 Y+ @
  65.                            here executed. Following state is normally STOP */+ `+ E) k% H' _0 w
  66. STOP = 8,             /*!< Persistent state. Following state is normally
    " ]$ d6 [  J# i9 n
  67.                            STOP_IDLE as soon as conditions for moving state
    9 L5 q% r% K& A6 a1 o% ]8 N$ R) q8 g
  68.                            machine are detected */. E/ ~9 ]& n, K+ C* a0 T" @
  69. STOP_IDLE = 9,        /*!< "Pass-through" state, the code to be executed only
    4 g( T! x9 ]) O% y# X0 ]8 G' [( v
  70.                            once between STOP and IDLE it’s intended to be here ' F9 W/ B! @; p" x5 l
  71.                            executed. Following state is normally IDLE */. j2 s- V: B/ }% J1 S- O' W8 H/ o
  72. FAULT_NOW = 10,       /*!< Persistent state, the state machine can be moved from% T( M2 q" Z6 i+ s+ _
  73.                            any condition directly to this state by 5 Y) s1 f; F. m* h# @7 l
  74.                            STM_FaultProcessing method. This method also manage # C, R: ~( W9 q8 ]% E9 j! J
  75.                            the passage to the only allowed following state that ! q, n% x% f6 Q7 D  D
  76.                            is FAULT_OVER */
    . b7 `& ?9 a1 J
  77. FAULT_OVER = 11       /*!< Persistent state where the application is intended to1 I& Y' [4 [/ B$ w. }7 r9 D9 S: i
  78.                           stay when the fault conditions disappeared. Following . a- x( x8 H$ \" d9 y
  79.                           state is normally STOP_IDLE, state machine is moved as . a+ f7 `+ A; p5 S( I
  80.                           soon as the user has acknowledged the fault condition.   j2 s4 Y! b5 @
  81.                       */  k) f  w# g( q6 a+ F! P8 D) V1 s
  82. } 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)
收藏 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
+ z/ K# f1 O4 h* _正好有块板子,等着慢慢学习

* B9 L8 P' `: i& g% u多谢捧场
- |" w0 Q6 ], ~: X2 c2 X羡慕你的HP3457和吉时利2000
STM1024 回答时间:2018-9-11 13:08:26
我也想知道,这个肯定是采用的串口通讯,但是格式具体是怎样的,ST似乎并没有公布,在代码中看不到

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版