Main Site Documentation

Project - FEZ Cerbot - Balancing code


FEZ Cerbot - Balancing code

The attached project will help you get started with your own balancing code for your FEZ Cerbot. You will need the following for this project

  1. FEZ Cerbot
  2. Accelerometer - I used the Seeed module
  3. Gyroscope - I used the Seeed module
  4. Button module
  5. Stand-offs or some other means of mounting the modules to the FEZ Cerbot

The button module is used to initiate the balancing code once everything is calibrated and the FEZ Cerbot is in the upright position.

Look at the photo of the FEZ Cerbot with the modules mounted, you will notice the Gyro and the Accelerometer are mounted at the bottom of the board. This is particularly important for the Accelerometer because it minimizes the effect of the whipping motion on the accelerometer readings. The location of the gyro is less important, but for symmetry I mounted it at the bottom as well.

The balancing algorithm uses a Complementary Filter to combine the readings from the Gyro and the Accelerometer to get an accurate enough reading on the current angle of the bot. Using this angle and it’s deviation from the intended angle (upright), the data is fed into a Proportional-Integral-Derivative (PID) controller which calculates the direction and velocity that the motors need to be run to adjust for the deviation from the target angle.

Read more:
Complementary Filter:
PID Controller:


@ taylorza - nice and clear code, very well done :slight_smile:


@ Justin - Thank you…


Thanks for sharing, as well as the additional references!


@ mhectorgato - It is a pleasure. I hope others get some use out of it.


it’s a fascination how short the program file is. Realy short and effectiv.
I like it. Hope some times I learn to code like this.


Thank you taylorzaKing ,
Very useful for me.
Even I am not the robot guy, the Complementary filter is useful for me.
How is the accurate for this algorithm compare to using Inclinometer?
Other question is the Torsion,the robot maybe has a Torsion, how Calculate it and correct it?



@ VB-Daniel - Thank you.

I would imagine using an Inclinometer rather than an Accelererometer would give much better accuracy, but I have never actually used an Inclinometer so I cannot confirm that.

I am not sure I understand the question, Torsion usually refers to some kind of twisting force or deformation which I do not think is a factor in the robot. If you are referring to the back and forth oscillation of the robot as it tries to balance, what I found was the following.

[ol]Place the accelerometer as close to the rotation axis as possible to minimize the impact of the horizontal acceleration.
Do not run the motors too fast[/ol]


When trying this, it seems to work well but the robot always moves forward. What numbers are used to calibrate it?


@ Gus - there are two things
[ol]TargetAngle - tweak this angle to compensate for the forward motion due toe the weight distribution, beware it is very sensitive so increase it in very small increments.
When you start the device in the upright position have it leaning slightly (very slightly) backwards[/ol]

The second point is the most significant, if it is already tending backwards the motors can react quick enough. I just retested and the current values work well for my bot, but maybe the weight distribution varies between the bots.


It would interesting to see if the code could be modified to only use one wheel for balance. This might avoid some of the creep.


That is an interesting idea, I will see if I can give that a try.

As is, my bot does not have any creep other than some rotational creep due to the differential between the two motors, but even that is minimal.

Here is a video I just took with the bot standing without any interruption, you can see it has minimal creep. It was standing for a few minutes before I switched it off.


I got it to work without changing the code. I first set the robot on am angle with something under the front caster. Let it calibrate then it works. Ideas?


@ Gus - I would think some of the pid parameters would have to be tuned due to motor differences, and battery voltage and weight.


@ Gus - So if I understand correctly, you let it go through the calibration sequence at startup with the bot at a slight angle and after calibration you lift it to the upright position and initiate the balancing?

If so, I would be interested to see what the readings are for the accelerometer when the device is level, maybe the skew is in the accelerometer readings. Did you use the Seeed accelerometer?


I was recently asked about having the FEZ Cerbot moving while balancing at the same time. I had a few ideas so I thought I would test one of them.

Here the FEZ Cerbot is balancing and at the same time avoiding obstacles that approach the reflector sensors. Each sensor is managed independently and the FEZ Cerbot will turn away from the on coming object.


That was pretty cool. :slight_smile:


@ taylorza - that is cool. Source code?


Definitely awesome!


@ Aron, @ ianlee74, @ Gus - Thank you.

@ ianlee74 - The code on codeshare has been updated. It also includes a new feature not shown in the video, the LEDs now cycle back and forth.