v61_0

Committer:
gr66
Date:
Fri Aug 06 14:52:00 2021 +0000
Revision:
7:164a2086348d
Parent:
6:798481567563
V60

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gr66 6:798481567563 1 // Based on Eugene Gonzalez's version of LSM9DS1_Demo
gr66 6:798481567563 2 // Modified by Sherry Yang for LSM6DS3 sensor
gr66 6:798481567563 3 // Modified by Brian Claus for LSM6DS33 sensor
gr66 6:798481567563 4 // Modified by Gilles Raynaud
gr66 6:798481567563 5 #ifndef _LSM6DS33_H__
gr66 6:798481567563 6 #define _LSM6DS33_H__
gr66 6:798481567563 7
gr66 6:798481567563 8 #include "mbed.h"
gr66 6:798481567563 9
gr66 6:798481567563 10 /////////////////////////////////////////
gr66 6:798481567563 11 // LSM6DS33 Accel/Gyro (XL/G) Registers //
gr66 6:798481567563 12 /////////////////////////////////////////
gr66 6:798481567563 13 #define RAM_ACCESS 0x01
gr66 6:798481567563 14 #define FIFO_CTRL1 0x06
gr66 6:798481567563 15 #define FIFO_CTRL2 0x07
gr66 6:798481567563 16 #define FIFO_CTRL3 0x08
gr66 6:798481567563 17 #define FIFO_CTRL4 0x09
gr66 6:798481567563 18 #define FIFO_CTRL5 0x0A
gr66 6:798481567563 19 #define ORIENT_CFG_G 0x0B
gr66 6:798481567563 20 #define INT1_CTRL 0x0D
gr66 6:798481567563 21 #define INT2_CTRL 0x0E
gr66 6:798481567563 22 #define WHO_AM_I_REG 0X0F
gr66 6:798481567563 23 #define CTRL1_XL 0x10
gr66 6:798481567563 24 #define CTRL2_G 0x11
gr66 6:798481567563 25 #define CTRL3_C 0x12
gr66 6:798481567563 26 #define CTRL4_C 0x13
gr66 6:798481567563 27 #define CTRL5_C 0x14
gr66 6:798481567563 28 #define CTRL6_C 0x15
gr66 6:798481567563 29 #define CTRL7_G 0x16
gr66 6:798481567563 30 #define CTRL8_XL 0x17
gr66 6:798481567563 31 #define CTRL9_XL 0x18
gr66 6:798481567563 32 #define CTRL10_C 0x19
gr66 6:798481567563 33 #define WAKE_UP_SRC 0x1B
gr66 6:798481567563 34 #define TAP_SRC 0x1C
gr66 6:798481567563 35 #define D6D_SRC 0x1D
gr66 6:798481567563 36 #define STATUS_REG 0x1E
gr66 6:798481567563 37 #define OUT_TEMP_L 0x20
gr66 6:798481567563 38 #define OUT_TEMP_H 0x21
gr66 6:798481567563 39 #define OUTX_L_G 0x22
gr66 6:798481567563 40 #define OUTX_H_G 0x23
gr66 6:798481567563 41 #define OUTY_L_G 0x24
gr66 6:798481567563 42 #define OUTY_H_G 0x25
gr66 6:798481567563 43 #define OUTZ_L_G 0x26
gr66 6:798481567563 44 #define OUTZ_H_G 0x27
gr66 6:798481567563 45 #define OUTX_L_XL 0x28
gr66 6:798481567563 46 #define OUTX_H_XL 0x29
gr66 6:798481567563 47 #define OUTY_L_XL 0x2A
gr66 6:798481567563 48 #define OUTY_H_XL 0x2B
gr66 6:798481567563 49 #define OUTZ_L_XL 0x2C
gr66 6:798481567563 50 #define OUTZ_H_XL 0x2D
gr66 6:798481567563 51 #define FIFO_STATUS1 0x3A
gr66 6:798481567563 52 #define FIFO_STATUS2 0x3B
gr66 6:798481567563 53 #define FIFO_STATUS3 0x3C
gr66 6:798481567563 54 #define FIFO_STATUS4 0x3D
gr66 6:798481567563 55 #define FIFO_DATA_OUT_L 0x3E
gr66 6:798481567563 56 #define FIFO_DATA_OUT_H 0x3F
gr66 6:798481567563 57 #define TIMESTAMP0_REG 0x40
gr66 6:798481567563 58 #define TIMESTAMP1_REG 0x41
gr66 6:798481567563 59 #define TIMESTAMP2_REG 0x42
gr66 6:798481567563 60 #define STEP_TIMESTAMP_L 0x49
gr66 6:798481567563 61 #define STEP_TIMESTAMP_H 0x4A
gr66 6:798481567563 62 #define STEP_COUNTER_L 0x4B
gr66 6:798481567563 63 #define STEP_COUNTER_H 0x4C
gr66 6:798481567563 64 #define FUNC_SR 0x53
gr66 6:798481567563 65 #define TAP_CFG 0x58
gr66 6:798481567563 66 #define TAP_THS_6D 0x59
gr66 6:798481567563 67 #define INT_DUR2 0x5A
gr66 6:798481567563 68 #define WAKE_UP_THS 0x5B
gr66 6:798481567563 69 #define WAKE_UP_DUR 0x5C
gr66 6:798481567563 70 #define FREE_FALL 0x5D
gr66 6:798481567563 71 #define MD1_CFG 0x5E
gr66 6:798481567563 72 #define MD2_CFG 0x5F
gr66 6:798481567563 73
gr66 6:798481567563 74 // Possible I2C addresses for the accel/gyro
gr66 6:798481567563 75 #define LSM6DS33_AG_I2C_ADDR(sa0) ((sa0) ? 0xD6 : 0xD4)
gr66 6:798481567563 76
gr66 6:798481567563 77 /**
gr66 6:798481567563 78 * LSM6DS33 Class - driver for the 6 DoF IMU
gr66 6:798481567563 79 */
gr66 6:798481567563 80 class LSM6DS33
gr66 6:798481567563 81 {
gr66 6:798481567563 82 public:
gr66 6:798481567563 83
gr66 6:798481567563 84 /// gyro_scale defines the possible full-scale ranges of the gyroscope:
gr66 6:798481567563 85 enum gyro_scale
gr66 6:798481567563 86 {
gr66 6:798481567563 87 G_SCALE_125DPS = 0x1 , // 00 << 3: +/- 125 degrees per second
gr66 6:798481567563 88 G_SCALE_250DPS = 0x0 , // 00 << 3: +/- 250 degrees per second
gr66 6:798481567563 89 G_SCALE_500DPS = 0x2 , // 01 << 3: +/- 500 dps
gr66 6:798481567563 90 G_SCALE_1000DPS = 0x4 , // 10 << 3: +/- 1000 dps
gr66 6:798481567563 91 G_SCALE_2000DPS = 0x6 // 11 << 3: +/- 2000 dps ////GR <<2 au lieu de 3
gr66 6:798481567563 92 };
gr66 6:798481567563 93
gr66 6:798481567563 94 /// gyro_odr defines all possible data rate/bandwidth combos of the gyro:
gr66 6:798481567563 95 enum gyro_odr
gr66 6:798481567563 96 { // ODR (Hz) --- Cutoff
gr66 6:798481567563 97 G_POWER_DOWN = 0x00, // 0 0
gr66 6:798481567563 98 G_ODR_13_BW_0 = 0x10, // 12.5 0.0081 low power
gr66 6:798481567563 99 G_ODR_26_BW_2 = 0x20, // 26 2.07 low power
gr66 6:798481567563 100 G_ODR_52_BW_16 = 0x30, // 52 16.32 low power
gr66 6:798481567563 101 G_ODR_104 = 0x40, // 104
gr66 6:798481567563 102 G_ODR_208 = 0x50, // 208
gr66 6:798481567563 103 G_ODR_416 = 0x60, // 416
gr66 6:798481567563 104 G_ODR_833 = 0x70, // 833
gr66 6:798481567563 105 G_ODR_1660 = 0x80 // 1660
gr66 6:798481567563 106 };
gr66 6:798481567563 107
gr66 6:798481567563 108 /// accel_scale defines all possible FSR's of the accelerometer:
gr66 6:798481567563 109 enum accel_scale
gr66 6:798481567563 110 {
gr66 6:798481567563 111 A_SCALE_2G, // 00: +/- 2g
gr66 6:798481567563 112 A_SCALE_16G,// 01: +/- 16g
gr66 6:798481567563 113 A_SCALE_4G, // 10: +/- 4g
gr66 6:798481567563 114 A_SCALE_8G // 11: +/- 8g
gr66 6:798481567563 115 };
gr66 6:798481567563 116
gr66 6:798481567563 117 /// accel_oder defines all possible output data rates of the accelerometer:
gr66 6:798481567563 118 enum accel_odr
gr66 6:798481567563 119 {
gr66 6:798481567563 120 A_POWER_DOWN, // Power-down mode (0x0)
gr66 6:798481567563 121 A_ODR_13, // 12.5 Hz (0x1) low power
gr66 6:798481567563 122 A_ODR_26, // 26 Hz (0x2) low power
gr66 6:798481567563 123 A_ODR_52, // 52 Hz (0x3) low power
gr66 6:798481567563 124 A_ODR_104, // 104 Hz (0x4) normal mode
gr66 6:798481567563 125 A_ODR_208, // 208 Hz (0x5) normal mode
gr66 6:798481567563 126 A_ODR_416, // 416 Hz (0x6) high performance
gr66 6:798481567563 127 A_ODR_833, // 833 Hz (0x7) high performance
gr66 6:798481567563 128 A_ODR_1660, // 1.66 kHz (0x8) high performance
gr66 6:798481567563 129 A_ODR_3330, // 3.33 kHz (0x9) high performance
gr66 6:798481567563 130 A_ODR_6660, // 6.66 kHz (0xA) high performance
gr66 6:798481567563 131 };
gr66 6:798481567563 132
gr66 6:798481567563 133 // accel_bw defines all possible bandwiths for low-pass filter of the accelerometer:
gr66 6:798481567563 134 enum accel_bw
gr66 6:798481567563 135 {
gr66 6:798481567563 136
gr66 6:798481567563 137 A_BW_400 = 0x0, // 400 Hz
gr66 6:798481567563 138 A_BW_200 = 0x1, // 200 Hz
gr66 6:798481567563 139 A_BW_100 = 0x2, // 100 Hz
gr66 6:798481567563 140 A_BW_50 = 0x3 // 50 Hz
gr66 6:798481567563 141 };
gr66 6:798481567563 142
gr66 6:798481567563 143
gr66 6:798481567563 144
gr66 6:798481567563 145 // We'll store the gyro, and accel, readings in a series of
gr66 6:798481567563 146 // public class variables. Each sensor gets three variables -- one for each
gr66 6:798481567563 147 // axis. Call readGyro(), and readAccel() first, before using
gr66 6:798481567563 148 // these variables!
gr66 6:798481567563 149 // These values are the RAW signed 16-bit readings from the sensors.
gr66 6:798481567563 150 int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope
gr66 6:798481567563 151 int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer
gr66 6:798481567563 152 int16_t temperature_raw;
gr66 6:798481567563 153 int32_t time_raw;
gr66 6:798481567563 154 // modif GR1 valeurs brutes en octet
gr66 6:798481567563 155 char gxh,gxl, gyh, gyl, gzh, gzl; // x, y, and z axis readings of the gyroscope
gr66 6:798481567563 156 char axh,axl, ayh, ayl, azh, azl; // x, y, and z axis readings of the accelerometer
gr66 6:798481567563 157 char gxoh,gxol, gyoh, gyol, gzoh, gzol;// offset
gr66 6:798481567563 158
gr66 6:798481567563 159 // floating-point values of scaled data in real-world units
gr66 6:798481567563 160 double gx, gy, gz;
gr66 6:798481567563 161 double gx_off,gy_off,gz_off;
gr66 6:798481567563 162 double ax, ay, az;
gr66 6:798481567563 163 double temperature_c, temperature_f; // temperature in celcius and fahrenheit
gr66 6:798481567563 164 double intr;
gr66 6:798481567563 165 double time;
gr66 6:798481567563 166
gr66 6:798481567563 167
gr66 6:798481567563 168 /** LSM6DS33 -- LSM6DS33 class constructor
gr66 6:798481567563 169 * The constructor will set up a handful of private variables, and set the
gr66 6:798481567563 170 * communication mode as well.
gr66 6:798481567563 171 * Input:
gr66 6:798481567563 172 * - interface = Either MODE_SPI or MODE_I2C, whichever you're using
gr66 6:798481567563 173 * to talk to the IC.
gr66 6:798481567563 174 * - xgAddr = If MODE_I2C, this is the I2C address of the accel/gyro.
gr66 6:798481567563 175 * If MODE_SPI, this is the chip select pin of the accel/gyro (CS_A/G)
gr66 6:798481567563 176 */
gr66 6:798481567563 177 LSM6DS33(PinName sda, PinName scl, uint8_t xgAddr = LSM6DS33_AG_I2C_ADDR(1));
gr66 6:798481567563 178
gr66 6:798481567563 179 /** begin() -- Initialize the gyro, and accelerometer.
gr66 6:798481567563 180 * This will set up the scale and output rate of each sensor. It'll also
gr66 6:798481567563 181 * "turn on" every sensor and every axis of every sensor.
gr66 6:798481567563 182 * Input:
gr66 6:798481567563 183 * - gScl = The scale of the gyroscope. This should be a gyro_scale value.
gr66 6:798481567563 184 * - aScl = The scale of the accelerometer. Should be a accel_scale value.
gr66 6:798481567563 185 * - gODR = Output data rate of the gyroscope. gyro_odr value.
gr66 6:798481567563 186 * - aODR = Output data rate of the accelerometer. accel_odr value.
gr66 6:798481567563 187 * Output: The function will return an unsigned 16-bit value. The most-sig
gr66 6:798481567563 188 * bytes of the output are the WHO_AM_I reading of the accel/gyro.
gr66 6:798481567563 189 * All parameters have a defaulted value, so you can call just "begin()".
gr66 6:798481567563 190 * Default values are FSR's of: +/- 245DPS, 4g, 2Gs; ODRs of 119 Hz for
gr66 6:798481567563 191 * gyro, 119 Hz for accelerometer.
gr66 6:798481567563 192 * Use the return value of this function to verify communication.
gr66 6:798481567563 193 */
gr66 6:798481567563 194 uint16_t begin(gyro_scale gScl = G_SCALE_250DPS,
gr66 6:798481567563 195 accel_scale aScl = A_SCALE_2G, gyro_odr gODR = G_ODR_104,
gr66 6:798481567563 196 accel_odr aODR = A_ODR_104);
gr66 6:798481567563 197 // modif GR1 readAllraw
gr66 6:798481567563 198 void readAllraw();
gr66 6:798481567563 199
gr66 6:798481567563 200 void readAll();
gr66 6:798481567563 201
gr66 6:798481567563 202 /** readGyro() -- Read the gyroscope output registers.
gr66 6:798481567563 203 * This function will read all six gyroscope output registers.
gr66 6:798481567563 204 * The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read
gr66 6:798481567563 205 * those _after_ calling readGyro().
gr66 6:798481567563 206 */
gr66 6:798481567563 207 void readGyro();
gr66 6:798481567563 208
gr66 6:798481567563 209 /** readAccel() -- Read the accelerometer output registers.
gr66 6:798481567563 210 * This function will read all six accelerometer output registers.
gr66 6:798481567563 211 * The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read
gr66 6:798481567563 212 * those _after_ calling readAccel().
gr66 6:798481567563 213 */
gr66 6:798481567563 214 void readAccel();
gr66 6:798481567563 215
gr66 6:798481567563 216 /** readTemp() -- Read the temperature output register.
gr66 6:798481567563 217 * This function will read two temperature output registers.
gr66 6:798481567563 218 * The combined readings are stored in the class' temperature variables. Read
gr66 6:798481567563 219 * those _after_ calling readTemp().
gr66 6:798481567563 220 */
gr66 6:798481567563 221 void readTemp();
gr66 6:798481567563 222
gr66 6:798481567563 223 /** Read Interrupt **/
gr66 6:798481567563 224 void readIntr();
gr66 6:798481567563 225
gr66 6:798481567563 226 /** setGyroScale() -- Set the full-scale range of the gyroscope.
gr66 6:798481567563 227 * This function can be called to set the scale of the gyroscope to
gr66 6:798481567563 228 * 245, 500, or 2000 degrees per second.
gr66 6:798481567563 229 * Input:
gr66 6:798481567563 230 * - gScl = The desired gyroscope scale. Must be one of three possible
gr66 6:798481567563 231 * values from the gyro_scale enum.
gr66 6:798481567563 232 */
gr66 6:798481567563 233 void setGyroScale(gyro_scale gScl);
gr66 6:798481567563 234
gr66 6:798481567563 235 /** setAccelScale() -- Set the full-scale range of the accelerometer.
gr66 6:798481567563 236 * This function can be called to set the scale of the accelerometer to
gr66 6:798481567563 237 * 2, 4, 8, or 16 g's.
gr66 6:798481567563 238 * Input:
gr66 6:798481567563 239 * - aScl = The desired accelerometer scale. Must be one of five possible
gr66 6:798481567563 240 * values from the accel_scale enum.
gr66 6:798481567563 241 */
gr66 6:798481567563 242 void setAccelScale(accel_scale aScl);
gr66 6:798481567563 243
gr66 6:798481567563 244 /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
gr66 6:798481567563 245 * Input:
gr66 6:798481567563 246 * - gRate = The desired output rate and cutoff frequency of the gyro.
gr66 6:798481567563 247 * Must be a value from the gyro_odr enum (check above).
gr66 6:798481567563 248 */
gr66 6:798481567563 249 void setGyroODR(gyro_odr gRate);
gr66 6:798481567563 250
gr66 6:798481567563 251 /** setAccelODR() -- Set the output data rate of the accelerometer
gr66 6:798481567563 252 * Input:
gr66 6:798481567563 253 * - aRate = The desired output rate of the accel.
gr66 6:798481567563 254 * Must be a value from the accel_odr enum (check above).
gr66 6:798481567563 255 */
gr66 6:798481567563 256 void setAccelODR(accel_odr aRate);
gr66 6:798481567563 257 /** calibration() -- Calculate the offset of the accel and the gyro
gr66 6:798481567563 258 */
gr66 6:798481567563 259 void calibration( int16_t iter);
gr66 6:798481567563 260
gr66 6:798481567563 261 private:
gr66 6:798481567563 262 /** xgAddress store the I2C address
gr66 6:798481567563 263 * for each sensor.
gr66 6:798481567563 264 */
gr66 6:798481567563 265 uint8_t xgAddress;
gr66 6:798481567563 266
gr66 6:798481567563 267 // I2C bus
gr66 6:798481567563 268 I2C i2c;
gr66 6:798481567563 269
gr66 6:798481567563 270 /** gScale, and aScale store the current scale range for each
gr66 6:798481567563 271 * sensor. Should be updated whenever that value changes.
gr66 6:798481567563 272 */
gr66 6:798481567563 273 gyro_scale gScale;
gr66 6:798481567563 274 accel_scale aScale;
gr66 6:798481567563 275
gr66 6:798481567563 276 /** gRes, and aRes store the current resolution for each sensor.
gr66 6:798481567563 277 * Units of these values would be DPS (or g's or Gs's) per ADC tick.
gr66 6:798481567563 278 * This value is calculated as (sensor scale) / (2^15).
gr66 6:798481567563 279 */
gr66 6:798481567563 280 float gRes, aRes;
gr66 6:798481567563 281
gr66 6:798481567563 282 /** initGyro() -- Sets up the gyroscope to begin reading.
gr66 6:798481567563 283 * This function steps through all three gyroscope control registers.
gr66 6:798481567563 284 */
gr66 6:798481567563 285 void initGyro();
gr66 6:798481567563 286
gr66 6:798481567563 287 /** initAccel() -- Sets up the accelerometer to begin reading.
gr66 6:798481567563 288 * This function steps through all accelerometer related control registers.
gr66 6:798481567563 289 */
gr66 6:798481567563 290 void initAccel();
gr66 6:798481567563 291
gr66 6:798481567563 292 /** Setup Interrupt **/
gr66 6:798481567563 293 void initIntr();
gr66 6:798481567563 294
gr66 6:798481567563 295 /** calcgRes() -- Calculate the resolution of the gyroscope.
gr66 6:798481567563 296 * This function will set the value of the gRes variable. gScale must
gr66 6:798481567563 297 * be set prior to calling this function.
gr66 6:798481567563 298 */
gr66 6:798481567563 299 void calcgRes();
gr66 6:798481567563 300
gr66 6:798481567563 301 /** calcaRes() -- Calculate the resolution of the accelerometer.
gr66 6:798481567563 302 * This function will set the value of the aRes variable. aScale must
gr66 6:798481567563 303 * be set prior to calling this function.
gr66 6:798481567563 304 */
gr66 6:798481567563 305 void calcaRes();
gr66 6:798481567563 306
gr66 6:798481567563 307 };
gr66 6:798481567563 308
gr66 6:798481567563 309 #endif // _LSM6DS33_H //