Two Wheel Dynamic Balancing Robot
Figure 1. A block diagram of the control system.
aviation terminology, that means “tilt.”). All of them have
some sort of catch in their implementation. In some cases,
the catch can translate to a lot of work for you, since you
need to keep filtering the signals. In other cases, the catch
can translate to a lot of money for a better sensor with greater
quality. In both cases, some filtering needs to be done. I
predict that, in a few years, sensors (or sensor systems) will be
available with little or no drift for less than $100.00.
The sensors I used are most commonly seen in other
systems, including camera stabilization, vehicle stabilization,
and cruise control systems. They are not that expensive and
are within the reach of any hobbyist or enthusiast. The rule
is, the more money you put in, the better sensors you will get
out — better meaning not only less drift in the case of gyros,
but also less noise, a faster sampling rate, etc. Bear in mind,
however, that this does not necessarily mean that you will
have less work to do on the filtering.
There is no rule on what to use to determine the tilt
angle. Some people use proximity sensors arranged to read
the distance from two points on the platform to the floor.
Others use inclinometers only, while still others use
compound sensors to filter each other out. The truth is that
there are many ways to do this and the best way will depend
primarily on your application. I chose to use a dual axis
Figure 2. The author demonstrates his prototype.
28 SERVO 07.2004
accelerometer and a gyro.
The accelerometer reports a vector to gravity and, thus,
the angle of the unit with respect to the ground. (Don’t get
confused; this is not an error. With this type of accelerometer,
it does report a vector to gravity.) This accelerometer works
as an inclinometer and the signal is very clean and precise.
The catch of this sensor is that, when moving forward (linear
acceleration), they start spitting wild values. They are very
sensitive to motion — and who can blame them, since they
where born to be accelerometers, not inclinometers. They are
great while the unit sits static, only tilting, and not moving
The accelerometers can be read in different forms,
depending on your actual sensor. I chose the analog output,
proportional to the actual tilt. Accelerometers are very
precise when reporting valid readings in the right conditions.
Keep in mind that, the closer you mount them to the pivot
point, the more accurate they will be because the platform
will not throw the sensor forward when tilting.
The gyros report the rate of turn and they tend to drift
with time, since they don’t know what the direction is to gravity
and are affected by temperature and other ambient factors —
including extent vibrations, to some degree. The gyros don’t
tell you what angle the robot platform is at, but rather how
fast it is tilting toward an axis. Thus, by integrating its output,
you can get the actual tilt angle. Oh, yes — with a drift. The
gyros I used send their values to the MCU through an analog
output with a voltage proportional to the rate of change.
To integrate the gyro, you can use the following formula
(or something similar, since it is very simple). Be aware that
the result will be the actual angle, but it will be biased and
will drift over time. So, you need to remove the bias and
correct for the drift to get the real angle value. Also, DT
should be measured accurately, as this integral sum will soon
report wrong angles as the errors accumulate.
The formula: A = A + (G_ADC * DT)
where A is the actual angle, G_ADC is the ADC reading of
the gyro, and DT is delta-T — the integral processing time.
DT can be set in two forms, either by determining the
difference in time from the last sample to the actual one (this
method is preferred) or by estimating it as: 1/samples per
second (ex. 1/100 Hz = 0.01 seconds). If you estimate it,
make sure that you get really close to reality, otherwise, your
integral will not work well. If you have access to timer
overflow interrupts in your microcontroller, then you can
estimate it very closely and get better results.
In my case, I reduced the resolution of the reading to get
a more steady integration, since the gyros also send some
noise. So, I sacrificed a bit of precision (not necessary, but still
precious, precision) and gathered a more stable reading.
Since the gyro integration will work okay with some
drift, you have to de-drift the integration somehow. This is
where filtering comes in handy. If you use a good filter, it will
also remove the noise from your sensors. Let me tell you that