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