LSM9DS1 the other library
Fork of LSM9DS1 by
Embed:
(wiki syntax)
Show/hide line numbers
LSM9DS1.h
00001 // Most of the Credit goes to jimblom 00002 // Modifications by Allen Wild 00003 #ifndef _LSM9DS1_H__ 00004 #define _LSM9DS1_H__ 00005 00006 #include "mbed.h" 00007 00008 ///////////////////////////////////////// 00009 // LSM9DS1 Accel/Gyro (XL/G) Registers // 00010 ///////////////////////////////////////// 00011 #define ACT_THS 0x04 00012 #define ACT_DUR 0x05 00013 #define INT_GEN_CFG_XL 0x06 00014 #define INT_GEN_THS_X_XL 0x07 00015 #define INT_GEN_THS_Y_XL 0x08 00016 #define INT_GEN_THS_Z_XL 0x09 00017 #define INT_GEN_DUR_XL 0x0A 00018 #define REFERENCE_G 0x0B 00019 #define INT1_CTRL 0x0C 00020 #define INT2_CTRL 0x0D 00021 #define WHO_AM_I_XG 0x0F 00022 #define CTRL_REG1_G 0x10 00023 #define CTRL_REG2_G 0x11 00024 #define CTRL_REG3_G 0x12 00025 #define ORIENT_CFG_G 0x13 00026 #define INT_GEN_SRC_G 0x14 00027 #define OUT_TEMP_L 0x15 00028 #define OUT_TEMP_H 0x16 00029 #define STATUS_REG_0 0x17 00030 #define OUT_X_L_G 0x18 00031 #define OUT_X_H_G 0x19 00032 #define OUT_Y_L_G 0x1A 00033 #define OUT_Y_H_G 0x1B 00034 #define OUT_Z_L_G 0x1C 00035 #define OUT_Z_H_G 0x1D 00036 #define CTRL_REG4 0x1E 00037 #define CTRL_REG5_XL 0x1F 00038 #define CTRL_REG6_XL 0x20 00039 #define CTRL_REG7_XL 0x21 00040 #define CTRL_REG8 0x22 00041 #define CTRL_REG9 0x23 00042 #define CTRL_REG10 0x24 00043 #define INT_GEN_SRC_XL 0x26 00044 #define STATUS_REG_1 0x27 00045 #define OUT_X_L_XL 0x28 00046 #define OUT_X_H_XL 0x29 00047 #define OUT_Y_L_XL 0x2A 00048 #define OUT_Y_H_XL 0x2B 00049 #define OUT_Z_L_XL 0x2C 00050 #define OUT_Z_H_XL 0x2D 00051 #define FIFO_CTRL 0x2E 00052 #define FIFO_SRC 0x2F 00053 #define INT_GEN_CFG_G 0x30 00054 #define INT_GEN_THS_XH_G 0x31 00055 #define INT_GEN_THS_XL_G 0x32 00056 #define INT_GEN_THS_YH_G 0x33 00057 #define INT_GEN_THS_YL_G 0x34 00058 #define INT_GEN_THS_ZH_G 0x35 00059 #define INT_GEN_THS_ZL_G 0x36 00060 #define INT_GEN_DUR_G 0x37 00061 00062 /////////////////////////////// 00063 // LSM9DS1 Magneto Registers // 00064 /////////////////////////////// 00065 #define OFFSET_X_REG_L_M 0x05 00066 #define OFFSET_X_REG_H_M 0x06 00067 #define OFFSET_Y_REG_L_M 0x07 00068 #define OFFSET_Y_REG_H_M 0x08 00069 #define OFFSET_Z_REG_L_M 0x09 00070 #define OFFSET_Z_REG_H_M 0x0A 00071 #define WHO_AM_I_M 0x0F 00072 #define CTRL_REG1_M 0x20 00073 #define CTRL_REG2_M 0x21 00074 #define CTRL_REG3_M 0x22 00075 #define CTRL_REG4_M 0x23 00076 #define CTRL_REG5_M 0x24 00077 #define STATUS_REG_M 0x27 00078 #define OUT_X_L_M 0x28 00079 #define OUT_X_H_M 0x29 00080 #define OUT_Y_L_M 0x2A 00081 #define OUT_Y_H_M 0x2B 00082 #define OUT_Z_L_M 0x2C 00083 #define OUT_Z_H_M 0x2D 00084 #define INT_CFG_M 0x30 00085 #define INT_SRC_M 0x30 00086 #define INT_THS_L_M 0x32 00087 #define INT_THS_H_M 0x33 00088 00089 //////////////////////////////// 00090 // LSM9DS1 WHO_AM_I Responses // 00091 //////////////////////////////// 00092 #define WHO_AM_I_AG_RSP 0x68 00093 #define WHO_AM_I_M_RSP 0x3D 00094 00095 // Possible I2C addresses for the accel/gyro and mag 00096 #define LSM9DS1_AG_I2C_ADDR(sa0) ((sa0) ? 0xD6 : 0xD4) 00097 #define LSM9DS1_M_I2C_ADDR(sa1) ((sa1) ? 0x3C : 0x38) 00098 00099 /** 00100 * LSM9DS1 Class - driver for the 9 DoF IMU 00101 */ 00102 class LSM9DS1 00103 { 00104 public: 00105 00106 /// gyro_scale defines the possible full-scale ranges of the gyroscope: 00107 /* 00108 enum gyro_scale 00109 { 00110 G_SCALE_245DPS = 0x0 << 3, // 00 << 3: +/- 245 degrees per second 00111 G_SCALE_500DPS = 0x1 << 3, // 01 << 3: +/- 500 dps 00112 G_SCALE_2000DPS = 0x3 << 3 // 11 << 3: +/- 2000 dps 00113 }; 00114 */ 00115 enum gyro_scale 00116 { 00117 G_SCALE_245DPS = 0x0, // 00: +/- 245 degrees per second 00118 G_SCALE_500DPS = 0x1, // 01: +/- 500 dps 00119 G_SCALE_2000DPS = 0x3 // 11: +/- 2000 dps 00120 }; 00121 00122 /// gyro_odr defines all possible data rate/bandwidth combos of the gyro: 00123 enum gyro_odr 00124 { // ODR (Hz) --- Cutoff 00125 G_POWER_DOWN = 0x00, // 0 0 00126 G_ODR_15_BW_0 = 0x20, // 14.9 0 00127 G_ODR_60_BW_16 = 0x40, // 59.5 16 00128 G_ODR_119_BW_14 = 0x60, // 119 14 00129 G_ODR_119_BW_31 = 0x61, // 119 31 00130 G_ODR_238_BW_14 = 0x80, // 238 14 00131 G_ODR_238_BW_29 = 0x81, // 238 29 00132 G_ODR_238_BW_63 = 0x82, // 238 63 00133 G_ODR_238_BW_78 = 0x83, // 238 78 00134 G_ODR_476_BW_21 = 0xA0, // 476 21 00135 G_ODR_476_BW_28 = 0xA1, // 476 28 00136 G_ODR_476_BW_57 = 0xA2, // 476 57 00137 G_ODR_476_BW_100 = 0xA3, // 476 100 00138 G_ODR_952_BW_33 = 0xC0, // 952 33 00139 G_ODR_952_BW_40 = 0xC1, // 952 40 00140 G_ODR_952_BW_58 = 0xC2, // 952 58 00141 G_ODR_952_BW_100 = 0xC3 // 952 100 00142 }; 00143 00144 /// accel_scale defines all possible FSR's of the accelerometer: 00145 enum accel_scale 00146 { 00147 A_SCALE_2G, // 00: +/- 2g 00148 A_SCALE_16G,// 01: +/- 16g 00149 A_SCALE_4G, // 10: +/- 4g 00150 A_SCALE_8G // 11: +/- 8g 00151 }; 00152 00153 /// accel_oder defines all possible output data rates of the accelerometer: 00154 enum accel_odr 00155 { 00156 A_POWER_DOWN, // Power-down mode (0x0) 00157 A_ODR_10, // 10 Hz (0x1) 00158 A_ODR_50, // 50 Hz (0x2) 00159 A_ODR_119, // 119 Hz (0x3) 00160 A_ODR_238, // 238 Hz (0x4) 00161 A_ODR_476, // 476 Hz (0x5) 00162 A_ODR_952 // 952 Hz (0x6) 00163 }; 00164 00165 // accel_bw defines all possible bandwiths for low-pass filter of the accelerometer: 00166 enum accel_bw 00167 { 00168 A_BW_AUTO_SCALE = 0x0, // Automatic BW scaling (0x0) 00169 A_BW_408 = 0x4, // 408 Hz (0x4) 00170 A_BW_211 = 0x5, // 211 Hz (0x5) 00171 A_BW_105 = 0x6, // 105 Hz (0x6) 00172 A_BW_50 = 0x7 // 50 Hz (0x7) 00173 }; 00174 00175 /// mag_scale defines all possible FSR's of the magnetometer: 00176 enum mag_scale 00177 { 00178 M_SCALE_4GS, // 00: +/- 4Gs 00179 M_SCALE_8GS, // 01: +/- 8Gs 00180 M_SCALE_12GS, // 10: +/- 12Gs 00181 M_SCALE_16GS, // 11: +/- 16Gs 00182 }; 00183 00184 /// mag_odr defines all possible output data rates of the magnetometer: 00185 enum mag_odr 00186 { 00187 M_ODR_0625, // 0.625 Hz (0x00) 00188 M_ODR_125, // 1.25 Hz (0x01) 00189 M_ODR_25, // 2.5 Hz (0x02) 00190 M_ODR_5, // 5 Hz (0x03) 00191 M_ODR_10, // 10 (0x04) 00192 M_ODR_20, // 20 Hz (0x05) 00193 M_ODR_40, // 40 Hz (0x06) 00194 M_ODR_80 // 80 Hz (0x07) 00195 }; 00196 00197 // We'll store the gyro, accel, and magnetometer readings in a series of 00198 // public class variables. Each sensor gets three variables -- one for each 00199 // axis. Call readGyro(), readAccel(), and readMag() first, before using 00200 // these variables! 00201 // These values are the RAW signed 16-bit readings from the sensors. 00202 int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope 00203 int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer 00204 int16_t mx_raw, my_raw, mz_raw; // x, y, and z axis readings of the magnetometer 00205 int16_t temperature_raw; 00206 int16_t gBiasRaw[3], aBiasRaw[3]; 00207 00208 // floating-point values of scaled data in real-world units 00209 float gx, gy, gz; 00210 float ax, ay, az; 00211 float mx, my, mz; 00212 float temperature_c, temperature_f; // temperature in celcius and fahrenheit 00213 float intr; 00214 float gBias[3], aBias[3]; 00215 00216 bool autoCalib; 00217 00218 /** LSM9DS1 -- LSM9DS1 class constructor 00219 * The constructor will set up a handful of private variables, and set the 00220 * communication mode as well. 00221 * Input: 00222 * - interface = Either MODE_SPI or MODE_I2C, whichever you're using 00223 * to talk to the IC. 00224 * - xgAddr = If MODE_I2C, this is the I2C address of the accel/gyro. 00225 * If MODE_SPI, this is the chip select pin of the accel/gyro (CS_A/G) 00226 * - mAddr = If MODE_I2C, this is the I2C address of the mag. 00227 * If MODE_SPI, this is the cs pin of the mag (CS_M) 00228 */ 00229 LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr = LSM9DS1_AG_I2C_ADDR(1), uint8_t mAddr = LSM9DS1_M_I2C_ADDR(1)); 00230 00231 /** begin() -- Initialize the gyro, accelerometer, and magnetometer. 00232 * This will set up the scale and output rate of each sensor. It'll also 00233 * "turn on" every sensor and every axis of every sensor. 00234 * Input: 00235 * - gScl = The scale of the gyroscope. This should be a gyro_scale value. 00236 * - aScl = The scale of the accelerometer. Should be a accel_scale value. 00237 * - mScl = The scale of the magnetometer. Should be a mag_scale value. 00238 * - gODR = Output data rate of the gyroscope. gyro_odr value. 00239 * - aODR = Output data rate of the accelerometer. accel_odr value. 00240 * - mODR = Output data rate of the magnetometer. mag_odr value. 00241 * Output: The function will return an unsigned 16-bit value. The most-sig 00242 * bytes of the output are the WHO_AM_I reading of the accel/gyro. The 00243 * least significant two bytes are the WHO_AM_I reading of the mag. 00244 * All parameters have a defaulted value, so you can call just "begin()". 00245 * Default values are FSR's of: +/- 245DPS, 4g, 2Gs; ODRs of 119 Hz for 00246 * gyro, 119 Hz for accelerometer, 80 Hz for magnetometer. 00247 * Use the return value of this function to verify communication. 00248 */ 00249 bool begin(gyro_scale gScl = G_SCALE_2000DPS, 00250 accel_scale aScl = A_SCALE_8G, mag_scale mScl = M_SCALE_4GS, 00251 gyro_odr gODR = G_ODR_119_BW_31, accel_odr aODR = A_ODR_119, 00252 mag_odr mODR = M_ODR_80); 00253 00254 /** readGyro() -- Read the gyroscope output registers. 00255 * This function will read all six gyroscope output registers. 00256 * The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read 00257 * those _after_ calling readGyro(). 00258 */ 00259 void readGyro(); 00260 00261 /** readAccel() -- Read the accelerometer output registers. 00262 * This function will read all six accelerometer output registers. 00263 * The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read 00264 * those _after_ calling readAccel(). 00265 */ 00266 void readAccel(); 00267 00268 /** readMag() -- Read the magnetometer output registers. 00269 * This function will read all six magnetometer output registers. 00270 * The readings are stored in the class' mx_raw, my_raw, and mz_raw variables. Read 00271 * those _after_ calling readMag(). 00272 */ 00273 void readMag(); 00274 00275 /** Read Interrupt **/ 00276 void readIntr(); 00277 00278 /** readTemp() -- Read the temperature output register. 00279 * This function will read two temperature output registers. 00280 * The combined readings are stored in the class' temperature variables. Read 00281 * those _after_ calling readTemp(). 00282 */ 00283 void readTemp(); 00284 00285 /** calibration() -- Calibrate Accel and Gyro sensor 00286 */ 00287 void calibration(); 00288 00289 /** setGyroScale() -- Set the full-scale range of the gyroscope. 00290 * This function can be called to set the scale of the gyroscope to 00291 * 245, 500, or 2000 degrees per second. 00292 * Input: 00293 * - gScl = The desired gyroscope scale. Must be one of three possible 00294 * values from the gyro_scale enum. 00295 */ 00296 void setGyroScale(gyro_scale gScl); 00297 00298 /** setAccelScale() -- Set the full-scale range of the accelerometer. 00299 * This function can be called to set the scale of the accelerometer to 00300 * 2, 4, 8, or 16 g's. 00301 * Input: 00302 * - aScl = The desired accelerometer scale. Must be one of five possible 00303 * values from the accel_scale enum. 00304 */ 00305 void setAccelScale(accel_scale aScl); 00306 00307 /** setMagScale() -- Set the full-scale range of the magnetometer. 00308 * This function can be called to set the scale of the magnetometer to 00309 * 4, 8, 12, or 16 Gs. 00310 * Input: 00311 * - mScl = The desired magnetometer scale. Must be one of four possible 00312 * values from the mag_scale enum. 00313 */ 00314 void setMagScale(mag_scale mScl); 00315 00316 /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope 00317 * Input: 00318 * - gRate = The desired output rate and cutoff frequency of the gyro. 00319 * Must be a value from the gyro_odr enum (check above). 00320 */ 00321 void setGyroODR(gyro_odr gRate); 00322 00323 /** setAccelODR() -- Set the output data rate of the accelerometer 00324 * Input: 00325 * - aRate = The desired output rate of the accel. 00326 * Must be a value from the accel_odr enum (check above). 00327 */ 00328 void setAccelODR(accel_odr aRate); 00329 00330 /** setMagODR() -- Set the output data rate of the magnetometer 00331 * Input: 00332 * - mRate = The desired output rate of the mag. 00333 * Must be a value from the mag_odr enum (check above). 00334 */ 00335 void setMagODR(mag_odr mRate); 00336 00337 00338 /** enableFIFO() -- Turn on FIFO state (CTRL_REG9) 00339 * Input: 00340 * - enable = true - turn on FIFO 00341 * false - turn off FIFO 00342 */ 00343 void enableXgFIFO(bool enable); 00344 00345 /** setFIFO() -- Set FIFO mode and FIFO threshold(FIFO_CTRL) 00346 * Input: 00347 * - fifoMode = 0: Bypass mode. FIFO turned off 00348 * 1: FIFO mode. Stops collecting data when FIFO is full 00349 * - fifoThs = maximum threshold is 0x1F(31) 00350 */ 00351 void setXgFIFO(uint8_t fifoMode, uint8_t fifoThs); 00352 00353 private: 00354 /** xgAddress and mAddress store the I2C address 00355 * for each sensor. 00356 */ 00357 uint8_t xgAddress, mAddress; 00358 00359 // I2C bus 00360 I2C i2c; 00361 00362 /** gScale, aScale, and mScale store the current scale range for each 00363 * sensor. Should be updated whenever that value changes. 00364 */ 00365 gyro_scale gScale; 00366 accel_scale aScale; 00367 mag_scale mScale; 00368 00369 /** gRes, aRes, and mRes store the current resolution for each sensor. 00370 * Units of these values would be DPS (or g's or Gs's) per ADC tick. 00371 * This value is calculated as (sensor scale) / (2^15). 00372 */ 00373 float gRes, aRes, mRes; 00374 00375 /** initGyro() -- Sets up the gyroscope to begin reading. 00376 * This function steps through all three gyroscope control registers. 00377 */ 00378 void initGyro(); 00379 00380 /** initAccel() -- Sets up the accelerometer to begin reading. 00381 * This function steps through all accelerometer related control registers. 00382 */ 00383 void initAccel(); 00384 00385 /** Initialize Interrupts **/ 00386 void initIntr(); 00387 00388 /** initMag() -- Sets up the magnetometer to begin reading. 00389 * This function steps through all magnetometer-related control registers. 00390 */ 00391 void initMag(); 00392 00393 /** calcgRes() -- Calculate the resolution of the gyroscope. 00394 * This function will set the value of the gRes variable. gScale must 00395 * be set prior to calling this function. 00396 */ 00397 void calcgRes(); 00398 00399 /** calcmRes() -- Calculate the resolution of the magnetometer. 00400 * This function will set the value of the mRes variable. mScale must 00401 * be set prior to calling this function. 00402 */ 00403 void calcmRes(); 00404 00405 /** calcaRes() -- Calculate the resolution of the accelerometer. 00406 * This function will set the value of the aRes variable. aScale must 00407 * be set prior to calling this function. 00408 */ 00409 void calcaRes(); 00410 }; 00411 00412 #endif // _LSM9DS1_H //
Generated on Wed Jul 13 2022 15:51:31 by
