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.cpp@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.cpp |
natvich | 1:57502185804c | 3 | SFE_LSM9DS1 Library Source 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 implements all functions of the LSM9DS1 class. Functions here range |
natvich | 1:57502185804c | 9 | from higher level stuff, like reading/writing LSM9DS1 registers to low-level, |
natvich | 1:57502185804c | 10 | hardware reads and writes. Both SPI and I2C handler functions can be found |
natvich | 1:57502185804c | 11 | towards the bottom of this file. |
natvich | 1:57502185804c | 12 | |
natvich | 1:57502185804c | 13 | Development environment specifics: |
natvich | 1:57502185804c | 14 | IDE: Arduino 1.6 |
natvich | 1:57502185804c | 15 | Hardware Platform: Arduino Uno |
natvich | 1:57502185804c | 16 | LSM9DS1 Breakout Version: 1.0 |
natvich | 1:57502185804c | 17 | |
natvich | 1:57502185804c | 18 | This code is beerware; if you see me (or any other SparkFun employee) at the |
natvich | 1:57502185804c | 19 | local, and you've found our code helpful, please buy us a round! |
natvich | 1:57502185804c | 20 | |
natvich | 1:57502185804c | 21 | Distributed as-is; no warranty is given. |
natvich | 1:57502185804c | 22 | ******************************************************************************/ |
natvich | 1:57502185804c | 23 | |
natvich | 1:57502185804c | 24 | #include "LSM9DS1.h" |
natvich | 1:57502185804c | 25 | #include "LSM9DS1_Registers.h" |
natvich | 1:57502185804c | 26 | #include "LSM9DS1_Types.h" |
natvich | 1:57502185804c | 27 | //#include <Wire.h> // Wire library is used for I2C |
natvich | 1:57502185804c | 28 | //#include <SPI.h> // SPI library is used for...SPI. |
natvich | 1:57502185804c | 29 | |
natvich | 1:57502185804c | 30 | //#if defined(ARDUINO) && ARDUINO >= 100 |
natvich | 1:57502185804c | 31 | // #include "Arduino.h" |
natvich | 1:57502185804c | 32 | //#else |
natvich | 1:57502185804c | 33 | // #include "WProgram.h" |
natvich | 1:57502185804c | 34 | //#endif |
natvich | 1:57502185804c | 35 | |
natvich | 1:57502185804c | 36 | #define LSM9DS1_COMMUNICATION_TIMEOUT 1000 |
natvich | 1:57502185804c | 37 | |
natvich | 1:57502185804c | 38 | float magSensitivity[4] = {0.00014, 0.00029, 0.00043, 0.00058}; |
natvich | 1:57502185804c | 39 | extern Serial pc; |
natvich | 1:57502185804c | 40 | |
natvich | 1:57502185804c | 41 | LSM9DS1::LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr) |
natvich | 1:57502185804c | 42 | :i2c(sda, scl) |
natvich | 1:57502185804c | 43 | { |
natvich | 1:57502185804c | 44 | init(IMU_MODE_I2C, xgAddr, mAddr); // dont know about 0xD6 or 0x3B |
natvich | 1:57502185804c | 45 | } |
natvich | 1:57502185804c | 46 | /* |
natvich | 1:57502185804c | 47 | LSM9DS1::LSM9DS1() |
natvich | 1:57502185804c | 48 | { |
natvich | 1:57502185804c | 49 | init(IMU_MODE_I2C, LSM9DS1_AG_ADDR(1), LSM9DS1_M_ADDR(1)); |
natvich | 1:57502185804c | 50 | } |
natvich | 1:57502185804c | 51 | |
natvich | 1:57502185804c | 52 | LSM9DS1::LSM9DS1(interface_mode interface, uint8_t xgAddr, uint8_t mAddr) |
natvich | 1:57502185804c | 53 | { |
natvich | 1:57502185804c | 54 | init(interface, xgAddr, mAddr); |
natvich | 1:57502185804c | 55 | } |
natvich | 1:57502185804c | 56 | */ |
natvich | 1:57502185804c | 57 | |
natvich | 1:57502185804c | 58 | void LSM9DS1::init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr) |
natvich | 1:57502185804c | 59 | { |
natvich | 1:57502185804c | 60 | settings.device.commInterface = interface; |
natvich | 1:57502185804c | 61 | settings.device.agAddress = xgAddr; |
natvich | 1:57502185804c | 62 | settings.device.mAddress = mAddr; |
natvich | 1:57502185804c | 63 | |
natvich | 1:57502185804c | 64 | settings.gyro.enabled = true; |
natvich | 1:57502185804c | 65 | settings.gyro.enableX = true; |
natvich | 1:57502185804c | 66 | settings.gyro.enableY = true; |
natvich | 1:57502185804c | 67 | settings.gyro.enableZ = true; |
natvich | 1:57502185804c | 68 | // gyro scale can be 245, 500, or 2000 |
natvich | 1:57502185804c | 69 | settings.gyro.scale = 245; |
natvich | 1:57502185804c | 70 | // gyro sample rate: value between 1-6 |
natvich | 1:57502185804c | 71 | // 1 = 14.9 4 = 238 |
natvich | 1:57502185804c | 72 | // 2 = 59.5 5 = 476 |
natvich | 1:57502185804c | 73 | // 3 = 119 6 = 952 |
natvich | 1:57502185804c | 74 | settings.gyro.sampleRate = 6; |
natvich | 1:57502185804c | 75 | // gyro cutoff frequency: value between 0-3 |
natvich | 1:57502185804c | 76 | // Actual value of cutoff frequency depends |
natvich | 1:57502185804c | 77 | // on sample rate. |
natvich | 1:57502185804c | 78 | settings.gyro.bandwidth = 0; |
natvich | 1:57502185804c | 79 | settings.gyro.lowPowerEnable = false; |
natvich | 1:57502185804c | 80 | settings.gyro.HPFEnable = false; |
natvich | 1:57502185804c | 81 | // Gyro HPF cutoff frequency: value between 0-9 |
natvich | 1:57502185804c | 82 | // Actual value depends on sample rate. Only applies |
natvich | 1:57502185804c | 83 | // if gyroHPFEnable is true. |
natvich | 1:57502185804c | 84 | settings.gyro.HPFCutoff = 0; |
natvich | 1:57502185804c | 85 | settings.gyro.flipX = false; |
natvich | 1:57502185804c | 86 | settings.gyro.flipY = false; |
natvich | 1:57502185804c | 87 | settings.gyro.flipZ = false; |
natvich | 1:57502185804c | 88 | settings.gyro.orientation = 0; |
natvich | 1:57502185804c | 89 | settings.gyro.latchInterrupt = true; |
natvich | 1:57502185804c | 90 | |
natvich | 1:57502185804c | 91 | settings.accel.enabled = true; |
natvich | 1:57502185804c | 92 | settings.accel.enableX = true; |
natvich | 1:57502185804c | 93 | settings.accel.enableY = true; |
natvich | 1:57502185804c | 94 | settings.accel.enableZ = true; |
natvich | 1:57502185804c | 95 | // accel scale can be 2, 4, 8, or 16 |
natvich | 1:57502185804c | 96 | settings.accel.scale = 2; |
natvich | 1:57502185804c | 97 | // accel sample rate can be 1-6 |
natvich | 1:57502185804c | 98 | // 1 = 10 Hz 4 = 238 Hz |
natvich | 1:57502185804c | 99 | // 2 = 50 Hz 5 = 476 Hz |
natvich | 1:57502185804c | 100 | // 3 = 119 Hz 6 = 952 Hz |
natvich | 1:57502185804c | 101 | settings.accel.sampleRate = 6; |
natvich | 1:57502185804c | 102 | // Accel cutoff freqeuncy can be any value between -1 - 3. |
natvich | 1:57502185804c | 103 | // -1 = bandwidth determined by sample rate |
natvich | 1:57502185804c | 104 | // 0 = 408 Hz 2 = 105 Hz |
natvich | 1:57502185804c | 105 | // 1 = 211 Hz 3 = 50 Hz |
natvich | 1:57502185804c | 106 | settings.accel.bandwidth = -1; |
natvich | 1:57502185804c | 107 | settings.accel.highResEnable = false; |
natvich | 1:57502185804c | 108 | // accelHighResBandwidth can be any value between 0-3 |
natvich | 1:57502185804c | 109 | // LP cutoff is set to a factor of sample rate |
natvich | 1:57502185804c | 110 | // 0 = ODR/50 2 = ODR/9 |
natvich | 1:57502185804c | 111 | // 1 = ODR/100 3 = ODR/400 |
natvich | 1:57502185804c | 112 | settings.accel.highResBandwidth = 0; |
natvich | 1:57502185804c | 113 | |
natvich | 1:57502185804c | 114 | settings.mag.enabled = true; |
natvich | 1:57502185804c | 115 | // mag scale can be 4, 8, 12, or 16 |
natvich | 1:57502185804c | 116 | settings.mag.scale = 4; |
natvich | 1:57502185804c | 117 | // mag data rate can be 0-7 |
natvich | 1:57502185804c | 118 | // 0 = 0.625 Hz 4 = 10 Hz |
natvich | 1:57502185804c | 119 | // 1 = 1.25 Hz 5 = 20 Hz |
natvich | 1:57502185804c | 120 | // 2 = 2.5 Hz 6 = 40 Hz |
natvich | 1:57502185804c | 121 | // 3 = 5 Hz 7 = 80 Hz |
natvich | 1:57502185804c | 122 | settings.mag.sampleRate = 7; |
natvich | 1:57502185804c | 123 | settings.mag.tempCompensationEnable = false; |
natvich | 1:57502185804c | 124 | // magPerformance can be any value between 0-3 |
natvich | 1:57502185804c | 125 | // 0 = Low power mode 2 = high performance |
natvich | 1:57502185804c | 126 | // 1 = medium performance 3 = ultra-high performance |
natvich | 1:57502185804c | 127 | settings.mag.XYPerformance = 3; |
natvich | 1:57502185804c | 128 | settings.mag.ZPerformance = 3; |
natvich | 1:57502185804c | 129 | settings.mag.lowPowerEnable = false; |
natvich | 1:57502185804c | 130 | // magOperatingMode can be 0-2 |
natvich | 1:57502185804c | 131 | // 0 = continuous conversion |
natvich | 1:57502185804c | 132 | // 1 = single-conversion |
natvich | 1:57502185804c | 133 | // 2 = power down |
natvich | 1:57502185804c | 134 | settings.mag.operatingMode = 0; |
natvich | 1:57502185804c | 135 | |
natvich | 1:57502185804c | 136 | settings.temp.enabled = true; |
natvich | 1:57502185804c | 137 | for (int i=0; i<3; i++) |
natvich | 1:57502185804c | 138 | { |
natvich | 1:57502185804c | 139 | gBias[i] = 0; |
natvich | 1:57502185804c | 140 | aBias[i] = 0; |
natvich | 1:57502185804c | 141 | mBias[i] = 0; |
natvich | 1:57502185804c | 142 | gBiasRaw[i] = 0; |
natvich | 1:57502185804c | 143 | aBiasRaw[i] = 0; |
natvich | 1:57502185804c | 144 | mBiasRaw[i] = 0; |
natvich | 1:57502185804c | 145 | } |
natvich | 1:57502185804c | 146 | _autoCalc = false; |
natvich | 1:57502185804c | 147 | } |
natvich | 1:57502185804c | 148 | |
natvich | 1:57502185804c | 149 | |
natvich | 1:57502185804c | 150 | uint16_t LSM9DS1::begin() |
natvich | 1:57502185804c | 151 | { |
natvich | 1:57502185804c | 152 | //! Todo: don't use _xgAddress or _mAddress, duplicating memory |
natvich | 1:57502185804c | 153 | _xgAddress = settings.device.agAddress; |
natvich | 1:57502185804c | 154 | _mAddress = settings.device.mAddress; |
natvich | 1:57502185804c | 155 | |
natvich | 1:57502185804c | 156 | constrainScales(); |
natvich | 1:57502185804c | 157 | // Once we have the scale values, we can calculate the resolution |
natvich | 1:57502185804c | 158 | // of each sensor. That's what these functions are for. One for each sensor |
natvich | 1:57502185804c | 159 | calcgRes(); // Calculate DPS / ADC tick, stored in gRes variable |
natvich | 1:57502185804c | 160 | calcmRes(); // Calculate Gs / ADC tick, stored in mRes variable |
natvich | 1:57502185804c | 161 | calcaRes(); // Calculate g / ADC tick, stored in aRes variable |
natvich | 1:57502185804c | 162 | |
natvich | 1:57502185804c | 163 | // Now, initialize our hardware interface. |
natvich | 1:57502185804c | 164 | if (settings.device.commInterface == IMU_MODE_I2C) // If we're using I2C |
natvich | 1:57502185804c | 165 | initI2C(); // Initialize I2C |
natvich | 1:57502185804c | 166 | else if (settings.device.commInterface == IMU_MODE_SPI) // else, if we're using SPI |
natvich | 1:57502185804c | 167 | initSPI(); // Initialize SPI |
natvich | 1:57502185804c | 168 | |
natvich | 1:57502185804c | 169 | // To verify communication, we can read from the WHO_AM_I register of |
natvich | 1:57502185804c | 170 | // each device. Store those in a variable so we can return them. |
natvich | 1:57502185804c | 171 | |
natvich | 1:57502185804c | 172 | uint8_t mTest = mReadByte(WHO_AM_I_M); // Read the gyro WHO_AM_I |
natvich | 1:57502185804c | 173 | uint8_t xgTest = xgReadByte(WHO_AM_I_XG); // Read the accel/mag WHO_AM_I |
natvich | 1:57502185804c | 174 | // pc.printf("%x, %x, %x, %x\n\r", mTest, xgTest, _xgAddress, _mAddress); |
natvich | 1:57502185804c | 175 | uint16_t whoAmICombined = (xgTest << 8) | mTest; |
natvich | 1:57502185804c | 176 | |
natvich | 1:57502185804c | 177 | if (whoAmICombined != ((WHO_AM_I_AG_RSP << 8) | WHO_AM_I_M_RSP)) |
natvich | 1:57502185804c | 178 | return 0; |
natvich | 1:57502185804c | 179 | |
natvich | 1:57502185804c | 180 | // Gyro initialization stuff: |
natvich | 1:57502185804c | 181 | initGyro(); // This will "turn on" the gyro. Setting up interrupts, etc. |
natvich | 1:57502185804c | 182 | |
natvich | 1:57502185804c | 183 | // Accelerometer initialization stuff: |
natvich | 1:57502185804c | 184 | initAccel(); // "Turn on" all axes of the accel. Set up interrupts, etc. |
natvich | 1:57502185804c | 185 | |
natvich | 1:57502185804c | 186 | // Magnetometer initialization stuff: |
natvich | 1:57502185804c | 187 | initMag(); // "Turn on" all axes of the mag. Set up interrupts, etc. |
natvich | 1:57502185804c | 188 | |
natvich | 1:57502185804c | 189 | // Once everything is initialized, return the WHO_AM_I registers we read: |
natvich | 1:57502185804c | 190 | // return whoAmICombined; |
natvich | 1:57502185804c | 191 | } |
natvich | 1:57502185804c | 192 | |
natvich | 1:57502185804c | 193 | void LSM9DS1::initGyro() |
natvich | 1:57502185804c | 194 | { |
natvich | 1:57502185804c | 195 | uint8_t tempRegValue = 0; |
natvich | 1:57502185804c | 196 | |
natvich | 1:57502185804c | 197 | // CTRL_REG1_G (Default value: 0x00) |
natvich | 1:57502185804c | 198 | // [ODR_G2][ODR_G1][ODR_G0][FS_G1][FS_G0][0][BW_G1][BW_G0] |
natvich | 1:57502185804c | 199 | // ODR_G[2:0] - Output data rate selection |
natvich | 1:57502185804c | 200 | // FS_G[1:0] - Gyroscope full-scale selection |
natvich | 1:57502185804c | 201 | // BW_G[1:0] - Gyroscope bandwidth selection |
natvich | 1:57502185804c | 202 | |
natvich | 1:57502185804c | 203 | // To disable gyro, set sample rate bits to 0. We'll only set sample |
natvich | 1:57502185804c | 204 | // rate if the gyro is enabled. |
natvich | 1:57502185804c | 205 | if (settings.gyro.enabled) |
natvich | 1:57502185804c | 206 | { |
natvich | 1:57502185804c | 207 | tempRegValue = (settings.gyro.sampleRate & 0x07) << 5; |
natvich | 1:57502185804c | 208 | } |
natvich | 1:57502185804c | 209 | switch (settings.gyro.scale) |
natvich | 1:57502185804c | 210 | { |
natvich | 1:57502185804c | 211 | case 500: |
natvich | 1:57502185804c | 212 | tempRegValue |= (0x1 << 3); |
natvich | 1:57502185804c | 213 | break; |
natvich | 1:57502185804c | 214 | case 2000: |
natvich | 1:57502185804c | 215 | tempRegValue |= (0x3 << 3); |
natvich | 1:57502185804c | 216 | break; |
natvich | 1:57502185804c | 217 | // Otherwise we'll set it to 245 dps (0x0 << 4) |
natvich | 1:57502185804c | 218 | } |
natvich | 1:57502185804c | 219 | tempRegValue |= (settings.gyro.bandwidth & 0x3); |
natvich | 1:57502185804c | 220 | xgWriteByte(CTRL_REG1_G, tempRegValue); |
natvich | 1:57502185804c | 221 | |
natvich | 1:57502185804c | 222 | // CTRL_REG2_G (Default value: 0x00) |
natvich | 1:57502185804c | 223 | // [0][0][0][0][INT_SEL1][INT_SEL0][OUT_SEL1][OUT_SEL0] |
natvich | 1:57502185804c | 224 | // INT_SEL[1:0] - INT selection configuration |
natvich | 1:57502185804c | 225 | // OUT_SEL[1:0] - Out selection configuration |
natvich | 1:57502185804c | 226 | xgWriteByte(CTRL_REG2_G, 0x00); |
natvich | 1:57502185804c | 227 | |
natvich | 1:57502185804c | 228 | // CTRL_REG3_G (Default value: 0x00) |
natvich | 1:57502185804c | 229 | // [LP_mode][HP_EN][0][0][HPCF3_G][HPCF2_G][HPCF1_G][HPCF0_G] |
natvich | 1:57502185804c | 230 | // LP_mode - Low-power mode enable (0: disabled, 1: enabled) |
natvich | 1:57502185804c | 231 | // HP_EN - HPF enable (0:disabled, 1: enabled) |
natvich | 1:57502185804c | 232 | // HPCF_G[3:0] - HPF cutoff frequency |
natvich | 1:57502185804c | 233 | tempRegValue = settings.gyro.lowPowerEnable ? (1<<7) : 0; |
natvich | 1:57502185804c | 234 | if (settings.gyro.HPFEnable) |
natvich | 1:57502185804c | 235 | { |
natvich | 1:57502185804c | 236 | tempRegValue |= (1<<6) | (settings.gyro.HPFCutoff & 0x0F); |
natvich | 1:57502185804c | 237 | } |
natvich | 1:57502185804c | 238 | xgWriteByte(CTRL_REG3_G, tempRegValue); |
natvich | 1:57502185804c | 239 | |
natvich | 1:57502185804c | 240 | // CTRL_REG4 (Default value: 0x38) |
natvich | 1:57502185804c | 241 | // [0][0][Zen_G][Yen_G][Xen_G][0][LIR_XL1][4D_XL1] |
natvich | 1:57502185804c | 242 | // Zen_G - Z-axis output enable (0:disable, 1:enable) |
natvich | 1:57502185804c | 243 | // Yen_G - Y-axis output enable (0:disable, 1:enable) |
natvich | 1:57502185804c | 244 | // Xen_G - X-axis output enable (0:disable, 1:enable) |
natvich | 1:57502185804c | 245 | // LIR_XL1 - Latched interrupt (0:not latched, 1:latched) |
natvich | 1:57502185804c | 246 | // 4D_XL1 - 4D option on interrupt (0:6D used, 1:4D used) |
natvich | 1:57502185804c | 247 | tempRegValue = 0; |
natvich | 1:57502185804c | 248 | if (settings.gyro.enableZ) tempRegValue |= (1<<5); |
natvich | 1:57502185804c | 249 | if (settings.gyro.enableY) tempRegValue |= (1<<4); |
natvich | 1:57502185804c | 250 | if (settings.gyro.enableX) tempRegValue |= (1<<3); |
natvich | 1:57502185804c | 251 | if (settings.gyro.latchInterrupt) tempRegValue |= (1<<1); |
natvich | 1:57502185804c | 252 | xgWriteByte(CTRL_REG4, tempRegValue); |
natvich | 1:57502185804c | 253 | |
natvich | 1:57502185804c | 254 | // ORIENT_CFG_G (Default value: 0x00) |
natvich | 1:57502185804c | 255 | // [0][0][SignX_G][SignY_G][SignZ_G][Orient_2][Orient_1][Orient_0] |
natvich | 1:57502185804c | 256 | // SignX_G - Pitch axis (X) angular rate sign (0: positive, 1: negative) |
natvich | 1:57502185804c | 257 | // Orient [2:0] - Directional user orientation selection |
natvich | 1:57502185804c | 258 | tempRegValue = 0; |
natvich | 1:57502185804c | 259 | if (settings.gyro.flipX) tempRegValue |= (1<<5); |
natvich | 1:57502185804c | 260 | if (settings.gyro.flipY) tempRegValue |= (1<<4); |
natvich | 1:57502185804c | 261 | if (settings.gyro.flipZ) tempRegValue |= (1<<3); |
natvich | 1:57502185804c | 262 | xgWriteByte(ORIENT_CFG_G, tempRegValue); |
natvich | 1:57502185804c | 263 | } |
natvich | 1:57502185804c | 264 | |
natvich | 1:57502185804c | 265 | void LSM9DS1::initAccel() |
natvich | 1:57502185804c | 266 | { |
natvich | 1:57502185804c | 267 | uint8_t tempRegValue = 0; |
natvich | 1:57502185804c | 268 | |
natvich | 1:57502185804c | 269 | // CTRL_REG5_XL (0x1F) (Default value: 0x38) |
natvich | 1:57502185804c | 270 | // [DEC_1][DEC_0][Zen_XL][Yen_XL][Zen_XL][0][0][0] |
natvich | 1:57502185804c | 271 | // DEC[0:1] - Decimation of accel data on OUT REG and FIFO. |
natvich | 1:57502185804c | 272 | // 00: None, 01: 2 samples, 10: 4 samples 11: 8 samples |
natvich | 1:57502185804c | 273 | // Zen_XL - Z-axis output enabled |
natvich | 1:57502185804c | 274 | // Yen_XL - Y-axis output enabled |
natvich | 1:57502185804c | 275 | // Xen_XL - X-axis output enabled |
natvich | 1:57502185804c | 276 | if (settings.accel.enableZ) tempRegValue |= (1<<5); |
natvich | 1:57502185804c | 277 | if (settings.accel.enableY) tempRegValue |= (1<<4); |
natvich | 1:57502185804c | 278 | if (settings.accel.enableX) tempRegValue |= (1<<3); |
natvich | 1:57502185804c | 279 | |
natvich | 1:57502185804c | 280 | xgWriteByte(CTRL_REG5_XL, tempRegValue); |
natvich | 1:57502185804c | 281 | |
natvich | 1:57502185804c | 282 | // CTRL_REG6_XL (0x20) (Default value: 0x00) |
natvich | 1:57502185804c | 283 | // [ODR_XL2][ODR_XL1][ODR_XL0][FS1_XL][FS0_XL][BW_SCAL_ODR][BW_XL1][BW_XL0] |
natvich | 1:57502185804c | 284 | // ODR_XL[2:0] - Output data rate & power mode selection |
natvich | 1:57502185804c | 285 | // FS_XL[1:0] - Full-scale selection |
natvich | 1:57502185804c | 286 | // BW_SCAL_ODR - Bandwidth selection |
natvich | 1:57502185804c | 287 | // BW_XL[1:0] - Anti-aliasing filter bandwidth selection |
natvich | 1:57502185804c | 288 | tempRegValue = 0; |
natvich | 1:57502185804c | 289 | // To disable the accel, set the sampleRate bits to 0. |
natvich | 1:57502185804c | 290 | if (settings.accel.enabled) |
natvich | 1:57502185804c | 291 | { |
natvich | 1:57502185804c | 292 | tempRegValue |= (settings.accel.sampleRate & 0x07) << 5; |
natvich | 1:57502185804c | 293 | } |
natvich | 1:57502185804c | 294 | switch (settings.accel.scale) |
natvich | 1:57502185804c | 295 | { |
natvich | 1:57502185804c | 296 | case 4: |
natvich | 1:57502185804c | 297 | tempRegValue |= (0x2 << 3); |
natvich | 1:57502185804c | 298 | break; |
natvich | 1:57502185804c | 299 | case 8: |
natvich | 1:57502185804c | 300 | tempRegValue |= (0x3 << 3); |
natvich | 1:57502185804c | 301 | break; |
natvich | 1:57502185804c | 302 | case 16: |
natvich | 1:57502185804c | 303 | tempRegValue |= (0x1 << 3); |
natvich | 1:57502185804c | 304 | break; |
natvich | 1:57502185804c | 305 | // Otherwise it'll be set to 2g (0x0 << 3) |
natvich | 1:57502185804c | 306 | } |
natvich | 1:57502185804c | 307 | if (settings.accel.bandwidth >= 0) |
natvich | 1:57502185804c | 308 | { |
natvich | 1:57502185804c | 309 | tempRegValue |= (1<<2); // Set BW_SCAL_ODR |
natvich | 1:57502185804c | 310 | tempRegValue |= (settings.accel.bandwidth & 0x03); |
natvich | 1:57502185804c | 311 | } |
natvich | 1:57502185804c | 312 | xgWriteByte(CTRL_REG6_XL, tempRegValue); |
natvich | 1:57502185804c | 313 | |
natvich | 1:57502185804c | 314 | // CTRL_REG7_XL (0x21) (Default value: 0x00) |
natvich | 1:57502185804c | 315 | // [HR][DCF1][DCF0][0][0][FDS][0][HPIS1] |
natvich | 1:57502185804c | 316 | // HR - High resolution mode (0: disable, 1: enable) |
natvich | 1:57502185804c | 317 | // DCF[1:0] - Digital filter cutoff frequency |
natvich | 1:57502185804c | 318 | // FDS - Filtered data selection |
natvich | 1:57502185804c | 319 | // HPIS1 - HPF enabled for interrupt function |
natvich | 1:57502185804c | 320 | tempRegValue = 0; |
natvich | 1:57502185804c | 321 | if (settings.accel.highResEnable) |
natvich | 1:57502185804c | 322 | { |
natvich | 1:57502185804c | 323 | tempRegValue |= (1<<7); // Set HR bit |
natvich | 1:57502185804c | 324 | tempRegValue |= (settings.accel.highResBandwidth & 0x3) << 5; |
natvich | 1:57502185804c | 325 | } |
natvich | 1:57502185804c | 326 | xgWriteByte(CTRL_REG7_XL, tempRegValue); |
natvich | 1:57502185804c | 327 | } |
natvich | 1:57502185804c | 328 | |
natvich | 1:57502185804c | 329 | // This is a function that uses the FIFO to accumulate sample of accelerometer and gyro data, average |
natvich | 1:57502185804c | 330 | // them, scales them to gs and deg/s, respectively, and then passes the biases to the main sketch |
natvich | 1:57502185804c | 331 | // for subtraction from all subsequent data. There are no gyro and accelerometer bias registers to store |
natvich | 1:57502185804c | 332 | // the data as there are in the ADXL345, a precursor to the LSM9DS0, or the MPU-9150, so we have to |
natvich | 1:57502185804c | 333 | // subtract the biases ourselves. This results in a more accurate measurement in general and can |
natvich | 1:57502185804c | 334 | // remove errors due to imprecise or varying initial placement. Calibration of sensor data in this manner |
natvich | 1:57502185804c | 335 | // is good practice. |
natvich | 1:57502185804c | 336 | void LSM9DS1::calibrate(bool autoCalc) |
natvich | 1:57502185804c | 337 | { |
natvich | 1:57502185804c | 338 | uint8_t data[6] = {0, 0, 0, 0, 0, 0}; |
natvich | 1:57502185804c | 339 | uint8_t samples = 0; |
natvich | 1:57502185804c | 340 | int ii; |
natvich | 1:57502185804c | 341 | int32_t aBiasRawTemp[3] = {0, 0, 0}; |
natvich | 1:57502185804c | 342 | int32_t gBiasRawTemp[3] = {0, 0, 0}; |
natvich | 1:57502185804c | 343 | |
natvich | 1:57502185804c | 344 | // Turn on FIFO and set threshold to 32 samples |
natvich | 1:57502185804c | 345 | enableFIFO(true); |
natvich | 1:57502185804c | 346 | setFIFO(FIFO_THS, 0x1F); |
natvich | 1:57502185804c | 347 | while (samples < 0x1F) |
natvich | 1:57502185804c | 348 | { |
natvich | 1:57502185804c | 349 | samples = (xgReadByte(FIFO_SRC) & 0x3F); // Read number of stored samples |
natvich | 1:57502185804c | 350 | } |
natvich | 1:57502185804c | 351 | for(ii = 0; ii < samples ; ii++) |
natvich | 1:57502185804c | 352 | { // Read the gyro data stored in the FIFO |
natvich | 1:57502185804c | 353 | readGyro(); |
natvich | 1:57502185804c | 354 | gBiasRawTemp[0] += gx; |
natvich | 1:57502185804c | 355 | gBiasRawTemp[1] += gy; |
natvich | 1:57502185804c | 356 | gBiasRawTemp[2] += gz; |
natvich | 1:57502185804c | 357 | readAccel(); |
natvich | 1:57502185804c | 358 | aBiasRawTemp[0] += ax; |
natvich | 1:57502185804c | 359 | aBiasRawTemp[1] += ay; |
natvich | 1:57502185804c | 360 | aBiasRawTemp[2] += az - (int16_t)(1./aRes); // Assumes sensor facing up! |
natvich | 1:57502185804c | 361 | } |
natvich | 1:57502185804c | 362 | for (ii = 0; ii < 3; ii++) |
natvich | 1:57502185804c | 363 | { |
natvich | 1:57502185804c | 364 | gBiasRaw[ii] = gBiasRawTemp[ii] / samples; |
natvich | 1:57502185804c | 365 | gBias[ii] = calcGyro(gBiasRaw[ii]); |
natvich | 1:57502185804c | 366 | aBiasRaw[ii] = aBiasRawTemp[ii] / samples; |
natvich | 1:57502185804c | 367 | aBias[ii] = calcAccel(aBiasRaw[ii]); |
natvich | 1:57502185804c | 368 | } |
natvich | 1:57502185804c | 369 | |
natvich | 1:57502185804c | 370 | enableFIFO(false); |
natvich | 1:57502185804c | 371 | setFIFO(FIFO_OFF, 0x00); |
natvich | 1:57502185804c | 372 | |
natvich | 1:57502185804c | 373 | if (autoCalc) _autoCalc = true; |
natvich | 1:57502185804c | 374 | } |
natvich | 1:57502185804c | 375 | |
natvich | 1:57502185804c | 376 | void LSM9DS1::calibrateMag(bool loadIn) |
natvich | 1:57502185804c | 377 | { |
natvich | 1:57502185804c | 378 | int i, j; |
natvich | 1:57502185804c | 379 | int16_t magMin[3] = {0, 0, 0}; |
natvich | 1:57502185804c | 380 | int16_t magMax[3] = {0, 0, 0}; // The road warrior |
natvich | 1:57502185804c | 381 | |
natvich | 1:57502185804c | 382 | for (i=0; i<128; i++) |
natvich | 1:57502185804c | 383 | { |
natvich | 1:57502185804c | 384 | while (!magAvailable()) |
natvich | 1:57502185804c | 385 | ; |
natvich | 1:57502185804c | 386 | readMag(); |
natvich | 1:57502185804c | 387 | int16_t magTemp[3] = {0, 0, 0}; |
natvich | 1:57502185804c | 388 | magTemp[0] = mx; |
natvich | 1:57502185804c | 389 | magTemp[1] = my; |
natvich | 1:57502185804c | 390 | magTemp[2] = mz; |
natvich | 1:57502185804c | 391 | for (j = 0; j < 3; j++) |
natvich | 1:57502185804c | 392 | { |
natvich | 1:57502185804c | 393 | if (magTemp[j] > magMax[j]) magMax[j] = magTemp[j]; |
natvich | 1:57502185804c | 394 | if (magTemp[j] < magMin[j]) magMin[j] = magTemp[j]; |
natvich | 1:57502185804c | 395 | } |
natvich | 1:57502185804c | 396 | } |
natvich | 1:57502185804c | 397 | for (j = 0; j < 3; j++) |
natvich | 1:57502185804c | 398 | { |
natvich | 1:57502185804c | 399 | mBiasRaw[j] = (magMax[j] + magMin[j]) / 2; |
natvich | 1:57502185804c | 400 | mBias[j] = calcMag(mBiasRaw[j]); |
natvich | 1:57502185804c | 401 | if (loadIn) |
natvich | 1:57502185804c | 402 | magOffset(j, mBiasRaw[j]); |
natvich | 1:57502185804c | 403 | } |
natvich | 1:57502185804c | 404 | |
natvich | 1:57502185804c | 405 | } |
natvich | 1:57502185804c | 406 | void LSM9DS1::magOffset(uint8_t axis, int16_t offset) |
natvich | 1:57502185804c | 407 | { |
natvich | 1:57502185804c | 408 | if (axis > 2) |
natvich | 1:57502185804c | 409 | return; |
natvich | 1:57502185804c | 410 | uint8_t msb, lsb; |
natvich | 1:57502185804c | 411 | msb = (offset & 0xFF00) >> 8; |
natvich | 1:57502185804c | 412 | lsb = offset & 0x00FF; |
natvich | 1:57502185804c | 413 | mWriteByte(OFFSET_X_REG_L_M + (2 * axis), lsb); |
natvich | 1:57502185804c | 414 | mWriteByte(OFFSET_X_REG_H_M + (2 * axis), msb); |
natvich | 1:57502185804c | 415 | } |
natvich | 1:57502185804c | 416 | |
natvich | 1:57502185804c | 417 | void LSM9DS1::initMag() |
natvich | 1:57502185804c | 418 | { |
natvich | 1:57502185804c | 419 | uint8_t tempRegValue = 0; |
natvich | 1:57502185804c | 420 | |
natvich | 1:57502185804c | 421 | // CTRL_REG1_M (Default value: 0x10) |
natvich | 1:57502185804c | 422 | // [TEMP_COMP][OM1][OM0][DO2][DO1][DO0][0][ST] |
natvich | 1:57502185804c | 423 | // TEMP_COMP - Temperature compensation |
natvich | 1:57502185804c | 424 | // OM[1:0] - X & Y axes op mode selection |
natvich | 1:57502185804c | 425 | // 00:low-power, 01:medium performance |
natvich | 1:57502185804c | 426 | // 10: high performance, 11:ultra-high performance |
natvich | 1:57502185804c | 427 | // DO[2:0] - Output data rate selection |
natvich | 1:57502185804c | 428 | // ST - Self-test enable |
natvich | 1:57502185804c | 429 | if (settings.mag.tempCompensationEnable) tempRegValue |= (1<<7); |
natvich | 1:57502185804c | 430 | tempRegValue |= (settings.mag.XYPerformance & 0x3) << 5; |
natvich | 1:57502185804c | 431 | tempRegValue |= (settings.mag.sampleRate & 0x7) << 2; |
natvich | 1:57502185804c | 432 | mWriteByte(CTRL_REG1_M, tempRegValue); |
natvich | 1:57502185804c | 433 | |
natvich | 1:57502185804c | 434 | // CTRL_REG2_M (Default value 0x00) |
natvich | 1:57502185804c | 435 | // [0][FS1][FS0][0][REBOOT][SOFT_RST][0][0] |
natvich | 1:57502185804c | 436 | // FS[1:0] - Full-scale configuration |
natvich | 1:57502185804c | 437 | // REBOOT - Reboot memory content (0:normal, 1:reboot) |
natvich | 1:57502185804c | 438 | // SOFT_RST - Reset config and user registers (0:default, 1:reset) |
natvich | 1:57502185804c | 439 | tempRegValue = 0; |
natvich | 1:57502185804c | 440 | switch (settings.mag.scale) |
natvich | 1:57502185804c | 441 | { |
natvich | 1:57502185804c | 442 | case 8: |
natvich | 1:57502185804c | 443 | tempRegValue |= (0x1 << 5); |
natvich | 1:57502185804c | 444 | break; |
natvich | 1:57502185804c | 445 | case 12: |
natvich | 1:57502185804c | 446 | tempRegValue |= (0x2 << 5); |
natvich | 1:57502185804c | 447 | break; |
natvich | 1:57502185804c | 448 | case 16: |
natvich | 1:57502185804c | 449 | tempRegValue |= (0x3 << 5); |
natvich | 1:57502185804c | 450 | break; |
natvich | 1:57502185804c | 451 | // Otherwise we'll default to 4 gauss (00) |
natvich | 1:57502185804c | 452 | } |
natvich | 1:57502185804c | 453 | mWriteByte(CTRL_REG2_M, tempRegValue); // +/-4Gauss |
natvich | 1:57502185804c | 454 | |
natvich | 1:57502185804c | 455 | // CTRL_REG3_M (Default value: 0x03) |
natvich | 1:57502185804c | 456 | // [I2C_DISABLE][0][LP][0][0][SIM][MD1][MD0] |
natvich | 1:57502185804c | 457 | // I2C_DISABLE - Disable I2C interace (0:enable, 1:disable) |
natvich | 1:57502185804c | 458 | // LP - Low-power mode cofiguration (1:enable) |
natvich | 1:57502185804c | 459 | // SIM - SPI mode selection (0:write-only, 1:read/write enable) |
natvich | 1:57502185804c | 460 | // MD[1:0] - Operating mode |
natvich | 1:57502185804c | 461 | // 00:continuous conversion, 01:single-conversion, |
natvich | 1:57502185804c | 462 | // 10,11: Power-down |
natvich | 1:57502185804c | 463 | tempRegValue = 0; |
natvich | 1:57502185804c | 464 | if (settings.mag.lowPowerEnable) tempRegValue |= (1<<5); |
natvich | 1:57502185804c | 465 | tempRegValue |= (settings.mag.operatingMode & 0x3); |
natvich | 1:57502185804c | 466 | mWriteByte(CTRL_REG3_M, tempRegValue); // Continuous conversion mode |
natvich | 1:57502185804c | 467 | |
natvich | 1:57502185804c | 468 | // CTRL_REG4_M (Default value: 0x00) |
natvich | 1:57502185804c | 469 | // [0][0][0][0][OMZ1][OMZ0][BLE][0] |
natvich | 1:57502185804c | 470 | // OMZ[1:0] - Z-axis operative mode selection |
natvich | 1:57502185804c | 471 | // 00:low-power mode, 01:medium performance |
natvich | 1:57502185804c | 472 | // 10:high performance, 10:ultra-high performance |
natvich | 1:57502185804c | 473 | // BLE - Big/little endian data |
natvich | 1:57502185804c | 474 | tempRegValue = 0; |
natvich | 1:57502185804c | 475 | tempRegValue = (settings.mag.ZPerformance & 0x3) << 2; |
natvich | 1:57502185804c | 476 | mWriteByte(CTRL_REG4_M, tempRegValue); |
natvich | 1:57502185804c | 477 | |
natvich | 1:57502185804c | 478 | // CTRL_REG5_M (Default value: 0x00) |
natvich | 1:57502185804c | 479 | // [0][BDU][0][0][0][0][0][0] |
natvich | 1:57502185804c | 480 | // BDU - Block data update for magnetic data |
natvich | 1:57502185804c | 481 | // 0:continuous, 1:not updated until MSB/LSB are read |
natvich | 1:57502185804c | 482 | tempRegValue = 0; |
natvich | 1:57502185804c | 483 | mWriteByte(CTRL_REG5_M, tempRegValue); |
natvich | 1:57502185804c | 484 | } |
natvich | 1:57502185804c | 485 | |
natvich | 1:57502185804c | 486 | uint8_t LSM9DS1::accelAvailable() |
natvich | 1:57502185804c | 487 | { |
natvich | 1:57502185804c | 488 | uint8_t status = xgReadByte(STATUS_REG_1); |
natvich | 1:57502185804c | 489 | |
natvich | 1:57502185804c | 490 | return (status & (1<<0)); |
natvich | 1:57502185804c | 491 | } |
natvich | 1:57502185804c | 492 | |
natvich | 1:57502185804c | 493 | uint8_t LSM9DS1::gyroAvailable() |
natvich | 1:57502185804c | 494 | { |
natvich | 1:57502185804c | 495 | uint8_t status = xgReadByte(STATUS_REG_1); |
natvich | 1:57502185804c | 496 | |
natvich | 1:57502185804c | 497 | return ((status & (1<<1)) >> 1); |
natvich | 1:57502185804c | 498 | } |
natvich | 1:57502185804c | 499 | |
natvich | 1:57502185804c | 500 | uint8_t LSM9DS1::tempAvailable() |
natvich | 1:57502185804c | 501 | { |
natvich | 1:57502185804c | 502 | uint8_t status = xgReadByte(STATUS_REG_1); |
natvich | 1:57502185804c | 503 | |
natvich | 1:57502185804c | 504 | return ((status & (1<<2)) >> 2); |
natvich | 1:57502185804c | 505 | } |
natvich | 1:57502185804c | 506 | |
natvich | 1:57502185804c | 507 | uint8_t LSM9DS1::magAvailable(lsm9ds1_axis axis) |
natvich | 1:57502185804c | 508 | { |
natvich | 1:57502185804c | 509 | uint8_t status; |
natvich | 1:57502185804c | 510 | status = mReadByte(STATUS_REG_M); |
natvich | 1:57502185804c | 511 | |
natvich | 1:57502185804c | 512 | return ((status & (1<<axis)) >> axis); |
natvich | 1:57502185804c | 513 | } |
natvich | 1:57502185804c | 514 | |
natvich | 1:57502185804c | 515 | void LSM9DS1::readAccel() |
natvich | 1:57502185804c | 516 | { |
natvich | 1:57502185804c | 517 | uint8_t temp[6]; // We'll read six bytes from the accelerometer into temp |
natvich | 1:57502185804c | 518 | xgReadBytes(OUT_X_L_XL, temp, 6); // Read 6 bytes, beginning at OUT_X_L_XL |
natvich | 1:57502185804c | 519 | ax = (temp[1] << 8) | temp[0]; // Store x-axis values into ax |
natvich | 1:57502185804c | 520 | ay = (temp[3] << 8) | temp[2]; // Store y-axis values into ay |
natvich | 1:57502185804c | 521 | az = (temp[5] << 8) | temp[4]; // Store z-axis values into az |
natvich | 1:57502185804c | 522 | if (_autoCalc) |
natvich | 1:57502185804c | 523 | { |
natvich | 1:57502185804c | 524 | ax -= aBiasRaw[X_AXIS]; |
natvich | 1:57502185804c | 525 | ay -= aBiasRaw[Y_AXIS]; |
natvich | 1:57502185804c | 526 | az -= aBiasRaw[Z_AXIS]; |
natvich | 1:57502185804c | 527 | } |
natvich | 1:57502185804c | 528 | } |
natvich | 1:57502185804c | 529 | |
natvich | 1:57502185804c | 530 | int16_t LSM9DS1::readAccel(lsm9ds1_axis axis) |
natvich | 1:57502185804c | 531 | { |
natvich | 1:57502185804c | 532 | uint8_t temp[2]; |
natvich | 1:57502185804c | 533 | int16_t value; |
natvich | 1:57502185804c | 534 | xgReadBytes(OUT_X_L_XL + (2 * axis), temp, 2); |
natvich | 1:57502185804c | 535 | value = (temp[1] << 8) | temp[0]; |
natvich | 1:57502185804c | 536 | |
natvich | 1:57502185804c | 537 | if (_autoCalc) |
natvich | 1:57502185804c | 538 | value -= aBiasRaw[axis]; |
natvich | 1:57502185804c | 539 | |
natvich | 1:57502185804c | 540 | return value; |
natvich | 1:57502185804c | 541 | } |
natvich | 1:57502185804c | 542 | |
natvich | 1:57502185804c | 543 | void LSM9DS1::readMag() |
natvich | 1:57502185804c | 544 | { |
natvich | 1:57502185804c | 545 | uint8_t temp[6]; // We'll read six bytes from the mag into temp |
natvich | 1:57502185804c | 546 | mReadBytes(OUT_X_L_M, temp, 6); // Read 6 bytes, beginning at OUT_X_L_M |
natvich | 1:57502185804c | 547 | mx = (temp[1] << 8) | temp[0]; // Store x-axis values into mx |
natvich | 1:57502185804c | 548 | my = (temp[3] << 8) | temp[2]; // Store y-axis values into my |
natvich | 1:57502185804c | 549 | mz = (temp[5] << 8) | temp[4]; // Store z-axis values into mz |
natvich | 1:57502185804c | 550 | } |
natvich | 1:57502185804c | 551 | |
natvich | 1:57502185804c | 552 | int16_t LSM9DS1::readMag(lsm9ds1_axis axis) |
natvich | 1:57502185804c | 553 | { |
natvich | 1:57502185804c | 554 | uint8_t temp[2]; |
natvich | 1:57502185804c | 555 | mReadBytes(OUT_X_L_M + (2 * axis), temp, 2); |
natvich | 1:57502185804c | 556 | return (temp[1] << 8) | temp[0]; |
natvich | 1:57502185804c | 557 | } |
natvich | 1:57502185804c | 558 | |
natvich | 1:57502185804c | 559 | void LSM9DS1::readTemp() |
natvich | 1:57502185804c | 560 | { |
natvich | 1:57502185804c | 561 | uint8_t temp[2]; // We'll read two bytes from the temperature sensor into temp |
natvich | 1:57502185804c | 562 | xgReadBytes(OUT_TEMP_L, temp, 2); // Read 2 bytes, beginning at OUT_TEMP_L |
natvich | 1:57502185804c | 563 | temperature = ((int16_t)temp[1] << 8) | temp[0]; |
natvich | 1:57502185804c | 564 | } |
natvich | 1:57502185804c | 565 | |
natvich | 1:57502185804c | 566 | void LSM9DS1::readGyro() |
natvich | 1:57502185804c | 567 | { |
natvich | 1:57502185804c | 568 | uint8_t temp[6]; // We'll read six bytes from the gyro into temp |
natvich | 1:57502185804c | 569 | xgReadBytes(OUT_X_L_G, temp, 6); // Read 6 bytes, beginning at OUT_X_L_G |
natvich | 1:57502185804c | 570 | gx = (temp[1] << 8) | temp[0]; // Store x-axis values into gx |
natvich | 1:57502185804c | 571 | gy = (temp[3] << 8) | temp[2]; // Store y-axis values into gy |
natvich | 1:57502185804c | 572 | gz = (temp[5] << 8) | temp[4]; // Store z-axis values into gz |
natvich | 1:57502185804c | 573 | if (_autoCalc) |
natvich | 1:57502185804c | 574 | { |
natvich | 1:57502185804c | 575 | gx -= gBiasRaw[X_AXIS]; |
natvich | 1:57502185804c | 576 | gy -= gBiasRaw[Y_AXIS]; |
natvich | 1:57502185804c | 577 | gz -= gBiasRaw[Z_AXIS]; |
natvich | 1:57502185804c | 578 | } |
natvich | 1:57502185804c | 579 | } |
natvich | 1:57502185804c | 580 | |
natvich | 1:57502185804c | 581 | int16_t LSM9DS1::readGyro(lsm9ds1_axis axis) |
natvich | 1:57502185804c | 582 | { |
natvich | 1:57502185804c | 583 | uint8_t temp[2]; |
natvich | 1:57502185804c | 584 | int16_t value; |
natvich | 1:57502185804c | 585 | |
natvich | 1:57502185804c | 586 | xgReadBytes(OUT_X_L_G + (2 * axis), temp, 2); |
natvich | 1:57502185804c | 587 | |
natvich | 1:57502185804c | 588 | value = (temp[1] << 8) | temp[0]; |
natvich | 1:57502185804c | 589 | |
natvich | 1:57502185804c | 590 | if (_autoCalc) |
natvich | 1:57502185804c | 591 | value -= gBiasRaw[axis]; |
natvich | 1:57502185804c | 592 | |
natvich | 1:57502185804c | 593 | return value; |
natvich | 1:57502185804c | 594 | } |
natvich | 1:57502185804c | 595 | |
natvich | 1:57502185804c | 596 | float LSM9DS1::calcGyro(int16_t gyro) |
natvich | 1:57502185804c | 597 | { |
natvich | 1:57502185804c | 598 | // Return the gyro raw reading times our pre-calculated DPS / (ADC tick): |
natvich | 1:57502185804c | 599 | return gRes * gyro; |
natvich | 1:57502185804c | 600 | } |
natvich | 1:57502185804c | 601 | |
natvich | 1:57502185804c | 602 | float LSM9DS1::calcAccel(int16_t accel) |
natvich | 1:57502185804c | 603 | { |
natvich | 1:57502185804c | 604 | // Return the accel raw reading times our pre-calculated g's / (ADC tick): |
natvich | 1:57502185804c | 605 | return aRes * accel; |
natvich | 1:57502185804c | 606 | } |
natvich | 1:57502185804c | 607 | |
natvich | 1:57502185804c | 608 | float LSM9DS1::calcMag(int16_t mag) |
natvich | 1:57502185804c | 609 | { |
natvich | 1:57502185804c | 610 | // Return the mag raw reading times our pre-calculated Gs / (ADC tick): |
natvich | 1:57502185804c | 611 | return mRes * mag; |
natvich | 1:57502185804c | 612 | } |
natvich | 1:57502185804c | 613 | |
natvich | 1:57502185804c | 614 | void LSM9DS1::setGyroScale(uint16_t gScl) |
natvich | 1:57502185804c | 615 | { |
natvich | 1:57502185804c | 616 | // Read current value of CTRL_REG1_G: |
natvich | 1:57502185804c | 617 | uint8_t ctrl1RegValue = xgReadByte(CTRL_REG1_G); |
natvich | 1:57502185804c | 618 | // Mask out scale bits (3 & 4): |
natvich | 1:57502185804c | 619 | ctrl1RegValue &= 0xE7; |
natvich | 1:57502185804c | 620 | switch (gScl) |
natvich | 1:57502185804c | 621 | { |
natvich | 1:57502185804c | 622 | case 500: |
natvich | 1:57502185804c | 623 | ctrl1RegValue |= (0x1 << 3); |
natvich | 1:57502185804c | 624 | settings.gyro.scale = 500; |
natvich | 1:57502185804c | 625 | break; |
natvich | 1:57502185804c | 626 | case 2000: |
natvich | 1:57502185804c | 627 | ctrl1RegValue |= (0x3 << 3); |
natvich | 1:57502185804c | 628 | settings.gyro.scale = 2000; |
natvich | 1:57502185804c | 629 | break; |
natvich | 1:57502185804c | 630 | default: // Otherwise we'll set it to 245 dps (0x0 << 4) |
natvich | 1:57502185804c | 631 | settings.gyro.scale = 245; |
natvich | 1:57502185804c | 632 | break; |
natvich | 1:57502185804c | 633 | } |
natvich | 1:57502185804c | 634 | xgWriteByte(CTRL_REG1_G, ctrl1RegValue); |
natvich | 1:57502185804c | 635 | |
natvich | 1:57502185804c | 636 | calcgRes(); |
natvich | 1:57502185804c | 637 | } |
natvich | 1:57502185804c | 638 | |
natvich | 1:57502185804c | 639 | void LSM9DS1::setAccelScale(uint8_t aScl) |
natvich | 1:57502185804c | 640 | { |
natvich | 1:57502185804c | 641 | // We need to preserve the other bytes in CTRL_REG6_XL. So, first read it: |
natvich | 1:57502185804c | 642 | uint8_t tempRegValue = xgReadByte(CTRL_REG6_XL); |
natvich | 1:57502185804c | 643 | // Mask out accel scale bits: |
natvich | 1:57502185804c | 644 | tempRegValue &= 0xE7; |
natvich | 1:57502185804c | 645 | |
natvich | 1:57502185804c | 646 | switch (aScl) |
natvich | 1:57502185804c | 647 | { |
natvich | 1:57502185804c | 648 | case 4: |
natvich | 1:57502185804c | 649 | tempRegValue |= (0x2 << 3); |
natvich | 1:57502185804c | 650 | settings.accel.scale = 4; |
natvich | 1:57502185804c | 651 | break; |
natvich | 1:57502185804c | 652 | case 8: |
natvich | 1:57502185804c | 653 | tempRegValue |= (0x3 << 3); |
natvich | 1:57502185804c | 654 | settings.accel.scale = 8; |
natvich | 1:57502185804c | 655 | break; |
natvich | 1:57502185804c | 656 | case 16: |
natvich | 1:57502185804c | 657 | tempRegValue |= (0x1 << 3); |
natvich | 1:57502185804c | 658 | settings.accel.scale = 16; |
natvich | 1:57502185804c | 659 | break; |
natvich | 1:57502185804c | 660 | default: // Otherwise it'll be set to 2g (0x0 << 3) |
natvich | 1:57502185804c | 661 | settings.accel.scale = 2; |
natvich | 1:57502185804c | 662 | break; |
natvich | 1:57502185804c | 663 | } |
natvich | 1:57502185804c | 664 | xgWriteByte(CTRL_REG6_XL, tempRegValue); |
natvich | 1:57502185804c | 665 | |
natvich | 1:57502185804c | 666 | // Then calculate a new aRes, which relies on aScale being set correctly: |
natvich | 1:57502185804c | 667 | calcaRes(); |
natvich | 1:57502185804c | 668 | } |
natvich | 1:57502185804c | 669 | |
natvich | 1:57502185804c | 670 | void LSM9DS1::setMagScale(uint8_t mScl) |
natvich | 1:57502185804c | 671 | { |
natvich | 1:57502185804c | 672 | // We need to preserve the other bytes in CTRL_REG6_XM. So, first read it: |
natvich | 1:57502185804c | 673 | uint8_t temp = mReadByte(CTRL_REG2_M); |
natvich | 1:57502185804c | 674 | // Then mask out the mag scale bits: |
natvich | 1:57502185804c | 675 | temp &= 0xFF^(0x3 << 5); |
natvich | 1:57502185804c | 676 | |
natvich | 1:57502185804c | 677 | switch (mScl) |
natvich | 1:57502185804c | 678 | { |
natvich | 1:57502185804c | 679 | case 8: |
natvich | 1:57502185804c | 680 | temp |= (0x1 << 5); |
natvich | 1:57502185804c | 681 | settings.mag.scale = 8; |
natvich | 1:57502185804c | 682 | break; |
natvich | 1:57502185804c | 683 | case 12: |
natvich | 1:57502185804c | 684 | temp |= (0x2 << 5); |
natvich | 1:57502185804c | 685 | settings.mag.scale = 12; |
natvich | 1:57502185804c | 686 | break; |
natvich | 1:57502185804c | 687 | case 16: |
natvich | 1:57502185804c | 688 | temp |= (0x3 << 5); |
natvich | 1:57502185804c | 689 | settings.mag.scale = 16; |
natvich | 1:57502185804c | 690 | break; |
natvich | 1:57502185804c | 691 | default: // Otherwise we'll default to 4 gauss (00) |
natvich | 1:57502185804c | 692 | settings.mag.scale = 4; |
natvich | 1:57502185804c | 693 | break; |
natvich | 1:57502185804c | 694 | } |
natvich | 1:57502185804c | 695 | |
natvich | 1:57502185804c | 696 | // And write the new register value back into CTRL_REG6_XM: |
natvich | 1:57502185804c | 697 | mWriteByte(CTRL_REG2_M, temp); |
natvich | 1:57502185804c | 698 | |
natvich | 1:57502185804c | 699 | // We've updated the sensor, but we also need to update our class variables |
natvich | 1:57502185804c | 700 | // First update mScale: |
natvich | 1:57502185804c | 701 | //mScale = mScl; |
natvich | 1:57502185804c | 702 | // Then calculate a new mRes, which relies on mScale being set correctly: |
natvich | 1:57502185804c | 703 | calcmRes(); |
natvich | 1:57502185804c | 704 | } |
natvich | 1:57502185804c | 705 | |
natvich | 1:57502185804c | 706 | void LSM9DS1::setGyroODR(uint8_t gRate) |
natvich | 1:57502185804c | 707 | { |
natvich | 1:57502185804c | 708 | // Only do this if gRate is not 0 (which would disable the gyro) |
natvich | 1:57502185804c | 709 | if ((gRate & 0x07) != 0) |
natvich | 1:57502185804c | 710 | { |
natvich | 1:57502185804c | 711 | // We need to preserve the other bytes in CTRL_REG1_G. So, first read it: |
natvich | 1:57502185804c | 712 | uint8_t temp = xgReadByte(CTRL_REG1_G); |
natvich | 1:57502185804c | 713 | // Then mask out the gyro ODR bits: |
natvich | 1:57502185804c | 714 | temp &= 0xFF^(0x7 << 5); |
natvich | 1:57502185804c | 715 | temp |= (gRate & 0x07) << 5; |
natvich | 1:57502185804c | 716 | // Update our settings struct |
natvich | 1:57502185804c | 717 | settings.gyro.sampleRate = gRate & 0x07; |
natvich | 1:57502185804c | 718 | // And write the new register value back into CTRL_REG1_G: |
natvich | 1:57502185804c | 719 | xgWriteByte(CTRL_REG1_G, temp); |
natvich | 1:57502185804c | 720 | } |
natvich | 1:57502185804c | 721 | } |
natvich | 1:57502185804c | 722 | |
natvich | 1:57502185804c | 723 | void LSM9DS1::setAccelODR(uint8_t aRate) |
natvich | 1:57502185804c | 724 | { |
natvich | 1:57502185804c | 725 | // Only do this if aRate is not 0 (which would disable the accel) |
natvich | 1:57502185804c | 726 | if ((aRate & 0x07) != 0) |
natvich | 1:57502185804c | 727 | { |
natvich | 1:57502185804c | 728 | // We need to preserve the other bytes in CTRL_REG1_XM. So, first read it: |
natvich | 1:57502185804c | 729 | uint8_t temp = xgReadByte(CTRL_REG6_XL); |
natvich | 1:57502185804c | 730 | // Then mask out the accel ODR bits: |
natvich | 1:57502185804c | 731 | temp &= 0x1F; |
natvich | 1:57502185804c | 732 | // Then shift in our new ODR bits: |
natvich | 1:57502185804c | 733 | temp |= ((aRate & 0x07) << 5); |
natvich | 1:57502185804c | 734 | settings.accel.sampleRate = aRate & 0x07; |
natvich | 1:57502185804c | 735 | // And write the new register value back into CTRL_REG1_XM: |
natvich | 1:57502185804c | 736 | xgWriteByte(CTRL_REG6_XL, temp); |
natvich | 1:57502185804c | 737 | } |
natvich | 1:57502185804c | 738 | } |
natvich | 1:57502185804c | 739 | |
natvich | 1:57502185804c | 740 | void LSM9DS1::setMagODR(uint8_t mRate) |
natvich | 1:57502185804c | 741 | { |
natvich | 1:57502185804c | 742 | // We need to preserve the other bytes in CTRL_REG5_XM. So, first read it: |
natvich | 1:57502185804c | 743 | uint8_t temp = mReadByte(CTRL_REG1_M); |
natvich | 1:57502185804c | 744 | // Then mask out the mag ODR bits: |
natvich | 1:57502185804c | 745 | temp &= 0xFF^(0x7 << 2); |
natvich | 1:57502185804c | 746 | // Then shift in our new ODR bits: |
natvich | 1:57502185804c | 747 | temp |= ((mRate & 0x07) << 2); |
natvich | 1:57502185804c | 748 | settings.mag.sampleRate = mRate & 0x07; |
natvich | 1:57502185804c | 749 | // And write the new register value back into CTRL_REG5_XM: |
natvich | 1:57502185804c | 750 | mWriteByte(CTRL_REG1_M, temp); |
natvich | 1:57502185804c | 751 | } |
natvich | 1:57502185804c | 752 | |
natvich | 1:57502185804c | 753 | void LSM9DS1::calcgRes() |
natvich | 1:57502185804c | 754 | { |
natvich | 1:57502185804c | 755 | gRes = ((float) settings.gyro.scale) / 32768.0; |
natvich | 1:57502185804c | 756 | } |
natvich | 1:57502185804c | 757 | |
natvich | 1:57502185804c | 758 | void LSM9DS1::calcaRes() |
natvich | 1:57502185804c | 759 | { |
natvich | 1:57502185804c | 760 | aRes = ((float) settings.accel.scale) / 32768.0; |
natvich | 1:57502185804c | 761 | } |
natvich | 1:57502185804c | 762 | |
natvich | 1:57502185804c | 763 | void LSM9DS1::calcmRes() |
natvich | 1:57502185804c | 764 | { |
natvich | 1:57502185804c | 765 | //mRes = ((float) settings.mag.scale) / 32768.0; |
natvich | 1:57502185804c | 766 | switch (settings.mag.scale) |
natvich | 1:57502185804c | 767 | { |
natvich | 1:57502185804c | 768 | case 4: |
natvich | 1:57502185804c | 769 | mRes = magSensitivity[0]; |
natvich | 1:57502185804c | 770 | break; |
natvich | 1:57502185804c | 771 | case 8: |
natvich | 1:57502185804c | 772 | mRes = magSensitivity[1]; |
natvich | 1:57502185804c | 773 | break; |
natvich | 1:57502185804c | 774 | case 12: |
natvich | 1:57502185804c | 775 | mRes = magSensitivity[2]; |
natvich | 1:57502185804c | 776 | break; |
natvich | 1:57502185804c | 777 | case 16: |
natvich | 1:57502185804c | 778 | mRes = magSensitivity[3]; |
natvich | 1:57502185804c | 779 | break; |
natvich | 1:57502185804c | 780 | } |
natvich | 1:57502185804c | 781 | |
natvich | 1:57502185804c | 782 | } |
natvich | 1:57502185804c | 783 | |
natvich | 1:57502185804c | 784 | void LSM9DS1::configInt(interrupt_select interrupt, uint8_t generator, |
natvich | 1:57502185804c | 785 | h_lactive activeLow, pp_od pushPull) |
natvich | 1:57502185804c | 786 | { |
natvich | 1:57502185804c | 787 | // Write to INT1_CTRL or INT2_CTRL. [interupt] should already be one of |
natvich | 1:57502185804c | 788 | // those two values. |
natvich | 1:57502185804c | 789 | // [generator] should be an OR'd list of values from the interrupt_generators enum |
natvich | 1:57502185804c | 790 | xgWriteByte(interrupt, generator); |
natvich | 1:57502185804c | 791 | |
natvich | 1:57502185804c | 792 | // Configure CTRL_REG8 |
natvich | 1:57502185804c | 793 | uint8_t temp; |
natvich | 1:57502185804c | 794 | temp = xgReadByte(CTRL_REG8); |
natvich | 1:57502185804c | 795 | |
natvich | 1:57502185804c | 796 | if (activeLow) temp |= (1<<5); |
natvich | 1:57502185804c | 797 | else temp &= ~(1<<5); |
natvich | 1:57502185804c | 798 | |
natvich | 1:57502185804c | 799 | if (pushPull) temp &= ~(1<<4); |
natvich | 1:57502185804c | 800 | else temp |= (1<<4); |
natvich | 1:57502185804c | 801 | |
natvich | 1:57502185804c | 802 | xgWriteByte(CTRL_REG8, temp); |
natvich | 1:57502185804c | 803 | } |
natvich | 1:57502185804c | 804 | |
natvich | 1:57502185804c | 805 | void LSM9DS1::configInactivity(uint8_t duration, uint8_t threshold, bool sleepOn) |
natvich | 1:57502185804c | 806 | { |
natvich | 1:57502185804c | 807 | uint8_t temp = 0; |
natvich | 1:57502185804c | 808 | |
natvich | 1:57502185804c | 809 | temp = threshold & 0x7F; |
natvich | 1:57502185804c | 810 | if (sleepOn) temp |= (1<<7); |
natvich | 1:57502185804c | 811 | xgWriteByte(ACT_THS, temp); |
natvich | 1:57502185804c | 812 | |
natvich | 1:57502185804c | 813 | xgWriteByte(ACT_DUR, duration); |
natvich | 1:57502185804c | 814 | } |
natvich | 1:57502185804c | 815 | |
natvich | 1:57502185804c | 816 | uint8_t LSM9DS1::getInactivity() |
natvich | 1:57502185804c | 817 | { |
natvich | 1:57502185804c | 818 | uint8_t temp = xgReadByte(STATUS_REG_0); |
natvich | 1:57502185804c | 819 | temp &= (0x10); |
natvich | 1:57502185804c | 820 | return temp; |
natvich | 1:57502185804c | 821 | } |
natvich | 1:57502185804c | 822 | |
natvich | 1:57502185804c | 823 | void LSM9DS1::configAccelInt(uint8_t generator, bool andInterrupts) |
natvich | 1:57502185804c | 824 | { |
natvich | 1:57502185804c | 825 | // Use variables from accel_interrupt_generator, OR'd together to create |
natvich | 1:57502185804c | 826 | // the [generator]value. |
natvich | 1:57502185804c | 827 | uint8_t temp = generator; |
natvich | 1:57502185804c | 828 | if (andInterrupts) temp |= 0x80; |
natvich | 1:57502185804c | 829 | xgWriteByte(INT_GEN_CFG_XL, temp); |
natvich | 1:57502185804c | 830 | } |
natvich | 1:57502185804c | 831 | |
natvich | 1:57502185804c | 832 | void LSM9DS1::configAccelThs(uint8_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait) |
natvich | 1:57502185804c | 833 | { |
natvich | 1:57502185804c | 834 | // Write threshold value to INT_GEN_THS_?_XL. |
natvich | 1:57502185804c | 835 | // axis will be 0, 1, or 2 (x, y, z respectively) |
natvich | 1:57502185804c | 836 | xgWriteByte(INT_GEN_THS_X_XL + axis, threshold); |
natvich | 1:57502185804c | 837 | |
natvich | 1:57502185804c | 838 | // Write duration and wait to INT_GEN_DUR_XL |
natvich | 1:57502185804c | 839 | uint8_t temp; |
natvich | 1:57502185804c | 840 | temp = (duration & 0x7F); |
natvich | 1:57502185804c | 841 | if (wait) temp |= 0x80; |
natvich | 1:57502185804c | 842 | xgWriteByte(INT_GEN_DUR_XL, temp); |
natvich | 1:57502185804c | 843 | } |
natvich | 1:57502185804c | 844 | |
natvich | 1:57502185804c | 845 | uint8_t LSM9DS1::getAccelIntSrc() |
natvich | 1:57502185804c | 846 | { |
natvich | 1:57502185804c | 847 | uint8_t intSrc = xgReadByte(INT_GEN_SRC_XL); |
natvich | 1:57502185804c | 848 | |
natvich | 1:57502185804c | 849 | // Check if the IA_XL (interrupt active) bit is set |
natvich | 1:57502185804c | 850 | if (intSrc & (1<<6)) |
natvich | 1:57502185804c | 851 | { |
natvich | 1:57502185804c | 852 | return (intSrc & 0x3F); |
natvich | 1:57502185804c | 853 | } |
natvich | 1:57502185804c | 854 | |
natvich | 1:57502185804c | 855 | return 0; |
natvich | 1:57502185804c | 856 | } |
natvich | 1:57502185804c | 857 | |
natvich | 1:57502185804c | 858 | void LSM9DS1::configGyroInt(uint8_t generator, bool aoi, bool latch) |
natvich | 1:57502185804c | 859 | { |
natvich | 1:57502185804c | 860 | // Use variables from accel_interrupt_generator, OR'd together to create |
natvich | 1:57502185804c | 861 | // the [generator]value. |
natvich | 1:57502185804c | 862 | uint8_t temp = generator; |
natvich | 1:57502185804c | 863 | if (aoi) temp |= 0x80; |
natvich | 1:57502185804c | 864 | if (latch) temp |= 0x40; |
natvich | 1:57502185804c | 865 | xgWriteByte(INT_GEN_CFG_G, temp); |
natvich | 1:57502185804c | 866 | } |
natvich | 1:57502185804c | 867 | |
natvich | 1:57502185804c | 868 | void LSM9DS1::configGyroThs(int16_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait) |
natvich | 1:57502185804c | 869 | { |
natvich | 1:57502185804c | 870 | uint8_t buffer[2]; |
natvich | 1:57502185804c | 871 | buffer[0] = (threshold & 0x7F00) >> 8; |
natvich | 1:57502185804c | 872 | buffer[1] = (threshold & 0x00FF); |
natvich | 1:57502185804c | 873 | // Write threshold value to INT_GEN_THS_?H_G and INT_GEN_THS_?L_G. |
natvich | 1:57502185804c | 874 | // axis will be 0, 1, or 2 (x, y, z respectively) |
natvich | 1:57502185804c | 875 | xgWriteByte(INT_GEN_THS_XH_G + (axis * 2), buffer[0]); |
natvich | 1:57502185804c | 876 | xgWriteByte(INT_GEN_THS_XH_G + 1 + (axis * 2), buffer[1]); |
natvich | 1:57502185804c | 877 | |
natvich | 1:57502185804c | 878 | // Write duration and wait to INT_GEN_DUR_XL |
natvich | 1:57502185804c | 879 | uint8_t temp; |
natvich | 1:57502185804c | 880 | temp = (duration & 0x7F); |
natvich | 1:57502185804c | 881 | if (wait) temp |= 0x80; |
natvich | 1:57502185804c | 882 | xgWriteByte(INT_GEN_DUR_G, temp); |
natvich | 1:57502185804c | 883 | } |
natvich | 1:57502185804c | 884 | |
natvich | 1:57502185804c | 885 | uint8_t LSM9DS1::getGyroIntSrc() |
natvich | 1:57502185804c | 886 | { |
natvich | 1:57502185804c | 887 | uint8_t intSrc = xgReadByte(INT_GEN_SRC_G); |
natvich | 1:57502185804c | 888 | |
natvich | 1:57502185804c | 889 | // Check if the IA_G (interrupt active) bit is set |
natvich | 1:57502185804c | 890 | if (intSrc & (1<<6)) |
natvich | 1:57502185804c | 891 | { |
natvich | 1:57502185804c | 892 | return (intSrc & 0x3F); |
natvich | 1:57502185804c | 893 | } |
natvich | 1:57502185804c | 894 | |
natvich | 1:57502185804c | 895 | return 0; |
natvich | 1:57502185804c | 896 | } |
natvich | 1:57502185804c | 897 | |
natvich | 1:57502185804c | 898 | void LSM9DS1::configMagInt(uint8_t generator, h_lactive activeLow, bool latch) |
natvich | 1:57502185804c | 899 | { |
natvich | 1:57502185804c | 900 | // Mask out non-generator bits (0-4) |
natvich | 1:57502185804c | 901 | uint8_t config = (generator & 0xE0); |
natvich | 1:57502185804c | 902 | // IEA bit is 0 for active-low, 1 for active-high. |
natvich | 1:57502185804c | 903 | if (activeLow == INT_ACTIVE_HIGH) config |= (1<<2); |
natvich | 1:57502185804c | 904 | // IEL bit is 0 for latched, 1 for not-latched |
natvich | 1:57502185804c | 905 | if (!latch) config |= (1<<1); |
natvich | 1:57502185804c | 906 | // As long as we have at least 1 generator, enable the interrupt |
natvich | 1:57502185804c | 907 | if (generator != 0) config |= (1<<0); |
natvich | 1:57502185804c | 908 | |
natvich | 1:57502185804c | 909 | mWriteByte(INT_CFG_M, config); |
natvich | 1:57502185804c | 910 | } |
natvich | 1:57502185804c | 911 | |
natvich | 1:57502185804c | 912 | void LSM9DS1::configMagThs(uint16_t threshold) |
natvich | 1:57502185804c | 913 | { |
natvich | 1:57502185804c | 914 | // Write high eight bits of [threshold] to INT_THS_H_M |
natvich | 1:57502185804c | 915 | mWriteByte(INT_THS_H_M, uint8_t((threshold & 0x7F00) >> 8)); |
natvich | 1:57502185804c | 916 | // Write low eight bits of [threshold] to INT_THS_L_M |
natvich | 1:57502185804c | 917 | mWriteByte(INT_THS_L_M, uint8_t(threshold & 0x00FF)); |
natvich | 1:57502185804c | 918 | } |
natvich | 1:57502185804c | 919 | |
natvich | 1:57502185804c | 920 | uint8_t LSM9DS1::getMagIntSrc() |
natvich | 1:57502185804c | 921 | { |
natvich | 1:57502185804c | 922 | uint8_t intSrc = mReadByte(INT_SRC_M); |
natvich | 1:57502185804c | 923 | |
natvich | 1:57502185804c | 924 | // Check if the INT (interrupt active) bit is set |
natvich | 1:57502185804c | 925 | if (intSrc & (1<<0)) |
natvich | 1:57502185804c | 926 | { |
natvich | 1:57502185804c | 927 | return (intSrc & 0xFE); |
natvich | 1:57502185804c | 928 | } |
natvich | 1:57502185804c | 929 | |
natvich | 1:57502185804c | 930 | return 0; |
natvich | 1:57502185804c | 931 | } |
natvich | 1:57502185804c | 932 | |
natvich | 1:57502185804c | 933 | void LSM9DS1::sleepGyro(bool enable) |
natvich | 1:57502185804c | 934 | { |
natvich | 1:57502185804c | 935 | uint8_t temp = xgReadByte(CTRL_REG9); |
natvich | 1:57502185804c | 936 | if (enable) temp |= (1<<6); |
natvich | 1:57502185804c | 937 | else temp &= ~(1<<6); |
natvich | 1:57502185804c | 938 | xgWriteByte(CTRL_REG9, temp); |
natvich | 1:57502185804c | 939 | } |
natvich | 1:57502185804c | 940 | |
natvich | 1:57502185804c | 941 | void LSM9DS1::enableFIFO(bool enable) |
natvich | 1:57502185804c | 942 | { |
natvich | 1:57502185804c | 943 | uint8_t temp = xgReadByte(CTRL_REG9); |
natvich | 1:57502185804c | 944 | if (enable) temp |= (1<<1); |
natvich | 1:57502185804c | 945 | else temp &= ~(1<<1); |
natvich | 1:57502185804c | 946 | xgWriteByte(CTRL_REG9, temp); |
natvich | 1:57502185804c | 947 | } |
natvich | 1:57502185804c | 948 | |
natvich | 1:57502185804c | 949 | void LSM9DS1::setFIFO(fifoMode_type fifoMode, uint8_t fifoThs) |
natvich | 1:57502185804c | 950 | { |
natvich | 1:57502185804c | 951 | // Limit threshold - 0x1F (31) is the maximum. If more than that was asked |
natvich | 1:57502185804c | 952 | // limit it to the maximum. |
natvich | 1:57502185804c | 953 | uint8_t threshold = fifoThs <= 0x1F ? fifoThs : 0x1F; |
natvich | 1:57502185804c | 954 | xgWriteByte(FIFO_CTRL, ((fifoMode & 0x7) << 5) | (threshold & 0x1F)); |
natvich | 1:57502185804c | 955 | } |
natvich | 1:57502185804c | 956 | |
natvich | 1:57502185804c | 957 | uint8_t LSM9DS1::getFIFOSamples() |
natvich | 1:57502185804c | 958 | { |
natvich | 1:57502185804c | 959 | return (xgReadByte(FIFO_SRC) & 0x3F); |
natvich | 1:57502185804c | 960 | } |
natvich | 1:57502185804c | 961 | |
natvich | 1:57502185804c | 962 | void LSM9DS1::constrainScales() |
natvich | 1:57502185804c | 963 | { |
natvich | 1:57502185804c | 964 | if ((settings.gyro.scale != 245) && (settings.gyro.scale != 500) && |
natvich | 1:57502185804c | 965 | (settings.gyro.scale != 2000)) |
natvich | 1:57502185804c | 966 | { |
natvich | 1:57502185804c | 967 | settings.gyro.scale = 245; |
natvich | 1:57502185804c | 968 | } |
natvich | 1:57502185804c | 969 | |
natvich | 1:57502185804c | 970 | if ((settings.accel.scale != 2) && (settings.accel.scale != 4) && |
natvich | 1:57502185804c | 971 | (settings.accel.scale != 8) && (settings.accel.scale != 16)) |
natvich | 1:57502185804c | 972 | { |
natvich | 1:57502185804c | 973 | settings.accel.scale = 2; |
natvich | 1:57502185804c | 974 | } |
natvich | 1:57502185804c | 975 | |
natvich | 1:57502185804c | 976 | if ((settings.mag.scale != 4) && (settings.mag.scale != 8) && |
natvich | 1:57502185804c | 977 | (settings.mag.scale != 12) && (settings.mag.scale != 16)) |
natvich | 1:57502185804c | 978 | { |
natvich | 1:57502185804c | 979 | settings.mag.scale = 4; |
natvich | 1:57502185804c | 980 | } |
natvich | 1:57502185804c | 981 | } |
natvich | 1:57502185804c | 982 | |
natvich | 1:57502185804c | 983 | void LSM9DS1::xgWriteByte(uint8_t subAddress, uint8_t data) |
natvich | 1:57502185804c | 984 | { |
natvich | 1:57502185804c | 985 | // Whether we're using I2C or SPI, write a byte using the |
natvich | 1:57502185804c | 986 | // gyro-specific I2C address or SPI CS pin. |
natvich | 1:57502185804c | 987 | if (settings.device.commInterface == IMU_MODE_I2C) { |
natvich | 1:57502185804c | 988 | printf("yo"); |
natvich | 1:57502185804c | 989 | I2CwriteByte(_xgAddress, subAddress, data); |
natvich | 1:57502185804c | 990 | } else if (settings.device.commInterface == IMU_MODE_SPI) { |
natvich | 1:57502185804c | 991 | SPIwriteByte(_xgAddress, subAddress, data); |
natvich | 1:57502185804c | 992 | } |
natvich | 1:57502185804c | 993 | } |
natvich | 1:57502185804c | 994 | |
natvich | 1:57502185804c | 995 | void LSM9DS1::mWriteByte(uint8_t subAddress, uint8_t data) |
natvich | 1:57502185804c | 996 | { |
natvich | 1:57502185804c | 997 | // Whether we're using I2C or SPI, write a byte using the |
natvich | 1:57502185804c | 998 | // accelerometer-specific I2C address or SPI CS pin. |
natvich | 1:57502185804c | 999 | if (settings.device.commInterface == IMU_MODE_I2C) |
natvich | 1:57502185804c | 1000 | return I2CwriteByte(_mAddress, subAddress, data); |
natvich | 1:57502185804c | 1001 | else if (settings.device.commInterface == IMU_MODE_SPI) |
natvich | 1:57502185804c | 1002 | return SPIwriteByte(_mAddress, subAddress, data); |
natvich | 1:57502185804c | 1003 | } |
natvich | 1:57502185804c | 1004 | |
natvich | 1:57502185804c | 1005 | uint8_t LSM9DS1::xgReadByte(uint8_t subAddress) |
natvich | 1:57502185804c | 1006 | { |
natvich | 1:57502185804c | 1007 | // Whether we're using I2C or SPI, read a byte using the |
natvich | 1:57502185804c | 1008 | // gyro-specific I2C address or SPI CS pin. |
natvich | 1:57502185804c | 1009 | if (settings.device.commInterface == IMU_MODE_I2C) |
natvich | 1:57502185804c | 1010 | return I2CreadByte(_xgAddress, subAddress); |
natvich | 1:57502185804c | 1011 | else if (settings.device.commInterface == IMU_MODE_SPI) |
natvich | 1:57502185804c | 1012 | return SPIreadByte(_xgAddress, subAddress); |
natvich | 1:57502185804c | 1013 | } |
natvich | 1:57502185804c | 1014 | |
natvich | 1:57502185804c | 1015 | void LSM9DS1::xgReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count) |
natvich | 1:57502185804c | 1016 | { |
natvich | 1:57502185804c | 1017 | // Whether we're using I2C or SPI, read multiple bytes using the |
natvich | 1:57502185804c | 1018 | // gyro-specific I2C address or SPI CS pin. |
natvich | 1:57502185804c | 1019 | if (settings.device.commInterface == IMU_MODE_I2C) { |
natvich | 1:57502185804c | 1020 | I2CreadBytes(_xgAddress, subAddress, dest, count); |
natvich | 1:57502185804c | 1021 | } else if (settings.device.commInterface == IMU_MODE_SPI) { |
natvich | 1:57502185804c | 1022 | SPIreadBytes(_xgAddress, subAddress, dest, count); |
natvich | 1:57502185804c | 1023 | } |
natvich | 1:57502185804c | 1024 | } |
natvich | 1:57502185804c | 1025 | |
natvich | 1:57502185804c | 1026 | uint8_t LSM9DS1::mReadByte(uint8_t subAddress) |
natvich | 1:57502185804c | 1027 | { |
natvich | 1:57502185804c | 1028 | // Whether we're using I2C or SPI, read a byte using the |
natvich | 1:57502185804c | 1029 | // accelerometer-specific I2C address or SPI CS pin. |
natvich | 1:57502185804c | 1030 | if (settings.device.commInterface == IMU_MODE_I2C) |
natvich | 1:57502185804c | 1031 | return I2CreadByte(_mAddress, subAddress); |
natvich | 1:57502185804c | 1032 | else if (settings.device.commInterface == IMU_MODE_SPI) |
natvich | 1:57502185804c | 1033 | return SPIreadByte(_mAddress, subAddress); |
natvich | 1:57502185804c | 1034 | } |
natvich | 1:57502185804c | 1035 | |
natvich | 1:57502185804c | 1036 | void LSM9DS1::mReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count) |
natvich | 1:57502185804c | 1037 | { |
natvich | 1:57502185804c | 1038 | // Whether we're using I2C or SPI, read multiple bytes using the |
natvich | 1:57502185804c | 1039 | // accelerometer-specific I2C address or SPI CS pin. |
natvich | 1:57502185804c | 1040 | if (settings.device.commInterface == IMU_MODE_I2C) |
natvich | 1:57502185804c | 1041 | I2CreadBytes(_mAddress, subAddress, dest, count); |
natvich | 1:57502185804c | 1042 | else if (settings.device.commInterface == IMU_MODE_SPI) |
natvich | 1:57502185804c | 1043 | SPIreadBytes(_mAddress, subAddress, dest, count); |
natvich | 1:57502185804c | 1044 | } |
natvich | 1:57502185804c | 1045 | |
natvich | 1:57502185804c | 1046 | void LSM9DS1::initSPI() |
natvich | 1:57502185804c | 1047 | { |
natvich | 1:57502185804c | 1048 | /* |
natvich | 1:57502185804c | 1049 | pinMode(_xgAddress, OUTPUT); |
natvich | 1:57502185804c | 1050 | digitalWrite(_xgAddress, HIGH); |
natvich | 1:57502185804c | 1051 | pinMode(_mAddress, OUTPUT); |
natvich | 1:57502185804c | 1052 | digitalWrite(_mAddress, HIGH); |
natvich | 1:57502185804c | 1053 | |
natvich | 1:57502185804c | 1054 | SPI.begin(); |
natvich | 1:57502185804c | 1055 | // Maximum SPI frequency is 10MHz, could divide by 2 here: |
natvich | 1:57502185804c | 1056 | SPI.setClockDivider(SPI_CLOCK_DIV2); |
natvich | 1:57502185804c | 1057 | // Data is read and written MSb first. |
natvich | 1:57502185804c | 1058 | SPI.setBitOrder(MSBFIRST); |
natvich | 1:57502185804c | 1059 | // Data is captured on rising edge of clock (CPHA = 0) |
natvich | 1:57502185804c | 1060 | // Base value of the clock is HIGH (CPOL = 1) |
natvich | 1:57502185804c | 1061 | SPI.setDataMode(SPI_MODE0); |
natvich | 1:57502185804c | 1062 | */ |
natvich | 1:57502185804c | 1063 | } |
natvich | 1:57502185804c | 1064 | |
natvich | 1:57502185804c | 1065 | void LSM9DS1::SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data) |
natvich | 1:57502185804c | 1066 | { |
natvich | 1:57502185804c | 1067 | /* |
natvich | 1:57502185804c | 1068 | digitalWrite(csPin, LOW); // Initiate communication |
natvich | 1:57502185804c | 1069 | |
natvich | 1:57502185804c | 1070 | // If write, bit 0 (MSB) should be 0 |
natvich | 1:57502185804c | 1071 | // If single write, bit 1 should be 0 |
natvich | 1:57502185804c | 1072 | SPI.transfer(subAddress & 0x3F); // Send Address |
natvich | 1:57502185804c | 1073 | SPI.transfer(data); // Send data |
natvich | 1:57502185804c | 1074 | |
natvich | 1:57502185804c | 1075 | digitalWrite(csPin, HIGH); // Close communication |
natvich | 1:57502185804c | 1076 | */ |
natvich | 1:57502185804c | 1077 | } |
natvich | 1:57502185804c | 1078 | |
natvich | 1:57502185804c | 1079 | uint8_t LSM9DS1::SPIreadByte(uint8_t csPin, uint8_t subAddress) |
natvich | 1:57502185804c | 1080 | { |
natvich | 1:57502185804c | 1081 | uint8_t temp; |
natvich | 1:57502185804c | 1082 | // Use the multiple read function to read 1 byte. |
natvich | 1:57502185804c | 1083 | // Value is returned to `temp`. |
natvich | 1:57502185804c | 1084 | SPIreadBytes(csPin, subAddress, &temp, 1); |
natvich | 1:57502185804c | 1085 | return temp; |
natvich | 1:57502185804c | 1086 | } |
natvich | 1:57502185804c | 1087 | |
natvich | 1:57502185804c | 1088 | void LSM9DS1::SPIreadBytes(uint8_t csPin, uint8_t subAddress, |
natvich | 1:57502185804c | 1089 | uint8_t * dest, uint8_t count) |
natvich | 1:57502185804c | 1090 | { |
natvich | 1:57502185804c | 1091 | // To indicate a read, set bit 0 (msb) of first byte to 1 |
natvich | 1:57502185804c | 1092 | uint8_t rAddress = 0x80 | (subAddress & 0x3F); |
natvich | 1:57502185804c | 1093 | // Mag SPI port is different. If we're reading multiple bytes, |
natvich | 1:57502185804c | 1094 | // set bit 1 to 1. The remaining six bytes are the address to be read |
natvich | 1:57502185804c | 1095 | if ((csPin == _mAddress) && count > 1) |
natvich | 1:57502185804c | 1096 | rAddress |= 0x40; |
natvich | 1:57502185804c | 1097 | |
natvich | 1:57502185804c | 1098 | /* |
natvich | 1:57502185804c | 1099 | digitalWrite(csPin, LOW); // Initiate communication |
natvich | 1:57502185804c | 1100 | SPI.transfer(rAddress); |
natvich | 1:57502185804c | 1101 | for (int i=0; i<count; i++) |
natvich | 1:57502185804c | 1102 | { |
natvich | 1:57502185804c | 1103 | dest[i] = SPI.transfer(0x00); // Read into destination array |
natvich | 1:57502185804c | 1104 | } |
natvich | 1:57502185804c | 1105 | digitalWrite(csPin, HIGH); // Close communication |
natvich | 1:57502185804c | 1106 | */ |
natvich | 1:57502185804c | 1107 | } |
natvich | 1:57502185804c | 1108 | |
natvich | 1:57502185804c | 1109 | void LSM9DS1::initI2C() |
natvich | 1:57502185804c | 1110 | { |
natvich | 1:57502185804c | 1111 | /* |
natvich | 1:57502185804c | 1112 | Wire.begin(); // Initialize I2C library |
natvich | 1:57502185804c | 1113 | */ |
natvich | 1:57502185804c | 1114 | |
natvich | 1:57502185804c | 1115 | //already initialized in constructor! |
natvich | 1:57502185804c | 1116 | } |
natvich | 1:57502185804c | 1117 | |
natvich | 1:57502185804c | 1118 | // Wire.h read and write protocols |
natvich | 1:57502185804c | 1119 | void LSM9DS1::I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data) |
natvich | 1:57502185804c | 1120 | { |
natvich | 1:57502185804c | 1121 | /* |
natvich | 1:57502185804c | 1122 | Wire.beginTransmission(address); // Initialize the Tx buffer |
natvich | 1:57502185804c | 1123 | Wire.write(subAddress); // Put slave register address in Tx buffer |
natvich | 1:57502185804c | 1124 | Wire.write(data); // Put data in Tx buffer |
natvich | 1:57502185804c | 1125 | Wire.endTransmission(); // Send the Tx buffer |
natvich | 1:57502185804c | 1126 | */ |
natvich | 1:57502185804c | 1127 | char temp_data[2] = {subAddress, data}; |
natvich | 1:57502185804c | 1128 | i2c.write(address, temp_data, 2); |
natvich | 1:57502185804c | 1129 | } |
natvich | 1:57502185804c | 1130 | |
natvich | 1:57502185804c | 1131 | uint8_t LSM9DS1::I2CreadByte(uint8_t address, uint8_t subAddress) |
natvich | 1:57502185804c | 1132 | { |
natvich | 1:57502185804c | 1133 | /* |
natvich | 1:57502185804c | 1134 | int timeout = LSM9DS1_COMMUNICATION_TIMEOUT; |
natvich | 1:57502185804c | 1135 | uint8_t data; // `data` will store the register data |
natvich | 1:57502185804c | 1136 | |
natvich | 1:57502185804c | 1137 | Wire.beginTransmission(address); // Initialize the Tx buffer |
natvich | 1:57502185804c | 1138 | Wire.write(subAddress); // Put slave register address in Tx buffer |
natvich | 1:57502185804c | 1139 | Wire.endTransmission(true); // Send the Tx buffer, but send a restart to keep connection alive |
natvich | 1:57502185804c | 1140 | Wire.requestFrom(address, (uint8_t) 1); // Read one byte from slave register address |
natvich | 1:57502185804c | 1141 | while ((Wire.available() < 1) && (timeout-- > 0)) |
natvich | 1:57502185804c | 1142 | delay(1); |
natvich | 1:57502185804c | 1143 | |
natvich | 1:57502185804c | 1144 | if (timeout <= 0) |
natvich | 1:57502185804c | 1145 | return 255; //! Bad! 255 will be misinterpreted as a good value. |
natvich | 1:57502185804c | 1146 | |
natvich | 1:57502185804c | 1147 | data = Wire.read(); // Fill Rx buffer with result |
natvich | 1:57502185804c | 1148 | return data; // Return data read from slave register |
natvich | 1:57502185804c | 1149 | */ |
natvich | 1:57502185804c | 1150 | char data; |
natvich | 1:57502185804c | 1151 | char temp[1] = {subAddress}; |
natvich | 1:57502185804c | 1152 | |
natvich | 1:57502185804c | 1153 | i2c.write(address, temp, 1); |
natvich | 1:57502185804c | 1154 | //i2c.write(address & 0xFE); |
natvich | 1:57502185804c | 1155 | temp[1] = 0x00; |
natvich | 1:57502185804c | 1156 | i2c.write(address, temp, 1); |
natvich | 1:57502185804c | 1157 | //i2c.write( address | 0x01); |
natvich | 1:57502185804c | 1158 | int a = i2c.read(address, &data, 1); |
natvich | 1:57502185804c | 1159 | return data; |
natvich | 1:57502185804c | 1160 | } |
natvich | 1:57502185804c | 1161 | |
natvich | 1:57502185804c | 1162 | uint8_t LSM9DS1::I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count) |
natvich | 1:57502185804c | 1163 | { |
natvich | 1:57502185804c | 1164 | /* |
natvich | 1:57502185804c | 1165 | int timeout = LSM9DS1_COMMUNICATION_TIMEOUT; |
natvich | 1:57502185804c | 1166 | Wire.beginTransmission(address); // Initialize the Tx buffer |
natvich | 1:57502185804c | 1167 | // Next send the register to be read. OR with 0x80 to indicate multi-read. |
natvich | 1:57502185804c | 1168 | Wire.write(subAddress | 0x80); // Put slave register address in Tx buffer |
natvich | 1:57502185804c | 1169 | |
natvich | 1:57502185804c | 1170 | Wire.endTransmission(true); // Send the Tx buffer, but send a restart to keep connection alive |
natvich | 1:57502185804c | 1171 | uint8_t i = 0; |
natvich | 1:57502185804c | 1172 | Wire.requestFrom(address, count); // Read bytes from slave register address |
natvich | 1:57502185804c | 1173 | while ((Wire.available() < count) && (timeout-- > 0)) |
natvich | 1:57502185804c | 1174 | delay(1); |
natvich | 1:57502185804c | 1175 | if (timeout <= 0) |
natvich | 1:57502185804c | 1176 | return -1; |
natvich | 1:57502185804c | 1177 | |
natvich | 1:57502185804c | 1178 | for (int i=0; i<count;) |
natvich | 1:57502185804c | 1179 | { |
natvich | 1:57502185804c | 1180 | if (Wire.available()) |
natvich | 1:57502185804c | 1181 | { |
natvich | 1:57502185804c | 1182 | dest[i++] = Wire.read(); |
natvich | 1:57502185804c | 1183 | } |
natvich | 1:57502185804c | 1184 | } |
natvich | 1:57502185804c | 1185 | return count; |
natvich | 1:57502185804c | 1186 | */ |
natvich | 1:57502185804c | 1187 | int i; |
natvich | 1:57502185804c | 1188 | char temp_dest[count]; |
natvich | 1:57502185804c | 1189 | char temp[1] = {subAddress}; |
natvich | 1:57502185804c | 1190 | i2c.write(address, temp, 1); |
natvich | 1:57502185804c | 1191 | i2c.read(address, temp_dest, count); |
natvich | 1:57502185804c | 1192 | |
natvich | 1:57502185804c | 1193 | //i2c doesn't take uint8_ts, but rather chars so do this nasty af conversion |
natvich | 1:57502185804c | 1194 | for (i=0; i < count; i++) { |
natvich | 1:57502185804c | 1195 | dest[i] = temp_dest[i]; |
natvich | 1:57502185804c | 1196 | } |
natvich | 1:57502185804c | 1197 | return count; |
natvich | 1:57502185804c | 1198 | } |