Welcome to RDK X3 Robot repository
5. Color follow
5. Color follow
5.1 Gameplay Introduction
This course mainly adds the function of PID control of car movement on the basis of color tracking. After opening the start switch, the car will adjust its position to keep the color object in the middle of the picture as far as possible.
5.2 Core Content analysis
The simplePID.py file is a simplePID controller, and the incremental function is an incremental PID algorithm function.
xxxxxxxxxxdef incremental(self, current_value, limit=0):self.err = self.setPoint - current_valueresult = self.last_result + self.Kp * (self.err - self.err_next) + self.Ki * self.err + self.Kd * (self.err - 2 * self.err_next + self.err_last)self.err_last = self.err_nextself.err_next = self.errif limit > 0:if result > limit:result = limitif result < -limit:result = -limitself.last_result = resultreturn result
Import and configure PID parameters, since the image pixels are 320*240, take the midpoint values of 160 and 120 as the target values. The PID parameter value can be adjusted according to the robot's motion effect.
xxxxxxxxxxfrom simplePID import PIDPID_X_Kp = 0.8PID_X_Ki = 0PID_X_Kd = 0.2PID_Y_Kp = 0.8PID_Y_Ki = 0PID_Y_Kd = 0.2PID_X = PID(160, PID_X_Kp, PID_X_Ki, PID_X_Kd)PID_Y = PID(120, PID_Y_Kp, PID_Y_Ki, PID_Y_Kd)
When the Start button is clicked, the car_control function is called to control the car movement. In order to keep the color object in the middle of the screen, the car moves with the color object.