Add Position Controll on MC SDK 5.x
Preface
In MC SDK 5.x, there are two control loops, the current loop and the speed (torque) loop. Some applications require the use of a position loop, such as motor control for UAV gimbal or subway gates. This article will explain how to add a simple position loop on top of the existing ST motor control library to enable 3-loop control for improved stability
Theoretical basis
If there is a gearbox motor, a speed loop needs to be added to reach the desired position faster, that is, three-loop control: position loop + speed loop + current loop, and the framework is the same as follows:
Implementation steps
Adding a DAC module to the MCSDK. If the MCU does not have a DAC module, the TIMER+RC filtering circuit can be used to simulate the DAC output. The following diagram shows the configuration for using a DAC, which requires configuring the user-defined DAC1/2.
Add the following variables or functions for position control:
- Position loop PID structure: PID_Handle_t PIDAngleHandle_M1
- Position control structure: Position_Handle_t
- Position error in angle: Position_GetErrorAngle
- Calculation of reference speed for position loop: Position_CalcSpeedReferrence
- Calculation of reference torque for position loop: Position_CalcTorqueReferrence
Refer to mc_position.h and mc_position.c for reference.
mc_task.c file modification
Add DAC output function
Add position control algorithm
In the medium frequency task function TSK_MediumFrequencyTaskM1 in mc_task.c, add position error calculation. Based on the error calculation, if the error is above a threshold, perform speed control, otherwise perform torque control.