【STM32电机培训】4 修改代码控制电机运转
6 D! K$ {( V, _0 _7 X! m/ u' W6 k( `4 \1 U" [, N9 Q) k
; i1 e: a1 ^6 ?: o
通过参加电机培训,对STM32控制无刷电机控制技术有了进一步的理解。本次就针对STSW-STM32100\STM32CubeExpansion_SPN7_V1.1.0演示代码进行修改,达到让电机按照自己的要求运转。
2 I3 d, h% ~+ k& k$ L( G 首先先关闭电位器调速功能。在MC_SixSetup_param.h文件中的“BASIC PARAMETERS”有对电机基本控制参数的定义,将#define POTENTIOMETER 从1 改成0.
$ D! ?) L! R8 w% `4 V, M( t2 e' T, @/ k2 G
% s9 v' {" U; t# o9 k" x) e! k
MC_SixSetup_param.h9 H5 _: X- q7 q: h
- <font size="3">/* ****************************************************************************
9 }3 \3 Y1 {. t - ============================================================================== ! e; w7 w" |, ?2 t2 }! l* H
- ###### BASIC PARAMETERS ######+ T i: [& l7 L* |) m) [
- ==============================================================================
! V; J, |8 `9 X - **************************************************************************** */ 1 M# n# j/ f2 N8 _4 V
- #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs *// X$ e1 g: z/ q6 s
- #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/
; z2 b* O) A T5 \- Z4 c - #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */ / N5 _3 X7 ]0 Z2 {7 \- L
- #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ 6 U# n+ E1 Q0 |+ f+ E9 o" z4 ?( H \0 o
$ Q' ~$ o' \: k, X& ?7 i3 o. I
. B* ^$ J, a% j' e% F( }6 y( {7 l- /* **************************************************************************** </font>
复制代码
9 a$ a6 H$ S# Q, [# y8 m
& b0 |3 Z/ U0 F" J( [7 Z+ r( G
, D4 u4 O" R* B* [ w1 j8 f5 W 接着在main_F302.c的主循环中添加代码,让电机以500转速运行5秒停止5秒,再以6000转速运行5秒。对电机的控制方法ST 官方在注释中写得非常清楚,改变电机运行参数很方便。
. [$ ? v" ]' C7 b* n
" ?7 B9 a! L' W8 g- M ^& Y2 q# v+ b1 m% e, r: c* _
3 c( h6 m+ |' `! E
: G1 w p, c: L9 S: O( G- imain_F302.c
% @, A! V% n1 F! |, J- K- D- <font size="3">/* **************************************************************************** * I4 U5 h0 L" r+ X; B* ^
- ==============================================================================
$ R! i7 O( i5 w$ e6 _$ D8 V - ###### BASIC PARAMETERS ######
6 a2 l& e% I- d+ B$ J - ==============================================================================
6 {8 I2 _* Q+ ] - **************************************************************************** */
* q1 O3 ?( g$ K; ]3 E - #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */4 ^" O( ~4 O; X. H& I5 a5 n$ ]9 n3 ~
- #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/ * S9 C# q. o! S
- #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */
. h9 z/ V) c! Z4 k& D$ m% O: T - #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ 8 }6 n2 q& S$ q' E% N/ \0 Z2 a
- - q; F* l0 ~' J7 h: {+ Y0 g* m
" `! v% Y; `0 w( g: w0 q- /* **************************************************************************** ; H8 W/ q! \: k; g) l; _
- L U- i; w7 ^$ ^5 y- s- 7 H, W- B0 N8 C* o h U1 T
- 2 ?: q l2 P3 j- T/ b. L4 \' E
- ============================================================================== 0 r& {- R1 ~3 R a. a7 p, R
- ###### How to use the 6Step FW Example project ######& @5 _6 E1 J0 u1 [" d, B ]
- ============================================================================== ; I+ f i* I8 W1 K/ }
- This workspace contains the middleware layer with Motor Control library to drive
3 ]! {0 ]/ L( S9 g - a motor connected on X-Nucleo board performing a 6-step control algorithm B; |5 z( Z& G, R" L, ?- a F
- allowing the motor speed regulation through a potentiometer. The 6-step algorithm
/ d$ H, m, R0 G - is based on 1shunt current sensing mode and sensorless algorithm for bEmf detection.9 G5 `9 `* Y, }- { u. K2 b& O3 n
- The workspace is provided for STM32Fxx-Nucleo in four different configurations,
7 h$ L x( S0 q s" l4 ?- N1 w - normal, demo, comm mode, boot mode. The "normal" mode waits the blue button event 6 o6 v) B3 m6 {/ m& q- l) N
- to start the motor, the "demo" mode starts and stop the motor automatically, the
. z% l) I! r% G7 u$ V - "comm" mode enables the communication protocol with external PC terminal and the
8 d* P# T+ k& u' j - "boot" mode enables the FW for external boot loader.. T% \ j* V% h- Q1 @: [
- + G! [0 t* k/ j* N+ A
- A list of APIs is provided to send command to 6Step lib, for instance:
; S9 d8 Z( X7 D* q/ `8 L - - v. Q$ D/ Q# i; W- ^1 `# H
- (#) MC_StartMotor() -> Start the motor
, w1 G# d8 ]7 y8 M6 ^! C3 U - , w0 f) O- w5 e; ]9 f
- (#) MC_StoptMotor() -> Stop the motor" W6 K+ x' x# ]) N
-
; y, L( F* p6 N% \. w- M - (#) MC_Set_Speed(...) -> Set the new motor speed
6 v. E! Z0 @: `) q# N2 m - 3 ?9 J9 [5 d, B, W) r, b$ l y
- The MC_SixStep_param.h contains the full list of MC parameters
2 N# \' U+ c2 s7 f4 ^+ X - & Y8 i+ B9 ^! C9 i
- ============================================================================== ^/ g- p* K0 O5 K) n- t% j
- ###### USER SPACE ######) F* X/ W* Q, ^( }9 U- W
- ============================================================================== 4 y6 C5 W/ I r' j4 o! t" U
- *****************************************************************************/ , M. i% L5 h6 Z- L: T
- 4 |5 r g9 p; o/ {: h( o5 w
- : L' u" L1 L; Z% m! F9 n( _5 z
- /****************************************************************************/
# x, u! R& g, j/ S# r# w. v - //添加的代码
% M) k0 }; v4 B, e - HAL_Delay(2000);
, e1 R, E, w$ `2 P - MC_Set_Speed(500);4 [" c$ [1 Z/ X( U0 q, ?& _
- MC_StartMotor();
) W: } n/ f7 R6 _3 q - HAL_Delay(5000);! v7 X( Q/ C& l; _1 {# U- \
- MC_StopMotor(); T+ l; ~" A0 { o7 j9 l
- MC_Set_Speed(6000);# e9 L6 S' N1 ~4 o5 [. s1 u- e6 G
- </font>
复制代码 6 w* j( N% O% ^( W0 F
) c& c$ w; S1 h6 C. t 本次实验是6步梯形模式,按照要求对IHM07M1板子上的条线进行相应设置。编译下载,通电运行,电机已经能够按照预定参数正确运行了。
) F# q0 D V& S/ N/ U9 [& v; A8 A3 k" q- C6 O. c0 `
H1 {) J# w! j; G. S
# {# v" O! L/ p% E4 U# A+ O
http://player.youku.com/player.php/sid/XMjk4NDc1OTkyOA==/v.swf
% h2 \( _! ^2 R p$ H' {1 ~( v- L6 j4 X
|
谢谢! 这个我看过了,我手上没有ST的Noclude板子,有别的无刷驱动板,想要用ST的电机库配置一个适合自己的模板,现在这个文档里面没有教怎么使用 Workbench, 自己不会配。 不知道楼主有没有资料了,关于使用Workbench
举个例子,数控线切割机床就是步进电机控制,每给一个脉冲,电机就转动1个角度。楼上应该去参阅步进电机相关资料,比如数控机床控制等等。本人也是门外汉。
楼主,你用的电机是STM32F302吗?我这边想用STM32F302+IHM07M1驱动板上的电位器进行控制速度,我看您这个功能可能跟我这个有点相关,不知道您这边可以加个QQ交流一下吗?我的是625212098
都是ST 官方的:
谢谢
只能用于ST官网的板子么?? 我手里有一块 无刷驱动板,MOS管的引脚连接的是TIM1的6个通道,和ST的驱动板子是不一样的,,不知道能不能用 这个程序 了???
https://www.stmcu.org.cn/document/detail/index/id-218177
ST官方的电机培训资料,你可以参考
楼主你这个工程是用的STM32CubeMX生成的工程吗?
我也只是班门弄斧而已。工程不是CubeMX生成的,是ST官方下载的。% K0 b, N" r4 _: s" i* T" r0 N
https://www.stmcu.org.cn/module/forum/thread-613011-1-1.html
步进电机每给一个脉冲转动一个角度,与无刷电机应该还是有区别的。
那么 就精准度来说,哪种电机更好一点呢9 B& C+ v# j, `5 E4 q