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