PID control robot movement

1. Experimental purpose

Control the movement of the robot, and control the running speed of the robot through the PID algorithm.

Since different models require different drive codes, here we only take the Mecanum wheel trolley as an example.

2. configuration pin information

  1. Import the ioc file from the Encoder project and name it Car_Motion.

No additional content is required, and the final chip configuration pins are shown in the following figure:

image-20220311171929134

3. Analysis of the experimental flow chart

image-20220519113617358

4. core code explanation

  1. Create new bsp_pid.h and bsp_pid.c, and add the following content to bsp_pid.h:

image-20220311182153477

  1. Create the following content in the bsp_pid.c file:

Initialize PID parameters:

image-20220311182232217

  1. Calculation formula of incremental PID.

image-20220311182312127

  1. Set the target value.

image-20220311182400564

  1. Calculate the PWM output value through PID calculation.

image-20220311182458461

  1. Clear the PID parameter data.

image-20220311182542931

  1. Create new bsp_motion.h and bsp_motion.c files.

image-20220311183152424

  1. Get and calculate the speed of the trolley from the encoder.

image-20220325115058394

  1. Re-update the PWM data of the motor according to the speed value, so as to achieve the effect of speed regulation.

image-20220325115219282

5. hardware connection

The motor connecting line needs to be connected to the corresponding motor as shown in the figure below, otherwise it may cause the problem that the program does not match the phenomenon. Motor 1 corresponds to the motor in the upper left corner of the body, Motor 2 corresponds to the motor in the lower left corner, Motor 3 corresponds to the motor in the upper right corner, and Motor 4 corresponds to the motor in the lower right corner.

image-20220311143208773

Since the power of the motor is relatively large, the expansion board should not be powered by USB 5V directly, but must be powered by DC 12V.

6. Experimental effect

Since the motor will turn when started, please stand up the trolley before the experiment, and the motor wheels are suspended in the air to avoid rampage.

After the program is programmed, the LED light flashes every 200 milliseconds. Press the button KEY1 once, the car moves forward, and press the KEY1 button again to stop the car.