Balance your robot with a Raspberry Pi

You may have seen the amazing two-wheel scooter, the Segway Human Transport system. It has only two wheels, a platform for a person to stand and a handle to guide the vehicle. The scooter operates on batteries located under the platform and between the wheels. Dean Kamen is the inventor of this amazing transporter, which can carry a person around while balancing on its two wheels without toppling over.

After watching the amazing Segway scooter, Mark Williams tried his hand at balancing a two-wheeled robot using the tiny credit card single board computer, the Raspberry Pi or RBPi. You can watch his success in the video clip here – it is almost like watching a human baby learn to take its first tottering steps.

Mark’s PiBBOT, or Pi Balancing roBOT, carries its own power source and the electronics, but unlike the Segway, does not have room for a passenger. The TFT displays the angles from the accelerometer, the gyro, the complimentary filter and the power drawn by the motors. There are two buttons on the top – one for turning on/off the motors and the other for resetting the gyro.

The PiBBOT uses the concept of an inverted pendulum to work. This is similar to how children balance a vertical stick on a finger on their outstretched hand – they move in the direction the stick is about to fall, thus attempting to keep its center of gravity below it. The balancing robot keeps itself vertical by using a control algorithm called PID or Proportional Integral Derivative. It does this by trying to keep the wheels under its center of gravity. Therefore, if the robot leans forward, the wheels carry the robot forward, trying to correct the lean. As the bottom of the robot moves forward, inertia keeps its top in the same place, thus righting it.

PiBBOT has an accelerator and a gyroscope to measure the angle of its lean. One axis of the accelerometer measures the current angle, while one axis of the gyroscope measures the rate of rotation. A well-timed software loop running in the RBPi keeps track of both. The RBPi makes calculations based on the measurements to provide power to the motors via the PWM. The RBPi must move the motors in the right direction to keep the robot upright.

Accurate angle measurements need readings from both the accelerometer and the gyro, which are then combined. Individual readings do not provide the necessary accuracy. The gyro measures the rate of rotation and requires to be tracked over time for calculating the current angle. The tracking usually includes noise, which causes the gyro to drift. However, gyros are useful for measuring quick changes in movement.

Unlike a gyro, accelerometers do not need tracking and they can sense both static positions as well as sudden movements – with gravity defining the static position of the robot. However, accelerometers are notorious for their noise levels. Both gyro and accelerometers perform well over certain sensitivity levels.

Mark is using a measurement range of 250dps with a sensitivity of 0.0875 dps/LSB for his gyro. For his accelerometer, he is using 8g full-scale, corresponding to 4mg/LSB and a full scale of 10. Read the full details here.