One of tim's planning questions, asking for guidance

fan177 注册会员
2023-02-25 21:22

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:

void adjustPwmDuty(float leftWheelSpeed,float rightWheelSpeed) {

  float leftSpeedDutyCycle = PIDCalculate(leftWheelSpeed);  //左轮子PWM占空比
  float rightSpeedDutyCycle = PIDCalculate(rightWheelSpeed); //右轮子PWM占空比

  if(leftWheelSpeed > rightWheelSpeed){   //如果左轮子速度大于右轮子速度


  }else if (leftWheelSpeed < rightWheelSpeed){  //如果左轮子速度小于右轮子速度

  }else{   //如果左右两个轮子速度相同


  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.

About the Author

Question Info

Publish Time
2023-02-25 21:22
Update Time
2023-02-25 21:22