Sparkfun's low cost IMU

Dependencies:   mbed

Committer:
TMDub88
Date:
Mon Oct 31 01:44:39 2016 +0000
Revision:
0:10233c4107e1
Tyler Woei-A-Sack

Who changed what in which revision?

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