【STM32电机培训】4 修改代码控制电机运转
f6 B! I2 N2 `: k1 J2 [. S. b, }# T7 K7 @8 a" _
2 @/ Z. `8 p# A' s% D
通过参加电机培训,对STM32控制无刷电机控制技术有了进一步的理解。本次就针对STSW-STM32100\STM32CubeExpansion_SPN7_V1.1.0演示代码进行修改,达到让电机按照自己的要求运转。
2 q1 D/ B. c$ v6 H2 P ^. ]0 ]! S 首先先关闭电位器调速功能。在MC_SixSetup_param.h文件中的“BASIC PARAMETERS”有对电机基本控制参数的定义,将#define POTENTIOMETER 从1 改成0.& w7 H- R. d2 U. w% B
* M. k* W0 [6 _0 H& S! m+ B% S1 h
* l, p( U) z+ @MC_SixSetup_param.h
' m5 e2 E3 d U" U/ B7 L! c- <font size="3">/* ****************************************************************************
; }+ K# x I3 C; F; [ - ==============================================================================
* d+ _6 ?% d) t - ###### BASIC PARAMETERS ######9 X0 J8 R' n8 f1 p! \, U
- ============================================================================== ' i; K8 Q7 _$ ?6 w: Y
- **************************************************************************** */ 6 b/ }4 p6 f* W
- #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */# l1 e; |. k$ I: I3 Q- V. k& v1 K! V6 C
- #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/
8 N$ x* _. L* r- Q, D - #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */
5 p9 \. {9 z5 x: L: Z& ] - #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ ' Z W. ?8 b2 r( f
- & |* w& v" w \, T* n' n
- / N) ]+ u& O) q4 m! M
- /* **************************************************************************** </font>
复制代码
+ d* s" J5 E$ x' O- v& z. C6 D: `# F8 o/ G
3 T5 |4 o9 }& k' ]8 {. u: ~: V
接着在main_F302.c的主循环中添加代码,让电机以500转速运行5秒停止5秒,再以6000转速运行5秒。对电机的控制方法ST 官方在注释中写得非常清楚,改变电机运行参数很方便。
6 m+ \9 @) m. }, ^6 d M/ E2 o
5 ?( l4 }/ y3 E j8 \. x) Q9 w! r- F" u P/ j: g3 L7 j+ P
! C# \8 B0 e- W1 f& v% u
1 m; ^# \4 \6 R8 s5 e7 _
main_F302.c8 M% _1 j4 Z2 T3 e2 i- G, k
- <font size="3">/* ****************************************************************************
/ t1 A, j/ @% Z0 }& D$ ]0 M - ============================================================================== ' H5 A/ T* P4 I9 A3 _2 b# m
- ###### BASIC PARAMETERS ######- K- r) s1 b/ M
- ==============================================================================
8 { b5 C% i) E9 V l) N- G - **************************************************************************** */ : i* c# t- u Z4 a1 t/ L4 J
- #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */: N( y6 h5 |5 Z# ]3 _; l
- #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/ " A- _( \4 q1 o3 r4 O
- #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */
7 E7 f5 z" ~6 f9 B; x - #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */
$ S; H6 G: m' U$ E# j - : h! y0 v3 e# L# T& e% @
1 O ^/ k4 ?7 S3 J. E- /* ****************************************************************************
9 v4 C, e3 ^0 {5 ]6 | - . Z" x( s4 N3 U+ Y0 S+ P
- # t4 Y0 K& Z; L+ v5 T5 o1 t# \
, E" P6 g+ z( M" _) N& ?- ============================================================================== 5 b2 C; Q+ g. u8 v% r
- ###### How to use the 6Step FW Example project ######
1 l6 Z0 m+ e$ p* a - ============================================================================== 0 y& U1 [/ o; q$ e) ]6 \; K7 V5 L
- This workspace contains the middleware layer with Motor Control library to drive
, k; H; w+ b! d! M - a motor connected on X-Nucleo board performing a 6-step control algorithm; i+ m4 ~9 k) E
- allowing the motor speed regulation through a potentiometer. The 6-step algorithm & b, Z0 h8 _: p
- is based on 1shunt current sensing mode and sensorless algorithm for bEmf detection.
: O) t& U# p& ^; a - The workspace is provided for STM32Fxx-Nucleo in four different configurations,
4 j1 U) \: W* g+ \% t - normal, demo, comm mode, boot mode. The "normal" mode waits the blue button event
" x0 }6 b/ i! Y3 f - to start the motor, the "demo" mode starts and stop the motor automatically, the
( K3 G& v @/ x6 ~; r+ n - "comm" mode enables the communication protocol with external PC terminal and the 0 y: H( @+ @: `
- "boot" mode enables the FW for external boot loader.' O0 o! X1 m. Y9 v! N8 y, e/ O
-
3 C' s; q# M3 f- E - A list of APIs is provided to send command to 6Step lib, for instance:
3 |: W$ v D, f' O. o, P -
6 Q" O! n' Q, o6 k5 c# A1 |; R - (#) MC_StartMotor() -> Start the motor
T/ A m8 }) a' X; G8 p/ X! n' K3 D -
, L5 A( Q5 y5 C# ^' V( `4 f - (#) MC_StoptMotor() -> Stop the motor6 Y9 L( q, Q4 d G! D
- 9 v9 Q. w. p' h5 D
- (#) MC_Set_Speed(...) -> Set the new motor speed/ ^9 X8 Z0 K/ q) \$ P3 P
-
( r# F: |6 }1 f/ b - The MC_SixStep_param.h contains the full list of MC parameters
9 F6 \1 q ~( \; C -
& K% |% J# t; ]5 O+ q9 O - ============================================================================== , @1 z' m+ i/ ]: ?4 @* O S3 k" c
- ###### USER SPACE ######
/ y- j: S* J$ p$ c8 j - ============================================================================== + t z; u2 V3 o) Y4 Q! n9 X* ^
- *****************************************************************************/ # P0 `+ a7 G( `+ |- G
- U6 t2 Y& R9 R% {/ f' \) y
-
( J# j# S5 o0 j: v- G: ~& ^3 [ C8 S - /****************************************************************************/
9 |, U4 q8 H) J( k: e) ] - //添加的代码) \6 h Z$ E( x/ T
- HAL_Delay(2000);+ e# c6 H2 I( i. ]7 j5 E! Q
- MC_Set_Speed(500);/ e! R" W( U) h# v* u [
- MC_StartMotor();8 V6 i" c5 m# T/ X
- HAL_Delay(5000);" P) K2 p b$ b
- MC_StopMotor();# X) E: H, x# h6 @% m
- MC_Set_Speed(6000);
: L: i+ O" l& l: Y - </font>
复制代码 7 h% \3 Z8 P* b+ H
3 u, F9 V( e" l$ S( K; z
本次实验是6步梯形模式,按照要求对IHM07M1板子上的条线进行相应设置。编译下载,通电运行,电机已经能够按照预定参数正确运行了。5 u1 o- U) _4 f3 k3 P& y- j
* \2 ~* ?$ N# x5 |; m
! A+ L+ O, U; ?2 I. w
; o' c" @6 W& P* A& J0 Q% uhttp://player.youku.com/player.php/sid/XMjk4NDc1OTkyOA==/v.swf
, U3 g, U' f! z0 A+ Z) l% V& {3 H* O$ W! C
|
谢谢! 这个我看过了,我手上没有ST的Noclude板子,有别的无刷驱动板,想要用ST的电机库配置一个适合自己的模板,现在这个文档里面没有教怎么使用 Workbench, 自己不会配。 不知道楼主有没有资料了,关于使用Workbench
举个例子,数控线切割机床就是步进电机控制,每给一个脉冲,电机就转动1个角度。楼上应该去参阅步进电机相关资料,比如数控机床控制等等。本人也是门外汉。
楼主,你用的电机是STM32F302吗?我这边想用STM32F302+IHM07M1驱动板上的电位器进行控制速度,我看您这个功能可能跟我这个有点相关,不知道您这边可以加个QQ交流一下吗?我的是625212098
都是ST 官方的:8 G" [6 a- r# U7 Z2 b
谢谢
只能用于ST官网的板子么?? 我手里有一块 无刷驱动板,MOS管的引脚连接的是TIM1的6个通道,和ST的驱动板子是不一样的,,不知道能不能用 这个程序 了???
https://www.stmcu.org.cn/document/detail/index/id-218177& R3 v" l0 S: O: T" z: P0 u
ST官方的电机培训资料,你可以参考
楼主你这个工程是用的STM32CubeMX生成的工程吗?
我也只是班门弄斧而已。工程不是CubeMX生成的,是ST官方下载的。
https://www.stmcu.org.cn/module/forum/thread-613011-1-1.html
步进电机每给一个脉冲转动一个角度,与无刷电机应该还是有区别的。
那么 就精准度来说,哪种电机更好一点呢