AHRS

Dependencies:   Eigen

Dependents:   IndNav_QK3_T265

Committer:
pmic
Date:
Mon Jan 27 10:54:13 2020 +0000
Revision:
30:9b0cd3caf0ec
Parent:
25:fe14dbcef82d
Correct magnetometer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
altb2 4:3c21fb0c9e84 1 /******************************************************************************
altb2 4:3c21fb0c9e84 2 SFE_LSM9DS1.h
altb2 4:3c21fb0c9e84 3 SFE_LSM9DS1 Library Header File
altb2 4:3c21fb0c9e84 4 Jim Lindblom @ SparkFun Electronics
altb2 4:3c21fb0c9e84 5 Original Creation Date: February 27, 2015
altb2 4:3c21fb0c9e84 6 https://github.com/sparkfun/LSM9DS1_Breakout
altb2 4:3c21fb0c9e84 7
altb2 4:3c21fb0c9e84 8 This file prototypes the LSM9DS1 class, implemented in SFE_LSM9DS1.cpp. In
altb2 4:3c21fb0c9e84 9 addition, it defines every register in the LSM9DS1 (both the Gyro and Accel/
altb2 4:3c21fb0c9e84 10 Magnetometer registers).
altb2 4:3c21fb0c9e84 11
altb2 4:3c21fb0c9e84 12 Development environment specifics:
altb2 4:3c21fb0c9e84 13 IDE: Arduino 1.6.0
altb2 4:3c21fb0c9e84 14 Hardware Platform: Arduino Uno
altb2 4:3c21fb0c9e84 15 LSM9DS1 Breakout Version: 1.0
altb2 4:3c21fb0c9e84 16
altb2 4:3c21fb0c9e84 17 This code is beerware; if you see me (or any other SparkFun employee) at the
altb2 4:3c21fb0c9e84 18 local, and you've found our code helpful, please buy us a round!
altb2 4:3c21fb0c9e84 19
altb2 4:3c21fb0c9e84 20 Distributed as-is; no warranty is given.
altb2 4:3c21fb0c9e84 21 ******************************************************************************/
altb2 4:3c21fb0c9e84 22 #ifndef __SparkFunLSM9DS1_H__
altb2 4:3c21fb0c9e84 23 #define __SparkFunLSM9DS1_H__
altb2 4:3c21fb0c9e84 24
altb2 4:3c21fb0c9e84 25 //#if defined(ARDUINO) && ARDUINO >= 100
altb2 4:3c21fb0c9e84 26 // #include "Arduino.h"
altb2 4:3c21fb0c9e84 27 //#else
altb2 4:3c21fb0c9e84 28 // #include "WProgram.h"
altb2 4:3c21fb0c9e84 29 // #include "pins_arduino.h"
altb2 4:3c21fb0c9e84 30 //#endif
altb2 4:3c21fb0c9e84 31
altb2 4:3c21fb0c9e84 32 #include "mbed.h"
altb2 4:3c21fb0c9e84 33 #include <stdint.h>
altb2 4:3c21fb0c9e84 34 #include "LSM9DS1_Registers.h"
altb2 4:3c21fb0c9e84 35 #include "LSM9DS1_Types.h"
altb2 4:3c21fb0c9e84 36
altb2 4:3c21fb0c9e84 37 #define LSM9DS1_AG_ADDR(sa0) ((sa0) == 0 ? 0x6A : 0x6B)
altb2 4:3c21fb0c9e84 38 #define LSM9DS1_M_ADDR(sa1) ((sa1) == 0 ? 0x1C : 0x1E)
altb2 4:3c21fb0c9e84 39
altb2 4:3c21fb0c9e84 40 enum lsm9ds1_axis {
altb2 4:3c21fb0c9e84 41 X_AXIS,
altb2 4:3c21fb0c9e84 42 Y_AXIS,
altb2 4:3c21fb0c9e84 43 Z_AXIS,
altb2 4:3c21fb0c9e84 44 ALL_AXIS
altb2 4:3c21fb0c9e84 45 };
altb2 4:3c21fb0c9e84 46
altb2 4:3c21fb0c9e84 47 class LSM9DS1
altb2 4:3c21fb0c9e84 48 {
altb2 4:3c21fb0c9e84 49 public:
altb2 4:3c21fb0c9e84 50 IMUSettings settings;
altb2 4:3c21fb0c9e84 51
altb2 4:3c21fb0c9e84 52 // We'll store the gyro, accel, and magnetometer readings in a series of
altb2 4:3c21fb0c9e84 53 // public class variables. Each sensor gets three variables -- one for each
altb2 4:3c21fb0c9e84 54 // axis. Call readGyro(), readAccel(), and readMag() first, before using
altb2 4:3c21fb0c9e84 55 // these variables!
altb2 4:3c21fb0c9e84 56 // These values are the RAW signed 16-bit readings from the sensors.
altb2 4:3c21fb0c9e84 57 int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope
altb2 4:3c21fb0c9e84 58 int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer
altb2 4:3c21fb0c9e84 59 int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer
altb2 4:3c21fb0c9e84 60 int16_t temperature; // Chip temperature
altb2 4:3c21fb0c9e84 61 float gBias[3], aBias[3], mBias[3];
altb2 4:3c21fb0c9e84 62 int16_t gBiasRaw[3], aBiasRaw[3], mBiasRaw[3];
altb2 4:3c21fb0c9e84 63
altb2 4:3c21fb0c9e84 64 float gyroX, gyroY, gyroZ; // x, y, and z axis readings of the gyroscope (float value)
altb2 4:3c21fb0c9e84 65 float accX, accY, accZ; // x, y, and z axis readings of the accelerometer (float value)
altb2 4:3c21fb0c9e84 66 float magX, magY, magZ; // x, y, and z axis readings of the magnetometer (float value)
altb2 4:3c21fb0c9e84 67
altb2 4:3c21fb0c9e84 68 // LSM9DS1 -- LSM9DS1 class constructor
altb2 4:3c21fb0c9e84 69 // The constructor will set up a handful of private variables, and set the
altb2 4:3c21fb0c9e84 70 // communication mode as well.
altb2 4:3c21fb0c9e84 71 /**Input:
altb2 4:3c21fb0c9e84 72 * - interface = Either IMU_MODE_SPI or IMU_MODE_I2C, whichever you're using
altb2 4:3c21fb0c9e84 73 * to talk to the IC.
altb2 4:3c21fb0c9e84 74 * - xgAddr = If IMU_MODE_I2C, this is the I2C address of the accel/gyroscope.
altb2 4:3c21fb0c9e84 75 * If IMU_MODE_SPI, this is the chip select pin of the gyro (CS_AG)
altb2 4:3c21fb0c9e84 76 * - mAddr = If IMU_MODE_I2C, this is the I2C address of the magnetometer.
altb2 4:3c21fb0c9e84 77 * If IMU_MODE_SPI, this is the cs pin of the magnetometer (CS_M)
altb2 4:3c21fb0c9e84 78
altb2 4:3c21fb0c9e84 79 */
altb2 25:fe14dbcef82d 80 LSM9DS1(I2C &);
altb2 4:3c21fb0c9e84 81 //LSM9DS1(interface_mode interface, uint8_t xgAddr, uint8_t mAddr);
altb2 4:3c21fb0c9e84 82 //LSM9DS1();
altb2 4:3c21fb0c9e84 83
altb2 4:3c21fb0c9e84 84
altb2 4:3c21fb0c9e84 85 /** begin() -- Initialize the gyro, accelerometer, and magnetometer.
altb2 4:3c21fb0c9e84 86 *This will set up the scale and output rate of each sensor. The values set
altb2 4:3c21fb0c9e84 87 * in the IMUSettings struct will take effect after calling this function.
altb2 4:3c21fb0c9e84 88 */
altb2 4:3c21fb0c9e84 89 uint16_t begin();
altb2 4:3c21fb0c9e84 90
altb2 4:3c21fb0c9e84 91 void calibrate(bool autoCalc = true);
altb2 4:3c21fb0c9e84 92 void calibrateMag(bool loadIn = true);
altb2 4:3c21fb0c9e84 93 void magOffset(uint8_t axis, int16_t offset);
altb2 4:3c21fb0c9e84 94
altb2 4:3c21fb0c9e84 95 /** accelAvailable() -- Polls the accelerometer status register to check
altb2 4:3c21fb0c9e84 96 * if new data is available.
altb2 4:3c21fb0c9e84 97 * Output: 1 - New data available
altb2 4:3c21fb0c9e84 98 * 0 - No new data available
altb2 4:3c21fb0c9e84 99 */
altb2 4:3c21fb0c9e84 100 uint8_t accelAvailable();
altb2 4:3c21fb0c9e84 101
altb2 4:3c21fb0c9e84 102 /** gyroAvailable() -- Polls the gyroscope status register to check
altb2 4:3c21fb0c9e84 103 * if new data is available.
altb2 4:3c21fb0c9e84 104 * Output: 1 - New data available
altb2 4:3c21fb0c9e84 105 * 0 - No new data available
altb2 4:3c21fb0c9e84 106 */
altb2 4:3c21fb0c9e84 107 uint8_t gyroAvailable();
altb2 4:3c21fb0c9e84 108
altb2 4:3c21fb0c9e84 109 /** gyroAvailable() -- Polls the temperature status register to check
altb2 4:3c21fb0c9e84 110 * if new data is available.
altb2 4:3c21fb0c9e84 111 * Output: 1 - New data available
altb2 4:3c21fb0c9e84 112 * 0 - No new data available
altb2 4:3c21fb0c9e84 113 */
altb2 4:3c21fb0c9e84 114 uint8_t tempAvailable();
altb2 4:3c21fb0c9e84 115
altb2 4:3c21fb0c9e84 116 /** magAvailable() -- Polls the accelerometer status register to check
altb2 4:3c21fb0c9e84 117 * if new data is available.
altb2 4:3c21fb0c9e84 118 * Input:
altb2 4:3c21fb0c9e84 119 * - axis can be either X_AXIS, Y_AXIS, Z_AXIS, to check for new data
altb2 4:3c21fb0c9e84 120 * on one specific axis. Or ALL_AXIS (default) to check for new data
altb2 4:3c21fb0c9e84 121 * on all axes.
altb2 4:3c21fb0c9e84 122 * Output: 1 - New data available
altb2 4:3c21fb0c9e84 123 * 0 - No new data available
altb2 4:3c21fb0c9e84 124 */
altb2 4:3c21fb0c9e84 125 uint8_t magAvailable(lsm9ds1_axis axis = ALL_AXIS);
altb2 4:3c21fb0c9e84 126
altb2 4:3c21fb0c9e84 127 /** readGyro() -- Read the gyroscope output registers.
altb2 4:3c21fb0c9e84 128 * This function will read all six gyroscope output registers.
altb2 4:3c21fb0c9e84 129 * The readings are stored in the class' gx, gy, and gz variables. Read
altb2 4:3c21fb0c9e84 130 * those _after_ calling readGyro().
altb2 4:3c21fb0c9e84 131 */
altb2 4:3c21fb0c9e84 132 void readGyro();
altb2 4:3c21fb0c9e84 133
altb2 4:3c21fb0c9e84 134 /** int16_t readGyro(axis) -- Read a specific axis of the gyroscope.
altb2 4:3c21fb0c9e84 135 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
altb2 4:3c21fb0c9e84 136 * Input:
altb2 4:3c21fb0c9e84 137 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
altb2 4:3c21fb0c9e84 138 * Output:
altb2 4:3c21fb0c9e84 139 * A 16-bit signed integer with sensor data on requested axis.
altb2 4:3c21fb0c9e84 140 */
altb2 4:3c21fb0c9e84 141 int16_t readGyro(lsm9ds1_axis axis);
altb2 4:3c21fb0c9e84 142
altb2 4:3c21fb0c9e84 143 /** readAccel() -- Read the accelerometer output registers.
altb2 4:3c21fb0c9e84 144 * This function will read all six accelerometer output registers.
altb2 4:3c21fb0c9e84 145 * The readings are stored in the class' ax, ay, and az variables. Read
altb2 4:3c21fb0c9e84 146 * those _after_ calling readAccel().
altb2 4:3c21fb0c9e84 147 */
altb2 4:3c21fb0c9e84 148 void readAccel();
altb2 4:3c21fb0c9e84 149
altb2 4:3c21fb0c9e84 150 /** int16_t readAccel(axis) -- Read a specific axis of the accelerometer.
altb2 4:3c21fb0c9e84 151 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
altb2 4:3c21fb0c9e84 152 * Input:
altb2 4:3c21fb0c9e84 153 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
altb2 4:3c21fb0c9e84 154 * Output:
altb2 4:3c21fb0c9e84 155 * A 16-bit signed integer with sensor data on requested axis.
altb2 4:3c21fb0c9e84 156 */
altb2 4:3c21fb0c9e84 157 int16_t readAccel(lsm9ds1_axis axis);
altb2 4:3c21fb0c9e84 158
altb2 4:3c21fb0c9e84 159 /** readMag() -- Read the magnetometer output registers.
altb2 4:3c21fb0c9e84 160 * This function will read all six magnetometer output registers.
altb2 4:3c21fb0c9e84 161 * The readings are stored in the class' mx, my, and mz variables. Read
altb2 4:3c21fb0c9e84 162 * those _after_ calling readMag().
altb2 4:3c21fb0c9e84 163 */
altb2 4:3c21fb0c9e84 164 void readMag();
altb2 4:3c21fb0c9e84 165
altb2 4:3c21fb0c9e84 166 /** int16_t readMag(axis) -- Read a specific axis of the magnetometer.
altb2 4:3c21fb0c9e84 167 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
altb2 4:3c21fb0c9e84 168 * Input:
altb2 4:3c21fb0c9e84 169 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
altb2 4:3c21fb0c9e84 170 * Output:
altb2 4:3c21fb0c9e84 171 * A 16-bit signed integer with sensor data on requested axis.
altb2 4:3c21fb0c9e84 172 */
altb2 4:3c21fb0c9e84 173 int16_t readMag(lsm9ds1_axis axis);
altb2 4:3c21fb0c9e84 174
altb2 4:3c21fb0c9e84 175 /** readTemp() -- Read the temperature output register.
altb2 4:3c21fb0c9e84 176 * This function will read two temperature output registers.
altb2 4:3c21fb0c9e84 177 * The combined readings are stored in the class' temperature variables. Read
altb2 4:3c21fb0c9e84 178 * those _after_ calling readTemp().
altb2 4:3c21fb0c9e84 179 */
altb2 4:3c21fb0c9e84 180 void readTemp();
altb2 4:3c21fb0c9e84 181
altb2 4:3c21fb0c9e84 182 /** calcGyro() -- Convert from RAW signed 16-bit value to degrees per second
altb2 4:3c21fb0c9e84 183 * This function reads in a signed 16-bit value and returns the scaled
altb2 4:3c21fb0c9e84 184 * DPS. This function relies on gScale and gRes being correct.
altb2 4:3c21fb0c9e84 185 * Input:
altb2 4:3c21fb0c9e84 186 * - gyro = A signed 16-bit raw reading from the gyroscope.
altb2 4:3c21fb0c9e84 187 */
altb2 4:3c21fb0c9e84 188 float calcGyro(int16_t gyro);
altb2 4:3c21fb0c9e84 189
altb2 4:3c21fb0c9e84 190 /** calcAccel() -- Convert from RAW signed 16-bit value to gravity (g's).
altb2 4:3c21fb0c9e84 191 * This function reads in a signed 16-bit value and returns the scaled
altb2 4:3c21fb0c9e84 192 * g's. This function relies on aScale and aRes being correct.
altb2 4:3c21fb0c9e84 193 * Input:
altb2 4:3c21fb0c9e84 194 * - accel = A signed 16-bit raw reading from the accelerometer.
altb2 4:3c21fb0c9e84 195 */
altb2 4:3c21fb0c9e84 196 float calcAccel(int16_t accel);
altb2 4:3c21fb0c9e84 197
altb2 4:3c21fb0c9e84 198 /** calcMag() -- Convert from RAW signed 16-bit value to Gauss (Gs)
altb2 4:3c21fb0c9e84 199 * This function reads in a signed 16-bit value and returns the scaled
altb2 4:3c21fb0c9e84 200 * Gs. This function relies on mScale and mRes being correct.
altb2 4:3c21fb0c9e84 201 * Input:
altb2 4:3c21fb0c9e84 202 * - mag = A signed 16-bit raw reading from the magnetometer.
altb2 4:3c21fb0c9e84 203 */
altb2 4:3c21fb0c9e84 204 float calcMag(int16_t mag);
altb2 4:3c21fb0c9e84 205
altb2 4:3c21fb0c9e84 206 /** setGyroScale() -- Set the full-scale range of the gyroscope.
altb2 4:3c21fb0c9e84 207 * This function can be called to set the scale of the gyroscope to
altb2 4:3c21fb0c9e84 208 * 245, 500, or 200 degrees per second.
altb2 4:3c21fb0c9e84 209 * Input:
altb2 4:3c21fb0c9e84 210 * - gScl = The desired gyroscope scale. Must be one of three possible
altb2 4:3c21fb0c9e84 211 * values from the gyro_scale.
altb2 4:3c21fb0c9e84 212 */
altb2 4:3c21fb0c9e84 213 void setGyroScale(uint16_t gScl);
altb2 4:3c21fb0c9e84 214
altb2 4:3c21fb0c9e84 215 /** setAccelScale() -- Set the full-scale range of the accelerometer.
altb2 4:3c21fb0c9e84 216 * This function can be called to set the scale of the accelerometer to
altb2 4:3c21fb0c9e84 217 * 2, 4, 6, 8, or 16 g's.
altb2 4:3c21fb0c9e84 218 * Input:
altb2 4:3c21fb0c9e84 219 * - aScl = The desired accelerometer scale. Must be one of five possible
altb2 4:3c21fb0c9e84 220 * values from the accel_scale.
altb2 4:3c21fb0c9e84 221 */
altb2 4:3c21fb0c9e84 222 void setAccelScale(uint8_t aScl);
altb2 4:3c21fb0c9e84 223
altb2 4:3c21fb0c9e84 224 /** setMagScale() -- Set the full-scale range of the magnetometer.
altb2 4:3c21fb0c9e84 225 * This function can be called to set the scale of the magnetometer to
altb2 4:3c21fb0c9e84 226 * 2, 4, 8, or 12 Gs.
altb2 4:3c21fb0c9e84 227 * Input:
altb2 4:3c21fb0c9e84 228 * - mScl = The desired magnetometer scale. Must be one of four possible
altb2 4:3c21fb0c9e84 229 * values from the mag_scale.
altb2 4:3c21fb0c9e84 230 */
altb2 4:3c21fb0c9e84 231 void setMagScale(uint8_t mScl);
altb2 4:3c21fb0c9e84 232
altb2 4:3c21fb0c9e84 233 /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
altb2 4:3c21fb0c9e84 234 * Input:
altb2 4:3c21fb0c9e84 235 * - gRate = The desired output rate and cutoff frequency of the gyro.
altb2 4:3c21fb0c9e84 236 */
altb2 4:3c21fb0c9e84 237 void setGyroODR(uint8_t gRate);
altb2 4:3c21fb0c9e84 238
altb2 4:3c21fb0c9e84 239 // setAccelODR() -- Set the output data rate of the accelerometer
altb2 4:3c21fb0c9e84 240 // Input:
altb2 4:3c21fb0c9e84 241 // - aRate = The desired output rate of the accel.
altb2 4:3c21fb0c9e84 242 void setAccelODR(uint8_t aRate);
altb2 4:3c21fb0c9e84 243
altb2 4:3c21fb0c9e84 244 // setMagODR() -- Set the output data rate of the magnetometer
altb2 4:3c21fb0c9e84 245 // Input:
altb2 4:3c21fb0c9e84 246 // - mRate = The desired output rate of the mag.
altb2 4:3c21fb0c9e84 247 void setMagODR(uint8_t mRate);
altb2 4:3c21fb0c9e84 248
altb2 4:3c21fb0c9e84 249 // configInactivity() -- Configure inactivity interrupt parameters
altb2 4:3c21fb0c9e84 250 // Input:
altb2 4:3c21fb0c9e84 251 // - duration = Inactivity duration - actual value depends on gyro ODR
altb2 4:3c21fb0c9e84 252 // - threshold = Activity Threshold
altb2 4:3c21fb0c9e84 253 // - sleepOn = Gyroscope operating mode during inactivity.
altb2 4:3c21fb0c9e84 254 // true: gyroscope in sleep mode
altb2 4:3c21fb0c9e84 255 // false: gyroscope in power-down
altb2 4:3c21fb0c9e84 256 void configInactivity(uint8_t duration, uint8_t threshold, bool sleepOn);
altb2 4:3c21fb0c9e84 257
altb2 4:3c21fb0c9e84 258 // configAccelInt() -- Configure Accelerometer Interrupt Generator
altb2 4:3c21fb0c9e84 259 // Input:
altb2 4:3c21fb0c9e84 260 // - generator = Interrupt axis/high-low events
altb2 4:3c21fb0c9e84 261 // Any OR'd combination of ZHIE_XL, ZLIE_XL, YHIE_XL, YLIE_XL, XHIE_XL, XLIE_XL
altb2 4:3c21fb0c9e84 262 // - andInterrupts = AND/OR combination of interrupt events
altb2 4:3c21fb0c9e84 263 // true: AND combination
altb2 4:3c21fb0c9e84 264 // false: OR combination
altb2 4:3c21fb0c9e84 265 void configAccelInt(uint8_t generator, bool andInterrupts = false);
altb2 4:3c21fb0c9e84 266
altb2 4:3c21fb0c9e84 267 // configAccelThs() -- Configure the threshold of an accelereomter axis
altb2 4:3c21fb0c9e84 268 // Input:
altb2 4:3c21fb0c9e84 269 // - threshold = Interrupt threshold. Possible values: 0-255.
altb2 4:3c21fb0c9e84 270 // Multiply by 128 to get the actual raw accel value.
altb2 4:3c21fb0c9e84 271 // - axis = Axis to be configured. Either X_AXIS, Y_AXIS, or Z_AXIS
altb2 4:3c21fb0c9e84 272 // - duration = Duration value must be above or below threshold to trigger interrupt
altb2 4:3c21fb0c9e84 273 // - wait = Wait function on duration counter
altb2 4:3c21fb0c9e84 274 // true: Wait for duration samples before exiting interrupt
altb2 4:3c21fb0c9e84 275 // false: Wait function off
altb2 4:3c21fb0c9e84 276 void configAccelThs(uint8_t threshold, lsm9ds1_axis axis, uint8_t duration = 0, bool wait = 0);
altb2 4:3c21fb0c9e84 277
altb2 4:3c21fb0c9e84 278 // configGyroInt() -- Configure Gyroscope Interrupt Generator
altb2 4:3c21fb0c9e84 279 // Input:
altb2 4:3c21fb0c9e84 280 // - generator = Interrupt axis/high-low events
altb2 4:3c21fb0c9e84 281 // Any OR'd combination of ZHIE_G, ZLIE_G, YHIE_G, YLIE_G, XHIE_G, XLIE_G
altb2 4:3c21fb0c9e84 282 // - aoi = AND/OR combination of interrupt events
altb2 4:3c21fb0c9e84 283 // true: AND combination
altb2 4:3c21fb0c9e84 284 // false: OR combination
altb2 4:3c21fb0c9e84 285 // - latch: latch gyroscope interrupt request.
altb2 4:3c21fb0c9e84 286 void configGyroInt(uint8_t generator, bool aoi, bool latch);
altb2 4:3c21fb0c9e84 287
altb2 4:3c21fb0c9e84 288 // configGyroThs() -- Configure the threshold of a gyroscope axis
altb2 4:3c21fb0c9e84 289 // Input:
altb2 4:3c21fb0c9e84 290 // - threshold = Interrupt threshold. Possible values: 0-0x7FF.
altb2 4:3c21fb0c9e84 291 // Value is equivalent to raw gyroscope value.
altb2 4:3c21fb0c9e84 292 // - axis = Axis to be configured. Either X_AXIS, Y_AXIS, or Z_AXIS
altb2 4:3c21fb0c9e84 293 // - duration = Duration value must be above or below threshold to trigger interrupt
altb2 4:3c21fb0c9e84 294 // - wait = Wait function on duration counter
altb2 4:3c21fb0c9e84 295 // true: Wait for duration samples before exiting interrupt
altb2 4:3c21fb0c9e84 296 // false: Wait function off
altb2 4:3c21fb0c9e84 297 void configGyroThs(int16_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait);
altb2 4:3c21fb0c9e84 298
altb2 4:3c21fb0c9e84 299 // configInt() -- Configure INT1 or INT2 (Gyro and Accel Interrupts only)
altb2 4:3c21fb0c9e84 300 // Input:
altb2 4:3c21fb0c9e84 301 // - interrupt = Select INT1 or INT2
altb2 4:3c21fb0c9e84 302 // Possible values: XG_INT1 or XG_INT2
altb2 4:3c21fb0c9e84 303 // - generator = Or'd combination of interrupt generators.
altb2 4:3c21fb0c9e84 304 // Possible values: INT_DRDY_XL, INT_DRDY_G, INT1_BOOT (INT1 only), INT2_DRDY_TEMP (INT2 only)
altb2 4:3c21fb0c9e84 305 // INT_FTH, INT_OVR, INT_FSS5, INT_IG_XL (INT1 only), INT1_IG_G (INT1 only), INT2_INACT (INT2 only)
altb2 4:3c21fb0c9e84 306 // - activeLow = Interrupt active configuration
altb2 4:3c21fb0c9e84 307 // Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW
altb2 4:3c21fb0c9e84 308 // - pushPull = Push-pull or open drain interrupt configuration
altb2 4:3c21fb0c9e84 309 // Can be either INT_PUSH_PULL or INT_OPEN_DRAIN
altb2 4:3c21fb0c9e84 310 void configInt(interrupt_select interupt, uint8_t generator,
altb2 4:3c21fb0c9e84 311 h_lactive activeLow = INT_ACTIVE_LOW, pp_od pushPull = INT_PUSH_PULL);
altb2 4:3c21fb0c9e84 312
altb2 4:3c21fb0c9e84 313 /** configMagInt() -- Configure Magnetometer Interrupt Generator
altb2 4:3c21fb0c9e84 314 * Input:
altb2 4:3c21fb0c9e84 315 * - generator = Interrupt axis/high-low events
altb2 4:3c21fb0c9e84 316 * Any OR'd combination of ZIEN, YIEN, XIEN
altb2 4:3c21fb0c9e84 317 * - activeLow = Interrupt active configuration
altb2 4:3c21fb0c9e84 318 * Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW
altb2 4:3c21fb0c9e84 319 * - latch: latch gyroscope interrupt request.
altb2 4:3c21fb0c9e84 320 */
altb2 4:3c21fb0c9e84 321 void configMagInt(uint8_t generator, h_lactive activeLow, bool latch = true);
altb2 4:3c21fb0c9e84 322
altb2 4:3c21fb0c9e84 323 /** configMagThs() -- Configure the threshold of a gyroscope axis
altb2 4:3c21fb0c9e84 324 * Input:
altb2 4:3c21fb0c9e84 325 * - threshold = Interrupt threshold. Possible values: 0-0x7FF.
altb2 4:3c21fb0c9e84 326 * Value is equivalent to raw magnetometer value.
altb2 4:3c21fb0c9e84 327 */
altb2 4:3c21fb0c9e84 328 void configMagThs(uint16_t threshold);
altb2 4:3c21fb0c9e84 329
altb2 4:3c21fb0c9e84 330 //! getGyroIntSrc() -- Get contents of Gyroscope interrupt source register
altb2 4:3c21fb0c9e84 331 uint8_t getGyroIntSrc();
altb2 4:3c21fb0c9e84 332
altb2 4:3c21fb0c9e84 333 //! getGyroIntSrc() -- Get contents of accelerometer interrupt source register
altb2 4:3c21fb0c9e84 334 uint8_t getAccelIntSrc();
altb2 4:3c21fb0c9e84 335
altb2 4:3c21fb0c9e84 336 //! getGyroIntSrc() -- Get contents of magnetometer interrupt source register
altb2 4:3c21fb0c9e84 337 uint8_t getMagIntSrc();
altb2 4:3c21fb0c9e84 338
altb2 4:3c21fb0c9e84 339 //! getGyroIntSrc() -- Get status of inactivity interrupt
altb2 4:3c21fb0c9e84 340 uint8_t getInactivity();
altb2 4:3c21fb0c9e84 341
altb2 4:3c21fb0c9e84 342 /** sleepGyro() -- Sleep or wake the gyroscope
altb2 4:3c21fb0c9e84 343 * Input:
altb2 4:3c21fb0c9e84 344 * - enable: True = sleep gyro. False = wake gyro.
altb2 4:3c21fb0c9e84 345 */
altb2 4:3c21fb0c9e84 346 void sleepGyro(bool enable = true);
altb2 4:3c21fb0c9e84 347
altb2 4:3c21fb0c9e84 348 /** enableFIFO() - Enable or disable the FIFO
altb2 4:3c21fb0c9e84 349 * Input:
altb2 4:3c21fb0c9e84 350 * - enable: true = enable, false = disable.
altb2 4:3c21fb0c9e84 351 */
altb2 4:3c21fb0c9e84 352 void enableFIFO(bool enable = true);
altb2 4:3c21fb0c9e84 353
altb2 4:3c21fb0c9e84 354 /** setFIFO() - Configure FIFO mode and Threshold
altb2 4:3c21fb0c9e84 355 * Input:
altb2 4:3c21fb0c9e84 356 * - fifoMode: Set FIFO mode to off, FIFO (stop when full), continuous, bypass
altb2 4:3c21fb0c9e84 357 * Possible inputs: FIFO_OFF, FIFO_THS, FIFO_CONT_TRIGGER, FIFO_OFF_TRIGGER, FIFO_CONT
altb2 4:3c21fb0c9e84 358 * - fifoThs: FIFO threshold level setting
altb2 4:3c21fb0c9e84 359 * Any value from 0-0x1F is acceptable.
altb2 4:3c21fb0c9e84 360 */
altb2 4:3c21fb0c9e84 361 void setFIFO(fifoMode_type fifoMode, uint8_t fifoThs);
altb2 4:3c21fb0c9e84 362
altb2 4:3c21fb0c9e84 363 //! getFIFOSamples() - Get number of FIFO samples
altb2 4:3c21fb0c9e84 364 uint8_t getFIFOSamples();
altb2 4:3c21fb0c9e84 365
altb2 4:3c21fb0c9e84 366
altb2 4:3c21fb0c9e84 367 protected:
altb2 4:3c21fb0c9e84 368 // x_mAddress and gAddress store the I2C address or SPI chip select pin
altb2 4:3c21fb0c9e84 369 // for each sensor.
altb2 4:3c21fb0c9e84 370 uint8_t _mAddress, _xgAddress;
altb2 4:3c21fb0c9e84 371
altb2 4:3c21fb0c9e84 372 // gRes, aRes, and mRes store the current resolution for each sensor.
altb2 4:3c21fb0c9e84 373 // Units of these values would be DPS (or g's or Gs's) per ADC tick.
altb2 4:3c21fb0c9e84 374 // This value is calculated as (sensor scale) / (2^15).
altb2 4:3c21fb0c9e84 375 float gRes, aRes, mRes;
altb2 4:3c21fb0c9e84 376
altb2 4:3c21fb0c9e84 377 // _autoCalc keeps track of whether we're automatically subtracting off
altb2 4:3c21fb0c9e84 378 // accelerometer and gyroscope bias calculated in calibrate().
altb2 4:3c21fb0c9e84 379 bool _autoCalc;
altb2 4:3c21fb0c9e84 380
altb2 4:3c21fb0c9e84 381 // init() -- Sets up gyro, accel, and mag settings to default.
altb2 4:3c21fb0c9e84 382 // - interface - Sets the interface mode (IMU_MODE_I2C or IMU_MODE_SPI)
altb2 4:3c21fb0c9e84 383 // - xgAddr - Sets either the I2C address of the accel/gyro or SPI chip
altb2 4:3c21fb0c9e84 384 // select pin connected to the CS_XG pin.
altb2 4:3c21fb0c9e84 385 // - mAddr - Sets either the I2C address of the magnetometer or SPI chip
altb2 4:3c21fb0c9e84 386 // select pin connected to the CS_M pin.
altb2 4:3c21fb0c9e84 387 void init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr);
altb2 4:3c21fb0c9e84 388
altb2 4:3c21fb0c9e84 389 // initGyro() -- Sets up the gyroscope to begin reading.
altb2 4:3c21fb0c9e84 390 // This function steps through all five gyroscope control registers.
altb2 4:3c21fb0c9e84 391 // Upon exit, the following parameters will be set:
altb2 4:3c21fb0c9e84 392 // - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled.
altb2 4:3c21fb0c9e84 393 // 95 Hz ODR, 12.5 Hz cutoff frequency.
altb2 4:3c21fb0c9e84 394 // - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency
altb2 4:3c21fb0c9e84 395 // set to 7.2 Hz (depends on ODR).
altb2 4:3c21fb0c9e84 396 // - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and
altb2 4:3c21fb0c9e84 397 // active high). Data-ready output enabled on DRDY_G.
altb2 4:3c21fb0c9e84 398 // - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower
altb2 4:3c21fb0c9e84 399 // address. Scale set to 245 DPS. SPI mode set to 4-wire.
altb2 4:3c21fb0c9e84 400 // - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled.
altb2 4:3c21fb0c9e84 401 void initGyro();
altb2 4:3c21fb0c9e84 402
altb2 4:3c21fb0c9e84 403 // initAccel() -- Sets up the accelerometer to begin reading.
altb2 4:3c21fb0c9e84 404 // This function steps through all accelerometer related control registers.
altb2 4:3c21fb0c9e84 405 // Upon exit these registers will be set as:
altb2 4:3c21fb0c9e84 406 // - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode.
altb2 4:3c21fb0c9e84 407 // - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update.
altb2 4:3c21fb0c9e84 408 // all axes enabled.
altb2 4:3c21fb0c9e84 409 // - CTRL_REG2_XM = 0x00: 2g scale. 773 Hz anti-alias filter BW.
altb2 4:3c21fb0c9e84 410 // - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin.
altb2 4:3c21fb0c9e84 411 void initAccel();
altb2 4:3c21fb0c9e84 412
altb2 4:3c21fb0c9e84 413 // initMag() -- Sets up the magnetometer to begin reading.
altb2 4:3c21fb0c9e84 414 // This function steps through all magnetometer-related control registers.
altb2 4:3c21fb0c9e84 415 // Upon exit these registers will be set as:
altb2 4:3c21fb0c9e84 416 // - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin.
altb2 4:3c21fb0c9e84 417 // - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt
altb2 4:3c21fb0c9e84 418 // requests don't latch. Temperature sensor disabled.
altb2 4:3c21fb0c9e84 419 // - CTRL_REG6_XM = 0x00: 2 Gs scale.
altb2 4:3c21fb0c9e84 420 // - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode.
altb2 4:3c21fb0c9e84 421 // - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts.
altb2 4:3c21fb0c9e84 422 void initMag();
altb2 4:3c21fb0c9e84 423
altb2 4:3c21fb0c9e84 424 // gReadByte() -- Reads a byte from a specified gyroscope register.
altb2 4:3c21fb0c9e84 425 // Input:
altb2 4:3c21fb0c9e84 426 // - subAddress = Register to be read from.
altb2 4:3c21fb0c9e84 427 // Output:
altb2 4:3c21fb0c9e84 428 // - An 8-bit value read from the requested address.
altb2 4:3c21fb0c9e84 429 uint8_t mReadByte(uint8_t subAddress);
altb2 4:3c21fb0c9e84 430
altb2 4:3c21fb0c9e84 431 // gReadBytes() -- Reads a number of bytes -- beginning at an address
altb2 4:3c21fb0c9e84 432 // and incrementing from there -- from the gyroscope.
altb2 4:3c21fb0c9e84 433 // Input:
altb2 4:3c21fb0c9e84 434 // - subAddress = Register to be read from.
altb2 4:3c21fb0c9e84 435 // - * dest = A pointer to an array of uint8_t's. Values read will be
altb2 4:3c21fb0c9e84 436 // stored in here on return.
altb2 4:3c21fb0c9e84 437 // - count = The number of bytes to be read.
altb2 4:3c21fb0c9e84 438 // Output: No value is returned, but the `dest` array will store
altb2 4:3c21fb0c9e84 439 // the data read upon exit.
altb2 4:3c21fb0c9e84 440 void mReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
altb2 4:3c21fb0c9e84 441
altb2 4:3c21fb0c9e84 442 // gWriteByte() -- Write a byte to a register in the gyroscope.
altb2 4:3c21fb0c9e84 443 // Input:
altb2 4:3c21fb0c9e84 444 // - subAddress = Register to be written to.
altb2 4:3c21fb0c9e84 445 // - data = data to be written to the register.
altb2 4:3c21fb0c9e84 446 void mWriteByte(uint8_t subAddress, uint8_t data);
altb2 4:3c21fb0c9e84 447
altb2 4:3c21fb0c9e84 448 // xmReadByte() -- Read a byte from a register in the accel/mag sensor
altb2 4:3c21fb0c9e84 449 // Input:
altb2 4:3c21fb0c9e84 450 // - subAddress = Register to be read from.
altb2 4:3c21fb0c9e84 451 // Output:
altb2 4:3c21fb0c9e84 452 // - An 8-bit value read from the requested register.
altb2 4:3c21fb0c9e84 453 uint8_t xgReadByte(uint8_t subAddress);
altb2 4:3c21fb0c9e84 454
altb2 4:3c21fb0c9e84 455 // xmReadBytes() -- Reads a number of bytes -- beginning at an address
altb2 4:3c21fb0c9e84 456 // and incrementing from there -- from the accelerometer/magnetometer.
altb2 4:3c21fb0c9e84 457 // Input:
altb2 4:3c21fb0c9e84 458 // - subAddress = Register to be read from.
altb2 4:3c21fb0c9e84 459 // - * dest = A pointer to an array of uint8_t's. Values read will be
altb2 4:3c21fb0c9e84 460 // stored in here on return.
altb2 4:3c21fb0c9e84 461 // - count = The number of bytes to be read.
altb2 4:3c21fb0c9e84 462 // Output: No value is returned, but the `dest` array will store
altb2 4:3c21fb0c9e84 463 // the data read upon exit.
altb2 4:3c21fb0c9e84 464 void xgReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
altb2 4:3c21fb0c9e84 465
altb2 4:3c21fb0c9e84 466 // xmWriteByte() -- Write a byte to a register in the accel/mag sensor.
altb2 4:3c21fb0c9e84 467 // Input:
altb2 4:3c21fb0c9e84 468 // - subAddress = Register to be written to.
altb2 4:3c21fb0c9e84 469 // - data = data to be written to the register.
altb2 4:3c21fb0c9e84 470 void xgWriteByte(uint8_t subAddress, uint8_t data);
altb2 4:3c21fb0c9e84 471
altb2 4:3c21fb0c9e84 472 // calcgRes() -- Calculate the resolution of the gyroscope.
altb2 4:3c21fb0c9e84 473 // This function will set the value of the gRes variable. gScale must
altb2 4:3c21fb0c9e84 474 // be set prior to calling this function.
altb2 4:3c21fb0c9e84 475 void calcgRes();
altb2 4:3c21fb0c9e84 476
altb2 4:3c21fb0c9e84 477 // calcmRes() -- Calculate the resolution of the magnetometer.
altb2 4:3c21fb0c9e84 478 // This function will set the value of the mRes variable. mScale must
altb2 4:3c21fb0c9e84 479 // be set prior to calling this function.
altb2 4:3c21fb0c9e84 480 void calcmRes();
altb2 4:3c21fb0c9e84 481
altb2 4:3c21fb0c9e84 482 // calcaRes() -- Calculate the resolution of the accelerometer.
altb2 4:3c21fb0c9e84 483 // This function will set the value of the aRes variable. aScale must
altb2 4:3c21fb0c9e84 484 // be set prior to calling this function.
altb2 4:3c21fb0c9e84 485 void calcaRes();
altb2 4:3c21fb0c9e84 486
altb2 4:3c21fb0c9e84 487 //////////////////////
altb2 4:3c21fb0c9e84 488 // Helper Functions //
altb2 4:3c21fb0c9e84 489 //////////////////////
altb2 4:3c21fb0c9e84 490 void constrainScales();
altb2 4:3c21fb0c9e84 491
altb2 4:3c21fb0c9e84 492 ///////////////////
altb2 4:3c21fb0c9e84 493 // SPI Functions //
altb2 4:3c21fb0c9e84 494 ///////////////////
altb2 4:3c21fb0c9e84 495 // initSPI() -- Initialize the SPI hardware.
altb2 4:3c21fb0c9e84 496 // This function will setup all SPI pins and related hardware.
altb2 4:3c21fb0c9e84 497 void initSPI();
altb2 4:3c21fb0c9e84 498
altb2 4:3c21fb0c9e84 499 // SPIwriteByte() -- Write a byte out of SPI to a register in the device
altb2 4:3c21fb0c9e84 500 // Input:
altb2 4:3c21fb0c9e84 501 // - csPin = The chip select pin of the slave device.
altb2 4:3c21fb0c9e84 502 // - subAddress = The register to be written to.
altb2 4:3c21fb0c9e84 503 // - data = Byte to be written to the register.
altb2 4:3c21fb0c9e84 504 void SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data);
altb2 4:3c21fb0c9e84 505
altb2 4:3c21fb0c9e84 506 // SPIreadByte() -- Read a single byte from a register over SPI.
altb2 4:3c21fb0c9e84 507 // Input:
altb2 4:3c21fb0c9e84 508 // - csPin = The chip select pin of the slave device.
altb2 4:3c21fb0c9e84 509 // - subAddress = The register to be read from.
altb2 4:3c21fb0c9e84 510 // Output:
altb2 4:3c21fb0c9e84 511 // - The byte read from the requested address.
altb2 4:3c21fb0c9e84 512 uint8_t SPIreadByte(uint8_t csPin, uint8_t subAddress);
altb2 4:3c21fb0c9e84 513
altb2 4:3c21fb0c9e84 514 // SPIreadBytes() -- Read a series of bytes, starting at a register via SPI
altb2 4:3c21fb0c9e84 515 // Input:
altb2 4:3c21fb0c9e84 516 // - csPin = The chip select pin of a slave device.
altb2 4:3c21fb0c9e84 517 // - subAddress = The register to begin reading.
altb2 4:3c21fb0c9e84 518 // - * dest = Pointer to an array where we'll store the readings.
altb2 4:3c21fb0c9e84 519 // - count = Number of registers to be read.
altb2 4:3c21fb0c9e84 520 // Output: No value is returned by the function, but the registers read are
altb2 4:3c21fb0c9e84 521 // all stored in the *dest array given.
altb2 4:3c21fb0c9e84 522 void SPIreadBytes(uint8_t csPin, uint8_t subAddress,
altb2 4:3c21fb0c9e84 523 uint8_t * dest, uint8_t count);
altb2 4:3c21fb0c9e84 524
altb2 4:3c21fb0c9e84 525 ///////////////////
altb2 4:3c21fb0c9e84 526 // I2C Functions //
altb2 4:3c21fb0c9e84 527 ///////////////////
altb2 4:3c21fb0c9e84 528 // initI2C() -- Initialize the I2C hardware.
altb2 4:3c21fb0c9e84 529 // This function will setup all I2C pins and related hardware.
altb2 4:3c21fb0c9e84 530 void initI2C();
altb2 4:3c21fb0c9e84 531
altb2 4:3c21fb0c9e84 532 // I2CwriteByte() -- Write a byte out of I2C to a register in the device
altb2 4:3c21fb0c9e84 533 // Input:
altb2 4:3c21fb0c9e84 534 // - address = The 7-bit I2C address of the slave device.
altb2 4:3c21fb0c9e84 535 // - subAddress = The register to be written to.
altb2 4:3c21fb0c9e84 536 // - data = Byte to be written to the register.
altb2 4:3c21fb0c9e84 537 void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data);
altb2 4:3c21fb0c9e84 538
altb2 4:3c21fb0c9e84 539 // I2CreadByte() -- Read a single byte from a register over I2C.
altb2 4:3c21fb0c9e84 540 // Input:
altb2 4:3c21fb0c9e84 541 // - address = The 7-bit I2C address of the slave device.
altb2 4:3c21fb0c9e84 542 // - subAddress = The register to be read from.
altb2 4:3c21fb0c9e84 543 // Output:
altb2 4:3c21fb0c9e84 544 // - The byte read from the requested address.
altb2 4:3c21fb0c9e84 545 uint8_t I2CreadByte(uint8_t address, uint8_t subAddress);
altb2 4:3c21fb0c9e84 546
altb2 4:3c21fb0c9e84 547 // I2CreadBytes() -- Read a series of bytes, starting at a register via SPI
altb2 4:3c21fb0c9e84 548 // Input:
altb2 4:3c21fb0c9e84 549 // - address = The 7-bit I2C address of the slave device.
altb2 4:3c21fb0c9e84 550 // - subAddress = The register to begin reading.
altb2 4:3c21fb0c9e84 551 // - * dest = Pointer to an array where we'll store the readings.
altb2 4:3c21fb0c9e84 552 // - count = Number of registers to be read.
altb2 4:3c21fb0c9e84 553 // Output: No value is returned by the function, but the registers read are
altb2 4:3c21fb0c9e84 554 // all stored in the *dest array given.
altb2 4:3c21fb0c9e84 555 uint8_t I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count);
altb2 4:3c21fb0c9e84 556
altb2 4:3c21fb0c9e84 557 private:
altb2 25:fe14dbcef82d 558 I2C &i2c;
altb2 4:3c21fb0c9e84 559 };
altb2 4:3c21fb0c9e84 560
altb2 4:3c21fb0c9e84 561 #endif // SFE_LSM9DS1_H //
altb2 4:3c21fb0c9e84 562