Welcome to RDK X5 Robot repository
15. PID control robot motion
15. PID controls robot motion
15. PID controls robot motion15.1. Experimental purpose15.2. Configure pin information15.3. Analysis of experimental flow chart15.4. Core code interpretation15.5. Hardware connection15.6. Experimental effect
15.1. Experimental purpose
The robot motion is controlled and the running speed of the robot is controlled by PID algorithm.
Because different models require different drive codes, here only the McNamum wheel car as an example for demonstration.
15.2. Configure pin information
- Import ioc file from Encoder project and name it Car_Motion.
There is no need to add additional content, and the final chip configuration pin is shown as follows:

15.3. Analysis of experimental flow chart

15.4. Core code interpretation
- Create bsp_pid.h and bsp_pid.c, and add the following information to bsp_pid.h:

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

- Calculation formula of incremental PID.

- Set the target value.

- Obtain PWM output value through PID calculation.

- Clear PID parameters.

- Create bsp_motion.h and bsp_motion.c files.

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

- According to the speed value, the PWM data of the motor is updated again, so as to achieve the role of speed regulation.

15.5. Hardware connection
The motor connection 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.

Because the power of the motor is relatively large, do not directly use USB 5V power supply for the expansion board, and must use battery power supply.
15.6. Experimental effect
Since the motor will turn when started, please set up the car before the experiment, and the motor wheels are suspended to avoid rampaging.
After burning the program, the LED light flashes every 200 milliseconds. Press KEY1 to make the car move forward, and press KEY1 again to stop the car.