【STM32电机培训】4 修改代码控制电机运转
& B# V( T8 k9 u" j0 [4 l0 X3 r; b2 a: C, p4 b, k% ] L
) n$ B6 u+ I0 l% Y 通过参加电机培训,对STM32控制无刷电机控制技术有了进一步的理解。本次就针对STSW-STM32100\STM32CubeExpansion_SPN7_V1.1.0演示代码进行修改,达到让电机按照自己的要求运转。
- {, u( y4 s8 n1 g 首先先关闭电位器调速功能。在MC_SixSetup_param.h文件中的“BASIC PARAMETERS”有对电机基本控制参数的定义,将#define POTENTIOMETER 从1 改成0.* \2 T( e' `8 K9 W+ d0 x
. C* h- P1 ?8 w( m
& q& [9 r, i# @ P) C. E! sMC_SixSetup_param.h3 O( [2 u2 i/ U) o* j: j6 {2 U
- <font size="3">/* **************************************************************************** 2 p0 N3 d7 Q: i% y, |1 X
- ==============================================================================
! R6 p* k( n/ l1 P/ k - ###### BASIC PARAMETERS ######. q. \# R* ^' I& J
- ============================================================================== $ J4 x2 C/ r5 ?2 @
- **************************************************************************** */ + T: X2 K( s% L" Z; d
- #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */
& n9 |0 U. ?/ g9 ]) H& { - #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/ ! m& G2 k* `+ L! m: w0 t
- #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */
4 ~8 {) ?2 C2 C - #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ 8 Z6 s$ d+ Z# O% `- x
& y+ `' j0 P8 H
3 v7 V' R/ |& |: Z: ~- z- h9 |- /* **************************************************************************** </font>
复制代码 # _% s6 A. S1 ]9 j; w
, V& {+ J* u8 r, Z i3 C; y
; _" @" T8 y0 `/ `/ J) @6 Z5 j
接着在main_F302.c的主循环中添加代码,让电机以500转速运行5秒停止5秒,再以6000转速运行5秒。对电机的控制方法ST 官方在注释中写得非常清楚,改变电机运行参数很方便。
% C4 z4 a( G' t. C3 ]9 _# ^. s) V* U
\7 h2 C5 N3 [. D. n( C
' i, B5 P/ d* L* i! H; T+ j& N& {$ L+ h( B4 M
main_F302.c: j B, ?. j# ^! s
- <font size="3">/* ****************************************************************************
* O2 Q/ C2 c+ P; H& ^ - ==============================================================================
1 G, n( C; o9 `4 g" z0 D% U - ###### BASIC PARAMETERS ######
9 {, s+ ?4 ~- |7 i+ u - ==============================================================================
# ?4 V# M' m5 C0 a - **************************************************************************** */
# ]4 k0 N2 b5 l; B0 z) q - #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */
: L- r% F9 |7 ]' F3 k0 d - #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/ ; M8 B! }" {% U6 M5 P
- #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */ & Y: ]# U7 b- m/ B0 \1 {' }
- #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ 0 V/ p: i3 P' a2 Q1 i" N
8 G0 h$ b# S% h" \& [% N% ?$ M- 7 }- A0 Y$ h" q' r" c- l
- /* ****************************************************************************
1 l0 y% E, m( T% G' l" \! { - 2 r9 P3 A/ U' a8 O2 K
- 1 O n5 K( L7 S7 K% A" z
- 2 U* ~& x+ Y9 R* \( Z4 T& N$ I
- ============================================================================== " F& U) X, q9 p( ~
- ###### How to use the 6Step FW Example project ######
+ r8 U; a9 y; q5 d2 \3 W - ==============================================================================
. w: a o& ^4 ^" s: o - This workspace contains the middleware layer with Motor Control library to drive
9 a! q2 |: b7 a$ b* D# ^ - a motor connected on X-Nucleo board performing a 6-step control algorithm- s0 G9 [8 f3 z( X7 m) S- L. m
- allowing the motor speed regulation through a potentiometer. The 6-step algorithm 3 V3 ?+ V' m5 _* z) \: q& K
- is based on 1shunt current sensing mode and sensorless algorithm for bEmf detection.
1 F. |. Y% [ U9 V - The workspace is provided for STM32Fxx-Nucleo in four different configurations,
! S9 o5 S. ^9 l1 |3 W. N- _; n - normal, demo, comm mode, boot mode. The "normal" mode waits the blue button event
$ G, X$ G" g9 w: b% Y - to start the motor, the "demo" mode starts and stop the motor automatically, the
* j. K ^+ f2 b. _9 g& k- [& Y2 [ - "comm" mode enables the communication protocol with external PC terminal and the
& K6 ]* P+ U0 u$ s8 q# ] - "boot" mode enables the FW for external boot loader.) b" A: u' C) b: ~3 n3 q
- ( _- ?/ J) `0 G0 U: M2 w
- A list of APIs is provided to send command to 6Step lib, for instance:6 H6 r% N- Z$ z( Q
-
W; H- l2 B, |! W/ e S: B - (#) MC_StartMotor() -> Start the motor
' w4 E6 B& o! \5 T6 l. P+ | -
7 c# k. Y. d, e$ G - (#) MC_StoptMotor() -> Stop the motor6 L" c7 U# b' Z% D7 V* w! |
- 8 p' E t6 o8 T
- (#) MC_Set_Speed(...) -> Set the new motor speed
' [& [3 t2 p% E# Q3 D -
" T A( x3 r1 L& w- R8 c4 J - The MC_SixStep_param.h contains the full list of MC parameters
w* a/ Q- H( J9 e/ A+ b* I$ | -
& h$ f9 Q" a0 t0 U$ q1 W* P - ==============================================================================
, j# N" O2 q8 v! \* S - ###### USER SPACE ######7 J; L2 W+ e5 @ @! i
- ============================================================================== - y4 _& K6 [9 |0 @. @$ d+ R) Q
- *****************************************************************************/ + O/ S4 o; R. B0 ~
- 0 J7 o- D1 g3 F0 ?! Y9 T" I
- , ^+ k' F, \- J+ A( [! Z% m
- /****************************************************************************/
+ A2 K+ Z5 b* |& ~ - //添加的代码
6 N6 p3 @# O: Y% H, o6 r - HAL_Delay(2000);
# M* E! L' [' _9 `9 _8 R! a - MC_Set_Speed(500);% h2 j- m) K T/ l
- MC_StartMotor();
8 X4 D; E4 r# O( W) L - HAL_Delay(5000);0 G% M# R2 T# K* b
- MC_StopMotor();$ P; g8 m/ q1 |$ K' C+ `8 s; E6 b
- MC_Set_Speed(6000);8 a1 }7 v9 q' S5 ^ W
- </font>
复制代码 4 n7 ^' j) G6 T) D
; X5 }1 y, T+ T3 u
本次实验是6步梯形模式,按照要求对IHM07M1板子上的条线进行相应设置。编译下载,通电运行,电机已经能够按照预定参数正确运行了。! j/ i i6 y8 E+ |6 l1 ?% x
* Q* k" ~7 ?6 S: @& B* A' @1 J7 [% Y" D0 f0 ]! G' F( z* M" @/ E2 d8 a, e* t
( j+ ]/ m( h S+ R& N1 w$ i% E+ a
http://player.youku.com/player.php/sid/XMjk4NDc1OTkyOA==/v.swf* Y6 R/ J' ^ \! ?0 F' m
1 x" P" U2 w+ s% ]; r |
谢谢! 这个我看过了,我手上没有ST的Noclude板子,有别的无刷驱动板,想要用ST的电机库配置一个适合自己的模板,现在这个文档里面没有教怎么使用 Workbench, 自己不会配。 不知道楼主有没有资料了,关于使用Workbench
举个例子,数控线切割机床就是步进电机控制,每给一个脉冲,电机就转动1个角度。楼上应该去参阅步进电机相关资料,比如数控机床控制等等。本人也是门外汉。
楼主,你用的电机是STM32F302吗?我这边想用STM32F302+IHM07M1驱动板上的电位器进行控制速度,我看您这个功能可能跟我这个有点相关,不知道您这边可以加个QQ交流一下吗?我的是625212098
都是ST 官方的:7 y3 ]' z& y6 w0 z6 A
+ R8 o% ^6 e3 z- |9 @: z
谢谢
只能用于ST官网的板子么?? 我手里有一块 无刷驱动板,MOS管的引脚连接的是TIM1的6个通道,和ST的驱动板子是不一样的,,不知道能不能用 这个程序 了???
https://www.stmcu.org.cn/document/detail/index/id-218177
ST官方的电机培训资料,你可以参考
楼主你这个工程是用的STM32CubeMX生成的工程吗?
我也只是班门弄斧而已。工程不是CubeMX生成的,是ST官方下载的。# I* g% g6 t" `" S. n1 Q
https://www.stmcu.org.cn/module/forum/thread-613011-1-1.html
步进电机每给一个脉冲转动一个角度,与无刷电机应该还是有区别的。
那么 就精准度来说,哪种电机更好一点呢. B! K z/ G/ @% X j