An accelerometer can be a very useful sensor for your robot. It measures acceleration or changes in motion. Most available sensors do this for 3 axis. Acceleration can be used to calculate tilt, movements, changes in speed and changes in position.
An accelerometer measures earth gravity too, a motionless sensor still reports an acceleration on the Z-axis of 1G (=9.81 m/sec^2). Thanks to earth’s gravity a motionless accelerometer can be used to calculate the tilt angle of the sensor. The acceleration (in G) that an accelerometer measures on each axis equals the cosine of the angle that the axis makes. This angle is in respect to a vector that is pointing straight down. When the accelerometer is accelerating, for example because your robot is going from stand-still to a motion, you can no longer calculate tilt angles reliably. But once the robot is in a constant motion you can. Some accelerometers, like the mindsensors accelerometer, can report tilt angles directly. But I think it is best to calculate it yourself from the acceleration as this is often more precise.
From the acceleration you can calculate speed, and from speed you can calculate position. Well, to be precise you can only do that if you know initial speed and position. As we normally do not build air-to-air missiles, we can assume the initial speed of our robots is zero. That is also what we assume for initial location.
A simple way to calculate the speed from the acceleration is to take the last known speed and add the acceleration (in m/sec^2) times number of second (since the last known speed was calculated). This process is called integration. It assumes that no big changes in acceleration have occurred between the two measurements. The less time that there is between two measurements the more likely this is to be true. Therefore it is often a good thing to have your sample rate as high as possible. To calculate position you have to integrate speed. Thus location = last location + speed * seconds since last time. This is called a second order integration because speed itself is the result of an integration.
During integration small errors in measured acceleration build up quickly. Suppose your sensor reports an acceleration of 1 milli-G where as in reality this is zero. After just one minute this small error results in a calculated speed of 59 cm/sec^2 or 2 km/hour. In a second order integration errors build up even faster. An error of one milli-G is much smaller than the error of the accelerometers we use. It is even smaller than the resolution of the sensors we use. Resolution normally is around 3 milli-G. This makes it impossible to get a reliable speed or position from just the accelerometer. But still acceleration can be of use for this in combination with other sensors like a GPS or wheel tacho’s. It can replace a GPS for a short time when there is no GPS signal. Or it can help to detect wheel slippage and correct for this.
If you want the best possible information from your sensor you need , as always, to deal with three aspects. Calibration, sensor mounting and sensor noise.
Calibrating an accelerometer minimizes the internal error of the sensor. There are, at least, three kinds of internal errors in accelerometers. Offset, scale and linearity.
Offset error means that your sensor reports too high, or low, consistently. This can be dealt with easily. The sensor value you get from a motionless sensor equals the offset error. On the Z-axis you’ll have to correct for gravity though.
Scale error means that the sensor reports a value that is a certain percentage of the actual acceleration, even when the offset is right. The scale error can be easily calculated as well thanks to gravity. It takes two measurements to calculate the scale, one while pointing the sensor straight down and one while pointing straight up. You know that the difference between to the should be 2 times G.
Linearity error means that the sensor is not consistent over its range. Or put it differently, the scale error differs with different magnitudes of acceleration. linearity errors are hard to overcome. Luckily the dynamic range of NXT robots is really small, you can therefore can ignore linearity errors.
Sensor mounting is the aspect you’ll have to deal with when you want good sensory input. A misaligned sensor will report faulty values. Suppose your sensor points down with 45 degrees your sensor will report an acceleration of 0.7 G when there is none (excepts earths gravity). If your robot is only free to move upright on a flat surface you can prevent these errors with careful construction. So in most cases this aspect can be ignored in software. If however your robot has freedom of tilt you must deal with this in the software. But this is not considered an error and won’t be discussed in this post.
Moving robots vibrate. These vibrations are also measured by the sensor and make the signal more noisy and harder to interpret. It is a good idea to mount your sensor somewhere were the vibrations are the least powerful. Do not mount it to high or on one side of the robot. As a rule of thumb, the best place is on the center of gravity or straight below it.
The third aspect is sensor noise. I do not mean noise due to vibrations as discussed above but the noise that is in the electronics itself. Don’t we all get tired with sensor noise? Accelerometers are particularly noisy. I won’t bother you with statistics and noise measurements this time. Take a look at the output of your sensor to get an idea. You’ll see values that are more then 5% of the expected value quite frequently. You’ll have to take noise into account when calibrating your sensor. Do not rely on a single measurement but take the average of multiple (400) measurements to eliminate noise during calibration. Most sensors have some internal ways to minimize noise. Most often this is done by a low pass filter, sometimes you can even tune this filter. You can always make your own low pass filter to get the noise levels even further down. But remember, a low pass filter makes your sensory output less responsive, it takes some time for real changes to be visible in there full extend.
The Mindsensors accelerometer is the most common accelerometer for the NXT. It has I2C commands to calibrate the sensor, Mindsensors also provides a program to calibrate it. The commands are quite cumbersome to implement. The program is easy to use, but you need to have the standard firmware to run it. You can also write your own calibration routine, this is not as hard as it may seem. There is one disadvantage though, the calibration settings are not stored in the sensor and need to be included in your programs. If you want to write your own calibration program, here is how.
- Make a function that returns the uncalibrated (raw) values.
- position the sensor in such a way that the axis to calibrate is pointing straight down. It doesn’t have to be spot on, a few degrees of do not matter very much.
- take the average of a number of raw sensor values and store/remember this as the minimum value.
- position the sensor straight up.
- take the average of a number of raw sensor values and store this as the maximum value.
- take the average of the minimum and maximum values. This is the sensor offset, it is used later to calculate calibrated values.
- take the difference between the minimum and maximum values. This range represents 2 G’s.
- divide the range by 2000. The result is the scale factor you need to get calibrated readings in milli-G.
- repeat this process for each of the axes.
Knowing both the offset and the scale factor you can now calculate a calibrated value from a raw value. Take the raw value, subtract the offset from it and multiply by the scale factor. The result is a calibrated value in milli-G. Just to make sure you understand, all three axes have their own offset and the scale factor.
Offset and scale do not change over time , so once you get these values you can hard code them in your programs. There is no need to calculate these values every time you run your program.