Allows for reading accelerometer, gyroscope, and magnetometer data from an LSM9DS0 IMU device
Dependents: uVGA_4180 uLCD_4180_mini ECE4781_Project
LSM9DS0.h@5:e6a15dcba942, 2015-01-11 (annotated)
- Committer:
- randrews33
- Date:
- Sun Jan 11 14:44:43 2015 +0000
- Revision:
- 5:e6a15dcba942
- Parent:
- 4:bf8f4e7c9905
Gave credit where credit was due
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
randrews33 | 4:bf8f4e7c9905 | 1 | //Most of the Credit goes to jimblom |
randrews33 | 0:1b975a6ae539 | 2 | #ifndef _LSM9DS0_H__ |
randrews33 | 0:1b975a6ae539 | 3 | #define _LSM9DS0_H__ |
randrews33 | 0:1b975a6ae539 | 4 | |
randrews33 | 0:1b975a6ae539 | 5 | #include "mbed.h" |
randrews33 | 0:1b975a6ae539 | 6 | #include "I2Cdev.h" |
randrews33 | 0:1b975a6ae539 | 7 | |
randrews33 | 0:1b975a6ae539 | 8 | |
randrews33 | 0:1b975a6ae539 | 9 | //////////////////////////// |
randrews33 | 0:1b975a6ae539 | 10 | // LSM9DS0 Gyro Registers // |
randrews33 | 0:1b975a6ae539 | 11 | //////////////////////////// |
randrews33 | 0:1b975a6ae539 | 12 | #define WHO_AM_I_G 0x0F |
randrews33 | 0:1b975a6ae539 | 13 | #define CTRL_REG1_G 0x20 |
randrews33 | 0:1b975a6ae539 | 14 | #define CTRL_REG2_G 0x21 |
randrews33 | 0:1b975a6ae539 | 15 | #define CTRL_REG3_G 0x22 |
randrews33 | 0:1b975a6ae539 | 16 | #define CTRL_REG4_G 0x23 |
randrews33 | 0:1b975a6ae539 | 17 | #define CTRL_REG5_G 0x24 |
randrews33 | 0:1b975a6ae539 | 18 | #define REFERENCE_G 0x25 |
randrews33 | 0:1b975a6ae539 | 19 | #define STATUS_REG_G 0x27 |
randrews33 | 0:1b975a6ae539 | 20 | #define OUT_X_L_G 0x28 |
randrews33 | 0:1b975a6ae539 | 21 | #define OUT_X_H_G 0x29 |
randrews33 | 0:1b975a6ae539 | 22 | #define OUT_Y_L_G 0x2A |
randrews33 | 0:1b975a6ae539 | 23 | #define OUT_Y_H_G 0x2B |
randrews33 | 0:1b975a6ae539 | 24 | #define OUT_Z_L_G 0x2C |
randrews33 | 0:1b975a6ae539 | 25 | #define OUT_Z_H_G 0x2D |
randrews33 | 0:1b975a6ae539 | 26 | #define FIFO_CTRL_REG_G 0x2E |
randrews33 | 0:1b975a6ae539 | 27 | #define FIFO_SRC_REG_G 0x2F |
randrews33 | 0:1b975a6ae539 | 28 | #define INT1_CFG_G 0x30 |
randrews33 | 0:1b975a6ae539 | 29 | #define INT1_SRC_G 0x31 |
randrews33 | 0:1b975a6ae539 | 30 | #define INT1_THS_XH_G 0x32 |
randrews33 | 0:1b975a6ae539 | 31 | #define INT1_THS_XL_G 0x33 |
randrews33 | 0:1b975a6ae539 | 32 | #define INT1_THS_YH_G 0x34 |
randrews33 | 0:1b975a6ae539 | 33 | #define INT1_THS_YL_G 0x35 |
randrews33 | 0:1b975a6ae539 | 34 | #define INT1_THS_ZH_G 0x36 |
randrews33 | 0:1b975a6ae539 | 35 | #define INT1_THS_ZL_G 0x37 |
randrews33 | 0:1b975a6ae539 | 36 | #define INT1_DURATION_G 0x38 |
randrews33 | 0:1b975a6ae539 | 37 | |
randrews33 | 0:1b975a6ae539 | 38 | ////////////////////////////////////////// |
randrews33 | 0:1b975a6ae539 | 39 | // LSM9DS0 Accel/Magneto (XM) Registers // |
randrews33 | 0:1b975a6ae539 | 40 | ////////////////////////////////////////// |
randrews33 | 0:1b975a6ae539 | 41 | #define OUT_TEMP_L_XM 0x05 |
randrews33 | 0:1b975a6ae539 | 42 | #define OUT_TEMP_H_XM 0x06 |
randrews33 | 0:1b975a6ae539 | 43 | #define STATUS_REG_M 0x07 |
randrews33 | 0:1b975a6ae539 | 44 | #define OUT_X_L_M 0x08 |
randrews33 | 0:1b975a6ae539 | 45 | #define OUT_X_H_M 0x09 |
randrews33 | 0:1b975a6ae539 | 46 | #define OUT_Y_L_M 0x0A |
randrews33 | 0:1b975a6ae539 | 47 | #define OUT_Y_H_M 0x0B |
randrews33 | 0:1b975a6ae539 | 48 | #define OUT_Z_L_M 0x0C |
randrews33 | 0:1b975a6ae539 | 49 | #define OUT_Z_H_M 0x0D |
randrews33 | 0:1b975a6ae539 | 50 | #define WHO_AM_I_XM 0x0F |
randrews33 | 0:1b975a6ae539 | 51 | #define INT_CTRL_REG_M 0x12 |
randrews33 | 0:1b975a6ae539 | 52 | #define INT_SRC_REG_M 0x13 |
randrews33 | 0:1b975a6ae539 | 53 | #define INT_THS_L_M 0x14 |
randrews33 | 0:1b975a6ae539 | 54 | #define INT_THS_H_M 0x15 |
randrews33 | 0:1b975a6ae539 | 55 | #define OFFSET_X_L_M 0x16 |
randrews33 | 0:1b975a6ae539 | 56 | #define OFFSET_X_H_M 0x17 |
randrews33 | 0:1b975a6ae539 | 57 | #define OFFSET_Y_L_M 0x18 |
randrews33 | 0:1b975a6ae539 | 58 | #define OFFSET_Y_H_M 0x19 |
randrews33 | 0:1b975a6ae539 | 59 | #define OFFSET_Z_L_M 0x1A |
randrews33 | 0:1b975a6ae539 | 60 | #define OFFSET_Z_H_M 0x1B |
randrews33 | 0:1b975a6ae539 | 61 | #define REFERENCE_X 0x1C |
randrews33 | 0:1b975a6ae539 | 62 | #define REFERENCE_Y 0x1D |
randrews33 | 0:1b975a6ae539 | 63 | #define REFERENCE_Z 0x1E |
randrews33 | 0:1b975a6ae539 | 64 | #define CTRL_REG0_XM 0x1F |
randrews33 | 0:1b975a6ae539 | 65 | #define CTRL_REG1_XM 0x20 |
randrews33 | 0:1b975a6ae539 | 66 | #define CTRL_REG2_XM 0x21 |
randrews33 | 0:1b975a6ae539 | 67 | #define CTRL_REG3_XM 0x22 |
randrews33 | 0:1b975a6ae539 | 68 | #define CTRL_REG4_XM 0x23 |
randrews33 | 0:1b975a6ae539 | 69 | #define CTRL_REG5_XM 0x24 |
randrews33 | 0:1b975a6ae539 | 70 | #define CTRL_REG6_XM 0x25 |
randrews33 | 0:1b975a6ae539 | 71 | #define CTRL_REG7_XM 0x26 |
randrews33 | 0:1b975a6ae539 | 72 | #define STATUS_REG_A 0x27 |
randrews33 | 0:1b975a6ae539 | 73 | #define OUT_X_L_A 0x28 |
randrews33 | 0:1b975a6ae539 | 74 | #define OUT_X_H_A 0x29 |
randrews33 | 0:1b975a6ae539 | 75 | #define OUT_Y_L_A 0x2A |
randrews33 | 0:1b975a6ae539 | 76 | #define OUT_Y_H_A 0x2B |
randrews33 | 0:1b975a6ae539 | 77 | #define OUT_Z_L_A 0x2C |
randrews33 | 0:1b975a6ae539 | 78 | #define OUT_Z_H_A 0x2D |
randrews33 | 0:1b975a6ae539 | 79 | #define FIFO_CTRL_REG 0x2E |
randrews33 | 0:1b975a6ae539 | 80 | #define FIFO_SRC_REG 0x2F |
randrews33 | 0:1b975a6ae539 | 81 | #define INT_GEN_1_REG 0x30 |
randrews33 | 0:1b975a6ae539 | 82 | #define INT_GEN_1_SRC 0x31 |
randrews33 | 0:1b975a6ae539 | 83 | #define INT_GEN_1_THS 0x32 |
randrews33 | 0:1b975a6ae539 | 84 | #define INT_GEN_1_DURATION 0x33 |
randrews33 | 0:1b975a6ae539 | 85 | #define INT_GEN_2_REG 0x34 |
randrews33 | 0:1b975a6ae539 | 86 | #define INT_GEN_2_SRC 0x35 |
randrews33 | 0:1b975a6ae539 | 87 | #define INT_GEN_2_THS 0x36 |
randrews33 | 0:1b975a6ae539 | 88 | #define INT_GEN_2_DURATION 0x37 |
randrews33 | 0:1b975a6ae539 | 89 | #define CLICK_CFG 0x38 |
randrews33 | 0:1b975a6ae539 | 90 | #define CLICK_SRC 0x39 |
randrews33 | 0:1b975a6ae539 | 91 | #define CLICK_THS 0x3A |
randrews33 | 0:1b975a6ae539 | 92 | #define TIME_LIMIT 0x3B |
randrews33 | 0:1b975a6ae539 | 93 | #define TIME_LATENCY 0x3C |
randrews33 | 0:1b975a6ae539 | 94 | #define TIME_WINDOW 0x3D |
randrews33 | 0:1b975a6ae539 | 95 | #define ACT_THS 0x3E |
randrews33 | 0:1b975a6ae539 | 96 | #define ACT_DUR 0x3F |
randrews33 | 0:1b975a6ae539 | 97 | |
randrews33 | 0:1b975a6ae539 | 98 | |
randrews33 | 0:1b975a6ae539 | 99 | class LSM9DS0 |
randrews33 | 0:1b975a6ae539 | 100 | { |
randrews33 | 0:1b975a6ae539 | 101 | public: |
randrews33 | 0:1b975a6ae539 | 102 | // gyro_scale defines the possible full-scale ranges of the gyroscope: |
randrews33 | 0:1b975a6ae539 | 103 | enum gyro_scale |
randrews33 | 0:1b975a6ae539 | 104 | { |
randrews33 | 0:1b975a6ae539 | 105 | G_SCALE_245DPS, // 00: +/- 245 degrees per second |
randrews33 | 0:1b975a6ae539 | 106 | G_SCALE_500DPS, // 01: +/- 500 dps |
randrews33 | 0:1b975a6ae539 | 107 | G_SCALE_2000DPS, // 10: +/- 2000 dps |
randrews33 | 0:1b975a6ae539 | 108 | }; |
randrews33 | 0:1b975a6ae539 | 109 | // accel_scale defines all possible FSR's of the accelerometer: |
randrews33 | 0:1b975a6ae539 | 110 | enum accel_scale |
randrews33 | 0:1b975a6ae539 | 111 | { |
randrews33 | 0:1b975a6ae539 | 112 | A_SCALE_2G, // 000: +/- 2g |
randrews33 | 0:1b975a6ae539 | 113 | A_SCALE_4G, // 001: +/- 4g |
randrews33 | 0:1b975a6ae539 | 114 | A_SCALE_6G, // 010: +/- 6g |
randrews33 | 0:1b975a6ae539 | 115 | A_SCALE_8G, // 011: +/- 8g |
randrews33 | 0:1b975a6ae539 | 116 | A_SCALE_16G // 100: +/- 16g |
randrews33 | 0:1b975a6ae539 | 117 | }; |
randrews33 | 0:1b975a6ae539 | 118 | // mag_scale defines all possible FSR's of the magnetometer: |
randrews33 | 0:1b975a6ae539 | 119 | enum mag_scale |
randrews33 | 0:1b975a6ae539 | 120 | { |
randrews33 | 0:1b975a6ae539 | 121 | M_SCALE_2GS, // 00: +/- 2Gs |
randrews33 | 0:1b975a6ae539 | 122 | M_SCALE_4GS, // 01: +/- 4Gs |
randrews33 | 0:1b975a6ae539 | 123 | M_SCALE_8GS, // 10: +/- 8Gs |
randrews33 | 0:1b975a6ae539 | 124 | M_SCALE_12GS, // 11: +/- 12Gs |
randrews33 | 0:1b975a6ae539 | 125 | }; |
randrews33 | 0:1b975a6ae539 | 126 | // gyro_odr defines all possible data rate/bandwidth combos of the gyro: |
randrews33 | 0:1b975a6ae539 | 127 | enum gyro_odr |
randrews33 | 0:1b975a6ae539 | 128 | { // ODR (Hz) --- Cutoff |
randrews33 | 0:1b975a6ae539 | 129 | G_ODR_95_BW_125 = 0x0, // 95 12.5 |
randrews33 | 0:1b975a6ae539 | 130 | G_ODR_95_BW_25 = 0x1, // 95 25 |
randrews33 | 0:1b975a6ae539 | 131 | // 0x2 and 0x3 define the same data rate and bandwidth |
randrews33 | 0:1b975a6ae539 | 132 | G_ODR_190_BW_125 = 0x4, // 190 12.5 |
randrews33 | 0:1b975a6ae539 | 133 | G_ODR_190_BW_25 = 0x5, // 190 25 |
randrews33 | 0:1b975a6ae539 | 134 | G_ODR_190_BW_50 = 0x6, // 190 50 |
randrews33 | 0:1b975a6ae539 | 135 | G_ODR_190_BW_70 = 0x7, // 190 70 |
randrews33 | 0:1b975a6ae539 | 136 | G_ODR_380_BW_20 = 0x8, // 380 20 |
randrews33 | 0:1b975a6ae539 | 137 | G_ODR_380_BW_25 = 0x9, // 380 25 |
randrews33 | 0:1b975a6ae539 | 138 | G_ODR_380_BW_50 = 0xA, // 380 50 |
randrews33 | 0:1b975a6ae539 | 139 | G_ODR_380_BW_100 = 0xB, // 380 100 |
randrews33 | 0:1b975a6ae539 | 140 | G_ODR_760_BW_30 = 0xC, // 760 30 |
randrews33 | 0:1b975a6ae539 | 141 | G_ODR_760_BW_35 = 0xD, // 760 35 |
randrews33 | 0:1b975a6ae539 | 142 | G_ODR_760_BW_50 = 0xE, // 760 50 |
randrews33 | 0:1b975a6ae539 | 143 | G_ODR_760_BW_100 = 0xF, // 760 100 |
randrews33 | 0:1b975a6ae539 | 144 | }; |
randrews33 | 0:1b975a6ae539 | 145 | // accel_oder defines all possible output data rates of the accelerometer: |
randrews33 | 0:1b975a6ae539 | 146 | enum accel_odr |
randrews33 | 0:1b975a6ae539 | 147 | { |
randrews33 | 0:1b975a6ae539 | 148 | A_POWER_DOWN, // Power-down mode (0x0) |
randrews33 | 0:1b975a6ae539 | 149 | A_ODR_3125, // 3.125 Hz (0x1) |
randrews33 | 0:1b975a6ae539 | 150 | A_ODR_625, // 6.25 Hz (0x2) |
randrews33 | 0:1b975a6ae539 | 151 | A_ODR_125, // 12.5 Hz (0x3) |
randrews33 | 0:1b975a6ae539 | 152 | A_ODR_25, // 25 Hz (0x4) |
randrews33 | 0:1b975a6ae539 | 153 | A_ODR_50, // 50 Hz (0x5) |
randrews33 | 0:1b975a6ae539 | 154 | A_ODR_100, // 100 Hz (0x6) |
randrews33 | 0:1b975a6ae539 | 155 | A_ODR_200, // 200 Hz (0x7) |
randrews33 | 0:1b975a6ae539 | 156 | A_ODR_400, // 400 Hz (0x8) |
randrews33 | 0:1b975a6ae539 | 157 | A_ODR_800, // 800 Hz (9) |
randrews33 | 0:1b975a6ae539 | 158 | A_ODR_1600 // 1600 Hz (0xA) |
randrews33 | 0:1b975a6ae539 | 159 | }; |
randrews33 | 0:1b975a6ae539 | 160 | // accel_oder defines all possible output data rates of the magnetometer: |
randrews33 | 0:1b975a6ae539 | 161 | enum mag_odr |
randrews33 | 0:1b975a6ae539 | 162 | { |
randrews33 | 0:1b975a6ae539 | 163 | M_ODR_3125, // 3.125 Hz (0x00) |
randrews33 | 0:1b975a6ae539 | 164 | M_ODR_625, // 6.25 Hz (0x01) |
randrews33 | 0:1b975a6ae539 | 165 | M_ODR_125, // 12.5 Hz (0x02) |
randrews33 | 0:1b975a6ae539 | 166 | M_ODR_25, // 25 Hz (0x03) |
randrews33 | 0:1b975a6ae539 | 167 | M_ODR_50, // 50 (0x04) |
randrews33 | 0:1b975a6ae539 | 168 | M_ODR_100, // 100 Hz (0x05) |
randrews33 | 0:1b975a6ae539 | 169 | }; |
randrews33 | 0:1b975a6ae539 | 170 | |
randrews33 | 0:1b975a6ae539 | 171 | // We'll store the gyro, accel, and magnetometer readings in a series of |
randrews33 | 0:1b975a6ae539 | 172 | // public class variables. Each sensor gets three variables -- one for each |
randrews33 | 0:1b975a6ae539 | 173 | // axis. Call readGyro(), readAccel(), and readMag() first, before using |
randrews33 | 0:1b975a6ae539 | 174 | // these variables! |
randrews33 | 0:1b975a6ae539 | 175 | // These values are the RAW signed 16-bit readings from the sensors. |
randrews33 | 0:1b975a6ae539 | 176 | int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope |
randrews33 | 0:1b975a6ae539 | 177 | int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer |
randrews33 | 0:1b975a6ae539 | 178 | int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer |
randrews33 | 4:bf8f4e7c9905 | 179 | int16_t temperature; |
randrews33 | 4:bf8f4e7c9905 | 180 | float abias[3]; |
randrews33 | 4:bf8f4e7c9905 | 181 | float gbias[3]; |
randrews33 | 4:bf8f4e7c9905 | 182 | |
randrews33 | 0:1b975a6ae539 | 183 | |
randrews33 | 0:1b975a6ae539 | 184 | // LSM9DS0 -- LSM9DS0 class constructor |
randrews33 | 0:1b975a6ae539 | 185 | // The constructor will set up a handful of private variables, and set the |
randrews33 | 0:1b975a6ae539 | 186 | // communication mode as well. |
randrews33 | 0:1b975a6ae539 | 187 | // Input: |
randrews33 | 0:1b975a6ae539 | 188 | // - interface = Either MODE_SPI or MODE_I2C, whichever you're using |
randrews33 | 0:1b975a6ae539 | 189 | // to talk to the IC. |
randrews33 | 0:1b975a6ae539 | 190 | // - gAddr = If MODE_I2C, this is the I2C address of the gyroscope. |
randrews33 | 0:1b975a6ae539 | 191 | // If MODE_SPI, this is the chip select pin of the gyro (CSG) |
randrews33 | 0:1b975a6ae539 | 192 | // - xmAddr = If MODE_I2C, this is the I2C address of the accel/mag. |
randrews33 | 0:1b975a6ae539 | 193 | // If MODE_SPI, this is the cs pin of the accel/mag (CSXM) |
randrews33 | 0:1b975a6ae539 | 194 | LSM9DS0(PinName sda, PinName scl, uint8_t gAddr, uint8_t xmAddr); |
randrews33 | 0:1b975a6ae539 | 195 | |
randrews33 | 0:1b975a6ae539 | 196 | // begin() -- Initialize the gyro, accelerometer, and magnetometer. |
randrews33 | 0:1b975a6ae539 | 197 | // This will set up the scale and output rate of each sensor. It'll also |
randrews33 | 0:1b975a6ae539 | 198 | // "turn on" every sensor and every axis of every sensor. |
randrews33 | 0:1b975a6ae539 | 199 | // Input: |
randrews33 | 0:1b975a6ae539 | 200 | // - gScl = The scale of the gyroscope. This should be a gyro_scale value. |
randrews33 | 0:1b975a6ae539 | 201 | // - aScl = The scale of the accelerometer. Should be a accel_scale value. |
randrews33 | 0:1b975a6ae539 | 202 | // - mScl = The scale of the magnetometer. Should be a mag_scale value. |
randrews33 | 0:1b975a6ae539 | 203 | // - gODR = Output data rate of the gyroscope. gyro_odr value. |
randrews33 | 0:1b975a6ae539 | 204 | // - aODR = Output data rate of the accelerometer. accel_odr value. |
randrews33 | 0:1b975a6ae539 | 205 | // - mODR = Output data rate of the magnetometer. mag_odr value. |
randrews33 | 0:1b975a6ae539 | 206 | // Output: The function will return an unsigned 16-bit value. The most-sig |
randrews33 | 0:1b975a6ae539 | 207 | // bytes of the output are the WHO_AM_I reading of the accel. The |
randrews33 | 0:1b975a6ae539 | 208 | // least significant two bytes are the WHO_AM_I reading of the gyro. |
randrews33 | 0:1b975a6ae539 | 209 | // All parameters have a defaulted value, so you can call just "begin()". |
randrews33 | 0:1b975a6ae539 | 210 | // Default values are FSR's of: +/- 245DPS, 2g, 2Gs; ODRs of 95 Hz for |
randrews33 | 0:1b975a6ae539 | 211 | // gyro, 100 Hz for accelerometer, 100 Hz for magnetometer. |
randrews33 | 0:1b975a6ae539 | 212 | // Use the return value of this function to verify communication. |
randrews33 | 0:1b975a6ae539 | 213 | uint16_t begin(gyro_scale gScl = G_SCALE_245DPS, |
randrews33 | 0:1b975a6ae539 | 214 | accel_scale aScl = A_SCALE_2G, mag_scale mScl = M_SCALE_2GS, |
randrews33 | 0:1b975a6ae539 | 215 | gyro_odr gODR = G_ODR_95_BW_125, accel_odr aODR = A_ODR_50, |
randrews33 | 0:1b975a6ae539 | 216 | mag_odr mODR = M_ODR_50); |
randrews33 | 0:1b975a6ae539 | 217 | |
randrews33 | 0:1b975a6ae539 | 218 | // readGyro() -- Read the gyroscope output registers. |
randrews33 | 0:1b975a6ae539 | 219 | // This function will read all six gyroscope output registers. |
randrews33 | 0:1b975a6ae539 | 220 | // The readings are stored in the class' gx, gy, and gz variables. Read |
randrews33 | 0:1b975a6ae539 | 221 | // those _after_ calling readGyro(). |
randrews33 | 0:1b975a6ae539 | 222 | void readGyro(); |
randrews33 | 0:1b975a6ae539 | 223 | |
randrews33 | 0:1b975a6ae539 | 224 | // readAccel() -- Read the accelerometer output registers. |
randrews33 | 0:1b975a6ae539 | 225 | // This function will read all six accelerometer output registers. |
randrews33 | 0:1b975a6ae539 | 226 | // The readings are stored in the class' ax, ay, and az variables. Read |
randrews33 | 0:1b975a6ae539 | 227 | // those _after_ calling readAccel(). |
randrews33 | 0:1b975a6ae539 | 228 | void readAccel(); |
randrews33 | 0:1b975a6ae539 | 229 | |
randrews33 | 0:1b975a6ae539 | 230 | // readMag() -- Read the magnetometer output registers. |
randrews33 | 0:1b975a6ae539 | 231 | // This function will read all six magnetometer output registers. |
randrews33 | 0:1b975a6ae539 | 232 | // The readings are stored in the class' mx, my, and mz variables. Read |
randrews33 | 0:1b975a6ae539 | 233 | // those _after_ calling readMag(). |
randrews33 | 0:1b975a6ae539 | 234 | void readMag(); |
randrews33 | 0:1b975a6ae539 | 235 | |
randrews33 | 4:bf8f4e7c9905 | 236 | // readTemp() -- Read the temperature output register. |
randrews33 | 4:bf8f4e7c9905 | 237 | // This function will read two temperature output registers. |
randrews33 | 4:bf8f4e7c9905 | 238 | // The combined readings are stored in the class' temperature variables. Read |
randrews33 | 4:bf8f4e7c9905 | 239 | // those _after_ calling readTemp(). |
randrews33 | 4:bf8f4e7c9905 | 240 | void readTemp(); |
randrews33 | 4:bf8f4e7c9905 | 241 | |
randrews33 | 0:1b975a6ae539 | 242 | // calcGyro() -- Convert from RAW signed 16-bit value to degrees per second |
randrews33 | 0:1b975a6ae539 | 243 | // This function reads in a signed 16-bit value and returns the scaled |
randrews33 | 0:1b975a6ae539 | 244 | // DPS. This function relies on gScale and gRes being correct. |
randrews33 | 0:1b975a6ae539 | 245 | // Input: |
randrews33 | 0:1b975a6ae539 | 246 | // - gyro = A signed 16-bit raw reading from the gyroscope. |
randrews33 | 0:1b975a6ae539 | 247 | float calcGyro(int16_t gyro); |
randrews33 | 0:1b975a6ae539 | 248 | |
randrews33 | 0:1b975a6ae539 | 249 | // calcAccel() -- Convert from RAW signed 16-bit value to gravity (g's). |
randrews33 | 0:1b975a6ae539 | 250 | // This function reads in a signed 16-bit value and returns the scaled |
randrews33 | 0:1b975a6ae539 | 251 | // g's. This function relies on aScale and aRes being correct. |
randrews33 | 0:1b975a6ae539 | 252 | // Input: |
randrews33 | 0:1b975a6ae539 | 253 | // - accel = A signed 16-bit raw reading from the accelerometer. |
randrews33 | 0:1b975a6ae539 | 254 | float calcAccel(int16_t accel); |
randrews33 | 0:1b975a6ae539 | 255 | |
randrews33 | 0:1b975a6ae539 | 256 | // calcMag() -- Convert from RAW signed 16-bit value to Gauss (Gs) |
randrews33 | 0:1b975a6ae539 | 257 | // This function reads in a signed 16-bit value and returns the scaled |
randrews33 | 0:1b975a6ae539 | 258 | // Gs. This function relies on mScale and mRes being correct. |
randrews33 | 0:1b975a6ae539 | 259 | // Input: |
randrews33 | 0:1b975a6ae539 | 260 | // - mag = A signed 16-bit raw reading from the magnetometer. |
randrews33 | 0:1b975a6ae539 | 261 | float calcMag(int16_t mag); |
randrews33 | 0:1b975a6ae539 | 262 | |
randrews33 | 0:1b975a6ae539 | 263 | // setGyroScale() -- Set the full-scale range of the gyroscope. |
randrews33 | 0:1b975a6ae539 | 264 | // This function can be called to set the scale of the gyroscope to |
randrews33 | 0:1b975a6ae539 | 265 | // 245, 500, or 200 degrees per second. |
randrews33 | 0:1b975a6ae539 | 266 | // Input: |
randrews33 | 0:1b975a6ae539 | 267 | // - gScl = The desired gyroscope scale. Must be one of three possible |
randrews33 | 0:1b975a6ae539 | 268 | // values from the gyro_scale enum. |
randrews33 | 0:1b975a6ae539 | 269 | void setGyroScale(gyro_scale gScl); |
randrews33 | 0:1b975a6ae539 | 270 | |
randrews33 | 0:1b975a6ae539 | 271 | // setAccelScale() -- Set the full-scale range of the accelerometer. |
randrews33 | 0:1b975a6ae539 | 272 | // This function can be called to set the scale of the accelerometer to |
randrews33 | 0:1b975a6ae539 | 273 | // 2, 4, 6, 8, or 16 g's. |
randrews33 | 0:1b975a6ae539 | 274 | // Input: |
randrews33 | 0:1b975a6ae539 | 275 | // - aScl = The desired accelerometer scale. Must be one of five possible |
randrews33 | 0:1b975a6ae539 | 276 | // values from the accel_scale enum. |
randrews33 | 0:1b975a6ae539 | 277 | void setAccelScale(accel_scale aScl); |
randrews33 | 0:1b975a6ae539 | 278 | |
randrews33 | 0:1b975a6ae539 | 279 | // setMagScale() -- Set the full-scale range of the magnetometer. |
randrews33 | 0:1b975a6ae539 | 280 | // This function can be called to set the scale of the magnetometer to |
randrews33 | 0:1b975a6ae539 | 281 | // 2, 4, 8, or 12 Gs. |
randrews33 | 0:1b975a6ae539 | 282 | // Input: |
randrews33 | 0:1b975a6ae539 | 283 | // - mScl = The desired magnetometer scale. Must be one of four possible |
randrews33 | 0:1b975a6ae539 | 284 | // values from the mag_scale enum. |
randrews33 | 0:1b975a6ae539 | 285 | void setMagScale(mag_scale mScl); |
randrews33 | 0:1b975a6ae539 | 286 | |
randrews33 | 0:1b975a6ae539 | 287 | // setGyroODR() -- Set the output data rate and bandwidth of the gyroscope |
randrews33 | 0:1b975a6ae539 | 288 | // Input: |
randrews33 | 0:1b975a6ae539 | 289 | // - gRate = The desired output rate and cutoff frequency of the gyro. |
randrews33 | 0:1b975a6ae539 | 290 | // Must be a value from the gyro_odr enum (check above, there're 14). |
randrews33 | 0:1b975a6ae539 | 291 | void setGyroODR(gyro_odr gRate); |
randrews33 | 0:1b975a6ae539 | 292 | |
randrews33 | 0:1b975a6ae539 | 293 | // setAccelODR() -- Set the output data rate of the accelerometer |
randrews33 | 0:1b975a6ae539 | 294 | // Input: |
randrews33 | 0:1b975a6ae539 | 295 | // - aRate = The desired output rate of the accel. |
randrews33 | 0:1b975a6ae539 | 296 | // Must be a value from the accel_odr enum (check above, there're 11). |
randrews33 | 0:1b975a6ae539 | 297 | void setAccelODR(accel_odr aRate); |
randrews33 | 0:1b975a6ae539 | 298 | |
randrews33 | 0:1b975a6ae539 | 299 | // setMagODR() -- Set the output data rate of the magnetometer |
randrews33 | 0:1b975a6ae539 | 300 | // Input: |
randrews33 | 0:1b975a6ae539 | 301 | // - mRate = The desired output rate of the mag. |
randrews33 | 0:1b975a6ae539 | 302 | // Must be a value from the mag_odr enum (check above, there're 6). |
randrews33 | 0:1b975a6ae539 | 303 | void setMagODR(mag_odr mRate); |
randrews33 | 0:1b975a6ae539 | 304 | |
randrews33 | 0:1b975a6ae539 | 305 | // configGyroInt() -- Configure the gyro interrupt output. |
randrews33 | 0:1b975a6ae539 | 306 | // Triggers can be set to either rising above or falling below a specified |
randrews33 | 0:1b975a6ae539 | 307 | // threshold. This function helps setup the interrupt configuration and |
randrews33 | 0:1b975a6ae539 | 308 | // threshold values for all axes. |
randrews33 | 0:1b975a6ae539 | 309 | // Input: |
randrews33 | 0:1b975a6ae539 | 310 | // - int1Cfg = A 8-bit value that is sent directly to the INT1_CFG_G |
randrews33 | 0:1b975a6ae539 | 311 | // register. This sets AND/OR and high/low interrupt gen for each axis |
randrews33 | 0:1b975a6ae539 | 312 | // - int1ThsX = 16-bit interrupt threshold value for x-axis |
randrews33 | 0:1b975a6ae539 | 313 | // - int1ThsY = 16-bit interrupt threshold value for y-axis |
randrews33 | 0:1b975a6ae539 | 314 | // - int1ThsZ = 16-bit interrupt threshold value for z-axis |
randrews33 | 0:1b975a6ae539 | 315 | // - duration = Duration an interrupt holds after triggered. This value |
randrews33 | 0:1b975a6ae539 | 316 | // is copied directly into the INT1_DURATION_G register. |
randrews33 | 0:1b975a6ae539 | 317 | // Before using this function, read about the INT1_CFG_G register and |
randrews33 | 0:1b975a6ae539 | 318 | // the related INT1* registers in the LMS9DS0 datasheet. |
randrews33 | 0:1b975a6ae539 | 319 | void configGyroInt(uint8_t int1Cfg, uint16_t int1ThsX = 0, |
randrews33 | 0:1b975a6ae539 | 320 | uint16_t int1ThsY = 0, uint16_t int1ThsZ = 0, |
randrews33 | 0:1b975a6ae539 | 321 | uint8_t duration = 0); |
randrews33 | 4:bf8f4e7c9905 | 322 | |
randrews33 | 4:bf8f4e7c9905 | 323 | void calLSM9DS0(float gbias[3], float abias[3]); |
randrews33 | 4:bf8f4e7c9905 | 324 | |
randrews33 | 0:1b975a6ae539 | 325 | |
randrews33 | 0:1b975a6ae539 | 326 | private: |
randrews33 | 4:bf8f4e7c9905 | 327 | // xmAddress and gAddress store the I2C address |
randrews33 | 0:1b975a6ae539 | 328 | // for each sensor. |
randrews33 | 0:1b975a6ae539 | 329 | uint8_t xmAddress, gAddress; |
randrews33 | 0:1b975a6ae539 | 330 | |
randrews33 | 0:1b975a6ae539 | 331 | // gScale, aScale, and mScale store the current scale range for each |
randrews33 | 0:1b975a6ae539 | 332 | // sensor. Should be updated whenever that value changes. |
randrews33 | 0:1b975a6ae539 | 333 | gyro_scale gScale; |
randrews33 | 0:1b975a6ae539 | 334 | accel_scale aScale; |
randrews33 | 0:1b975a6ae539 | 335 | mag_scale mScale; |
randrews33 | 0:1b975a6ae539 | 336 | |
randrews33 | 0:1b975a6ae539 | 337 | // gRes, aRes, and mRes store the current resolution for each sensor. |
randrews33 | 0:1b975a6ae539 | 338 | // Units of these values would be DPS (or g's or Gs's) per ADC tick. |
randrews33 | 0:1b975a6ae539 | 339 | // This value is calculated as (sensor scale) / (2^15). |
randrews33 | 0:1b975a6ae539 | 340 | float gRes, aRes, mRes; |
randrews33 | 0:1b975a6ae539 | 341 | |
randrews33 | 0:1b975a6ae539 | 342 | // initGyro() -- Sets up the gyroscope to begin reading. |
randrews33 | 0:1b975a6ae539 | 343 | // This function steps through all five gyroscope control registers. |
randrews33 | 0:1b975a6ae539 | 344 | // Upon exit, the following parameters will be set: |
randrews33 | 0:1b975a6ae539 | 345 | // - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled. |
randrews33 | 0:1b975a6ae539 | 346 | // 95 Hz ODR, 12.5 Hz cutoff frequency. |
randrews33 | 0:1b975a6ae539 | 347 | // - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency |
randrews33 | 0:1b975a6ae539 | 348 | // set to 7.2 Hz (depends on ODR). |
randrews33 | 0:1b975a6ae539 | 349 | // - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and |
randrews33 | 0:1b975a6ae539 | 350 | // active high). Data-ready output enabled on DRDY_G. |
randrews33 | 0:1b975a6ae539 | 351 | // - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower |
randrews33 | 0:1b975a6ae539 | 352 | // address. Scale set to 245 DPS. SPI mode set to 4-wire. |
randrews33 | 0:1b975a6ae539 | 353 | // - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled. |
randrews33 | 0:1b975a6ae539 | 354 | void initGyro(); |
randrews33 | 0:1b975a6ae539 | 355 | |
randrews33 | 0:1b975a6ae539 | 356 | // initAccel() -- Sets up the accelerometer to begin reading. |
randrews33 | 0:1b975a6ae539 | 357 | // This function steps through all accelerometer related control registers. |
randrews33 | 0:1b975a6ae539 | 358 | // Upon exit these registers will be set as: |
randrews33 | 0:1b975a6ae539 | 359 | // - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode. |
randrews33 | 0:1b975a6ae539 | 360 | // - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update. |
randrews33 | 0:1b975a6ae539 | 361 | // all axes enabled. |
randrews33 | 0:1b975a6ae539 | 362 | // - CTRL_REG2_XM = 0x00: +/- 2g scale. 773 Hz anti-alias filter BW. |
randrews33 | 0:1b975a6ae539 | 363 | // - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin. |
randrews33 | 0:1b975a6ae539 | 364 | void initAccel(); |
randrews33 | 0:1b975a6ae539 | 365 | |
randrews33 | 0:1b975a6ae539 | 366 | // initMag() -- Sets up the magnetometer to begin reading. |
randrews33 | 0:1b975a6ae539 | 367 | // This function steps through all magnetometer-related control registers. |
randrews33 | 0:1b975a6ae539 | 368 | // Upon exit these registers will be set as: |
randrews33 | 0:1b975a6ae539 | 369 | // - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin. |
randrews33 | 0:1b975a6ae539 | 370 | // - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt |
randrews33 | 0:1b975a6ae539 | 371 | // requests don't latch. Temperature sensor disabled. |
randrews33 | 0:1b975a6ae539 | 372 | // - CTRL_REG6_XM = 0x00: +/- 2 Gs scale. |
randrews33 | 0:1b975a6ae539 | 373 | // - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode. |
randrews33 | 0:1b975a6ae539 | 374 | // - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts. |
randrews33 | 0:1b975a6ae539 | 375 | void initMag(); |
randrews33 | 0:1b975a6ae539 | 376 | |
randrews33 | 0:1b975a6ae539 | 377 | // gReadByte() -- Reads a byte from a specified gyroscope register. |
randrews33 | 0:1b975a6ae539 | 378 | // Input: |
randrews33 | 0:1b975a6ae539 | 379 | // - subAddress = Register to be read from. |
randrews33 | 0:1b975a6ae539 | 380 | // Output: |
randrews33 | 0:1b975a6ae539 | 381 | // - An 8-bit value read from the requested address. |
randrews33 | 0:1b975a6ae539 | 382 | uint8_t gReadByte(uint8_t subAddress); |
randrews33 | 0:1b975a6ae539 | 383 | |
randrews33 | 0:1b975a6ae539 | 384 | // gReadBytes() -- Reads a number of bytes -- beginning at an address |
randrews33 | 0:1b975a6ae539 | 385 | // and incrementing from there -- from the gyroscope. |
randrews33 | 0:1b975a6ae539 | 386 | // Input: |
randrews33 | 0:1b975a6ae539 | 387 | // - subAddress = Register to be read from. |
randrews33 | 0:1b975a6ae539 | 388 | // - * dest = A pointer to an array of uint8_t's. Values read will be |
randrews33 | 0:1b975a6ae539 | 389 | // stored in here on return. |
randrews33 | 0:1b975a6ae539 | 390 | // - count = The number of bytes to be read. |
randrews33 | 0:1b975a6ae539 | 391 | // Output: No value is returned, but the `dest` array will store |
randrews33 | 0:1b975a6ae539 | 392 | // the data read upon exit. |
randrews33 | 0:1b975a6ae539 | 393 | void gReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count); |
randrews33 | 0:1b975a6ae539 | 394 | |
randrews33 | 0:1b975a6ae539 | 395 | // gWriteByte() -- Write a byte to a register in the gyroscope. |
randrews33 | 0:1b975a6ae539 | 396 | // Input: |
randrews33 | 0:1b975a6ae539 | 397 | // - subAddress = Register to be written to. |
randrews33 | 0:1b975a6ae539 | 398 | // - data = data to be written to the register. |
randrews33 | 0:1b975a6ae539 | 399 | void gWriteByte(uint8_t subAddress, uint8_t data); |
randrews33 | 0:1b975a6ae539 | 400 | |
randrews33 | 0:1b975a6ae539 | 401 | // xmReadByte() -- Read a byte from a register in the accel/mag sensor |
randrews33 | 0:1b975a6ae539 | 402 | // Input: |
randrews33 | 0:1b975a6ae539 | 403 | // - subAddress = Register to be read from. |
randrews33 | 0:1b975a6ae539 | 404 | // Output: |
randrews33 | 0:1b975a6ae539 | 405 | // - An 8-bit value read from the requested register. |
randrews33 | 0:1b975a6ae539 | 406 | uint8_t xmReadByte(uint8_t subAddress); |
randrews33 | 0:1b975a6ae539 | 407 | |
randrews33 | 0:1b975a6ae539 | 408 | // xmReadBytes() -- Reads a number of bytes -- beginning at an address |
randrews33 | 0:1b975a6ae539 | 409 | // and incrementing from there -- from the accelerometer/magnetometer. |
randrews33 | 0:1b975a6ae539 | 410 | // Input: |
randrews33 | 0:1b975a6ae539 | 411 | // - subAddress = Register to be read from. |
randrews33 | 0:1b975a6ae539 | 412 | // - * dest = A pointer to an array of uint8_t's. Values read will be |
randrews33 | 0:1b975a6ae539 | 413 | // stored in here on return. |
randrews33 | 0:1b975a6ae539 | 414 | // - count = The number of bytes to be read. |
randrews33 | 0:1b975a6ae539 | 415 | // Output: No value is returned, but the `dest` array will store |
randrews33 | 0:1b975a6ae539 | 416 | // the data read upon exit. |
randrews33 | 0:1b975a6ae539 | 417 | void xmReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count); |
randrews33 | 0:1b975a6ae539 | 418 | |
randrews33 | 0:1b975a6ae539 | 419 | // xmWriteByte() -- Write a byte to a register in the accel/mag sensor. |
randrews33 | 0:1b975a6ae539 | 420 | // Input: |
randrews33 | 0:1b975a6ae539 | 421 | // - subAddress = Register to be written to. |
randrews33 | 0:1b975a6ae539 | 422 | // - data = data to be written to the register. |
randrews33 | 0:1b975a6ae539 | 423 | void xmWriteByte(uint8_t subAddress, uint8_t data); |
randrews33 | 0:1b975a6ae539 | 424 | |
randrews33 | 0:1b975a6ae539 | 425 | // calcgRes() -- Calculate the resolution of the gyroscope. |
randrews33 | 0:1b975a6ae539 | 426 | // This function will set the value of the gRes variable. gScale must |
randrews33 | 0:1b975a6ae539 | 427 | // be set prior to calling this function. |
randrews33 | 0:1b975a6ae539 | 428 | void calcgRes(); |
randrews33 | 0:1b975a6ae539 | 429 | |
randrews33 | 0:1b975a6ae539 | 430 | // calcmRes() -- Calculate the resolution of the magnetometer. |
randrews33 | 0:1b975a6ae539 | 431 | // This function will set the value of the mRes variable. mScale must |
randrews33 | 0:1b975a6ae539 | 432 | // be set prior to calling this function. |
randrews33 | 0:1b975a6ae539 | 433 | void calcmRes(); |
randrews33 | 0:1b975a6ae539 | 434 | |
randrews33 | 0:1b975a6ae539 | 435 | // calcaRes() -- Calculate the resolution of the accelerometer. |
randrews33 | 0:1b975a6ae539 | 436 | // This function will set the value of the aRes variable. aScale must |
randrews33 | 0:1b975a6ae539 | 437 | // be set prior to calling this function. |
randrews33 | 0:1b975a6ae539 | 438 | void calcaRes(); |
randrews33 | 0:1b975a6ae539 | 439 | |
randrews33 | 0:1b975a6ae539 | 440 | |
randrews33 | 0:1b975a6ae539 | 441 | /////////////////// |
randrews33 | 0:1b975a6ae539 | 442 | // I2C Functions // |
randrews33 | 0:1b975a6ae539 | 443 | /////////////////// |
randrews33 | 4:bf8f4e7c9905 | 444 | I2Cdev* i2c_; |
randrews33 | 0:1b975a6ae539 | 445 | |
randrews33 | 0:1b975a6ae539 | 446 | |
randrews33 | 0:1b975a6ae539 | 447 | // I2CwriteByte() -- Write a byte out of I2C to a register in the device |
randrews33 | 0:1b975a6ae539 | 448 | // Input: |
randrews33 | 0:1b975a6ae539 | 449 | // - address = The 7-bit I2C address of the slave device. |
randrews33 | 0:1b975a6ae539 | 450 | // - subAddress = The register to be written to. |
randrews33 | 0:1b975a6ae539 | 451 | // - data = Byte to be written to the register. |
randrews33 | 0:1b975a6ae539 | 452 | void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data); |
randrews33 | 0:1b975a6ae539 | 453 | |
randrews33 | 0:1b975a6ae539 | 454 | // I2CreadByte() -- Read a single byte from a register over I2C. |
randrews33 | 0:1b975a6ae539 | 455 | // Input: |
randrews33 | 0:1b975a6ae539 | 456 | // - address = The 7-bit I2C address of the slave device. |
randrews33 | 0:1b975a6ae539 | 457 | // - subAddress = The register to be read from. |
randrews33 | 0:1b975a6ae539 | 458 | // Output: |
randrews33 | 0:1b975a6ae539 | 459 | // - The byte read from the requested address. |
randrews33 | 0:1b975a6ae539 | 460 | uint8_t I2CreadByte(uint8_t address, uint8_t subAddress); |
randrews33 | 0:1b975a6ae539 | 461 | |
randrews33 | 0:1b975a6ae539 | 462 | // I2CreadBytes() -- Read a series of bytes, starting at a register via SPI |
randrews33 | 0:1b975a6ae539 | 463 | // Input: |
randrews33 | 0:1b975a6ae539 | 464 | // - address = The 7-bit I2C address of the slave device. |
randrews33 | 0:1b975a6ae539 | 465 | // - subAddress = The register to begin reading. |
randrews33 | 0:1b975a6ae539 | 466 | // - * dest = Pointer to an array where we'll store the readings. |
randrews33 | 0:1b975a6ae539 | 467 | // - count = Number of registers to be read. |
randrews33 | 0:1b975a6ae539 | 468 | // Output: No value is returned by the function, but the registers read are |
randrews33 | 0:1b975a6ae539 | 469 | // all stored in the *dest array given. |
randrews33 | 0:1b975a6ae539 | 470 | void I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count); |
randrews33 | 0:1b975a6ae539 | 471 | }; |
randrews33 | 0:1b975a6ae539 | 472 | |
randrews33 | 0:1b975a6ae539 | 473 | #endif // _LSM9DS0_H // |