Welcome to ROSMASTER R2 repository
15. PID control of robot motion
15. PID control of robot motion
15. PID control of robot motion15.1. Purpose of the experiment15.2. Configure the pin information15.3. Experimental flowchart analysis15.4. Core Code Explanation15.5 Hardware Connection15.6. Experimental effect
15.1. Purpose of the experiment
To control the robot motion and control the running speed of the robot by PID algorithm.
Since different models require different drive codes, here we only take the McNamee wheeled cart as an example for demonstration.
15.2. Configure the pin information
- Import the ioc file from Encoder's project and name it Car_Motion.
There is no need to add extra content, and finally the chip configuration pins are shown below:

15.3. Experimental flowchart analysis

15.4. Core Code Explanation
- Create a new bsp_pid.h and bsp_pid.c, and add the following to bsp_pid.h:

- Create the following new contents in the bsp_pid.c file:
Initialize the PID parameters:

- The formula for incremental PID.

- Set the target value.

- PWM output value by PID calculation.

- Clear PID parameter data.

- New bsp_motion.h and bsp_motion.c files.

- Obtain and calculate the speed of the cart from the encoder.

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

15.5 Hardware Connection
The motor connecting wires need to be connected to the corresponding motors as shown in the following figure, 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.

Due to the relatively large power of the motor, the expansion board should not use USB 5V power supply directly, but must use DC 12V power supply.
15.6. Experimental effect
Since the motor will spin when it starts, please set up the cart before the experiment, with the motor wheels hanging in the air, to avoid running across the road.
After burning the program, the LED flashes every 200 milliseconds. Press the key KEY1, the cart advances, press KEY1 again the cart stops.