【STM32电机培训】4 修改代码控制电机运转
6 V; C9 `# L) D7 G$ C7 P3 g9 |7 o) _. A( D( A/ Y
. }5 ?% C- T) ?3 |+ Z+ v1 @. h
通过参加电机培训,对STM32控制无刷电机控制技术有了进一步的理解。本次就针对STSW-STM32100\STM32CubeExpansion_SPN7_V1.1.0演示代码进行修改,达到让电机按照自己的要求运转。
: G+ W: [7 x* E- V2 c7 |6 [ 首先先关闭电位器调速功能。在MC_SixSetup_param.h文件中的“BASIC PARAMETERS”有对电机基本控制参数的定义,将#define POTENTIOMETER 从1 改成0.
1 H2 n" z# J- A9 t% J' b
9 N/ i2 Z- ^' }0 G1 l6 ]. B# w* J6 [1 I- ?& N' G2 b
MC_SixSetup_param.h3 @; Y7 f! c8 X: q, ] t
- <font size="3">/* **************************************************************************** \% J5 w2 Q' p) @1 j
- ==============================================================================
/ u# Q+ W! d r! u& C1 i; f - ###### BASIC PARAMETERS ######
/ b1 e( g, `3 q" `7 U - ==============================================================================
1 \ R$ v7 e0 U2 O' ~& Y - **************************************************************************** */
2 P0 k& _- k. o - #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */- j( r! H, ?, \: n. R0 D+ P
- #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/
* e8 t0 o) p7 ~$ Y# S& W' _9 Z) d - #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */ 6 f" l4 u1 |- K
- #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ $ G" d9 w2 J0 u4 J
- 3 `/ f$ M6 Z/ Q% W9 P
; q9 o" { e5 ]& W: t- /* **************************************************************************** </font>
复制代码
0 c2 L: w; [# u9 U. }/ G7 L z/ | t# G9 W
+ `: F6 y3 e' |7 q) v 接着在main_F302.c的主循环中添加代码,让电机以500转速运行5秒停止5秒,再以6000转速运行5秒。对电机的控制方法ST 官方在注释中写得非常清楚,改变电机运行参数很方便。: {' r" d2 ~6 @4 x n" _
( o5 J& Z- p T# c
# h8 x# d1 `4 R) b
* q) R: A1 ~, z2 c8 }
, F& E$ i* u6 x; E6 C7 }/ w }main_F302.c
/ X4 w9 A, w. d# s2 D) q9 M/ D* D3 d- <font size="3">/* ****************************************************************************
8 \* Q0 N" Z0 Q9 X t - ==============================================================================
* ^8 ?: U' R8 u3 j8 c - ###### BASIC PARAMETERS ######1 D/ L& X Q" O) R' `2 g) `
- ==============================================================================
3 E+ S! S$ m! f- O& W9 b - **************************************************************************** */
6 _! C6 A! r: q2 d, S" T+ D* c - #define NUM_POLE_PAIRS 7 /*!< Number of Motor Pole pairs */4 g2 O( P) o. z2 U+ z) P+ D
- #define DIRECTION 0 /*!< Set motor direction CW = 0 and CCW = 1*/
0 Z6 g- e' B% N% k - #define TARGET_SPEED 3000 /*!< Target speed in closed loop control */ 4 r1 a+ K* `# Z. f. g
- #define POTENTIOMETER 0 /*!< Enable (1)/Disable (0) the potentiometer */ & t% x5 l% w8 C( h
- ( V/ _. [3 `1 U8 h0 _( p! O( h
- 1 g( n# R9 o! c# u' P5 ~
- /* **************************************************************************** & G$ s' i$ v" Y B; C' ]
( m+ q% D# I, x3 u$ k9 I$ {: H# O- * Y1 F" e' K3 y# Z$ ?! m
- ) T5 D- c6 g- Z( R( o
- ============================================================================== & `" T- x. h r
- ###### How to use the 6Step FW Example project ######) v+ W& N) @' h2 I# F9 ]. v2 E
- ==============================================================================
/ D- f4 {6 w# i" t( k# e% ]* k - This workspace contains the middleware layer with Motor Control library to drive 1 M3 L& ~6 n2 X3 d5 {
- a motor connected on X-Nucleo board performing a 6-step control algorithm
) }4 |2 [1 z/ v2 t7 |3 N$ y% S* O2 x - allowing the motor speed regulation through a potentiometer. The 6-step algorithm
6 ~1 v _" |- B - is based on 1shunt current sensing mode and sensorless algorithm for bEmf detection.. f. a: Q/ }' e' ]1 i
- The workspace is provided for STM32Fxx-Nucleo in four different configurations, , C0 I5 V4 B1 q c, ?
- normal, demo, comm mode, boot mode. The "normal" mode waits the blue button event 2 L: Y! I9 R- k9 J8 y( l
- to start the motor, the "demo" mode starts and stop the motor automatically, the 9 C1 r- G% p5 A0 u
- "comm" mode enables the communication protocol with external PC terminal and the 8 Y; X \* a& P+ m, c& h
- "boot" mode enables the FW for external boot loader.
* N0 Z: z% X' p/ }6 Z3 {4 D2 W! ^ - " J1 T0 j3 B- j1 e" s! b3 Y
- A list of APIs is provided to send command to 6Step lib, for instance:8 I! j/ |# Q+ F2 r
- ( m5 r9 |, w) C# y6 b G
- (#) MC_StartMotor() -> Start the motor# ~6 a; k- H8 f7 ^& I- m
-
) k) I/ `, }2 ]) E6 a6 E) S9 z - (#) MC_StoptMotor() -> Stop the motor
1 T2 x" r9 z" W. y. L - 0 d$ l8 n! j8 S1 \9 t
- (#) MC_Set_Speed(...) -> Set the new motor speed
7 e3 y. g7 c9 ^# ]+ d/ m0 Q1 v -
$ o6 P4 @7 L( b* z, c$ l - The MC_SixStep_param.h contains the full list of MC parameters
8 h5 }) r, @. k4 Q: [ -
0 d! j& v1 U% d1 k. v1 S" }3 w4 j2 r - ==============================================================================
, j* f) M" A7 o+ E4 L& W/ L - ###### USER SPACE ######
! b& J& z, f& |8 | - ============================================================================== ' F \, S: D. N* d9 w, L
- *****************************************************************************/ * t$ N; P; R8 ^1 V2 p6 r4 t
- + O' {6 l5 W# _3 a8 O2 ^, W- ~
-
8 U- N) O+ `4 c2 u2 Y* ` - /****************************************************************************/ : ]4 S" l9 u% h. x. E
- //添加的代码
3 h. ~! Q; L3 w: e7 p - HAL_Delay(2000);$ X! ?& R- |4 w+ ~' h
- MC_Set_Speed(500);
8 b/ d9 y7 [2 P - MC_StartMotor();9 a' d- _% c# F3 k; _. p% V2 A4 c
- HAL_Delay(5000);. w# B3 b' ^7 e, i8 }* c: v! ^5 r
- MC_StopMotor();
$ n \* Q: {" q$ L - MC_Set_Speed(6000);
$ c- M+ J) f. o4 n4 l/ ]% i - </font>
复制代码 I2 I6 q5 [& y; @. A
" a) F: w7 ?3 O! L& c- o
本次实验是6步梯形模式,按照要求对IHM07M1板子上的条线进行相应设置。编译下载,通电运行,电机已经能够按照预定参数正确运行了。
, J+ T7 b/ G7 ~
: y0 |; U) T$ A; b$ g
m9 b/ m0 u8 a9 Q$ B
2 c% g9 ~/ a& i, J: \$ i7 ]. ~http://player.youku.com/player.php/sid/XMjk4NDc1OTkyOA==/v.swf% M5 l \' N6 p3 v2 M
/ }$ A$ K+ [% E% u) b |
谢谢! 这个我看过了,我手上没有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官方下载的。2 o; R3 I8 h9 q2 g7 }7 M
https://www.stmcu.org.cn/module/forum/thread-613011-1-1.html
步进电机每给一个脉冲转动一个角度,与无刷电机应该还是有区别的。
那么 就精准度来说,哪种电机更好一点呢