IMU driver (this thing is a mess).

Dependents:   PM2_Libary PM2_Libary

Committer:
pmic
Date:
Wed Feb 23 07:16:58 2022 +0000
Revision:
0:211f27847e85
Included driver for LSM9DS1 IMU.

Who changed what in which revision?

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