IMU libraries for mbed

Dependencies:   PinDetect mbed

Fork of LSM9DS1_Library_cal by jim hamblen

Committer:
bhill42
Date:
Sun Mar 13 23:28:41 2016 +0000
Revision:
3:541db6c69ffa
Parent:
2:36abf8e18ade
BEFORE PICOJPEG

Who changed what in which revision?

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