Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
LSM6DS3.h
00001 // Based on Eugene Gonzalez's version of LSM9DS1_Demo 00002 // Modified by Sherry Yang for LSM6DS3 sensor 00003 #ifndef _LSM6DS3_H__ 00004 #define _LSM6DS3_H__ 00005 00006 #include "mbed.h" 00007 00008 ///////////////////////////////////////// 00009 // LSM6DS3 Accel/Gyro (XL/G) Registers // 00010 ///////////////////////////////////////// 00011 #define RAM_ACCESS 0x01 00012 #define SENSOR_SYNC_TIME 0x04 00013 #define SENSOR_SYNC_EN 0x05 00014 #define FIFO_CTRL1 0x06 00015 #define FIFO_CTRL2 0x07 00016 #define FIFO_CTRL3 0x08 00017 #define FIFO_CTRL4 0x09 00018 #define FIFO_CTRL5 0x0A 00019 #define ORIENT_CFG_G 0x0B 00020 #define REFERENCE_G 0x0C 00021 #define INT1_CTRL 0x0D 00022 #define INT2_CTRL 0x0E 00023 #define WHO_AM_I_REG 0X0F 00024 #define CTRL1_XL 0x10 00025 #define CTRL2_G 0x11 00026 #define CTRL3_C 0x12 00027 #define CTRL4_C 0x13 00028 #define CTRL5_C 0x14 00029 #define CTRL6_C 0x15 00030 #define CTRL7_G 0x16 00031 #define CTRL8_XL 0x17 00032 #define CTRL9_XL 0x18 00033 #define CTRL10_C 0x19 00034 #define MASTER_CONFIG 0x1A 00035 #define WAKE_UP_SRC 0x1B 00036 #define TAP_SRC 0x1C 00037 #define D6D_SRC 0x1D 00038 #define STATUS_REG 0x1E 00039 #define OUT_TEMP_L 0x20 00040 #define OUT_TEMP_H 0x21 00041 #define OUTX_L_G 0x22 00042 #define OUTX_H_G 0x23 00043 #define OUTY_L_G 0x24 00044 #define OUTY_H_G 0x25 00045 #define OUTZ_L_G 0x26 00046 #define OUTZ_H_G 0x27 00047 #define OUTX_L_XL 0x28 00048 #define OUTX_H_XL 0x29 00049 #define OUTY_L_XL 0x2A 00050 #define OUTY_H_XL 0x2B 00051 #define OUTZ_L_XL 0x2C 00052 #define OUTZ_H_XL 0x2D 00053 #define SENSORHUB1_REG 0x2E 00054 #define SENSORHUB2_REG 0x2F 00055 #define SENSORHUB3_REG 0x30 00056 #define SENSORHUB4_REG 0x31 00057 #define SENSORHUB5_REG 0x32 00058 #define SENSORHUB6_REG 0x33 00059 #define SENSORHUB7_REG 0x34 00060 #define SENSORHUB8_REG 0x35 00061 #define SENSORHUB9_REG 0x36 00062 #define SENSORHUB10_REG 0x37 00063 #define SENSORHUB11_REG 0x38 00064 #define SENSORHUB12_REG 0x39 00065 #define FIFO_STATUS1 0x3A 00066 #define FIFO_STATUS2 0x3B 00067 #define FIFO_STATUS3 0x3C 00068 #define FIFO_STATUS4 0x3D 00069 #define FIFO_DATA_OUT_L 0x3E 00070 #define FIFO_DATA_OUT_H 0x3F 00071 #define TIMESTAMP0_REG 0x40 00072 #define TIMESTAMP1_REG 0x41 00073 #define TIMESTAMP2_REG 0x42 00074 #define STEP_COUNTER_L 0x4B 00075 #define STEP_COUNTER_H 0x4C 00076 #define FUNC_SR 0x53 00077 #define TAP_CFG 0x58 00078 #define TAP_THS_6D 0x59 00079 #define INT_DUR2 0x5A 00080 #define WAKE_UP_THS 0x5B 00081 #define WAKE_UP_DUR 0x5C 00082 #define FREE_FALL 0x5D 00083 #define MD1_CFG 0x5E 00084 #define MD2_CFG 0x5F 00085 00086 // Possible I2C addresses for the accel/gyro 00087 #define LSM6DS3_AG_I2C_ADDR(sa0) ((sa0) ? 0xD6 : 0xD4) 00088 00089 /** 00090 * LSM6DS3 Class - driver for the 9 DoF IMU 00091 */ 00092 class LSM6DS3 00093 { 00094 public: 00095 00096 /// gyro_scale defines the possible full-scale ranges of the gyroscope: 00097 enum gyro_scale 00098 { 00099 G_SCALE_245DPS = 0x0 << 3, // 00 << 3: +/- 245 degrees per second 00100 G_SCALE_500DPS = 0x1 << 3, // 01 << 3: +/- 500 dps 00101 G_SCALE_1000DPS = 0x2 << 3, // 10 << 3: +/- 1000 dps 00102 G_SCALE_2000DPS = 0x3 << 3 // 11 << 3: +/- 2000 dps 00103 }; 00104 00105 /// gyro_odr defines all possible data rate/bandwidth combos of the gyro: 00106 enum gyro_odr 00107 { // ODR (Hz) --- Cutoff 00108 G_POWER_DOWN = 0x00, // 0 0 00109 G_ODR_13_BW_0 = 0x10, // 12.5 0.0081 low power 00110 G_ODR_26_BW_2 = 0x20, // 26 2.07 low power 00111 G_ODR_52_BW_16 = 0x30, // 52 16.32 low power 00112 G_ODR_104 = 0x40, // 104 00113 G_ODR_208 = 0x50, // 208 00114 G_ODR_416 = 0x60, // 416 00115 G_ODR_833 = 0x70, // 833 00116 G_ODR_1660 = 0x80 // 1660 00117 }; 00118 00119 /// accel_scale defines all possible FSR's of the accelerometer: 00120 enum accel_scale 00121 { 00122 A_SCALE_2G, // 00: +/- 2g 00123 A_SCALE_16G,// 01: +/- 16g 00124 A_SCALE_4G, // 10: +/- 4g 00125 A_SCALE_8G // 11: +/- 8g 00126 }; 00127 00128 /// accel_oder defines all possible output data rates of the accelerometer: 00129 enum accel_odr 00130 { 00131 A_POWER_DOWN, // Power-down mode (0x0) 00132 A_ODR_13, // 12.5 Hz (0x1) low power 00133 A_ODR_26, // 26 Hz (0x2) low power 00134 A_ODR_52, // 52 Hz (0x3) low power 00135 A_ODR_104, // 104 Hz (0x4) normal mode 00136 A_ODR_208, // 208 Hz (0x5) normal mode 00137 A_ODR_416, // 416 Hz (0x6) high performance 00138 A_ODR_833, // 833 Hz (0x7) high performance 00139 A_ODR_1660, // 1.66 kHz (0x8) high performance 00140 A_ODR_3330, // 3.33 kHz (0x9) high performance 00141 A_ODR_6660, // 6.66 kHz (0xA) high performance 00142 }; 00143 00144 // accel_bw defines all possible bandwiths for low-pass filter of the accelerometer: 00145 enum accel_bw 00146 { 00147 A_BW_AUTO_SCALE = 0x0, // Automatic BW scaling (0x0) 00148 A_BW_408 = 0x4, // 408 Hz (0x4) 00149 A_BW_211 = 0x5, // 211 Hz (0x5) 00150 A_BW_105 = 0x6, // 105 Hz (0x6) 00151 A_BW_50 = 0x7 // 50 Hz (0x7) 00152 }; 00153 00154 00155 00156 // We'll store the gyro, and accel, readings in a series of 00157 // public class variables. Each sensor gets three variables -- one for each 00158 // axis. Call readGyro(), and readAccel() first, before using 00159 // these variables! 00160 // These values are the RAW signed 16-bit readings from the sensors. 00161 int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope 00162 int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer 00163 int16_t temperature_raw; 00164 00165 // floating-point values of scaled data in real-world units 00166 float gx, gy, gz; 00167 float ax, ay, az; 00168 float temperature_c, temperature_f; // temperature in celcius and fahrenheit 00169 float intr; 00170 00171 00172 /** LSM6DS3 -- LSM6DS3 class constructor 00173 * The constructor will set up a handful of private variables, and set the 00174 * communication mode as well. 00175 * Input: 00176 * - interface = Either MODE_SPI or MODE_I2C, whichever you're using 00177 * to talk to the IC. 00178 * - xgAddr = If MODE_I2C, this is the I2C address of the accel/gyro. 00179 * If MODE_SPI, this is the chip select pin of the accel/gyro (CS_A/G) 00180 */ 00181 LSM6DS3(PinName sda, PinName scl, uint8_t xgAddr = LSM6DS3_AG_I2C_ADDR(1)); 00182 00183 /** begin() -- Initialize the gyro, and accelerometer. 00184 * This will set up the scale and output rate of each sensor. It'll also 00185 * "turn on" every sensor and every axis of every sensor. 00186 * Input: 00187 * - gScl = The scale of the gyroscope. This should be a gyro_scale value. 00188 * - aScl = The scale of the accelerometer. Should be a accel_scale value. 00189 * - gODR = Output data rate of the gyroscope. gyro_odr value. 00190 * - aODR = Output data rate of the accelerometer. accel_odr value. 00191 * Output: The function will return an unsigned 16-bit value. The most-sig 00192 * bytes of the output are the WHO_AM_I reading of the accel/gyro. 00193 * All parameters have a defaulted value, so you can call just "begin()". 00194 * Default values are FSR's of: +/- 245DPS, 4g, 2Gs; ODRs of 119 Hz for 00195 * gyro, 119 Hz for accelerometer. 00196 * Use the return value of this function to verify communication. 00197 */ 00198 uint16_t begin(gyro_scale gScl = G_SCALE_245DPS, 00199 accel_scale aScl = A_SCALE_2G, gyro_odr gODR = G_ODR_104, 00200 accel_odr aODR = A_ODR_104); 00201 00202 /** readGyro() -- Read the gyroscope output registers. 00203 * This function will read all six gyroscope output registers. 00204 * The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read 00205 * those _after_ calling readGyro(). 00206 */ 00207 void readGyro(); 00208 00209 /** readAccel() -- Read the accelerometer output registers. 00210 * This function will read all six accelerometer output registers. 00211 * The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read 00212 * those _after_ calling readAccel(). 00213 */ 00214 void readAccel(); 00215 00216 /** readTemp() -- Read the temperature output register. 00217 * This function will read two temperature output registers. 00218 * The combined readings are stored in the class' temperature variables. Read 00219 * those _after_ calling readTemp(). 00220 */ 00221 void readTemp(); 00222 00223 /** Read Interrupt **/ 00224 void readIntr(); 00225 00226 /** setGyroScale() -- Set the full-scale range of the gyroscope. 00227 * This function can be called to set the scale of the gyroscope to 00228 * 245, 500, or 2000 degrees per second. 00229 * Input: 00230 * - gScl = The desired gyroscope scale. Must be one of three possible 00231 * values from the gyro_scale enum. 00232 */ 00233 void setGyroScale(gyro_scale gScl); 00234 00235 /** setAccelScale() -- Set the full-scale range of the accelerometer. 00236 * This function can be called to set the scale of the accelerometer to 00237 * 2, 4, 8, or 16 g's. 00238 * Input: 00239 * - aScl = The desired accelerometer scale. Must be one of five possible 00240 * values from the accel_scale enum. 00241 */ 00242 void setAccelScale(accel_scale aScl); 00243 00244 /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope 00245 * Input: 00246 * - gRate = The desired output rate and cutoff frequency of the gyro. 00247 * Must be a value from the gyro_odr enum (check above). 00248 */ 00249 void setGyroODR(gyro_odr gRate); 00250 00251 /** setAccelODR() -- Set the output data rate of the accelerometer 00252 * Input: 00253 * - aRate = The desired output rate of the accel. 00254 * Must be a value from the accel_odr enum (check above). 00255 */ 00256 void setAccelODR(accel_odr aRate); 00257 00258 00259 private: 00260 /** xgAddress store the I2C address 00261 * for each sensor. 00262 */ 00263 uint8_t xgAddress; 00264 00265 // I2C bus 00266 I2C i2c; 00267 00268 /** gScale, and aScale store the current scale range for each 00269 * sensor. Should be updated whenever that value changes. 00270 */ 00271 gyro_scale gScale; 00272 accel_scale aScale; 00273 00274 /** gRes, and aRes store the current resolution for each sensor. 00275 * Units of these values would be DPS (or g's or Gs's) per ADC tick. 00276 * This value is calculated as (sensor scale) / (2^15). 00277 */ 00278 float gRes, aRes; 00279 00280 /** initGyro() -- Sets up the gyroscope to begin reading. 00281 * This function steps through all three gyroscope control registers. 00282 */ 00283 void initGyro(); 00284 00285 /** initAccel() -- Sets up the accelerometer to begin reading. 00286 * This function steps through all accelerometer related control registers. 00287 */ 00288 void initAccel(); 00289 00290 /** Setup Interrupt **/ 00291 void initIntr(); 00292 00293 /** calcgRes() -- Calculate the resolution of the gyroscope. 00294 * This function will set the value of the gRes variable. gScale must 00295 * be set prior to calling this function. 00296 */ 00297 void calcgRes(); 00298 00299 /** calcaRes() -- Calculate the resolution of the accelerometer. 00300 * This function will set the value of the aRes variable. aScale must 00301 * be set prior to calling this function. 00302 */ 00303 void calcaRes(); 00304 }; 00305 00306 #endif // _LSM6DS3_H // 00307
Generated on Tue Jul 12 2022 17:55:36 by
