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