Skip to main content
Associate
July 31, 2026
Question

Speed behaviour MC_ProgramSpeedMotor1()

  • July 31, 2026
  • 3 replies
  • 91 views

Hi ​ ​@ST MICROCONTOLLER team ,


We are using MCSDK v6.3.0 IN STM32G431VBT6.

While running the motor in MC_ProgramSpeedMotor1(400,1000)  we found that the average speed read using MC_GetMecSpeedAverageMotor1  is returning speed in range of 382rpm to 416rpm eventhough the commanded speed is 400rpm.
How can we control the motor speed in a range of 398-402 rpm say (+-2 rpm from the commanded speed)

Could you please look into the attached images and pseudo code and let me know how can we achieve the requirement to keep the motor speed within (+ or -2 rpm of commanded speed )

code flow:

HAL_Delay(10);

MC_StartMotor1();

while (MC_GetSTMStateMotor1() != RUN)

{

}

MC_ProgramSpeedRampMotor1(400, 1000);

while (1)
{

     velocity1 = MC_GetMecSpeedAverageMotor1();

}

 

Complete speed ramp
Zoomed image indicating the velocity1


while the torque_qd = MC_GetIqdrefMotor1
torque_qd.q stays within 250 to 260 range

 

3 replies

GMA
ST Technical Moderator
August 3, 2026

Hello ​@abhi_ram,

Tune the Speed PI regulator parameters.
It could be done with the Advanced Configuration feature of the MC Pilot tool.

If you agree with the answer, please accept it by clicking on 'Best answer'.Best regards.GMA
abhi_ramAuthor
Associate
August 3, 2026

Hi ​@GMA ,

Could you just say based on STM_CUBE IDE  ,MC_SDKv6.3.0, which parameters should be adjusted ?

this is what i found in MCSDK regarding the Speed PI regulator parameters 

 

PID_Handle_t PIDSpeedHandle_M1 =

{

.hDefKpGain = (int16_t)PID_SPEED_KP_DEFAULT,

.hDefKiGain = (int16_t)PID_SPEED_KI_DEFAULT,

.wUpperIntegralLimit = (int32_t)(IQMAX * SP_KIDIV),

.wLowerIntegralLimit = -(int32_t)(IQMAX * SP_KIDIV),

.hUpperOutputLimit = (int16_t)IQMAX,

.hLowerOutputLimit = -(int16_t)IQMAX,

.hKpDivisor = (uint16_t)SP_KPDIV,

.hKiDivisor = (uint16_t)SP_KIDIV,

.hKpDivisorPOW2 = (uint16_t)SP_KPDIV_LOG,

.hKiDivisorPOW2 = (uint16_t)SP_KIDIV_LOG,

.hDefKdGain = 0x0000U,

.hKdDivisor = 0x0000U,

.hKdDivisorPOW2 = 0x0000U,

};

#define PID_SPEED_KP_DEFAULT 2542/(SPEED_UNIT/10) /* Workbench compute the gain for 01Hz unit*/

#define PID_SPEED_KI_DEFAULT  51/(SPEED_UNIT/10) /* Workbench compute the gain for 01Hz unit*/

#define PID_SPEED_KD_DEFAULT 0/(SPEED_UNIT/10) /* Workbench compute the gain for 01Hz unit*/.

 

Additionally ,
It will be helpful if you have any tutorial or document for the PID tuning in workbench , Please share the document or link for better understanding of tuning working principle.


how the behaviour should be noticed ?
How can i understand whether the Kp value should be increased or decreased based on performance of motor behaviour? How the ideal value of Kp and Ki value can be fixed based on  the motor performace.?