jm6wud
LSM9DS1.h@0:67431d0a9143, 2021-01-03 (annotated)
- Committer:
- jm6wud
- Date:
- Sun Jan 03 15:48:10 2021 +0000
- Revision:
- 0:67431d0a9143
jm6wud
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jm6wud | 0:67431d0a9143 | 1 | // Most of the Credit goes to jimblom |
jm6wud | 0:67431d0a9143 | 2 | // Modifications by Allen Wild |
jm6wud | 0:67431d0a9143 | 3 | #ifndef _LSM9DS1_H__ |
jm6wud | 0:67431d0a9143 | 4 | #define _LSM9DS1_H__ |
jm6wud | 0:67431d0a9143 | 5 | |
jm6wud | 0:67431d0a9143 | 6 | #include "mbed.h" |
jm6wud | 0:67431d0a9143 | 7 | |
jm6wud | 0:67431d0a9143 | 8 | ///////////////////////////////////////// |
jm6wud | 0:67431d0a9143 | 9 | // LSM9DS1 Accel/Gyro (XL/G) Registers // |
jm6wud | 0:67431d0a9143 | 10 | ///////////////////////////////////////// |
jm6wud | 0:67431d0a9143 | 11 | #define ACT_THS 0x04 |
jm6wud | 0:67431d0a9143 | 12 | #define ACT_DUR 0x05 |
jm6wud | 0:67431d0a9143 | 13 | #define INT_GEN_CFG_XL 0x06 |
jm6wud | 0:67431d0a9143 | 14 | #define INT_GEN_THS_X_XL 0x07 |
jm6wud | 0:67431d0a9143 | 15 | #define INT_GEN_THS_Y_XL 0x08 |
jm6wud | 0:67431d0a9143 | 16 | #define INT_GEN_THS_Z_XL 0x09 |
jm6wud | 0:67431d0a9143 | 17 | #define INT_GEN_DUR_XL 0x0A |
jm6wud | 0:67431d0a9143 | 18 | #define REFERENCE_G 0x0B |
jm6wud | 0:67431d0a9143 | 19 | #define INT1_CTRL 0x0C |
jm6wud | 0:67431d0a9143 | 20 | #define INT2_CTRL 0x0D |
jm6wud | 0:67431d0a9143 | 21 | #define WHO_AM_I_XG 0x0F |
jm6wud | 0:67431d0a9143 | 22 | #define CTRL_REG1_G 0x10 |
jm6wud | 0:67431d0a9143 | 23 | #define CTRL_REG2_G 0x11 |
jm6wud | 0:67431d0a9143 | 24 | #define CTRL_REG3_G 0x12 |
jm6wud | 0:67431d0a9143 | 25 | #define ORIENT_CFG_G 0x13 |
jm6wud | 0:67431d0a9143 | 26 | #define INT_GEN_SRC_G 0x14 |
jm6wud | 0:67431d0a9143 | 27 | #define OUT_TEMP_L 0x15 |
jm6wud | 0:67431d0a9143 | 28 | #define OUT_TEMP_H 0x16 |
jm6wud | 0:67431d0a9143 | 29 | #define STATUS_REG_0 0x17 |
jm6wud | 0:67431d0a9143 | 30 | #define OUT_X_L_G 0x18 |
jm6wud | 0:67431d0a9143 | 31 | #define OUT_X_H_G 0x19 |
jm6wud | 0:67431d0a9143 | 32 | #define OUT_Y_L_G 0x1A |
jm6wud | 0:67431d0a9143 | 33 | #define OUT_Y_H_G 0x1B |
jm6wud | 0:67431d0a9143 | 34 | #define OUT_Z_L_G 0x1C |
jm6wud | 0:67431d0a9143 | 35 | #define OUT_Z_H_G 0x1D |
jm6wud | 0:67431d0a9143 | 36 | #define CTRL_REG4 0x1E |
jm6wud | 0:67431d0a9143 | 37 | #define CTRL_REG5_XL 0x1F |
jm6wud | 0:67431d0a9143 | 38 | #define CTRL_REG6_XL 0x20 |
jm6wud | 0:67431d0a9143 | 39 | #define CTRL_REG7_XL 0x21 |
jm6wud | 0:67431d0a9143 | 40 | #define CTRL_REG8 0x22 |
jm6wud | 0:67431d0a9143 | 41 | #define CTRL_REG9 0x23 |
jm6wud | 0:67431d0a9143 | 42 | #define CTRL_REG10 0x24 |
jm6wud | 0:67431d0a9143 | 43 | #define INT_GEN_SRC_XL 0x26 |
jm6wud | 0:67431d0a9143 | 44 | #define STATUS_REG_1 0x27 |
jm6wud | 0:67431d0a9143 | 45 | #define OUT_X_L_XL 0x28 |
jm6wud | 0:67431d0a9143 | 46 | #define OUT_X_H_XL 0x29 |
jm6wud | 0:67431d0a9143 | 47 | #define OUT_Y_L_XL 0x2A |
jm6wud | 0:67431d0a9143 | 48 | #define OUT_Y_H_XL 0x2B |
jm6wud | 0:67431d0a9143 | 49 | #define OUT_Z_L_XL 0x2C |
jm6wud | 0:67431d0a9143 | 50 | #define OUT_Z_H_XL 0x2D |
jm6wud | 0:67431d0a9143 | 51 | #define FIFO_CTRL 0x2E |
jm6wud | 0:67431d0a9143 | 52 | #define FIFO_SRC 0x2F |
jm6wud | 0:67431d0a9143 | 53 | #define INT_GEN_CFG_G 0x30 |
jm6wud | 0:67431d0a9143 | 54 | #define INT_GEN_THS_XH_G 0x31 |
jm6wud | 0:67431d0a9143 | 55 | #define INT_GEN_THS_XL_G 0x32 |
jm6wud | 0:67431d0a9143 | 56 | #define INT_GEN_THS_YH_G 0x33 |
jm6wud | 0:67431d0a9143 | 57 | #define INT_GEN_THS_YL_G 0x34 |
jm6wud | 0:67431d0a9143 | 58 | #define INT_GEN_THS_ZH_G 0x35 |
jm6wud | 0:67431d0a9143 | 59 | #define INT_GEN_THS_ZL_G 0x36 |
jm6wud | 0:67431d0a9143 | 60 | #define INT_GEN_DUR_G 0x37 |
jm6wud | 0:67431d0a9143 | 61 | |
jm6wud | 0:67431d0a9143 | 62 | /////////////////////////////// |
jm6wud | 0:67431d0a9143 | 63 | // LSM9DS1 Magneto Registers // |
jm6wud | 0:67431d0a9143 | 64 | /////////////////////////////// |
jm6wud | 0:67431d0a9143 | 65 | #define OFFSET_X_REG_L_M 0x05 |
jm6wud | 0:67431d0a9143 | 66 | #define OFFSET_X_REG_H_M 0x06 |
jm6wud | 0:67431d0a9143 | 67 | #define OFFSET_Y_REG_L_M 0x07 |
jm6wud | 0:67431d0a9143 | 68 | #define OFFSET_Y_REG_H_M 0x08 |
jm6wud | 0:67431d0a9143 | 69 | #define OFFSET_Z_REG_L_M 0x09 |
jm6wud | 0:67431d0a9143 | 70 | #define OFFSET_Z_REG_H_M 0x0A |
jm6wud | 0:67431d0a9143 | 71 | #define WHO_AM_I_M 0x0F |
jm6wud | 0:67431d0a9143 | 72 | #define CTRL_REG1_M 0x20 |
jm6wud | 0:67431d0a9143 | 73 | #define CTRL_REG2_M 0x21 |
jm6wud | 0:67431d0a9143 | 74 | #define CTRL_REG3_M 0x22 |
jm6wud | 0:67431d0a9143 | 75 | #define CTRL_REG4_M 0x23 |
jm6wud | 0:67431d0a9143 | 76 | #define CTRL_REG5_M 0x24 |
jm6wud | 0:67431d0a9143 | 77 | #define STATUS_REG_M 0x27 |
jm6wud | 0:67431d0a9143 | 78 | #define OUT_X_L_M 0x28 |
jm6wud | 0:67431d0a9143 | 79 | #define OUT_X_H_M 0x29 |
jm6wud | 0:67431d0a9143 | 80 | #define OUT_Y_L_M 0x2A |
jm6wud | 0:67431d0a9143 | 81 | #define OUT_Y_H_M 0x2B |
jm6wud | 0:67431d0a9143 | 82 | #define OUT_Z_L_M 0x2C |
jm6wud | 0:67431d0a9143 | 83 | #define OUT_Z_H_M 0x2D |
jm6wud | 0:67431d0a9143 | 84 | #define INT_CFG_M 0x30 |
jm6wud | 0:67431d0a9143 | 85 | #define INT_SRC_M 0x30 |
jm6wud | 0:67431d0a9143 | 86 | #define INT_THS_L_M 0x32 |
jm6wud | 0:67431d0a9143 | 87 | #define INT_THS_H_M 0x33 |
jm6wud | 0:67431d0a9143 | 88 | |
jm6wud | 0:67431d0a9143 | 89 | //////////////////////////////// |
jm6wud | 0:67431d0a9143 | 90 | // LSM9DS1 WHO_AM_I Responses // |
jm6wud | 0:67431d0a9143 | 91 | //////////////////////////////// |
jm6wud | 0:67431d0a9143 | 92 | #define WHO_AM_I_AG_RSP 0x68 |
jm6wud | 0:67431d0a9143 | 93 | #define WHO_AM_I_M_RSP 0x3D |
jm6wud | 0:67431d0a9143 | 94 | |
jm6wud | 0:67431d0a9143 | 95 | // Possible I2C addresses for the accel/gyro and mag |
jm6wud | 0:67431d0a9143 | 96 | #define LSM9DS1_AG_I2C_ADDR(sa0) ((sa0) ? 0xD6 : 0xD4) |
jm6wud | 0:67431d0a9143 | 97 | #define LSM9DS1_M_I2C_ADDR(sa1) ((sa1) ? 0x3C : 0x38) |
jm6wud | 0:67431d0a9143 | 98 | |
jm6wud | 0:67431d0a9143 | 99 | /** |
jm6wud | 0:67431d0a9143 | 100 | * LSM9DS1 Class - driver for the 9 DoF IMU |
jm6wud | 0:67431d0a9143 | 101 | */ |
jm6wud | 0:67431d0a9143 | 102 | class LSM9DS1 |
jm6wud | 0:67431d0a9143 | 103 | { |
jm6wud | 0:67431d0a9143 | 104 | public: |
jm6wud | 0:67431d0a9143 | 105 | |
jm6wud | 0:67431d0a9143 | 106 | /// gyro_scale defines the possible full-scale ranges of the gyroscope: |
jm6wud | 0:67431d0a9143 | 107 | enum gyro_scale |
jm6wud | 0:67431d0a9143 | 108 | { |
jm6wud | 0:67431d0a9143 | 109 | G_SCALE_245DPS = 0x0 << 3, // 00 << 3: +/- 245 degrees per second |
jm6wud | 0:67431d0a9143 | 110 | G_SCALE_500DPS = 0x1 << 3, // 01 << 3: +/- 500 dps |
jm6wud | 0:67431d0a9143 | 111 | G_SCALE_2000DPS = 0x3 << 3 // 11 << 3: +/- 2000 dps |
jm6wud | 0:67431d0a9143 | 112 | }; |
jm6wud | 0:67431d0a9143 | 113 | |
jm6wud | 0:67431d0a9143 | 114 | /// gyro_odr defines all possible data rate/bandwidth combos of the gyro: |
jm6wud | 0:67431d0a9143 | 115 | enum gyro_odr |
jm6wud | 0:67431d0a9143 | 116 | { // ODR (Hz) --- Cutoff |
jm6wud | 0:67431d0a9143 | 117 | G_POWER_DOWN = 0x00, // 0 0 |
jm6wud | 0:67431d0a9143 | 118 | G_ODR_15_BW_0 = 0x20, // 14.9 0 |
jm6wud | 0:67431d0a9143 | 119 | G_ODR_60_BW_16 = 0x40, // 59.5 16 |
jm6wud | 0:67431d0a9143 | 120 | G_ODR_119_BW_14 = 0x60, // 119 14 |
jm6wud | 0:67431d0a9143 | 121 | G_ODR_119_BW_31 = 0x61, // 119 31 |
jm6wud | 0:67431d0a9143 | 122 | G_ODR_238_BW_14 = 0x80, // 238 14 |
jm6wud | 0:67431d0a9143 | 123 | G_ODR_238_BW_29 = 0x81, // 238 29 |
jm6wud | 0:67431d0a9143 | 124 | G_ODR_238_BW_63 = 0x82, // 238 63 |
jm6wud | 0:67431d0a9143 | 125 | G_ODR_238_BW_78 = 0x83, // 238 78 |
jm6wud | 0:67431d0a9143 | 126 | G_ODR_476_BW_21 = 0xA0, // 476 21 |
jm6wud | 0:67431d0a9143 | 127 | G_ODR_476_BW_28 = 0xA1, // 476 28 |
jm6wud | 0:67431d0a9143 | 128 | G_ODR_476_BW_57 = 0xA2, // 476 57 |
jm6wud | 0:67431d0a9143 | 129 | G_ODR_476_BW_100 = 0xA3, // 476 100 |
jm6wud | 0:67431d0a9143 | 130 | G_ODR_952_BW_33 = 0xC0, // 952 33 |
jm6wud | 0:67431d0a9143 | 131 | G_ODR_952_BW_40 = 0xC1, // 952 40 |
jm6wud | 0:67431d0a9143 | 132 | G_ODR_952_BW_58 = 0xC2, // 952 58 |
jm6wud | 0:67431d0a9143 | 133 | G_ODR_952_BW_100 = 0xC3 // 952 100 |
jm6wud | 0:67431d0a9143 | 134 | }; |
jm6wud | 0:67431d0a9143 | 135 | |
jm6wud | 0:67431d0a9143 | 136 | /// accel_scale defines all possible FSR's of the accelerometer: |
jm6wud | 0:67431d0a9143 | 137 | enum accel_scale |
jm6wud | 0:67431d0a9143 | 138 | { |
jm6wud | 0:67431d0a9143 | 139 | A_SCALE_2G, // 00: +/- 2g |
jm6wud | 0:67431d0a9143 | 140 | A_SCALE_16G,// 01: +/- 16g |
jm6wud | 0:67431d0a9143 | 141 | A_SCALE_4G, // 10: +/- 4g |
jm6wud | 0:67431d0a9143 | 142 | A_SCALE_8G // 11: +/- 8g |
jm6wud | 0:67431d0a9143 | 143 | }; |
jm6wud | 0:67431d0a9143 | 144 | |
jm6wud | 0:67431d0a9143 | 145 | /// accel_oder defines all possible output data rates of the accelerometer: |
jm6wud | 0:67431d0a9143 | 146 | enum accel_odr |
jm6wud | 0:67431d0a9143 | 147 | { |
jm6wud | 0:67431d0a9143 | 148 | A_POWER_DOWN, // Power-down mode (0x0) |
jm6wud | 0:67431d0a9143 | 149 | A_ODR_10, // 10 Hz (0x1) |
jm6wud | 0:67431d0a9143 | 150 | A_ODR_50, // 50 Hz (0x2) |
jm6wud | 0:67431d0a9143 | 151 | A_ODR_119, // 119 Hz (0x3) |
jm6wud | 0:67431d0a9143 | 152 | A_ODR_238, // 238 Hz (0x4) |
jm6wud | 0:67431d0a9143 | 153 | A_ODR_476, // 476 Hz (0x5) |
jm6wud | 0:67431d0a9143 | 154 | A_ODR_952 // 952 Hz (0x6) |
jm6wud | 0:67431d0a9143 | 155 | }; |
jm6wud | 0:67431d0a9143 | 156 | |
jm6wud | 0:67431d0a9143 | 157 | // accel_bw defines all possible bandwiths for low-pass filter of the accelerometer: |
jm6wud | 0:67431d0a9143 | 158 | enum accel_bw |
jm6wud | 0:67431d0a9143 | 159 | { |
jm6wud | 0:67431d0a9143 | 160 | A_BW_AUTO_SCALE = 0x0, // Automatic BW scaling (0x0) |
jm6wud | 0:67431d0a9143 | 161 | A_BW_408 = 0x4, // 408 Hz (0x4) |
jm6wud | 0:67431d0a9143 | 162 | A_BW_211 = 0x5, // 211 Hz (0x5) |
jm6wud | 0:67431d0a9143 | 163 | A_BW_105 = 0x6, // 105 Hz (0x6) |
jm6wud | 0:67431d0a9143 | 164 | A_BW_50 = 0x7 // 50 Hz (0x7) |
jm6wud | 0:67431d0a9143 | 165 | }; |
jm6wud | 0:67431d0a9143 | 166 | |
jm6wud | 0:67431d0a9143 | 167 | /// mag_scale defines all possible FSR's of the magnetometer: |
jm6wud | 0:67431d0a9143 | 168 | enum mag_scale |
jm6wud | 0:67431d0a9143 | 169 | { |
jm6wud | 0:67431d0a9143 | 170 | M_SCALE_4GS, // 00: +/- 4Gs |
jm6wud | 0:67431d0a9143 | 171 | M_SCALE_8GS, // 01: +/- 8Gs |
jm6wud | 0:67431d0a9143 | 172 | M_SCALE_12GS, // 10: +/- 12Gs |
jm6wud | 0:67431d0a9143 | 173 | M_SCALE_16GS, // 11: +/- 16Gs |
jm6wud | 0:67431d0a9143 | 174 | }; |
jm6wud | 0:67431d0a9143 | 175 | |
jm6wud | 0:67431d0a9143 | 176 | /// mag_odr defines all possible output data rates of the magnetometer: |
jm6wud | 0:67431d0a9143 | 177 | enum mag_odr |
jm6wud | 0:67431d0a9143 | 178 | { |
jm6wud | 0:67431d0a9143 | 179 | M_ODR_0625, // 0.625 Hz (0x00) |
jm6wud | 0:67431d0a9143 | 180 | M_ODR_125, // 1.25 Hz (0x01) |
jm6wud | 0:67431d0a9143 | 181 | M_ODR_25, // 2.5 Hz (0x02) |
jm6wud | 0:67431d0a9143 | 182 | M_ODR_5, // 5 Hz (0x03) |
jm6wud | 0:67431d0a9143 | 183 | M_ODR_10, // 10 (0x04) |
jm6wud | 0:67431d0a9143 | 184 | M_ODR_20, // 20 Hz (0x05) |
jm6wud | 0:67431d0a9143 | 185 | M_ODR_40, // 40 Hz (0x06) |
jm6wud | 0:67431d0a9143 | 186 | M_ODR_80 // 80 Hz (0x07) |
jm6wud | 0:67431d0a9143 | 187 | }; |
jm6wud | 0:67431d0a9143 | 188 | |
jm6wud | 0:67431d0a9143 | 189 | // We'll store the gyro, accel, and magnetometer readings in a series of |
jm6wud | 0:67431d0a9143 | 190 | // public class variables. Each sensor gets three variables -- one for each |
jm6wud | 0:67431d0a9143 | 191 | // axis. Call readGyro(), readAccel(), and readMag() first, before using |
jm6wud | 0:67431d0a9143 | 192 | // these variables! |
jm6wud | 0:67431d0a9143 | 193 | // These values are the RAW signed 16-bit readings from the sensors. |
jm6wud | 0:67431d0a9143 | 194 | int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope |
jm6wud | 0:67431d0a9143 | 195 | int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer |
jm6wud | 0:67431d0a9143 | 196 | int16_t mx_raw, my_raw, mz_raw; // x, y, and z axis readings of the magnetometer |
jm6wud | 0:67431d0a9143 | 197 | int16_t temperature_raw; |
jm6wud | 0:67431d0a9143 | 198 | |
jm6wud | 0:67431d0a9143 | 199 | // floating-point values of scaled data in real-world units |
jm6wud | 0:67431d0a9143 | 200 | float gx, gy, gz; |
jm6wud | 0:67431d0a9143 | 201 | float ax, ay, az; |
jm6wud | 0:67431d0a9143 | 202 | float mx, my, mz; |
jm6wud | 0:67431d0a9143 | 203 | float temperature_c, temperature_f; // temperature in celcius and fahrenheit |
jm6wud | 0:67431d0a9143 | 204 | |
jm6wud | 0:67431d0a9143 | 205 | |
jm6wud | 0:67431d0a9143 | 206 | /** LSM9DS1 -- LSM9DS1 class constructor |
jm6wud | 0:67431d0a9143 | 207 | * The constructor will set up a handful of private variables, and set the |
jm6wud | 0:67431d0a9143 | 208 | * communication mode as well. |
jm6wud | 0:67431d0a9143 | 209 | * Input: |
jm6wud | 0:67431d0a9143 | 210 | * - interface = Either MODE_SPI or MODE_I2C, whichever you're using |
jm6wud | 0:67431d0a9143 | 211 | * to talk to the IC. |
jm6wud | 0:67431d0a9143 | 212 | * - xgAddr = If MODE_I2C, this is the I2C address of the accel/gyro. |
jm6wud | 0:67431d0a9143 | 213 | * If MODE_SPI, this is the chip select pin of the accel/gyro (CS_A/G) |
jm6wud | 0:67431d0a9143 | 214 | * - mAddr = If MODE_I2C, this is the I2C address of the mag. |
jm6wud | 0:67431d0a9143 | 215 | * If MODE_SPI, this is the cs pin of the mag (CS_M) |
jm6wud | 0:67431d0a9143 | 216 | */ |
jm6wud | 0:67431d0a9143 | 217 | LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr = LSM9DS1_AG_I2C_ADDR(1), uint8_t mAddr = LSM9DS1_M_I2C_ADDR(1)); |
jm6wud | 0:67431d0a9143 | 218 | |
jm6wud | 0:67431d0a9143 | 219 | /** begin() -- Initialize the gyro, accelerometer, and magnetometer. |
jm6wud | 0:67431d0a9143 | 220 | * This will set up the scale and output rate of each sensor. It'll also |
jm6wud | 0:67431d0a9143 | 221 | * "turn on" every sensor and every axis of every sensor. |
jm6wud | 0:67431d0a9143 | 222 | * Input: |
jm6wud | 0:67431d0a9143 | 223 | * - gScl = The scale of the gyroscope. This should be a gyro_scale value. |
jm6wud | 0:67431d0a9143 | 224 | * - aScl = The scale of the accelerometer. Should be a accel_scale value. |
jm6wud | 0:67431d0a9143 | 225 | * - mScl = The scale of the magnetometer. Should be a mag_scale value. |
jm6wud | 0:67431d0a9143 | 226 | * - gODR = Output data rate of the gyroscope. gyro_odr value. |
jm6wud | 0:67431d0a9143 | 227 | * - aODR = Output data rate of the accelerometer. accel_odr value. |
jm6wud | 0:67431d0a9143 | 228 | * - mODR = Output data rate of the magnetometer. mag_odr value. |
jm6wud | 0:67431d0a9143 | 229 | * Output: The function will return an unsigned 16-bit value. The most-sig |
jm6wud | 0:67431d0a9143 | 230 | * bytes of the output are the WHO_AM_I reading of the accel/gyro. The |
jm6wud | 0:67431d0a9143 | 231 | * least significant two bytes are the WHO_AM_I reading of the mag. |
jm6wud | 0:67431d0a9143 | 232 | * All parameters have a defaulted value, so you can call just "begin()". |
jm6wud | 0:67431d0a9143 | 233 | * Default values are FSR's of: +/- 245DPS, 4g, 2Gs; ODRs of 119 Hz for |
jm6wud | 0:67431d0a9143 | 234 | * gyro, 119 Hz for accelerometer, 80 Hz for magnetometer. |
jm6wud | 0:67431d0a9143 | 235 | * Use the return value of this function to verify communication. |
jm6wud | 0:67431d0a9143 | 236 | */ |
jm6wud | 0:67431d0a9143 | 237 | uint16_t begin(gyro_scale gScl = G_SCALE_245DPS, |
jm6wud | 0:67431d0a9143 | 238 | accel_scale aScl = A_SCALE_2G, mag_scale mScl = M_SCALE_4GS, |
jm6wud | 0:67431d0a9143 | 239 | gyro_odr gODR = G_ODR_119_BW_14, accel_odr aODR = A_ODR_119, |
jm6wud | 0:67431d0a9143 | 240 | mag_odr mODR = M_ODR_80); |
jm6wud | 0:67431d0a9143 | 241 | |
jm6wud | 0:67431d0a9143 | 242 | /** readGyro() -- Read the gyroscope output registers. |
jm6wud | 0:67431d0a9143 | 243 | * This function will read all six gyroscope output registers. |
jm6wud | 0:67431d0a9143 | 244 | * The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read |
jm6wud | 0:67431d0a9143 | 245 | * those _after_ calling readGyro(). |
jm6wud | 0:67431d0a9143 | 246 | */ |
jm6wud | 0:67431d0a9143 | 247 | void readGyro(); |
jm6wud | 0:67431d0a9143 | 248 | |
jm6wud | 0:67431d0a9143 | 249 | /** readAccel() -- Read the accelerometer output registers. |
jm6wud | 0:67431d0a9143 | 250 | * This function will read all six accelerometer output registers. |
jm6wud | 0:67431d0a9143 | 251 | * The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read |
jm6wud | 0:67431d0a9143 | 252 | * those _after_ calling readAccel(). |
jm6wud | 0:67431d0a9143 | 253 | */ |
jm6wud | 0:67431d0a9143 | 254 | void readAccel(); |
jm6wud | 0:67431d0a9143 | 255 | |
jm6wud | 0:67431d0a9143 | 256 | /** readMag() -- Read the magnetometer output registers. |
jm6wud | 0:67431d0a9143 | 257 | * This function will read all six magnetometer output registers. |
jm6wud | 0:67431d0a9143 | 258 | * The readings are stored in the class' mx_raw, my_raw, and mz_raw variables. Read |
jm6wud | 0:67431d0a9143 | 259 | * those _after_ calling readMag(). |
jm6wud | 0:67431d0a9143 | 260 | */ |
jm6wud | 0:67431d0a9143 | 261 | void readMag(); |
jm6wud | 0:67431d0a9143 | 262 | |
jm6wud | 0:67431d0a9143 | 263 | /** readTemp() -- Read the temperature output register. |
jm6wud | 0:67431d0a9143 | 264 | * This function will read two temperature output registers. |
jm6wud | 0:67431d0a9143 | 265 | * The combined readings are stored in the class' temperature variables. Read |
jm6wud | 0:67431d0a9143 | 266 | * those _after_ calling readTemp(). |
jm6wud | 0:67431d0a9143 | 267 | */ |
jm6wud | 0:67431d0a9143 | 268 | void readTemp(); |
jm6wud | 0:67431d0a9143 | 269 | |
jm6wud | 0:67431d0a9143 | 270 | /** setGyroScale() -- Set the full-scale range of the gyroscope. |
jm6wud | 0:67431d0a9143 | 271 | * This function can be called to set the scale of the gyroscope to |
jm6wud | 0:67431d0a9143 | 272 | * 245, 500, or 2000 degrees per second. |
jm6wud | 0:67431d0a9143 | 273 | * Input: |
jm6wud | 0:67431d0a9143 | 274 | * - gScl = The desired gyroscope scale. Must be one of three possible |
jm6wud | 0:67431d0a9143 | 275 | * values from the gyro_scale enum. |
jm6wud | 0:67431d0a9143 | 276 | */ |
jm6wud | 0:67431d0a9143 | 277 | void setGyroScale(gyro_scale gScl); |
jm6wud | 0:67431d0a9143 | 278 | |
jm6wud | 0:67431d0a9143 | 279 | /** setAccelScale() -- Set the full-scale range of the accelerometer. |
jm6wud | 0:67431d0a9143 | 280 | * This function can be called to set the scale of the accelerometer to |
jm6wud | 0:67431d0a9143 | 281 | * 2, 4, 8, or 16 g's. |
jm6wud | 0:67431d0a9143 | 282 | * Input: |
jm6wud | 0:67431d0a9143 | 283 | * - aScl = The desired accelerometer scale. Must be one of five possible |
jm6wud | 0:67431d0a9143 | 284 | * values from the accel_scale enum. |
jm6wud | 0:67431d0a9143 | 285 | */ |
jm6wud | 0:67431d0a9143 | 286 | void setAccelScale(accel_scale aScl); |
jm6wud | 0:67431d0a9143 | 287 | |
jm6wud | 0:67431d0a9143 | 288 | /** setMagScale() -- Set the full-scale range of the magnetometer. |
jm6wud | 0:67431d0a9143 | 289 | * This function can be called to set the scale of the magnetometer to |
jm6wud | 0:67431d0a9143 | 290 | * 4, 8, 12, or 16 Gs. |
jm6wud | 0:67431d0a9143 | 291 | * Input: |
jm6wud | 0:67431d0a9143 | 292 | * - mScl = The desired magnetometer scale. Must be one of four possible |
jm6wud | 0:67431d0a9143 | 293 | * values from the mag_scale enum. |
jm6wud | 0:67431d0a9143 | 294 | */ |
jm6wud | 0:67431d0a9143 | 295 | void setMagScale(mag_scale mScl); |
jm6wud | 0:67431d0a9143 | 296 | |
jm6wud | 0:67431d0a9143 | 297 | /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope |
jm6wud | 0:67431d0a9143 | 298 | * Input: |
jm6wud | 0:67431d0a9143 | 299 | * - gRate = The desired output rate and cutoff frequency of the gyro. |
jm6wud | 0:67431d0a9143 | 300 | * Must be a value from the gyro_odr enum (check above). |
jm6wud | 0:67431d0a9143 | 301 | */ |
jm6wud | 0:67431d0a9143 | 302 | void setGyroODR(gyro_odr gRate); |
jm6wud | 0:67431d0a9143 | 303 | |
jm6wud | 0:67431d0a9143 | 304 | /** setAccelODR() -- Set the output data rate of the accelerometer |
jm6wud | 0:67431d0a9143 | 305 | * Input: |
jm6wud | 0:67431d0a9143 | 306 | * - aRate = The desired output rate of the accel. |
jm6wud | 0:67431d0a9143 | 307 | * Must be a value from the accel_odr enum (check above). |
jm6wud | 0:67431d0a9143 | 308 | */ |
jm6wud | 0:67431d0a9143 | 309 | void setAccelODR(accel_odr aRate); |
jm6wud | 0:67431d0a9143 | 310 | |
jm6wud | 0:67431d0a9143 | 311 | /** setMagODR() -- Set the output data rate of the magnetometer |
jm6wud | 0:67431d0a9143 | 312 | * Input: |
jm6wud | 0:67431d0a9143 | 313 | * - mRate = The desired output rate of the mag. |
jm6wud | 0:67431d0a9143 | 314 | * Must be a value from the mag_odr enum (check above). |
jm6wud | 0:67431d0a9143 | 315 | */ |
jm6wud | 0:67431d0a9143 | 316 | void setMagODR(mag_odr mRate); |
jm6wud | 0:67431d0a9143 | 317 | |
jm6wud | 0:67431d0a9143 | 318 | bool whoAmI(); |
jm6wud | 0:67431d0a9143 | 319 | |
jm6wud | 0:67431d0a9143 | 320 | private: |
jm6wud | 0:67431d0a9143 | 321 | /** xgAddress and mAddress store the I2C address |
jm6wud | 0:67431d0a9143 | 322 | * for each sensor. |
jm6wud | 0:67431d0a9143 | 323 | */ |
jm6wud | 0:67431d0a9143 | 324 | uint8_t xgAddress, mAddress; |
jm6wud | 0:67431d0a9143 | 325 | |
jm6wud | 0:67431d0a9143 | 326 | // I2C bus |
jm6wud | 0:67431d0a9143 | 327 | I2C i2c; |
jm6wud | 0:67431d0a9143 | 328 | |
jm6wud | 0:67431d0a9143 | 329 | /** gScale, aScale, and mScale store the current scale range for each |
jm6wud | 0:67431d0a9143 | 330 | * sensor. Should be updated whenever that value changes. |
jm6wud | 0:67431d0a9143 | 331 | */ |
jm6wud | 0:67431d0a9143 | 332 | gyro_scale gScale; |
jm6wud | 0:67431d0a9143 | 333 | accel_scale aScale; |
jm6wud | 0:67431d0a9143 | 334 | mag_scale mScale; |
jm6wud | 0:67431d0a9143 | 335 | |
jm6wud | 0:67431d0a9143 | 336 | /** gRes, aRes, and mRes store the current resolution for each sensor. |
jm6wud | 0:67431d0a9143 | 337 | * Units of these values would be DPS (or g's or Gs's) per ADC tick. |
jm6wud | 0:67431d0a9143 | 338 | * This value is calculated as (sensor scale) / (2^15). |
jm6wud | 0:67431d0a9143 | 339 | */ |
jm6wud | 0:67431d0a9143 | 340 | float gRes, aRes, mRes; |
jm6wud | 0:67431d0a9143 | 341 | |
jm6wud | 0:67431d0a9143 | 342 | /** initGyro() -- Sets up the gyroscope to begin reading. |
jm6wud | 0:67431d0a9143 | 343 | * This function steps through all three gyroscope control registers. |
jm6wud | 0:67431d0a9143 | 344 | */ |
jm6wud | 0:67431d0a9143 | 345 | void initGyro(); |
jm6wud | 0:67431d0a9143 | 346 | |
jm6wud | 0:67431d0a9143 | 347 | /** initAccel() -- Sets up the accelerometer to begin reading. |
jm6wud | 0:67431d0a9143 | 348 | * This function steps through all accelerometer related control registers. |
jm6wud | 0:67431d0a9143 | 349 | */ |
jm6wud | 0:67431d0a9143 | 350 | void initAccel(); |
jm6wud | 0:67431d0a9143 | 351 | |
jm6wud | 0:67431d0a9143 | 352 | /** initMag() -- Sets up the magnetometer to begin reading. |
jm6wud | 0:67431d0a9143 | 353 | * This function steps through all magnetometer-related control registers. |
jm6wud | 0:67431d0a9143 | 354 | */ |
jm6wud | 0:67431d0a9143 | 355 | void initMag(); |
jm6wud | 0:67431d0a9143 | 356 | |
jm6wud | 0:67431d0a9143 | 357 | /** calcgRes() -- Calculate the resolution of the gyroscope. |
jm6wud | 0:67431d0a9143 | 358 | * This function will set the value of the gRes variable. gScale must |
jm6wud | 0:67431d0a9143 | 359 | * be set prior to calling this function. |
jm6wud | 0:67431d0a9143 | 360 | */ |
jm6wud | 0:67431d0a9143 | 361 | void calcgRes(); |
jm6wud | 0:67431d0a9143 | 362 | |
jm6wud | 0:67431d0a9143 | 363 | /** calcmRes() -- Calculate the resolution of the magnetometer. |
jm6wud | 0:67431d0a9143 | 364 | * This function will set the value of the mRes variable. mScale must |
jm6wud | 0:67431d0a9143 | 365 | * be set prior to calling this function. |
jm6wud | 0:67431d0a9143 | 366 | */ |
jm6wud | 0:67431d0a9143 | 367 | void calcmRes(); |
jm6wud | 0:67431d0a9143 | 368 | |
jm6wud | 0:67431d0a9143 | 369 | /** calcaRes() -- Calculate the resolution of the accelerometer. |
jm6wud | 0:67431d0a9143 | 370 | * This function will set the value of the aRes variable. aScale must |
jm6wud | 0:67431d0a9143 | 371 | * be set prior to calling this function. |
jm6wud | 0:67431d0a9143 | 372 | */ |
jm6wud | 0:67431d0a9143 | 373 | void calcaRes(); |
jm6wud | 0:67431d0a9143 | 374 | }; |
jm6wud | 0:67431d0a9143 | 375 | |
jm6wud | 0:67431d0a9143 | 376 | #endif // _LSM9DS1_H // |
jm6wud | 0:67431d0a9143 | 377 |