IMUs — study guide
The concept's fragments, read in order.
For your capstone Fly a site and deliver a survey-grade map
On the mapping flight the IMU sets how evenly the aircraft flies. Its gyroscopes run the autopilot's fastest loop and its accelerometers give it level and feed the height estimate, so the 250 photos land on straight lines at a steady height only if the IMU tells the truth.
Say you set the aircraft on the tailgate to measure its gyroscope bias and someone bumps the truck halfway through: the setup software starts over, because the average it was building now holds a real turn. In the air, vibration is the reading to watch: one open-source autopilot's rule of thumb calls under 30 m/s^2 normally acceptable and over 60 m/s^2 trouble for holding position or height.
A sense of motion on one small chip
An inertial measurement unit, or IMU, is a chip that feels its own motion without looking at anything outside itself. Inside are two kinds of sensor, three of each. Accelerometers measure the push along each of three axes at right angles; gyroscopes measure how fast the chip is turning about each of those axes. Three plus three makes a 6-axis IMU. Many parts add a magnetometer, a three-axis compass sensor, and are sold as 9-axis.
What comes out is a stream of numbers: six or nine fresh readings, many times a second, waiting in the chip's registers for a controller to fetch over a bus such as I2C. None of those numbers is a position or an angle. The chip reports rates of turn and accelerations, here and now, and anything else is worked out in software by adding those readings up over time.
The strength and the weakness come from the same place. An IMU needs no satellites, no light and no landmarks, so it keeps answering in a tunnel, in the dark and in the middle of a spin, and it answers fast. But every small error in a reading is added up along with the reading, so anything worked out from an IMU alone wanders away from the truth: angles slowly, positions alarmingly fast. That is why robots and aircraft pair it with sensors that do not drift, combining them into one estimate, which is a subject of its own.
This concept covers what each sensor actually measures and how, the axes and units, turning raw register counts into units, noise and bias and the drift they cause, calibrating at rest, mounting and vibration, and reading one over I2C.
An accelerometer feels gravity even when it sits still
Inside a micro-machined accelerometer is a speck of silicon called the proof mass, hung on springs etched from the same silicon. When the case is pushed, the springs have to drag the mass along with it, so they stretch, and the chip measures how far the mass has shifted. The reading is proportional to that spring force, the push the case is giving the mass, worked out per unit of mass so that it comes in units of acceleration. There is one such arrangement for each axis.
That is why an accelerometer feels gravity while sitting perfectly still. Gravity pulls the mass down, the springs hold it up, and the table holds up the case. The springs are pushing up, so the reading is 1 g pointing up, about 9.81 m/s^2. The convention that open-source robot software uses for IMU drivers writes exactly this down: at rest with its z axis pointing up, the z axis reads +g. The reading points up, away from the ground, but it lies exactly along gravity's line, so software that wants the direction of down simply reverses it. Drop the sensor and the reading falls to zero, because nothing is holding the mass back and case and mass fall together, much as a bathroom scale in an elevator shows your full weight standing still, more as the car starts up, and nothing at all if the car were to drop freely, since it only ever measures the push holding you up. Engineers call the quantity it reports specific force, to keep it apart from the acceleration of the case itself.
At rest, that 1 g makes a level. Tilt the board and the 1 g shares itself out among the three axes, following the sine and cosine of the tilt angles, so a robot standing still can work out its roll and pitch from three numbers. It cannot work out which way it faces, because turning about the vertical changes nothing about gravity's share.
The catch is that the springs cannot tell gravity from motion. A robot speeding up forward drags the mass backward exactly as tipping the nose up would, and the chip reports the sum of the two. So the accelerometer's sense of level holds only while the vehicle is not speeding up, slowing down or turning, and its sense of motion is usable only after gravity's share is subtracted, which needs the tilt known precisely. Neither half of that works alone, and that circle is the accelerometer's whole character.
A gyroscope measures turning, not angle
The gyroscope on an IMU measures rate of turn: how many degrees, or radians, per second the chip is rotating about each of its three axes at this instant. Hold the board tilted at 30 degrees and keep it there, and the gyroscope reads zero. Roll it at 30 degrees per second, and it reads 30 whatever angle it happens to be passing through. It is a speedometer for turning rather than an odometer: it says how fast the turn is going right now and nothing directly about which way the body is pointing.
There is no spinning wheel inside. A tiny silicon mass is driven to vibrate back and forth along one direction. While the chip holds still, the mass simply shuttles along that line. When the chip turns, the Coriolis effect pushes the moving mass sideways, at right angles to its vibration, with a force proportional to the rate of turn, and the push flips each time the mass reverses. Fine electrodes beside the mass sense that sideways wobble, typically as a change in capacitance, and the chip reports its size as a rate. No turn, no sideways push, a reading of zero.
To get an angle out of a rate, software adds the rate up over time. Turning at 30 degrees per second for 2 seconds is 60 degrees of turn. That adding up, integration, is the only way a gyroscope yields an angle, and it carries a cost: every small error in every reading is added up too, so the angle it produces slowly wanders.
Gyroscopes earn their place anyway because they are quick and smooth, and they pay little attention to gravity or to the vehicle speeding up. Within a fraction of a second they say exactly how the body is rotating, which is what a controller needs to catch a tip or a skid before it grows.
Three axes each, six or nine in all
Every sensor on an IMU works along three axes at right angles, named x, y and z and fixed to the chip itself. A breakout board usually prints small arrows showing which way each one points. The axes are right-handed: lay the fingers of your right hand along x, curl them toward y, and your thumb points along z. Robot software commonly takes x forward, y to the left and z up; aircraft autopilots commonly take x forward, y to the right and z down. Both are right-handed, and mixing them up flips the signs of readings without any error message.
The accelerometer measures along each axis. The gyroscope measures turning about each axis, and its sign follows the same hand: point the thumb along the axis and the curl of the fingers is the positive direction. Turning about the forward axis is called roll, about the sideways axis pitch, and about the vertical axis yaw.
A 9-axis IMU adds a magnetometer, which measures the Earth's magnetic field along each of the three axes and so gives a heading. A 6-axis unit has no such reference. Gravity pins down its roll and pitch, but which way it faces is known only from the gyroscope's running total, which drifts, so the driver convention for robot IMUs says outright that without a magnetometer the heading is arbitrary. The magnetometer has troubles of its own, since it reads every field nearby, from motors, wiring and steel, as well as the Earth's.
The axes belong to the chip, not to the vehicle. If the board is mounted turned or upside down, every reading arrives in the wrong frame until software rotates it into the vehicle's, which is why setup software asks how the board is mounted before it does anything else.
From register counts to g and degrees per second
An IMU does not hand out g or degrees per second. Each axis passes through an analog-to-digital converter and lands in the chip's registers as a signed whole number. On one common 6-axis chip that number is 16 bits in two's complement, running from -32768 to 32767. The count means nothing until you know what range the chip was set to.
The range is a setting. One common chip's datasheet lists accelerometer ranges of plus or minus 2, 4, 8 or 16 g and gyroscope ranges of plus or minus 250, 500, 1000 or 2000 degrees per second. The full span of counts always covers the chosen range, so the range sets the sensitivity: at plus or minus 2 g, 32,768 counts cover 2 g, which is 16384 counts per g, and at plus or minus 250 degrees per second it is 32,768 over 250, about 131 counts per degree per second. Converting is one division. A raw accelerometer reading of 16384 at that range is 1.00 g, or 9.81 m/s^2; a raw gyroscope reading of -1310 is -10.0 degrees per second, or about -0.175 radians per second, the unit most robot software expects. Assume the wrong range and every reading is off by a factor of 2, 4 or 8, so a robot sitting on a desk that reports 0.5 g or 2 g usually means the range in the code does not match the range set in the chip.
The range is a trade. The same 65,536 steps spread over a wider range make each step coarser: at plus or minus 2 g one count is about 0.0006 m/s^2, at plus or minus 16 g it is eight times that. A range too narrow clips, pinning the reading at its limit while the real motion goes higher. Choose the narrowest range that still covers the hardest real jolt, vibration included.
The other setting is the output data rate, how many new readings the chip produces each second, usually paired with a low-pass filter that smooths each one. One common chip's datasheet lists output rates from a few readings a second up to about 1,000 for its accelerometer and 8,000 for its gyroscope, and one open-source autopilot samples its gyroscopes 1,000 times a second by default. Sampling far below the rate at which things shake opens the door to aliasing, the false slow signal a too-slow sample rate invents.
Noise, bias, and why adding up drifts
Every reading is the true value plus two kinds of error. Noise is random scatter, different on every sample, and it shrinks when you average. Bias is a steady offset, what the sensor reads when nothing is happening, and averaging does nothing to it. Out of the box the bias can be large: one common chip's datasheet allows its gyroscopes to read up to 20 degrees per second while sitting still, and its accelerometers to be off by about 0.05 to 0.08 g. Bias also wanders slowly on its own and moves with temperature, so even a measured bias goes stale.
Integration turns bias into drift. An angle from a gyroscope is the rate added up over time, so a constant bias adds the same small wrong amount every second and the angle error grows in a straight line with time. Take an illustrative leftover bias of 0.1 degrees per second after calibration: after a minute the angle is 6 degrees wrong, after six minutes 36. It is like walking with your eyes closed and counting paces, where a stride slightly too long on every step adds up to an error that keeps growing the longer you walk. Noise adds a smaller random wander on top.
The accelerometer is worse, because position needs two integrations, once to get velocity and again to get position, and a constant bias then gives a position error of half the bias times the time squared. Take an illustrative bias of 0.01 g, about 0.098 m/s^2, which is roughly what a tilt error of half a degree leaks from gravity into the horizontal. After 10 seconds the position is about 4.9 m wrong; after a minute, about 177 m. Ten times the time is a hundred times the error. A 2007 study that integrated a stationary MEMS IMU on its own measured an average drift of about 150 m after 60 seconds, the same order.
The working rule follows. An IMU alone is excellent for rates right now, good for angles over seconds to minutes depending on its bias, and good for positions only over a few seconds. Anything longer needs a sensor that does not drift to pull the estimate back: gravity for tilt, a compass for heading, wheels, cameras or satellites for position.
Calibrating at rest
The error most worth removing is bias, and the moment to measure it is when the true answer is known. A gyroscope that is not turning should read zero on every axis. So set it down, leave it alone, and average a few seconds of readings: that average is the bias, and the program subtracts it from every reading after. Autopilot setup software does exactly this, asking for the vehicle to sit still on a surface and starting over if it moves, and a robot program can do the same in its first seconds after power-up, which is why such a robot should not be picked up while it boots.
An accelerometer at rest is harder, because its true reading is 1 g in whatever direction is up, and up depends on the tilt. Averaging a still accelerometer gives bias and gravity mixed together. The way out is to put it in known orientations, each axis pointing straight up and then straight down. One open-source autopilot walks the user through six positions: level, on its right side, on its left side, nose down, nose up, and on its back. For each axis, the midpoint of the up and down readings is the offset and half their difference is the scale, which should be exactly 1 g. An axis that reads +1.02 g pointing up and -0.98 g pointing down has an offset of 0.02 g and a scale of 1.00.
Stillness matters more than precision. That same autopilot accepts each position within about 20 degrees of exact, except the level one, and says holding still is what counts. The level position does matter most, because the level the calibration records is the attitude the controller will treat as level from then on.
Temperature moves the bias of both sensors, so a sensor calibrated on a warm bench carries a slightly different bias on a cold morning, and letting the electronics warm up before calibrating gives a more honest number. Calibration is maintenance, not a one-time ritual: repeat it after the board is remounted, after a large change in temperature, or whenever a vehicle that used to sit level starts to lean.
Mounting, alignment and vibration
An IMU measures in its own axes, so the software must know how it sits on the vehicle. Setup software for open-source autopilots takes a board orientation in steps of 45 or 90 degrees and rotates every reading before using it. Get that setting wrong and the vehicle's idea of forward is sideways or backward, and a controller then corrects in the wrong direction, making every error larger instead of smaller. A mount that is tilted by a fraction of a degree reads as a permanent lean of the same amount, the thing a level calibration trims out.
The mount should be firm, so the sensor moves exactly as the body does, and the board should sit away from anything that shakes, and for a 9-axis unit away from heavy current, which the magnetometer reads as a field. A loose board or a cable tugging on it adds motion of its own that the IMU cannot tell from the vehicle's.
Vibration is the bigger enemy. Motors, propellers and gear trains shake the frame many times a second, and an accelerometer reports every shake faithfully. Large swings can exceed the chosen range and clip, which one open-source autopilot counts each time an accelerometer hits its 16 g limit. Shaking faster than the sample rate can handle aliases into a slow, false motion that the software believes; the same project's documentation names that aliasing as a common cause of an aircraft leaning while commanded level. Its rule of thumb for the vibration it measures is that below 30 m/s^2 is normally acceptable and above 60 m/s^2 nearly always causes trouble holding position or height.
The fixes go in order: balance the propellers or rotating parts, tie down anything that rattles, mount the board on foam or soft mounts that let the frame shake without shaking the sensor, and only then filter in software. Reducing vibration at its source beats filtering it, because every software filter that smooths a reading also delays it, and the controller then reacts to motion that is already past.
Reading one over I2C
Over I2C an IMU is a peripheral at a 7-bit address, and one common chip lets a pin choose between two addresses so that two of them can share a bus. Inside the chip is a row of numbered registers, each holding a byte. Some are configuration you write, some are measurements you read, and one is an identity register that always returns the same fixed value. Reading that identity register first is the cheap way to confirm the right chip is answering before trusting anything else it says.
Setup is a handful of register writes: the accelerometer range, the gyroscope range, the output data rate and the filter. Each write is one short transfer, START, the address with the write bit, the register number, the value, STOP, with every byte acknowledged.
Reading uses a two-part transfer. The controller writes the number of the first measurement register, then sends a repeated START, a START with no STOP before it so that nothing can slip in between, followed by the address with the read bit. The chip then sends bytes starting at that register and steps to the next register on its own. The controller acknowledges each byte and answers the last with a NACK before the STOP. One common chip lays out fourteen bytes this way: accelerometer x, y and z, a temperature, then gyroscope x, y and z, two bytes each with the high byte first. The program joins each pair into a signed 16-bit count and divides by the sensitivity for the range it set.
At the common 400 kHz clock each byte takes nine clock ticks, so that fourteen-byte read costs about 0.4 ms of bus time. That is comfortable for a few hundred readings a second, crowded at a thousand with other sensors on the same two wires, and out of reach at several thousand; many IMU chips also offer SPI, and one common family's datasheet allows the sensor registers of its SPI version to be read at up to 20 MHz, which is the reason to reach for SPI when the rate climbs. The chip with that fourteen-byte layout can also pulse a data-ready pin when a fresh set of readings is waiting, so the program can read each sample exactly once instead of polling and guessing.
Why both capstones lean on an IMU
On a small self-driving car, the gyroscope's yaw rate says how fast the car is actually turning, which the steering code can compare with the turn it asked for, and the heading it adds up holds for a while between other cues before it drifts. The accelerometers notice bumps, a collision, or a ramp tilting the car. What the IMU cannot do is keep track of where the car is: position from its accelerometers alone is meters wrong within seconds. The car's wheels, its camera and the rest correct that drift, and combining them is the work of the concepts that come after this one.
On a survey drone the IMU is the sensor everything else waits on. The autopilot's fastest loop runs on its gyroscopes, hundreds of times a second, and the accelerometers give it level and feed its height and position estimate, so a vibrating or badly calibrated IMU shows up as an aircraft that leans, wanders or holds height poorly even with a perfect satellite fix. How the autopilot builds its loops and modes on these readings is a concept of its own.
For both builds the IMU's needs are the same short list, and they are the builder's or the pilot's to meet: a board mounted firmly, the right way round, with its orientation set in software; a level calibration done on a flat surface; the sensor left still while it measures its gyroscope bias; and vibration kept down at its source. A survey depends on the aircraft flying evenly spaced lines at a steady height and speed while the camera fires, and every one of those starts with an IMU that tells the truth about how the aircraft is moving.
Sources
The published studies, standards and guidance this page cites. Links open on other sites.
- An introduction to inertial navigation (Oliver J. Woodman, University of Cambridge Computer Laboratory Technical Report UCAM-CL-TR-696, 2007) www.cl.cam.ac.uk
- REP 145, Conventions for IMU Sensor Drivers (ROS Enhancement Proposal, 2015) www.ros.org
- Gyroscope Technology and Applications: A Review in the Industrial Perspective (Passaro et al., Sensors, MDPI, 2017) www.mdpi.com
- REP 103, Standard Units of Measure and Coordinate Conventions (ROS Enhancement Proposal) www.ros.org
- Flight Controller/Sensor Orientation (PX4 Guide) docs.px4.io
- sysfs-bus-iio, the Industrial I/O sysfs interface (Linux kernel documentation) www.kernel.org
- Complete Parameter List (ArduPilot Copter documentation) ardupilot.org
- Gyroscope Calibration (PX4 Guide) docs.px4.io
- Accelerometer Calibration (ArduPilot Copter documentation) ardupilot.org
- Measuring Vibration (ArduPilot Copter documentation) ardupilot.org
- Vibration Isolation (PX4 Guide) docs.px4.io
- The SMBus Protocol (Linux kernel documentation) docs.kernel.org
Next on your path to Fly a site and deliver a survey-grade map: Cameras ▸