【STM32电机培训】4 修改代码控制电机运转/ E6 |3 b2 |: |( v9 @4 |( z
1 Q& {# Z* i. t% U0 `5 k
5 i9 \% V0 p! k) N7 _, c. { 通过参加电机培训,对STM32控制无刷电机控制技术有了进一步的理解。本次就针对STSW-STM32100\STM32CubeExpansion_SPN7_V1.1.0演示代码进行修改,达到让电机按照自己的要求运转。
- Y4 r8 a- i9 E: f9 n6 o- p6 b 首先先关闭电位器调速功能。在MC_SixSetup_param.h文件中的“BASIC PARAMETERS”有对电机基本控制参数的定义,将#define POTENTIOMETER 从1 改成0.
6 Z0 F$ Y5 m+ t" ~7 x" a. `( A: o) O+ a" [! |" Q6 H0 D
) ?' l8 `& R4 a1 H$ v5 X& b
MC_SixSetup_param.h
$ }$ C( k3 h; Q( s4 j- <font size="3">/* ****************************************************************************
; I# j4 m- f6 Q3 B" f - ==============================================================================
6 z0 u4 P3 S A/ o& W4 R - ###### BASIC PARAMETERS ######+ n9 h+ ^7 V* c0 T! o
- ============================================================================== 9 M0 g, e7 P2 ?2 U* u1 L$ f
- **************************************************************************** */
' T- ] |3 p, E! g3 q$ S - #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */4 U7 m* H7 g2 Y2 D
- #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/
: @# K2 a+ q" r - #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */
1 P' \: x( x; c. S3 e: B/ p6 R0 U. K - #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */
* m2 b! m4 K$ l
- N* C. m# J. t/ c" i( r; @$ j
# X1 i/ C# ^; D" t- /* **************************************************************************** </font>
复制代码
/ m6 R: j; b7 h8 R7 }/ P" ^6 K8 u P" `" r+ W7 J
0 }% M6 ~9 g% Y& U 接着在main_F302.c的主循环中添加代码,让电机以500转速运行5秒停止5秒,再以6000转速运行5秒。对电机的控制方法ST 官方在注释中写得非常清楚,改变电机运行参数很方便。
+ M: ]7 s2 v; h/ S9 i9 b) g A. J m1 Z, m, i" O4 `$ W
& u4 [4 r d" z. I
) O+ Y; s7 A$ L$ r
( Z9 b' N! ]/ m0 p
main_F302.c
% c3 C$ p# P2 f: v w. G: W- <font size="3">/* ****************************************************************************
8 A: @& D& ^0 V& R - ==============================================================================
' d( J2 r- n& i9 ~ - ###### BASIC PARAMETERS ######5 E8 g9 m ?( b G, P* B) [1 _7 x
- ============================================================================== # S+ W) I& X2 c! K. M- s5 A
- **************************************************************************** */ # f; b/ d y) p7 T% ?$ q5 S4 s
- #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */: F' N& q+ j9 W# ^6 y
- #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/ ! C+ E9 V2 F6 U6 R( t* J9 }
- #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */
?# O! W4 e2 E, R( \4 z - #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ . ]) v5 {. T- |& i5 ]# @! a% p0 C
! D' x" Z+ k/ R `1 N+ d7 E
# B/ m3 L* {" H# P1 s- /* **************************************************************************** " v. O) y3 l2 w" Y; f
9 U! R$ I3 h6 \6 Z9 X- 6 y: A$ ?8 {/ k
" K$ g# T7 e U- ==============================================================================
' S u- P, [9 v3 G `- J - ###### How to use the 6Step FW Example project ###### p1 h7 R% H% a, U/ r% V
- ============================================================================== 0 R' P8 U1 B" k2 |2 Q0 z; S! d
- This workspace contains the middleware layer with Motor Control library to drive
4 v" D9 S! n( v+ k" d - a motor connected on X-Nucleo board performing a 6-step control algorithm
8 R+ l4 R, r7 J; I# ]# \* d5 C - allowing the motor speed regulation through a potentiometer. The 6-step algorithm ! L3 I5 ]4 p! l! ~
- is based on 1shunt current sensing mode and sensorless algorithm for bEmf detection.3 h- M2 _9 a. Y% \
- The workspace is provided for STM32Fxx-Nucleo in four different configurations, $ z: A* I$ {6 ~% M& P
- normal, demo, comm mode, boot mode. The "normal" mode waits the blue button event
/ R( Z* |$ p3 ^, D o - to start the motor, the "demo" mode starts and stop the motor automatically, the 7 L( O9 T( T" N
- "comm" mode enables the communication protocol with external PC terminal and the
6 B! b( g- l# B$ ], ^6 Y5 F, R - "boot" mode enables the FW for external boot loader.! G& e3 E/ x0 _4 U a2 A @
-
9 L+ j1 n4 w4 I% A3 A" Q2 d% ?( Y - A list of APIs is provided to send command to 6Step lib, for instance:) X* n( o% C G" k4 H) K M5 F: N
- , r: M% A1 H- V% i* o
- (#) MC_StartMotor() -> Start the motor# T- _' J5 z# Y' i, n: n5 ?- B ^/ I
- ( \. [, q9 X1 `3 e6 g) U W
- (#) MC_StoptMotor() -> Stop the motor$ [/ b& N' N( D
-
4 y0 u6 e6 U: n8 S) t" H& S, b - (#) MC_Set_Speed(...) -> Set the new motor speed
# u/ s; ?. n# v: o -
7 P! j3 u1 M% B - The MC_SixStep_param.h contains the full list of MC parameters 5 ~0 i$ y( s6 _ l) }; y& d/ H
-
; J. W! w2 g+ L5 d# ~, w) P5 A - ==============================================================================
& }0 q8 @, y- w/ ^: Y - ###### USER SPACE ######7 O/ X' o6 C6 a; r4 p9 v" i) D/ A
- ============================================================================== 4 v0 \; r; D3 n# ~& ^
- *****************************************************************************/ ; s" r, H6 \$ {' E* `1 m. y1 R9 ]
- ; d1 {; l5 }. S( ]
-
. X& X2 m+ ~+ l( b5 H, ]$ m1 Q - /****************************************************************************/
/ G3 m4 F+ l1 C) g. V - //添加的代码
0 f. d% S! u5 r - HAL_Delay(2000);3 B% T* o# G) f6 S8 E
- MC_Set_Speed(500);
( X: i: ^+ ^( D* L - MC_StartMotor();# J) x& H7 Y( U. ~& G2 a( i
- HAL_Delay(5000);
1 K& r0 z: W2 v - MC_StopMotor();. a9 A8 \+ q6 n* a" d
- MC_Set_Speed(6000);
% O. ^$ d7 b' v; @2 S0 A# L ] - </font>
复制代码
# l' A* Y7 I8 r6 m& W* S3 s- ^+ Z2 N/ ^9 B6 s
本次实验是6步梯形模式,按照要求对IHM07M1板子上的条线进行相应设置。编译下载,通电运行,电机已经能够按照预定参数正确运行了。
+ S4 m8 W" S3 K$ `% k( R& l* T% [* E a
, S) C& [3 Z+ B+ p: K% n0 c3 K, Y
]5 T" A+ \: a, D( f6 g
http://player.youku.com/player.php/sid/XMjk4NDc1OTkyOA==/v.swf* E3 m& k% M0 W9 S- C g8 c
& x ~1 V8 |) o5 t. k' k0 U |
谢谢! 这个我看过了,我手上没有ST的Noclude板子,有别的无刷驱动板,想要用ST的电机库配置一个适合自己的模板,现在这个文档里面没有教怎么使用 Workbench, 自己不会配。 不知道楼主有没有资料了,关于使用Workbench
举个例子,数控线切割机床就是步进电机控制,每给一个脉冲,电机就转动1个角度。楼上应该去参阅步进电机相关资料,比如数控机床控制等等。本人也是门外汉。
楼主,你用的电机是STM32F302吗?我这边想用STM32F302+IHM07M1驱动板上的电位器进行控制速度,我看您这个功能可能跟我这个有点相关,不知道您这边可以加个QQ交流一下吗?我的是625212098
都是ST 官方的:
' c {7 r5 e9 l% p( @
谢谢
只能用于ST官网的板子么?? 我手里有一块 无刷驱动板,MOS管的引脚连接的是TIM1的6个通道,和ST的驱动板子是不一样的,,不知道能不能用 这个程序 了???
https://www.stmcu.org.cn/document/detail/index/id-2181778 Z4 h+ k2 X) Q3 S. Y5 O
ST官方的电机培训资料,你可以参考
楼主你这个工程是用的STM32CubeMX生成的工程吗?
我也只是班门弄斧而已。工程不是CubeMX生成的,是ST官方下载的。
https://www.stmcu.org.cn/module/forum/thread-613011-1-1.html
步进电机每给一个脉冲转动一个角度,与无刷电机应该还是有区别的。
那么 就精准度来说,哪种电机更好一点呢