Email me
How the balance loop works
← Self-balancing robot

How it works

How the balance loop works

The controller is six lines long; the difficult parts are the measured derivative, feedback sign and timing around it.

In this section I will cover how the robot actually balances. A two-wheeled balancing robot is essentially an inverted pendulum. Probably the easiest way to think about this is balancing a broom on your palm. When the broom starts to fall, you naturally move your hand underneath it to catch it. The wheels of this robot do basically the same thing. If the body tilts backwards, the wheels drive in that direction to catch it, and vice versa.

The controller

While you might think that this would result in some pretty complex code, it’s actually fairly simple:

int BalancePD::compute(float angle, float gyro)
{
    float angle_bias = mid_angle_ - angle;
    float gyro_bias = 0 - gyro;
    return static_cast<int>(-kp_ / 100.0f * angle_bias - gyro_bias * kd_ / 100.0f);
}

As the name implies, mid_angle_ is the setpoint. While in the robot car’s current form it is set to 0, it may not always be the case because it depends on where the centre of mass sits relative to the axle. For example, if the mass is slightly forward, then we need to shift mid_angle_ slightly positive, since a forward tilt reads positive. I will probably need to change this when I add the Radxa Q6A, additional battery, and camera, but for now it balances well.

The / 100.0f on both gains is just scaling. It means I can keep kp_ and kd_ as whole numbers when tuning and in the telemetry, rather than dealing with fiddly decimals.

The actual theory and how it compares

A textbook PID controller looks something like this:

u(t) = Kp * e(t)  +  Ki * ∫e dτ  +  Kd * de/dt

e(t) is the error, which is what my angle_bias is. So those six lines are a PID controller, they just don’t look like one. Four reasons why:

  • No integral term. Mine is a PD. The integral lives in VelocityPI, which integrates encoder counts instead of angle.
  • The derivative is measured, not calculated. Differentiating a noisy angle only makes it noisier, so rather than work out the rate of change I just ask the gyro, which measures it directly. That’s why compute() takes gyro as its own argument.
  • The sign is flipped. Because angle_bias is mid_angle_ - angle, what I have is the textbook expression negated. The reason for this is that an inverted pendulum needs you to drive towards the fall, not away from it, same as moving your hand under the broom.
  • The gains hide unit conversions. The gyro returns raw counts rather than degrees per second, so kd_ is a unit conversion as much as a gain.

dt doesn’t appear anywhere either. The loop runs at a fixed 200 Hz, so it’s constant and absorbed into the gains like everything else.

Checking the setup

Once I got everything flashed and set up, I put the car on the floor, held it upright and then tilted it backwards and forwards. While it didn’t balance on the first attempt, the wheels drove the right way. This is largely because I had the reference code from Yahboom to go off. If I hadn’t had their code and I got it wrong, instead of driving under the falling centre of mass, the wheels would have accelerated the fall and the car would have gone flying.