PID Tuning

From P20 onwards, our robots have used PID controllers to control the speed of the drive motors.  Code in the Raspberry Pi decides a target speed for each motor.  The motors are fitted with encoders which report the actual speed of rotation.  It’s the PID algorithm’s job to set the power to the motor to try to get it to match the target speed.  This system should allow the robot to drive at the target speed, even if it is on a steep slope, or if the batteries are low, for example.  In the 2019 games, our robot struggled to turn in one of the games because the surface was so grippy; a PID based system would have solved this.

If you’re new to PID, take a look here.  Motor encoders and PID algorithms greatly improve the standard of robot control that can be achieved and are well worth investigating.

The difficulty with PID algorithms is that it is necessary to find appropriate values for the P, I and D control terms.  It’s widely reported that this can be a bit of a “black art”.

Our control board for the robot includes a new design for the H-bridges (motor drivers) and I noted that the standard of control being achieved was disappointing.  It became clear that the change in the H-bridges required an update in the PID parameters.

In the past I have set the parameters by trial and error, changing the parameters one at a time.  I decided it was time to find a more thorough approach.

I wrote code to run on the robot’s control board that cycles through 10 step changes in speed, with each speed running for 2 seconds, see the video below.  Each time the PID algorithm runs it takes the absolute difference between the target and actual speeds, which is added to a running total.  At the end, this “sum of errors” is reported.  The lower the score, the better the control terms are working.  The test is repeated for different PID control terms in loops, and a minimum found.  The process was run with a rather weighty mecanum wheel attached to the motor to provide some inertia. 

The PID tuner running

I accept that this is a rather crude “scatter gun” approach to PID tuning, but it appears to have found some good parameters.

For the record, the motors are these.  They have a magnetic encoder that provides 11 pulses per revolution of the motor (which equates to 374 pulses for each revolution of the wheel due to the gearbox).  The PID algorithm runs 50 times per second on an Arduino compatible, it uses this PID library by Brett Beauregard.  The PID control terms found were P: 1.50, I: 4.10, D: 0.001.

Leave a comment