本帖最后由 子曰好人 于 2018-9-2 10:21 编辑
! P1 z' X# ^8 B5 D5 i9 y) J" m1 ]9 A+ u- d- T
经过前期的准备,可以使用电机套件完成一些实验任务了。 首先是任务1和任务2的要求: 任务一和任务二基于workbench生成的代码来做并不难,因为代码里面提供了很多api函数并且做了很多软件上的保护,可以放心写代码,硬件不会那么容易损坏。
- @' o6 k f- p1 P7 Y拿到一份陌生的代码除了了解我前面几篇帖子所说的内容外,在开始写代码前还需要了解api函数。我们可以打开mc_api.h看一看workbench都给我们提供了什么函数。7 y4 y1 l6 M: q6 ~ I' h# I
- /** @addtogroup MCIAPI1 G, u# C& Z0 S3 `/ _; x
- * @{0 a# W$ G# u& v
- */8 m2 l0 u! Q8 P# y/ D' T
- ! y# o( ?. o& A) z9 d5 u
- typedef enum {UDRC_STATE_IDLE, UDRC_STATE_REQUESTED, UDRC_STATE_EOC} UDRC_State_t;
/ A$ ]1 c) T# W4 y1 v z - 2 w9 Z" z4 i$ K* X" r4 {6 R; I
- /* Starts Motor 1 */1 v! B; P3 q1 |
- bool MC_StartMotor1(void);
% i. L, ?# I% d4 S+ h
' d% X9 F$ }# w6 J6 W- /* Stops Motor 1 */. d9 _: C7 ]" M4 T
- bool MC_StopMotor1(void);8 S( E$ c6 D3 ?. r, V+ @3 ]; u
2 r, y! z& n) L7 ]+ ]! \# [- /* Programs a Speed ramp for Motor 1 */
$ o# i- r, Y) @2 @8 h - void MC_ProgramSpeedRampMotor1( int16_t hFinalSpeed, uint16_t hDurationms );
- \ y8 m7 x0 R( L {$ s; h - : U j2 R: \8 A+ x" k
- /* Programs a Torque ramp for Motor 1 */* Q2 B/ C! E) c" M$ h W
- void MC_ProgramTorqueRampMotor1( int16_t hFinalTorque, uint16_t hDurationms );
" C1 P; w n: x. J+ w& D - 0 X# }% s# Q0 Q& S" T# M. U5 J
- /* Programs a current reference for Motor 1 */
. v7 f& [: K- f3 N+ [ - void MC_SetCurrentReferenceMotor1( Curr_Components Iqdref );* y$ N5 W. e( d" E
- 0 h' ~5 P2 ?) C; o& J% O
- /* Returns the state of the last submited command for Motor 1 */$ C5 s& d2 k# m% x+ ~" P, O
- MCI_CommandState_t MC_GetCommandStateMotor1( void);8 g- a: {4 [# d2 B/ N7 n. N6 ^
/ |4 n9 [! u8 s0 X$ A. S8 x* U/ @- /* Stops the execution of the current speed ramp for Motor 1 if any */1 H+ g# ], D$ D" a2 g9 x
- bool MC_StopSpeedRampMotor1(void);6 u$ [1 Q8 I5 Y
( x1 u. z# I# I5 ?/ o- /* Returns true if the last submited ramp for Motor 1 has completed, false otherwise */9 A! |" P4 e' o% d
- bool MC_HasRampCompletedMotor1(void);
5 D2 Y$ K! A7 H6 F/ U* L - # Z- W. K" v+ ~$ g N( s) r- z, q) A
- /* Returns the current mechanical rotor speed reference set for Motor 1, expressed in dHz (tenth of Hertz) */
{6 {: {( m4 G - int16_t MC_GetMecSpeedReferenceMotor1(void);9 ^' A4 C; I6 \! }
1 k9 W, U0 x+ v( W q- /* Returns the last computed average mechanical rotor speed for Motor 1, expressed in dHz (tenth of Hertz) */
. {( d5 y* e% D ]* ? - int16_t MC_GetMecSpeedAverageMotor1(void);! w# Y( a n* h8 r
- " @. L- a4 \4 P2 m3 M1 k; {
- /* Returns the final speed of the last ramp programmed for Motor 1, if this ramp was a speed ramp */8 |- y) K4 E8 S! w" C
- int16_t MC_GetLastRampFinalSpeedMotor1(void);
2 ]7 o, p: l" h3 M2 b9 g# w
! B5 w& Q0 }/ {3 F/ b, _$ ?( w2 n: p- /* Returns the current Control Mode for Motor 1 (either Speed or Torque) */& Y1 E s) D# Y; @9 X- F$ Y) W
- STC_Modality_t MC_GetControlModeMotor1(void);- L+ v. ^- P F8 h' J
: I# L) w* d: M6 Y; Z" |' Z- /* Returns the direction imposed by the last command on Motor 1 */
0 H7 a6 O6 h, r+ C: f+ M* [ - int16_t MC_GetImposedDirectionMotor1(void);
' B! l% F: d8 p. { - ! A) n: g8 g7 Y8 C$ T7 f3 D
- /* Returns the current reliability of the speed sensor used for Motor 1 */
+ P1 z: R7 e( G8 i# P - bool MC_GetSpeedSensorReliabilityMotor1(void);) y& ?% `" N7 b/ T+ m
/ f+ s( s( H$ r* o- /* returns the amplitude of the phase current injected in Motor 1 */
. y0 q) L! h6 v: O9 M - int16_t MC_GetPhaseCurrentAmplitudeMotor1(void);4 }; [' ]- ? l
4 ?' P/ t. l0 \: F" w) z1 E$ U: W- /* returns the amplitude of the phase voltage applied to Motor 1 *// g* \* }7 P7 g9 a
- int16_t MC_GetPhaseVoltageAmplitudeMotor1(void);. K9 W `% p$ J/ Q3 G( V
/ X2 E* Q; B% H, k9 A* v- /* returns current Ia and Ib values for Motor 1 */
6 t, H1 R. ? u0 d4 R - Curr_Components MC_GetIabMotor1(void);0 F! L! }( \6 `3 E8 y3 V
- - I4 J, `* N. |; @2 Q8 X. s; J
- /* returns current Ialpha and Ibeta values for Motor 1 */
6 [1 G6 Q0 T7 N; M - Curr_Components MC_GetIalphabetaMotor1(void);
/ f1 P' b; q. b* T5 R" Z6 R Q - % y. v# }/ k8 x+ x7 D F
- /* returns current Iq and Id values for Motor 1 */ c7 k- ^3 ~, p( n, U" F+ W
- Curr_Components MC_GetIqdMotor1(void);$ @$ c4 ^" B) o* j) ~
2 l/ ^$ U" Z% h- /* returns Iq and Id reference values for Motor 1 */
2 j% E, g6 L9 e t - Curr_Components MC_GetIqdrefMotor1(void);# T4 m% Y4 ]. a( b% k" j7 |/ Q' h
- 5 t/ }' n, v# p' C0 G e3 w" w
- /* returns current Vq and Vd values for Motor 1 */' b+ V, P0 S' O1 e" r/ }( Q' C
- Volt_Components MC_GetVqdMotor1(void);/ O2 Y8 u- e1 p
6 y3 a! \( H& I- l: O/ y2 W7 k0 H: c- /* returns current Valpha and Vbeta values for Motor 1 */
: J5 O3 @. X1 ?9 `" }: k - Volt_Components MC_GetValphabetaMotor1(void);
) a; C) Z7 R. q D- i+ x/ s - : D2 s* Q2 ]4 P
- /* returns the electrical angle of the rotor of Motor 1, in DDP format */
1 i! u: {& \; f* ]" p1 Y - int16_t MC_GetElAngledppMotor1(void);+ W' _* B1 w, J/ Y
- $ {* d" `5 ?; m H; M$ i ?
- /* returns the current electrical torque reference for Motor 1 */6 ?1 Q3 _- X) [
- int16_t MC_GetTerefMotor1(void);8 n; P! F; b6 [9 F; d
- 3 m5 I% s0 `5 P4 j! h5 i& ^
- /* Sets the reference value for Id */
* T3 b0 O6 c! R8 T. E" O - void MC_SetIdrefMotor1( int16_t hNewIdref );9 E# {% N6 t) u+ d! m. s+ d
- 4 m9 w: T6 z* e; c$ M3 X1 \
- /* re-initializes Iq and Id references to their default values */
3 Q$ Y9 l7 D: U% R1 T2 U - void MC_Clear_IqdrefMotor1(void);
( K4 b* ^0 g% A% |/ B5 h& `# M - 6 H9 d& h6 U, F# o- F
- /* Acknowledge a Motor Control fault on Motor 1 *// J* F! K. m( x3 a& r' Z0 a
- bool MC_AcknowledgeFaultMotor1( void );
# F2 ?6 v7 l+ b1 R: r - + e2 r) `5 V$ K6 f
- /* Returns a bitfiled showing faults that occured since the State Machine of Motor 1 was moved to FAULT_NOW state */
& V, v) d( g5 u) S7 I$ W9 v - uint16_t MC_GetOccurredFaultsMotor1(void);6 b& a4 c' v* o# k/ ~' @
. D6 j2 D3 d( F% P8 J- /* Returns a bitfield showing all current faults on Motor 1 */
& X% i: J! G# O. m, q9 y1 x% l# ?) l' ? - uint16_t MC_GetCurrentFaultsMotor1(void);
/ B: }) V$ D' p4 H- w |% e: |0 S( h! a1 P
5 a3 g5 s d Y/ f. `$ S- /* returns the current state of Motor 1 state machine */5 n7 {8 ]' `# ~2 W
- State_t MC_GetSTMStateMotor1(void);
) {7 F1 f1 P/ P! ^0 H
* F* `& p! _7 b; k- /* programs a user defined ADC regular conversion */
( t( N! ^$ H0 H) e1 h$ q - void MC_ProgramRegularConversion(uint8_t bChannel, uint8_t bSampleTime);
4 j, } d, N- a/ Z8 @
7 g9 D- K# e5 G! X+ }- /* Returns the value of the last executed user defined ADC regular conversion */' F" N% u% y( G8 r! J0 S5 ^
- uint16_t MC_GetRegularConversionValue(void);
) @) D. Z- V* ]1 d8 _% Z
. H7 J* } M% \/ D( A- /* Returns the status of the last requested user defined ADC regular conversion */
4 ^; ~: q' E" T- k4 h - UDRC_State_t MC_GetRegularConversionState(void);
复制代码上面这些api函数是对单个电机的操作,包含了启动、调速、调转矩、以及电机各种状态的获取。 看了任务一的要求,个人认为电机控制还是需要与外部交互好一点,电机启停不可控对于大功率的电机控制来说比较危险。 任务一代码: - void task_1(void); L( l% ~3 G0 L! d* ?. ]4 ~
- {9 i" o; E& G- Z- H2 J) A7 E" ~( W0 [
- MC_ProgramSpeedRampMotor1(3000/6,1000);# N0 C+ B- w9 G0 n- {6 R- S: A. o& f
- MC_StartMotor1();
4 w0 v. H6 c9 k) j; g - HAL_Delay(10000);
9 A7 T1 E6 y% a6 g6 d/ t; S - MC_StopMotor1();
/ I$ T ?1 S/ K7 r& Q9 I - HAL_Delay(10000);- ]" g1 x- Q# U/ m( _7 B
- }
复制代码主函数添加的代码: - /* Infinite loop */
. x0 y: j3 E& c0 N ` - /* USER CODE BEGIN WHILE */& O7 M: [: z# p& ~
- while (1)
3 g0 j" H" z) z6 d# y' c* G: x0 E - {/ R# B( I( X/ s4 I/ f
-
4 _. O, U6 n; n5 M0 @& ` - if(GPIO_PIN_SET == HAL_GPIO_ReadPin(LED11_GPIO_Port,LED11_Pin))1 o* t3 ]8 H n9 v( z3 n
- {& Y7 x) v4 p1 K @ l7 A# L
- task_1();8 {4 Y+ A( d; s; u. U
- }
" g2 Z, x: d- r' y9 D* ]* n# Y( b - else
7 l! R# e- ^2 G5 J, C) } - {# D, N! ~- m1 d) K2 {' k3 V
- MC_StopMotor1();3 Y8 l& n, _1 ]* N
- }
/ l r2 \( l. v( P. j - realspeed = MC_GetMecSpeedAverageMotor1()*6;, E" f; e. L0 U; h% R
- /* USER CODE END WHILE */
复制代码按键中断处理函数代码在ui_task.c这个文件中: - void UI_HandleStartStopButton_cb (void)
( P) \# \+ W6 e# D" Q8 L, L - {
( D: J, Y5 j: \ - /* USER CODE BEGIN START_STOP_BTN */
$ F" z7 {- R% @; U' Q5 F0 ^ - HAL_GPIO_TogglePin(LED11_GPIO_Port,LED11_Pin); O4 _8 }6 e3 I @% o
- /* USER CODE END START_STOP_BTN */( r- `0 C" J- m1 \- m, ]8 K
- }
# D5 K; z! Q* N9 w
复制代码这样可以通过按键来控制任务的运行并且可以通过led的亮灭来观察系统所处的状态。LED11是驱动板上的一颗led,引脚号是PB2,可以通过cubemx来设置初始化代码。 通过monitor可以看到速度变化曲线图: ok,任务一完成。 ------------------------------------------------------------- {, D( }! \% u, t" f: d% @
任务二代码: - void task_2(void)- L2 D0 c, b2 W1 a
- {
|$ f8 W& Q0 m$ ~9 l - int16_t Speed_Kp,Speed_Ki;
1 d( X6 v, h! ?" w5 \7 N3 t9 } - MCT_Handle_t * pMctHdl;
6 Q9 i) C: b3 U - MC_ProgramSpeedRampMotor1(3000/6,1000);; a3 S3 y* Z* y
- MC_StartMotor1();9 h8 {6 ^; t7 G: g
- HAL_Delay(3000);
- Y) X v8 r% X" W: s - MC_StopMotor1();+ N& r0 O5 C. @# d0 S
- HAL_Delay(1000); % k+ A9 |, K+ o8 q2 P% `+ v, i/ ?, Y
- pMctHdl = GetMCT(M1);0 F; l8 }# k- p
- Speed_Kp = PID_GetKP(pMctHdl->pPIDSpeed);, S6 Y/ Q' B: m+ H. i
- Speed_Ki = PID_GetKI(pMctHdl->pPIDSpeed);
* R$ S# ^& v' K, E) s8 S i) U5 H - PID_SetKP(pMctHdl->pPIDSpeed,Speed_Kp*2);8 @: N* F: B* m! J/ K
- PID_SetKI(pMctHdl->pPIDSpeed,Speed_Ki*2);
: e8 M$ n/ S) z) j: b - MC_ProgramSpeedRampMotor1(3000/6,1000);- T7 i, Z6 z; B7 R( _1 G. r/ u
- MC_StartMotor1();) B! q2 w8 B" N9 L7 |1 J
- HAL_Delay(3000);0 v! d: W. i5 p9 `
- MC_StopMotor1();3 S8 W: ^ _9 ]! ]# a3 x% i2 p
- HAL_Delay(1000);, A, n6 r. i: N
- PID_SetKP(pMctHdl->pPIDSpeed,Speed_Kp/2);
0 l2 Y& \, T' A# s7 f - PID_SetKI(pMctHdl->pPIDSpeed,Speed_Ki/2);0 c: H4 _1 s/ n0 b. f) S
- MC_ProgramSpeedRampMotor1(3000/6,1000);
1 l: o7 x) O& K - MC_StartMotor1();
9 Z# d2 {* J9 t4 }! }0 S# z - HAL_Delay(3000);* h3 O9 k( F3 ? J6 y! j
- MC_StopMotor1();
9 l' D8 G# m) W3 m. u+ o - HAL_GPIO_WritePin(LED11_GPIO_Port,LED11_Pin,GPIO_PIN_RESET); F7 a& `, A. J5 l
- }
复制代码主函数代码和任务一相同,只是把task_1()改成task_2()就行了。最后一行把LED引脚拉低是为了让代码只运行一次,多次运行可以通过按键进行操作。 可以看到和预期结果是一致的,原始PI调节速度快,有超调现象;Kp、Ki同时增大一倍的时候,调节加快了,并且超调现象被抑制了;当Kp、Ki同时缩小一倍时可以看到虽然调节很快,但是有严重的超调现象。 & j# A% x8 u p1 s% M
|8 d4 o9 E( L. s通过monitor的图形显示速度波形极大的方便调试PID的过程,ST考虑得真周到。
! D& p- k% d/ i% @3 U' n9 G% i6 K 我在这里有个疑问,这个plotter只能显示参考速度的波形吗,还能不能添加其他变量来查看系统某个变量的变化趋势呢。之前我一直是用的Jscope,但是Jscope需要连接Jlink来查看,对于这款套件来说需要额外连接Jlink调试器,有一点点麻烦。后面调试有必要我还是会使用Jscope来查看一些变量的波形,并且分享到论坛。
! y% V' {3 j. O4 O8 H4 h % ?. l3 @8 _: v
任务一和任务二已完成,欢迎大家留言讨论。2 p& I5 T* ~* {6 F. O
5 S" B8 r( `( i$ p+ S) |% }5 |5 j+ t; F' S- o# `1 p/ u0 ~- D
( p4 q( r, e9 O9 R& S
4 e j- d' R& K* B |
/* Programs a Speed ramp for Motor 1 */
void MC_ProgramSpeedRampMotor1( int16_t hFinalSpeed, uint16_t hDurationms );
这个函数的第二个参数你改过吗,第二个参数就是达到目标值的时间,越小加速度越快6 R$ `7 w$ w9 h) R8 X7 T, g
考虑另外一个问题,电机惯性较大,电机驱动板的驱动能力有限,也许最多就只能10+s才能达到目标转速
1.在我的程序里按键的作用起的是传递信号的作用,按键中断中翻转LED11引脚,主函数通过读取LED11引脚电平值做是否启动任务的判断。( y `5 ^0 `+ O
2.在我的代码里启停是可控的,通过LED11引脚电平来判断是否执行task1。另外,task1的要求就是延时不断启停,我这样写是没有问题的
直接扔好几个问题
回复20#和21#:1 E; Q5 K. h7 x4 V1 p5 q
User_label那个自己想写什么名字无所谓,根据自己喜好就OK
回复22#和23#:
不用跟我的代码完全一样,这个只是我用于控制实验进行与否的一种方式,你也可以通过其他方式来做
好的,有时间我去了解一下通讯协议的代码,尝试显示其他变量。另外一个就是觉得plotter采样率有点低,Jscope采样率1kHz在好些时候都显得不太够用。
我没试过其他电压,一直用的12V,你是不是正负极接反了
不是,我是从24V慢慢升上去的,
我后面尝试一下高电压再给你反馈吧,主要是现在我也没有高电压的电机,所以一直用的12V
好的,还是小心点吧
上电40V驱动板坏了,底板还是好的,这个可能需要告知一下ST了
板子和上位机通讯协议是啥
我还没看,你可以去串口中断函数中了解通信协议
正常呀,这属于C语言基本功,MCT_Handle_t这个结构体在mc_tuning.h中定义的,你要在其他文件中使用当然要包含定义这个结构体的头文件嘛
那就好,我还以为是我工程建立的有问题