Installing LSM303DLHC, L3GD20, and BMP180 Drivers
You will need to install all of the following libraries to make use of the Adafruit 10DOF breakout:
For information on the Adafruit Sensor Library and the Unified Sensor Drivers used for all of these sensors, feel free to have a look at our related learning guide: Using the Adafruit Unified Sensor Driver
Downloading Libraries
To use these libraries, you should download them from the Arduino Library Manager.
Open up the Arduino Library Manager:
Search for the Adafruit Unified Sensor library and install it
Search for the Adafruit L3GD20 library and install it
Search for the Adafruit BMP085 Unified library and install it
Search for the Adafruit 10DOF library and install it
Adafruit_10DOF Library Helper Functions
The Adafruit_10DOF.cpp file (from Adafruit_10DOF) includes the following helper functions that are useful when working with typicaly 10DOF projectsbool accelGetOrientation ( sensors_event_t *event, sensors_vec_t *orientation )
This function reads the LSM303DLHC accelerometer data (supplied via the 'event' variable), and converts it into equivalent pitch (x) and roll (y) values, populating the supplied 'orientation' variables .pitch and .roll fields accordingly.Arguments
- event: The sensors_event_t variable containing the data from the accelerometer
- orientation: The sensors_vec_t object that will have its .pitch and .roll fields populated
- true if the operation was successful,
- false if there was an error
See the 'pitchrollheading' example in the Adafruit_10DOF library for an example of how to use this helper function
sensors_event_t accel_event; sensors_vec_t orientation; /* Calculate pitch and roll from the raw accelerometer data */ accel.getEvent(&accel_event); if (dof.accelGetOrientation(&accel_event, &orientation)) { /* 'orientation' should have valid .roll and .pitch fields */ Serial.print(F("Roll: ")); Serial.print(orientation.roll); Serial.print(F("; ")); Serial.print(F("Pitch: ")); Serial.print(orientation.pitch); Serial.print(F("; ")); }
bool magGetOrientation ( sensors_axis_t axis, sensors_event_t *event, sensors_vec_t *mag_orientation )
This function populates the .heading field in mag_orientation with the correct angular data (0-359°). Heading increases when rotating clockwise around the specified axis.Arguments
- axis: The given axis (SENSOR_AXIS_X, SENSOR_AXIS_Y, or SENSOR_AXIS_Z)
- event: The raw magnetometer sensor data to use when calculating out heading
- orientation: The sensors_vec_t object where we will assign an 'orientation.heading' value
Returns
- true if the operation was successful,
- false if there was an error
See the 'pitchrollheading' example in the Adafruit_10DOF library for an example of how to use this helper function
sensors_event_t mag_event; sensors_vec_t orientation; /* Calculate the heading using the magnetometer */ mag.getEvent(&mag_event); if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation)) { /* 'orientation' should have valid .heading data now */ Serial.print(F("Heading: ")); Serial.print(orientation.heading); Serial.print(F("; ")); }
bool magTiltCompensation ( sensors_axis_t axis, sensors_event_t *mag_event, sensors_event_t *accel_event )
This function uses the accelerometer data provided in accel_event to compensate the magnetic sensor measurements in mag_event to compensate for situations where the sensor is tilted (the pitch and roll angles are not equal to 0°).Arguments
- axis: The given axis (SENSOR_AXIS_X, SENSOR_AXIS_Y, or SENSOR_AXIS_Z) that is parallel to the gravity of the Earth
- mag_event: The raw magnetometer data to adjust for tilt
- accel_event: The accelerometer event data to use to determine the tilt when compensating the mag_event values
Returns
- true if the operation was successful,
- false if there was an error
sensors_event_t accel_event; sensors_event_t mag_event; ... // Get a sensor event from the accelerometer and magnetometer accel.getEvent(&accel_event); mag.getEvent(&mag_event); if (dof.magTiltCompensation(SENSOR_AXIS_Z, &mag_event, &accel_event)) { // Do something with the compensated data in mag_event! } else { // Oops ... something went wrong (probably bad data) }
Example Sketch
If you run the pitchrollheading sketch in the examples folder, you can see a practical example using these helper functions above, which should result in output similar to the image below:Page last edited March 08, 2024
Text editor powered by tinymce.