Part of this answer quotes GPT, GPT_Pro for better problem solving
Your question is very interesting. There are many factors that need to be considered to achieve a balanced car control. The control of a balanced car is mainly to control the speed of the left and right wheels, so that it can maintain balance and achieve the movement of the car before and after. Since you are using stm32 microcontroller, there are only four timers, two to measure the speed of the left and right wheels, one to update the PID, and one to drive the motor.
Now you have a problem: the PID needs to calculate the speed of the left wheel and the right wheel respectively, but the motor can only output one PWM, how to achieve the output of the left and right wheels different waveform.
Here I recommend that you use the differential technique where the PWM signal is applied to the forward and reverse ports of the motor respectively. As shown below:
According to the different speeds of the left and right wheels, the PID algorithm is used to update the duty ratio of PWM signal, so as to realize the control of the different speeds of the left and right wheels.
Specific code implementation is as follows:
//根据左右轮子速度,来调整正反向电机PWM占空比
void adjustPwmDuty(float leftWheelSpeed,float rightWheelSpeed) {
//根据PID算法来更新左右两个轮子电机PWM占空比
float leftSpeedDutyCycle = PIDCalculate(leftWheelSpeed); //左轮子PWM占空比
float rightSpeedDutyCycle = PIDCalculate(rightWheelSpeed); //右轮子PWM占空比
//根据左右两个轮子的速度不同来决定正反向电机的占空比
if(leftWheelSpeed > rightWheelSpeed){ //如果左轮子速度大于右轮子速度
//将左轮子PWM占空比作用在正向电机上
Motor_postive_dutycycle(leftSpeedDutyCycle);
//将右轮子PWM占空比作用在反向电机上
Motor_negative_dutycycle(rightSpeedDutyCycle);
}else if (leftWheelSpeed < rightWheelSpeed){ //如果左轮子速度小于右轮子速度
//将左轮子PWM占空比作用在反向电机上
Motor_negative_dutycycle(leftSpeedDutyCycle);
//将右轮子PWM占空比作用在正向电机上
Motor_postive_dutycycle(rightSpeedDutyCycle);
}else{ //如果左右两个轮子速度相同
//将左右两个轮子的PWM占空比作用在正向和反向电机上
Motor_postive_dutycycle(leftSpeedDutyCycle);
Motor_negative_dutycycle(rightSpeedDutyCycle);
}
delay_ms(50); //延时50ms
}
The above is the use of STM32 timer and differential signal to achieve a balanced car method. I hope I can help you.
If the answer is helpful, please accept it.