【STM32电机培训】4 修改代码控制电机运转
" ?0 T: G h: t: D. _
: q) v2 L/ @/ j' f' l/ k& j6 J
4 H1 q0 x/ W! h9 m& b 通过参加电机培训,对STM32控制无刷电机控制技术有了进一步的理解。本次就针对STSW-STM32100\STM32CubeExpansion_SPN7_V1.1.0演示代码进行修改,达到让电机按照自己的要求运转。
. I( D( b8 _" l$ g8 C8 h 首先先关闭电位器调速功能。在MC_SixSetup_param.h文件中的“BASIC PARAMETERS”有对电机基本控制参数的定义,将#define POTENTIOMETER 从1 改成0.
; Q2 A( ?6 S! z- [: Q. \$ d: L {- H! Y3 t% ~5 E* \
. S* t W& W; e0 l5 ?MC_SixSetup_param.h
0 z/ J0 Z* I2 @( M1 m* K- <font size="3">/* **************************************************************************** + U8 i" M, g' s2 }/ j" ^7 Z
- ==============================================================================
6 w- H+ o: f$ j7 ~, | - ###### BASIC PARAMETERS ######
8 W+ [% X: `4 G1 ]& B+ E) _ - ==============================================================================
0 M* I% x4 l" G; r1 o5 E3 n/ h) D - **************************************************************************** */
7 J) A( D1 y3 V$ M9 J' ~+ d- [ - #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */
; M. w1 T% F% u7 U - #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/
5 ?8 J6 D# q: j8 e+ ~0 d) F0 S - #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */ ; O. p$ K- v5 g( |% E
- #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ 6 U; W7 x) e: c3 J6 j' ^
* o3 Y* i% D9 m) y, [% J; G- ( _/ U! ^ Y" o \6 T
- /* **************************************************************************** </font>
复制代码 1 _" V/ c# L/ k, N
; E. u+ F$ ~3 ~8 H+ H" |& \) ^
6 }7 Q- ?. }) p7 u 接着在main_F302.c的主循环中添加代码,让电机以500转速运行5秒停止5秒,再以6000转速运行5秒。对电机的控制方法ST 官方在注释中写得非常清楚,改变电机运行参数很方便。
' z( ~. h) n! `( G% ^6 Z" {7 \' E& t \8 T |* q. p- n4 ]
. V) T) s0 g2 Z. m1 K! }8 L
* M+ a w* v: k
' m" X, s/ ` M+ o6 smain_F302.c
# ^* J) n9 c1 e" j* c/ O2 U- <font size="3">/* ****************************************************************************
7 Z! Q( m& U) e R( j2 g$ Q - ============================================================================== 1 i+ j: K8 f" d) f
- ###### BASIC PARAMETERS ######
/ S, y8 E. _: b! ~, [ - ==============================================================================
! p" S* l/ T' Y, S9 ? - **************************************************************************** */ & ? U$ W" u& Y* t
- #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */
|4 C( L* J6 \' W. L - #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/
- m' h @* I# H2 v+ C% |# T f - #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */
3 g8 B, a& h8 R - #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */
) Q" e* Z6 H) f! B1 u+ Y- e
4 ?& z, w# w3 M# }5 H( M+ Z" I) \
0 e, m3 c5 X# l7 t- /* **************************************************************************** 1 e" \! r: l- [! k; k
0 N/ n( X/ ?1 E. d- - Y' m- v& s4 ~7 R1 U; l
- 0 v4 g% A3 H4 d+ }( K. Q
- ==============================================================================
5 z, d4 _! O* e! u- x - ###### How to use the 6Step FW Example project ######" t8 J/ q7 p. N+ t
- ==============================================================================
# J7 w2 A8 [; m2 i4 i3 j - This workspace contains the middleware layer with Motor Control library to drive
8 T8 B0 s0 p! x/ U1 l7 z+ X - a motor connected on X-Nucleo board performing a 6-step control algorithm
+ K. a' O! u( P/ o) E4 s - allowing the motor speed regulation through a potentiometer. The 6-step algorithm 8 p- n. l7 E+ i+ Y
- is based on 1shunt current sensing mode and sensorless algorithm for bEmf detection.
* a: G: L7 u6 Z [0 Z. [+ @9 n - The workspace is provided for STM32Fxx-Nucleo in four different configurations, " c6 |6 x: a1 b5 X: H3 H
- normal, demo, comm mode, boot mode. The "normal" mode waits the blue button event
# t1 |8 x( Z5 f - to start the motor, the "demo" mode starts and stop the motor automatically, the
# z) d$ W/ k, W& H' c - "comm" mode enables the communication protocol with external PC terminal and the
$ i) `5 Y# g" Z: j% e4 o- A - "boot" mode enables the FW for external boot loader.
" q: H F9 P0 O- T - , d* L5 G. x/ V
- A list of APIs is provided to send command to 6Step lib, for instance:$ M5 F! k: E8 q! X, s
- % @4 T0 l! H! i, q
- (#) MC_StartMotor() -> Start the motor
6 B$ y7 w; V0 z/ Y' ^6 V. k -
9 w+ m( P2 | q - (#) MC_StoptMotor() -> Stop the motor
3 G; x, ?9 [8 G( G1 o9 R' K - + h6 n& A' W( }: P6 |1 d' k2 \; L
- (#) MC_Set_Speed(...) -> Set the new motor speed3 o Y) q I) Z6 ?
-
! z, m ?6 F7 H - The MC_SixStep_param.h contains the full list of MC parameters , E3 s1 J& F8 x1 B/ N% D
-
3 y8 X, \6 C& h: _0 K+ e - ==============================================================================
! ^+ ], r. |& b& h, r3 q - ###### USER SPACE ######, {; U* J1 W5 z/ Y6 U Q8 t5 T
- ============================================================================== 6 t. r2 L' r4 h1 t; n3 d
- *****************************************************************************/ , h }; z R) ]1 t [) [7 C
- 1 Y B1 S' w7 A! p
- . V0 o9 {( O' {' X
- /****************************************************************************/
; q& z0 z3 F. l1 v - //添加的代码
6 J% f6 a3 s3 U; i - HAL_Delay(2000);
' l; x) r$ Z! m: T) j3 y - MC_Set_Speed(500);2 S1 _. w |9 a8 d, w4 c6 P
- MC_StartMotor();
- ~ U5 B+ m Q- J* `8 ^! ]9 }/ D - HAL_Delay(5000);4 L! E/ w2 I+ D$ @1 z
- MC_StopMotor();6 v& A" u1 l$ ~# a! e
- MC_Set_Speed(6000);
/ n6 G( e A* H, | - </font>
复制代码 ' L) G/ Q4 `7 r. l( _. u
3 @! j8 }3 A% I4 ~! Y# `2 J7 }
本次实验是6步梯形模式,按照要求对IHM07M1板子上的条线进行相应设置。编译下载,通电运行,电机已经能够按照预定参数正确运行了。
! \9 i- h8 W' i, G) K( b, c5 }/ n
) ~9 _2 i: E' T- U& J* l, V: H. \2 |$ [2 k6 M6 w9 O
: o/ v0 C; i9 Uhttp://player.youku.com/player.php/sid/XMjk4NDc1OTkyOA==/v.swf
( O- l* r l: a! `) \+ u- A; I3 P/ y
|
谢谢! 这个我看过了,我手上没有ST的Noclude板子,有别的无刷驱动板,想要用ST的电机库配置一个适合自己的模板,现在这个文档里面没有教怎么使用 Workbench, 自己不会配。 不知道楼主有没有资料了,关于使用Workbench
举个例子,数控线切割机床就是步进电机控制,每给一个脉冲,电机就转动1个角度。楼上应该去参阅步进电机相关资料,比如数控机床控制等等。本人也是门外汉。
楼主,你用的电机是STM32F302吗?我这边想用STM32F302+IHM07M1驱动板上的电位器进行控制速度,我看您这个功能可能跟我这个有点相关,不知道您这边可以加个QQ交流一下吗?我的是625212098
都是ST 官方的:& R# m! ?" |+ k6 F; `* T
谢谢
只能用于ST官网的板子么?? 我手里有一块 无刷驱动板,MOS管的引脚连接的是TIM1的6个通道,和ST的驱动板子是不一样的,,不知道能不能用 这个程序 了???
https://www.stmcu.org.cn/document/detail/index/id-218177
ST官方的电机培训资料,你可以参考
楼主你这个工程是用的STM32CubeMX生成的工程吗?
我也只是班门弄斧而已。工程不是CubeMX生成的,是ST官方下载的。$ Q% H ~' r$ a8 f9 s, o+ K4 M
https://www.stmcu.org.cn/module/forum/thread-613011-1-1.html
步进电机每给一个脉冲转动一个角度,与无刷电机应该还是有区别的。
那么 就精准度来说,哪种电机更好一点呢