lll

Dependencies:   PinDetect mbed

Dependents:   ECE4180_Robot

Fork of LSM9DS1_Library_cal by jim hamblen

Committer:
jmar7
Date:
Mon Oct 26 14:55:43 2015 +0000
Revision:
0:e8167f37725c
Child:
1:87d535bf8c53
First Library Commit;

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