The HMC5843 compass sensor.
The image shows a carrier board from Sparkfun.
This compass is accessed over I2C at a fixed address of 0x3C. Since the device is really a magnetometer then the floating point maths library is used to calculate the bearing, roll and pitch in degrees.
The raw magnetometer values can also be accessed via the various 'getRaw' functions.
The device allows you to specify a refresh rate (ie how many times per second the readings are updated) and this can be set up in Project Designer. Although not particularly recommended; it is possible to modify the refresh rate at run time using the various 'refresh' functions.
Assuming you have called the device 'myCompass' in Project Designer then here is some example code:
// read the compass values and store the results
// Print out the values in degrees
cout << " Bearing:" << myCompass.getBearing();
cout << " Roll:" << myCompass.getRoll();
cout << " Pitch:" << myCompass.getPitch();
// Or just dump everything in one go
cout << myCompass;
// Alternatively the raw x,y,z magnetometer values can be shown
cout << Raw: << myCompass.getRawX() << ",";
cout << myCompass.getRawY() << ",";
cout << myCompass.getRawZ();
- getRawX - Returns the raw magnetometer X value.
- getRawY - Returns the raw magnetometer Y value.
- getRawZ - Returns the raw magnetometer Z value.
- isFunctional - Returns TRUE if the device is functioning correctly.
- refresh10Hz - Changes the default refresh rate to ten times per second (ie every 100ms).
- refresh1Hz - Changes the default refresh rate to once per second.
- refresh20Hz - Changes the default refresh rate to twenty times per second (ie every 50ms).
- refresh2Hz - Changes the default refresh rate to twice per second (ie every 500ms).
- refresh50Hz - Changes the default refresh rate to fifty times per second (ie every 20ms).
- refresh5Hz - Changes the default refresh rate to five times per second (ie every 200ms).
- dump - Dump the last read sensor values to the standard output device.
- dumpTo - Dump the contents of the sensor to the specified output stream.
- getBearing - Returns the compass bearing, in degrees, at the time of the last read().
- getPitch - Returns the compass pitch, in degrees, at the time of the last read().
- getRoll - Returns the compass roll, in degrees, at the time of the last read().
- getTimeLastRead - Returns the value of the clock at the time when the sensor was last read.
- read - Read the sensor and store its current values. Returns TRUE if the sensor has been read successfully, or FALSE if the sensor is busy in which case the read values are unchanged.