44 SERVO 07.2016
detect only a percentage of the maximum magnetic field.
The actual amount each element detects will be
proportional to the SINE or COSINE of the angle between
the north heading and the element’s heading.
Once the forces on elements X and Y are measured
(call them Fy and Fx), the compass’ (and thus the robot’s)
heading can be calculated using ATAN2(Fy,Fx) — a function
available in most computer languages. Note: ATAN2 is
preferred over the ATAN functions because ATAN2 works
with angles in all four quadrants.
The above calculation requires that the sensing
elements be level — that is, the compass should be parallel
with the Earth’s magnetic field. If the compass is tilted,
then the reading from each of the sensing elements must
be adjusted because the relative tilt angles will reduce the
field detected by the elements. This simple example explains
why it can be important to have multiple sensors.
Acceleration data, for example, can detect tilt that can be
used to modify the magnetometer data to ensure accurate
readings even when the compass is not level.
In a similar manner, the compass information can be
used to augment the data from other sensors to provide
accurate information about the orientation of the entire
When combining the
readings from multiple sensors,
the mathematics can quickly
become complex and involve both
trigonometry and matrix
operations. The complexity
becomes even worse when you
realize that all the data must be
(filtered) to get more usable
information. Fortunately, there are
IMUs that have an integrated
processor that performs the math
operations for you. Such devices are far easier to use, but
(as you would expect) the convenience comes at a much
higher cost. In general, more expensive units will give you
more reliable data at faster data rates.
What Do You Really Need?
Many projects do not require a full IMU. If, for
example, your mobile robot is always operating on a level
floor, you should be able to get usable compass headings
from a simple magnetometer. If your robot operates over
rough terrain, though, you will need a tilt-compensated
compass (or purchase a compass and accelerometer and
learn to write your own compensation code).
If you are building a Segway-like robot that does not
make sudden starts and stops, then a simple accelerometer
might provide all the tilt information you need for
balancing. Keep in mind, however, that sudden movements
can invalidate the tilt information for short periods of time.
All of the above problems are important, but there is
even more that must be considered. Most orientation
sensors are interfaced using an I2C bus which can be
complicated — especially for beginners. Many
microcontrollers have I2C libraries that help with the
interfacing, so check before you begin reinventing the
wheel. The HMC5883 compass shown in Figure 3 is
available from many sources (including Parallax) in a variety
of footprints. Its simplicity is a good place to start if you
want to experiment with an I2C device
More recently, additional interfaces for inertial sensors
have emerged. For example, the ADXL335 accelerometer
shown in Figure 4 provides analog outputs that are
proportional to the unit’s tilt. This makes interfacing
extremely easy for processors with integrated A/D inputs
such as the Arduino. The resolution is often slightly
reduced when analog interfacing is used, but that should
not be a problem for most hobby applications.
Some sensors offer both an I2C and a TTL serial
Post comments on this article at www.servomagazine.com/index.php/
FIGURE 3 FIGURE 4