Accelerometers normally come in either 2 axis, or 3 axis versions. Sometimes you can buy a combo board that contains an accelerometer and, say, a compass. I don't directly support these combos so you need to declare the two individual sensors.
So what do they do? Accelerometers measure acceleration along each of their axes. This could be used to monitor vehicle acceleration, vibrations, and any other kind of movement - after all a movement requires an acceleration!
The axes are normally called X, Y and Z. A two axis device normally provides X and Y, whereas a 3 axis device provides X, Y and Z. Most devices have a small circle on the package to indicate the X axis.
However the actual direction of the axes will depend on how you mount the device onto your robot. The convention is that X axis should be the forward direction of travel, the Y axis is to the right of that, and Z is down. (I think thats correct!)
These devices either use one ADC pin for each axis, or provide an I2C interface but you will need to check the individual data sheets to see what power supply they need. Be carefull some of them require a 3.3v supply so connecting them to the usual 5v will fry them!
All accelerometers return one value per axis and this value is in 'mG' ie thousands-of-a-G where G is the gravitational constant of 9.8m/s/s. Each value may be positive or negative and a two axis device will set the Z value to zero.
Since all of the devices have been implemented in the same way then it means that you can swap one device for another by only changing the #include and the MAKE command used to create the device.
So here is the generic way to work with a <DEVICE> of a given <MAKE> and <MODEL>:-
// Include the relevant sensor file
// Create the device
<DEVICE> accel = MAKE_<DEVICE>(F0,F1,F2);
In your appInitHardware you should initialise the device:-
Then in your main loop you can read the sensor using:-
Each axis can then be read independently into a variable of type 'ACCEL_TYPE':-
ACCEL_TYPE x = accel.accelerometer.x_axis_mG;
ACCEL_TYPE y = accel.accelerometer.y_axis_mG;
ACCEL_TYPE z = accel.accelerometer.z_axis_mG;
Or dumped to the current rprintf destination using:-
Whilst the above code will show changing values then please be aware that the manufacturing tolerances of these devices is not very good. This means that two similar devices may output wildly different values. If you want to achieve a more accurate measurement then you must calibrate the software to match your device. This is done as follows:-
1. Use the above code to dump out the x,y,z values
2. Carefully rotate the sensor around the x, y and then z axes whilst taking care not to bounce it up and down
3. Note down the minimum and maximum values that you see for each axis
4. In appInitHardware: after you have initialised the sensor then calibrate each axis by passing in the minimum and maximum values that you noted down. ie to calibrate the x axis use
Now re-run your tests and you should find that the calibrated axes will return values between -1000 and +1000.
- DimensionEngineering/ACCM3D2.h - The ACCM3D2 is a 3 axis accelerometer from Dimension Engineering. See http://www.dimensionengineering.com/DE-ACCM3D2.htm
- AnalogDevices/ADXL335.h - The ADXL335 is a 3 axis accelerometer from Analog Devices capable of measuring ±3g.
- AnalogDevices/ADXL345.h - The ADXL345 is a 3 axis accelerometer from Analog Devices capable of measuring ±16g.
- Freescale/MMA7260QT.h - The MMA7260QT is a 3 axis accelerometer from Freescale capable of measuring up to ±6g in four different sensitivity ranges.