Skip to content

PiTeR balance tuning

campbellsan edited this page Oct 24, 2014 · 2 revisions

Back to PiTeR balancing and driving

Here is how I went about tuning PiTeRs PID balancing algorithm. I have read lots of conflicting advice on various great sites across the net, but this is what worked for me. It is a bit more procedural than most, and as such is probably more repeatable. I don't recommend changing the parameters willy-nilly, there are just too many combinations. Like the proverbial monkeys, you are very unlikely to come up with the PID equivalent of Romeo and Juliet at random. That being said, I found the balancing algorithm still worked as I added extra hardware like the head and speaker. It follows that if you construct your robot with roughly the same dimensions as PiTeR, the tunings supplied in the open source code (in sketch/PiTeR/PiTeR.ino) may well just work for you.


Tune the Proportional term (Kp) Start with a Kp value that is just lower than is necessary to get the robot to the vertical. Set Ki and Kd to zero during this step. Start with a low Kp so that the robot leans against your finger because it is not producing enough torque to overcome gravity. Now move your finger away from the robot so that the lean angle is increased. The robot should compensate with increased torque to the wheels and will ‘follow’ your finger rather than allowing the angle to increase. Keep increasing Kp until it does this. Note this lower bound of Kp.

Gradually continue to increase Kp until the robot produces enough torque to pass the vertical and head off in the opposite direction from your finger. Be ready to catch it with your other hand! When it does this, you have gone too far, back off Kp one notch at a time until it once again obediently but lightly follows your finger. Note the upper bound of the Kp range.


Tune the Integral term (Ki) Now set Kp to a value midway between the lower and upper bounds and turn your attention to Ki. Increase Ki until your robot stands on its own. Do not increase Ki past this point or you will just introduce undesirable overshoot.

Now observe the balancing behaviour. The value of the Ki term continuously flips from positive to negative which introduces oscillation. If the robot oscillates over too large a distance, set Ki back to zero and increase Kp one notch. Now increase Ki until the robot stands once more. You should find that Ki is lower than it was at the previous attempt. The oscillation should also be reduced, because we are supplying more proportional torque and less integral torque. Integral torque comes from the accumulation of error and so takes time to build up and time to reduce. It is this lag which causes the oscillation.

Keep adjusting Kp and Ki until your robot stands up without continuously oscillating back and forth. Try for a clean release with the robot at the set point. You may need to release the robot many times to satisfy yourself that you have reached the optimum tuning of Kp and Ki. Do not worry too much about the recovery from a loss of balance at this time. Do record how long the robot stands before falling away from a clean release. Longer is better.


Tune the Derivative term (Kd) Having reached an ideal value for Ki we are ready to dial in some Kd. As mentioned previously, Kd amplifies the difference between successive errors, it is thus very sensitive to random fluctuations of the sensors. The less we need the better if we are to avoid the jitters. The derivative term comes into play when an already stable system receives some disturbance. It acts to damp oscillation resulting from the disturbance. In the case of a balancing robot, such a disturbance can come from a push. Gravity will, however, quite happily take a minor bit of noise from the sensors and amplify it into a disturbance too. As we dial in more Kd we are looking to see a reduction in the length of time the robot takes to stop wobbling. If the robot turns jittery before you see a reduction in the oscillation, you may need to return to tuning Ki to reduce the natural oscillation present without damping.

If you reach a position where the robot starts to oscillate back and forth (possibly lazily at first), covering an ever increasing distance with each wobble then sorry, but you need to start again from the top. You have encountered one of two problems; either you are not putting enough energy in to achieve stable balance, or you are putting in too much. In the former case, gravity is pumping in extra energy which you are not counteracting, in the latter you are putting extra energy in which is causing overshoot. In my experience, it is quite hard to know the difference from observing the robot behaviour or even from the telemetry. It is therefore better to remind yourself this takes patience, zero Ki and Kd, choose a new value for Kp between the lower and upper bounds and begin again. Work methodically away from the Kp mid range value so that you see which is the right direction to take Kp.

After you have optimised Kd, your robot should stand up and should recover from gravity and finger induced disturbances. THIS IS NOT THE SAME AS STANDING STILL. You may be lucky and your robot may stand still. However, if it does not, do not worry. A robot cannot stand still purposefully unless it knows when it is moving. Currently it only knows what angle it is standing at. It cannot know how fast it is moving without using motor encoder data. At this stage therefore, expect a disturbance to send your robot skating across the room. Like an astronaut in space, there is nothing to stop it until it collides with something.

All the robot cares about at this stage is that it stays upright. If you can’t wait to see your robot stand still, try letting it run on a short pile carpet. If it is close to standing still, it will more likely do so on carpet than on a desktop or hard floor. It follows that you should do your tuning on a hard surface to avoid getting a false sense of completeness from a higher friction surface.