Balancing Robot PID

Hey guys, any of you have experience in making balancing robots? I am having a bit of trouble with the integral part. The integral keep on increasing and it only reduce when the robot tilts to the other side. How is the code suppose to be?

Hello Compadre,

What formula are u using?

Have you added an anti-windup part? this sounds as a saturation of the integral part.


In general if you are responding to a step-change (rapid increase in error) or the output is saturating, you should just zero the stored integral value, its not doing anything useful until the system is approaching the target smoothly in the linear response realm.

A good test for a servomotor PID is to just have it hold still and you work against it by hand, feeling for its behaviour, especially when you release the pressure.


whats a anti-windup part

would you have to put in a balancing circuit of some sort?

“Windup” is the behavior of an integral controler that keeps acting in the same direction even after the error sign has changed. It’s due to the integrator’s nature: if it’s stored value is positive and the error is negative then the stored value will decrease slowly as the new errors is integrated over time.

There are several technics to reduce the windup effect. A simple one is, like MarkT suggested, to reset the integrator value when you’re responding to a fast change in the setpoint value. When you’re getting close to the setpoint, you then enable again the integrator.

From SESMD... addendum to prior post.

absolute value of accelerometer result - control value (for each axis) used to set drive current to each motor. Values pos or neg used to set polarity of current to appropriate motor.

Stan (sesmd)

Sorry about all the addendums, but another point came to mind, which would make things easier..... it would be extremely useful to keep the Center of Gravity of your balancing robot as LOW as possible. If it (the COG) migrates upwards, you may reach a point where escalating oscillations may develope in one or both axes, resulting in loss of control, and a 'crash'.


No, C.o.G. wants to be as high as possible to make the balancing easier. Try balancing sticks of different lengths and you'll soon see shorter sticks fall over much faster.

Oscillations in a PID system are principally controlled by the D term, set the I term to zero in the first instance and get the balance (no pun intended) between P and D terms roughly right first).