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