本帖最后由 子曰好人 于 2018-9-2 10:21 编辑 ! _1 ?/ n8 u6 O) g* \2 {7 L
; x% w, I/ ]1 l' r$ @: C经过前期的准备,可以使用电机套件完成一些实验任务了。 首先是任务1和任务2的要求: 任务一和任务二基于workbench生成的代码来做并不难,因为代码里面提供了很多api函数并且做了很多软件上的保护,可以放心写代码,硬件不会那么容易损坏。
9 K5 N4 m2 M- @+ ]# z' j' `拿到一份陌生的代码除了了解我前面几篇帖子所说的内容外,在开始写代码前还需要了解api函数。我们可以打开mc_api.h看一看workbench都给我们提供了什么函数。
# v/ C! W$ F6 S, A, X! m9 q - /** @addtogroup MCIAPI x' |0 R5 T$ h1 p% k( W6 `3 B+ D% {
- * @{" F8 y$ Y6 q% d% p( n7 y* W* K
- */
8 w7 S) Q" S7 S( \' P7 M
d3 [. |5 i2 H9 u# |% P1 o8 W% n- typedef enum {UDRC_STATE_IDLE, UDRC_STATE_REQUESTED, UDRC_STATE_EOC} UDRC_State_t;
1 U& c+ J4 w* L/ n' P
( U3 O1 N" t0 K- /* Starts Motor 1 */
$ A- T7 ?% G7 N - bool MC_StartMotor1(void);
8 N- ^5 r- f$ E
$ o r5 h B! e( f) S* Y- /* Stops Motor 1 */
5 f+ Q# b' a" y5 L - bool MC_StopMotor1(void);# r+ J/ |, A* E
6 ^2 ]0 r' x# S" y- /* Programs a Speed ramp for Motor 1 */
) m. L% O9 e& c+ I( j7 j g - void MC_ProgramSpeedRampMotor1( int16_t hFinalSpeed, uint16_t hDurationms );
. x0 J4 L3 E5 i( _$ ]9 K
' M/ T4 a3 P9 Q# N$ c8 H- /* Programs a Torque ramp for Motor 1 */9 w" Z$ V7 @. X/ @$ I+ b) _# Y# h1 E2 A/ c
- void MC_ProgramTorqueRampMotor1( int16_t hFinalTorque, uint16_t hDurationms ); Y5 ]" W, @. c
- / N3 y4 @0 ?% n% o8 r: D
- /* Programs a current reference for Motor 1 */2 L3 q5 Z9 ~# Z6 X. _8 W* {
- void MC_SetCurrentReferenceMotor1( Curr_Components Iqdref );/ I9 z* X6 |$ l+ ^
- p4 G% b3 l d0 V% V- P
- /* Returns the state of the last submited command for Motor 1 */( R7 }% @+ q: v1 z2 r
- MCI_CommandState_t MC_GetCommandStateMotor1( void);
" p k; z0 v. x& n
0 _8 q& I5 A& k2 M. j5 I- /* Stops the execution of the current speed ramp for Motor 1 if any */6 S3 ?( B# S7 o- M9 J
- bool MC_StopSpeedRampMotor1(void);% ~& n9 l! i# u X; r( T/ t
- 4 t: Z& E( i* ]! E9 ]
- /* Returns true if the last submited ramp for Motor 1 has completed, false otherwise */
% L/ L. A: B8 `+ Y, Y - bool MC_HasRampCompletedMotor1(void);
2 N( a3 B _5 p; ^( S4 l3 h* V
: {- ?* t; X! u2 W3 `6 G- /* Returns the current mechanical rotor speed reference set for Motor 1, expressed in dHz (tenth of Hertz) */+ R4 s+ G! e6 F' B- i, Q2 C, ?
- int16_t MC_GetMecSpeedReferenceMotor1(void);
# f! n, q7 K# X2 e& w4 Z% {3 y
U' _3 Z% d& [; V: A' n- /* Returns the last computed average mechanical rotor speed for Motor 1, expressed in dHz (tenth of Hertz) */7 B' v! l n5 V! P6 ?+ L
- int16_t MC_GetMecSpeedAverageMotor1(void);
6 [) ? j* j6 H - / u3 `. i7 m) l- v4 x4 A- P# C
- /* Returns the final speed of the last ramp programmed for Motor 1, if this ramp was a speed ramp */
" Q! T9 T5 c. f: T4 r# O! j - int16_t MC_GetLastRampFinalSpeedMotor1(void);
7 U9 g# T0 A7 r% ?
, {. k# x0 V" K0 J6 G3 D, o- /* Returns the current Control Mode for Motor 1 (either Speed or Torque) */7 L+ Z; f a! s( V
- STC_Modality_t MC_GetControlModeMotor1(void);1 o" F4 x7 P2 b7 Z3 f! k% o
% s' i5 ~6 M4 f8 @- /* Returns the direction imposed by the last command on Motor 1 */: X& L6 `0 r7 `
- int16_t MC_GetImposedDirectionMotor1(void);
. q3 A! a! m5 ?6 w# W/ F0 ^4 K - % S8 l$ A1 a& z: Y
- /* Returns the current reliability of the speed sensor used for Motor 1 */# u9 X% U* a7 Q1 M4 p( U1 n4 ^6 V# s
- bool MC_GetSpeedSensorReliabilityMotor1(void);; t0 |3 _+ }( F3 n. b4 |
7 |1 v) u3 j$ p$ E; \* |- /* returns the amplitude of the phase current injected in Motor 1 */
+ A" Z5 @" c- J" p. G3 d - int16_t MC_GetPhaseCurrentAmplitudeMotor1(void);% g. t" t7 x5 n9 j/ D& i; v
- 4 B2 w3 ~5 K/ g
- /* returns the amplitude of the phase voltage applied to Motor 1 */
( [% f1 `# l+ v2 o - int16_t MC_GetPhaseVoltageAmplitudeMotor1(void);
2 L2 q7 }2 Z; ?# V# o - + _" t4 [- q; b `5 q9 @9 z# R2 R
- /* returns current Ia and Ib values for Motor 1 */
, B/ Q: q0 L) v) ^ - Curr_Components MC_GetIabMotor1(void);, g* W( P! t) K
' K5 F W0 X) t* F3 C0 S# p% i- /* returns current Ialpha and Ibeta values for Motor 1 */7 \" O# ]; F% N) ~# P: y( f, [; C( G+ W
- Curr_Components MC_GetIalphabetaMotor1(void);# X5 |2 G& Y2 ~
, {2 K! c" l9 Q) D( \3 w. G7 m- /* returns current Iq and Id values for Motor 1 */
( y4 `# N' }; l/ [& q; k - Curr_Components MC_GetIqdMotor1(void);
0 s& x g! b5 _' N5 i* U& s - , L) v) y7 L& f% o( J' G
- /* returns Iq and Id reference values for Motor 1 */
{2 f! B8 {& @/ { - Curr_Components MC_GetIqdrefMotor1(void);
& ^- }, u$ Z' s - 2 t9 B6 @; ^; k
- /* returns current Vq and Vd values for Motor 1 */
}0 y" K0 C! H( o6 `' s7 E - Volt_Components MC_GetVqdMotor1(void);3 R; w$ T3 C' e
8 C/ R( j+ u; {. Z) I W8 C- /* returns current Valpha and Vbeta values for Motor 1 */; I) j! x/ N6 k; @ c
- Volt_Components MC_GetValphabetaMotor1(void);3 ^; j" j1 P% k4 ^0 }7 v
, B: c) N/ E2 N/ U4 k/ j Y3 q- v- /* returns the electrical angle of the rotor of Motor 1, in DDP format */: p3 v" h! F& e
- int16_t MC_GetElAngledppMotor1(void);! U1 j2 P- v3 M3 |7 | L
- 4 Y7 T* C1 f8 ?# a6 q
- /* returns the current electrical torque reference for Motor 1 */
: a3 q1 R/ P( w) Q0 n - int16_t MC_GetTerefMotor1(void);
) f+ X: x+ ~: U+ A I) v
, P. r( b- }) Y0 z0 @- /* Sets the reference value for Id */
& Q! ]9 l8 G0 B/ k - void MC_SetIdrefMotor1( int16_t hNewIdref );
* U; y$ X1 \4 p; i9 \ - 2 \+ I- g5 Z# @3 u& Q; n8 P
- /* re-initializes Iq and Id references to their default values */
9 d; o/ Y6 x3 Z+ `& \ - void MC_Clear_IqdrefMotor1(void);
: h8 i* Q, |& I; W3 }0 D" Z5 H - 4 B7 j5 G3 q9 h8 f5 O
- /* Acknowledge a Motor Control fault on Motor 1 */+ S B$ l. b+ y3 z0 r" O5 o
- bool MC_AcknowledgeFaultMotor1( void ); r; ^/ f" `3 w5 t
- $ Y* d9 ]' d, ^0 W" g4 L
- /* Returns a bitfiled showing faults that occured since the State Machine of Motor 1 was moved to FAULT_NOW state */
H( k) T% T: a4 {! j) E% Z, O0 ? - uint16_t MC_GetOccurredFaultsMotor1(void);" T( x: Z( E4 l' K5 I( `( f
) k! R. i7 x' Y. ]- /* Returns a bitfield showing all current faults on Motor 1 */
5 |( W' C( |9 e% \" j y - uint16_t MC_GetCurrentFaultsMotor1(void);
. W& i% S( R1 u' u/ l) D' L - ; b% w/ f9 q2 Q) o, F w
- /* returns the current state of Motor 1 state machine */
) d- C0 e/ \ t0 P - State_t MC_GetSTMStateMotor1(void);" z: K, J1 d/ R3 p5 e; ]9 j
5 z" _' `) Z: b- u! N: Z" H- /* programs a user defined ADC regular conversion */
2 K0 _8 X: Y0 i+ `# u* M! m - void MC_ProgramRegularConversion(uint8_t bChannel, uint8_t bSampleTime); g5 _7 ^0 R* M) K2 A9 @8 y
3 M3 V5 I' m6 S1 I9 N" S4 x( E- /* Returns the value of the last executed user defined ADC regular conversion */
5 N) R9 V( N6 b; q - uint16_t MC_GetRegularConversionValue(void);' n- d) K0 l6 E6 l' } K# }& |
1 Z A% O2 N! }% Q. l. A6 u- /* Returns the status of the last requested user defined ADC regular conversion */
+ ^' j9 q6 @! G8 S, K2 n - UDRC_State_t MC_GetRegularConversionState(void);
复制代码上面这些api函数是对单个电机的操作,包含了启动、调速、调转矩、以及电机各种状态的获取。 看了任务一的要求,个人认为电机控制还是需要与外部交互好一点,电机启停不可控对于大功率的电机控制来说比较危险。 任务一代码: - void task_1(void)
3 P1 b; H' P( B0 [3 j7 d - {
- }0 S/ f0 F8 ?6 u) j4 X3 | - MC_ProgramSpeedRampMotor1(3000/6,1000);
6 r E1 p9 W* C% q: H' C5 S - MC_StartMotor1();
$ j8 k/ m# d; M$ ~; ^/ y2 Z. ^ - HAL_Delay(10000);5 L' z' h: O2 i) Q1 V" }
- MC_StopMotor1();( @% w0 N- v) ?: |
- HAL_Delay(10000);
* a: Z) E8 @3 d8 [5 }7 m - }
复制代码主函数添加的代码: - /* Infinite loop */" n' f3 b3 V& w' e) h* e/ n% F
- /* USER CODE BEGIN WHILE */* j5 {) b8 I* Z" N; T4 u' B: |: {$ \
- while (1)0 M8 C# I" f7 |# }3 l) u7 A) L/ s
- {
* f2 z$ h2 j# R; [# a$ P8 r5 c9 P -
6 T- _5 b7 y7 U* T; {) | - if(GPIO_PIN_SET == HAL_GPIO_ReadPin(LED11_GPIO_Port,LED11_Pin))
% l5 p! a, o1 ?0 b; u& K R, Y) m# b - {$ A- o! b" u* H6 l
- task_1();: U3 B# h8 n F6 ^2 X
- }
: U A" p6 R8 K7 w) @ - else3 o' H c' |+ x& h
- {
$ X% i9 W" @9 j: c/ F - MC_StopMotor1();
5 ~' ^) @7 Y; t9 Y0 V - }
7 \, h" ~: H3 {) q1 B - realspeed = MC_GetMecSpeedAverageMotor1()*6;
% f1 e) N! N! z& U# Y/ ?6 ?& H' t - /* USER CODE END WHILE */
复制代码按键中断处理函数代码在ui_task.c这个文件中: - void UI_HandleStartStopButton_cb (void)& b. |! A7 d# l/ E; p
- {
' Q1 L. p* k& e* P6 o - /* USER CODE BEGIN START_STOP_BTN */# [! k& h7 e* t \: C
- HAL_GPIO_TogglePin(LED11_GPIO_Port,LED11_Pin);
" I. n' {# @: Y6 D3 o( x0 ` - /* USER CODE END START_STOP_BTN */
U" G& M3 b1 v0 r' y - }
9 s* @9 H* y0 j9 W* ?7 h: z: S- T
复制代码这样可以通过按键来控制任务的运行并且可以通过led的亮灭来观察系统所处的状态。LED11是驱动板上的一颗led,引脚号是PB2,可以通过cubemx来设置初始化代码。 通过monitor可以看到速度变化曲线图: ok,任务一完成。 -------------------------------------------------------------
0 f9 t! l y9 w: T& W3 O 任务二代码: - void task_2(void), P2 \$ h7 N( q, C& i$ G
- {
; K N/ o; k' F4 M - int16_t Speed_Kp,Speed_Ki;' C. \ d" e) d; y, Z( B
- MCT_Handle_t * pMctHdl;
: q! L5 r, N/ ]/ }" ]. d - MC_ProgramSpeedRampMotor1(3000/6,1000);
3 \+ ?, k2 V4 J6 c; ^$ X - MC_StartMotor1();
5 M' ~6 X5 u0 P: V' E - HAL_Delay(3000);) X0 v, r0 `0 C. J2 a$ b
- MC_StopMotor1();
; G1 ^* Q+ p, |& Z - HAL_Delay(1000);
4 ~1 m% z7 Z0 g9 y6 `. q - pMctHdl = GetMCT(M1);+ D' n& i# P; ?* w
- Speed_Kp = PID_GetKP(pMctHdl->pPIDSpeed);
5 i+ V- w' C. K6 @" w0 K, l( I: f, b - Speed_Ki = PID_GetKI(pMctHdl->pPIDSpeed);5 z( b( \. V m& u& x. Y3 g
- PID_SetKP(pMctHdl->pPIDSpeed,Speed_Kp*2);* b/ c7 _$ f& U: Y' V) i+ Q1 R
- PID_SetKI(pMctHdl->pPIDSpeed,Speed_Ki*2);( W+ z4 {/ p3 O/ K H
- MC_ProgramSpeedRampMotor1(3000/6,1000);, V6 C; H! Z; k1 j- u
- MC_StartMotor1();+ ?- H/ @- B, j5 c j
- HAL_Delay(3000);
9 n! g) K4 Q0 @( ]- Y% q% D - MC_StopMotor1();
' k3 U) A, W, K7 Q" _" W( B - HAL_Delay(1000);
; ]8 L% d' _; a/ ?) s% ] P - PID_SetKP(pMctHdl->pPIDSpeed,Speed_Kp/2);# y) r; i/ x' q& `# Q7 W
- PID_SetKI(pMctHdl->pPIDSpeed,Speed_Ki/2);
: t( Z* t% d: [, Z: w" ~& ^+ D; I - MC_ProgramSpeedRampMotor1(3000/6,1000);
- j- I- \" z) B3 \9 R: ]3 V - MC_StartMotor1();) [1 c% V5 ]. O1 V# F
- HAL_Delay(3000);: J, Z/ S/ K _/ X
- MC_StopMotor1(); : G' ^' Y# N2 [
- HAL_GPIO_WritePin(LED11_GPIO_Port,LED11_Pin,GPIO_PIN_RESET);5 W+ c5 s, s( @# N
- }
复制代码主函数代码和任务一相同,只是把task_1()改成task_2()就行了。最后一行把LED引脚拉低是为了让代码只运行一次,多次运行可以通过按键进行操作。 可以看到和预期结果是一致的,原始PI调节速度快,有超调现象;Kp、Ki同时增大一倍的时候,调节加快了,并且超调现象被抑制了;当Kp、Ki同时缩小一倍时可以看到虽然调节很快,但是有严重的超调现象。
' E" \/ u7 I+ L+ d. V7 w 2 E- P; T6 X4 l, d( q, k, @& y
通过monitor的图形显示速度波形极大的方便调试PID的过程,ST考虑得真周到。 + t( F+ ]( U% e. w3 W7 e5 K
我在这里有个疑问,这个plotter只能显示参考速度的波形吗,还能不能添加其他变量来查看系统某个变量的变化趋势呢。之前我一直是用的Jscope,但是Jscope需要连接Jlink来查看,对于这款套件来说需要额外连接Jlink调试器,有一点点麻烦。后面调试有必要我还是会使用Jscope来查看一些变量的波形,并且分享到论坛。
1 v% N& N" |4 F, t$ t1 i * R2 E3 X8 x7 p+ m2 N
任务一和任务二已完成,欢迎大家留言讨论。8 B) Z. W: |0 r6 ~& F- |) y' ]' p. C7 z
( k( s8 w% h% a8 q
, K& U) f5 w( v$ P) q7 s; ~6 R6 J. R2 w3 W" F2 \0 t
8 X; m! h5 U3 J; o; z V" @ |
/* Programs a Speed ramp for Motor 1 */5 d, i7 X9 D, L, x5 K1 p1 {
void MC_ProgramSpeedRampMotor1( int16_t hFinalSpeed, uint16_t hDurationms );2 _2 q5 O6 ?' J1 e K
这个函数的第二个参数你改过吗,第二个参数就是达到目标值的时间,越小加速度越快- z0 L) L6 K" d' a' }$ V$ T# n2 u
+ k) q2 m0 a9 j; C; T; [, E
考虑另外一个问题,电机惯性较大,电机驱动板的驱动能力有限,也许最多就只能10+s才能达到目标转速
1.在我的程序里按键的作用起的是传递信号的作用,按键中断中翻转LED11引脚,主函数通过读取LED11引脚电平值做是否启动任务的判断。3 h, u2 ]" ~, I
2.在我的代码里启停是可控的,通过LED11引脚电平来判断是否执行task1。另外,task1的要求就是延时不断启停,我这样写是没有问题的
直接扔好几个问题
回复20#和21#:; ]3 o. R, Z' i9 I+ m
User_label那个自己想写什么名字无所谓,根据自己喜好就OK8 b6 U# n3 m5 L+ E
回复22#和23#:! ~5 N/ S4 w x! U8 K' ~9 B
不用跟我的代码完全一样,这个只是我用于控制实验进行与否的一种方式,你也可以通过其他方式来做
好的,有时间我去了解一下通讯协议的代码,尝试显示其他变量。另外一个就是觉得plotter采样率有点低,Jscope采样率1kHz在好些时候都显得不太够用。
我没试过其他电压,一直用的12V,你是不是正负极接反了
不是,我是从24V慢慢升上去的,
我后面尝试一下高电压再给你反馈吧,主要是现在我也没有高电压的电机,所以一直用的12V
好的,还是小心点吧
上电40V驱动板坏了,底板还是好的,这个可能需要告知一下ST了
板子和上位机通讯协议是啥
我还没看,你可以去串口中断函数中了解通信协议
正常呀,这属于C语言基本功,MCT_Handle_t这个结构体在mc_tuning.h中定义的,你要在其他文件中使用当然要包含定义这个结构体的头文件嘛
那就好,我还以为是我工程建立的有问题