Adafruit ICM20X Sensor Library
Adafruit_ICM20X.h
Go to the documentation of this file.
1 
19 #ifndef _ADAFRUIT_ICM20X_H
20 #define _ADAFRUIT_ICM20X_H
21 
22 #include "Arduino.h"
23 #include <Adafruit_BusIO_Register.h>
24 #include <Adafruit_I2CDevice.h>
25 #include <Adafruit_Sensor.h>
26 #include <Wire.h>
27 
28 // Misc configuration macros
29 #define I2C_MASTER_RESETS_BEFORE_FAIL \
30  5
31 #define NUM_FINISHED_CHECKS \
33  100
34 
35 // Bank 0
36 #define ICM20X_B0_WHOAMI 0x00
37 #define ICM20X_B0_USER_CTRL 0x03
38 #define ICM20X_B0_LP_CONFIG 0x05
39 #define ICM20X_B0_REG_INT_PIN_CFG 0xF
40 #define ICM20X_B0_REG_INT_ENABLE 0x10
41 #define ICM20X_B0_REG_INT_ENABLE_1 0x11
42 #define ICM20X_B0_I2C_MST_STATUS \
43  0x17
44 #define ICM20X_B0_REG_BANK_SEL 0x7F
45 #define ICM20X_B0_PWR_MGMT_1 0x06
46 #define ICM20X_B0_ACCEL_XOUT_H 0x2D
47 #define ICM20X_B0_GYRO_XOUT_H 0x33
48 
49 // Bank 2
50 #define ICM20X_B2_GYRO_SMPLRT_DIV 0x00
51 #define ICM20X_B2_GYRO_CONFIG_1 0x01
52 #define ICM20X_B2_ACCEL_SMPLRT_DIV_1 0x10
53 #define ICM20X_B2_ACCEL_SMPLRT_DIV_2 0x11
54 #define ICM20X_B2_ACCEL_CONFIG_1 0x14
55 
56 // Bank 3
57 #define ICM20X_B3_I2C_MST_ODR_CONFIG 0x0
58 #define ICM20X_B3_I2C_MST_CTRL 0x1
59 #define ICM20X_B3_I2C_MST_DELAY_CTRL 0x2
60 #define ICM20X_B3_I2C_SLV0_ADDR \
61  0x3
62 #define ICM20X_B3_I2C_SLV0_REG \
63  0x4
64 #define ICM20X_B3_I2C_SLV0_CTRL 0x5
65 #define ICM20X_B3_I2C_SLV0_DO 0x6
66 
67 #define ICM20X_B3_I2C_SLV4_ADDR \
68  0x13
69 #define ICM20X_B3_I2C_SLV4_REG \
70  0x14
71 #define ICM20X_B3_I2C_SLV4_CTRL 0x15
72 #define ICM20X_B3_I2C_SLV4_DO 0x16
73 #define ICM20X_B3_I2C_SLV4_DI 0x17
74 
75 #define ICM20948_CHIP_ID 0xEA
76 #define ICM20649_CHIP_ID 0xE1
77 
78 
79 typedef enum {
80  ICM20X_ACCEL_FREQ_246_0_HZ = 0x1,
81  ICM20X_ACCEL_FREQ_111_4_HZ = 0x2,
82  ICM20X_ACCEL_FREQ_50_4_HZ = 0x3,
83  ICM20X_ACCEL_FREQ_23_9_HZ = 0x4,
84  ICM20X_ACCEL_FREQ_11_5_HZ = 0x5,
85  ICM20X_ACCEL_FREQ_5_7_HZ = 0x6,
86  ICM20X_ACCEL_FREQ_473_HZ = 0x7,
88 
90 typedef enum {
91  ICM20X_GYRO_FREQ_196_6_HZ = 0x0,
92  ICM20X_GYRO_FREQ_151_8_HZ = 0x1,
93  ICM20X_GYRO_FREQ_119_5_HZ = 0x2,
94  ICM20X_GYRO_FREQ_51_2_HZ = 0x3,
95  ICM20X_GYRO_FREQ_23_9_HZ = 0x4,
96  ICM20X_GYRO_FREQ_11_6_HZ = 0x5,
97  ICM20X_GYRO_FREQ_5_7_HZ = 0x6,
98  ICM20X_GYRO_FREQ_361_4_HZ = 0x7,
99 
101 
102 class Adafruit_ICM20X;
103 
105 class Adafruit_ICM20X_Accelerometer : public Adafruit_Sensor {
106 public:
111  _theICM20X = parent;
112  }
113  bool getEvent(sensors_event_t *);
114  void getSensor(sensor_t *);
115 
116 private:
117  int _sensorID = 0x20A;
118  Adafruit_ICM20X *_theICM20X = NULL;
119 };
120 
122 class Adafruit_ICM20X_Gyro : public Adafruit_Sensor {
123 public:
126  Adafruit_ICM20X_Gyro(Adafruit_ICM20X *parent) { _theICM20X = parent; }
127  bool getEvent(sensors_event_t *);
128  void getSensor(sensor_t *);
129 
130 private:
131  int _sensorID = 0x20B;
132  Adafruit_ICM20X *_theICM20X = NULL;
133 };
134 
136 class Adafruit_ICM20X_Magnetometer : public Adafruit_Sensor {
137 public:
141  Adafruit_ICM20X_Magnetometer(Adafruit_ICM20X *parent) { _theICM20X = parent; }
142  bool getEvent(sensors_event_t *);
143  void getSensor(sensor_t *);
144 
145 private:
146  int _sensorID = 0x20C;
147  Adafruit_ICM20X *_theICM20X = NULL;
148 };
149 
151 class Adafruit_ICM20X_Temp : public Adafruit_Sensor {
152 public:
155  Adafruit_ICM20X_Temp(Adafruit_ICM20X *parent) { _theICM20X = parent; }
156  bool getEvent(sensors_event_t *);
157  void getSensor(sensor_t *);
158 
159 private:
160  int _sensorID = 0x20D;
161  Adafruit_ICM20X *_theICM20X = NULL;
162 };
163 
169 public:
170  Adafruit_ICM20X();
171  ~Adafruit_ICM20X();
172 
173  bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI,
174  int32_t sensor_id = 0);
175  bool begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
176  int8_t mosi_pin, int32_t sensor_id = 0);
177 
178  uint8_t getGyroRateDivisor(void);
179  void setGyroRateDivisor(uint8_t new_gyro_divisor);
180 
181  uint16_t getAccelRateDivisor(void);
182  void setAccelRateDivisor(uint16_t new_accel_divisor);
183 
184  bool enableAccelDLPF(bool enable, icm20x_accel_cutoff_t cutoff_freq);
185  bool enableGyrolDLPF(bool enable, icm20x_gyro_cutoff_t cutoff_freq);
186 
187  void reset(void);
188 
189  // TODO: bool-ify
190  void setInt1ActiveLow(bool active_low);
191  void setInt2ActiveLow(bool active_low);
192 
193  Adafruit_Sensor *getAccelerometerSensor(void);
194  Adafruit_Sensor *getGyroSensor(void);
195  Adafruit_Sensor *getMagnetometerSensor(void);
196  Adafruit_Sensor *getTemperatureSensor(void);
197 
198  bool getEvent(sensors_event_t *accel, sensors_event_t *gyro,
199  sensors_event_t *temp, sensors_event_t *mag = NULL);
200 
201  uint8_t readExternalRegister(uint8_t slv_addr, uint8_t reg_addr);
202  bool writeExternalRegister(uint8_t slv_addr, uint8_t reg_addr, uint8_t value);
203  bool configureI2CMaster(void);
204  bool enableI2CMaster(bool enable_i2c_master);
205  void resetI2CMaster(void);
206  void setI2CBypass(bool bypass_i2c);
207 
208 protected:
209  float temperature,
210  accX,
211  accY,
212  accZ,
213  gyroX,
214  gyroY,
215  gyroZ,
216  magX,
217  magY,
218  magZ;
219 
220  Adafruit_I2CDevice *i2c_dev = NULL;
221  Adafruit_SPIDevice *spi_dev = NULL;
222 
224  NULL;
225  Adafruit_ICM20X_Gyro *gyro_sensor = NULL;
227  NULL;
228  Adafruit_ICM20X_Temp *temp_sensor = NULL;
229  uint16_t _sensorid_accel,
230  _sensorid_gyro,
231  _sensorid_mag,
233 
234  void _read(void);
235  virtual void scaleValues(void);
236  virtual bool begin_I2C(uint8_t i2c_add, TwoWire *wire, int32_t sensor_id);
237  // virtual bool _init(int32_t sensor_id);
238  bool _init(int32_t sensor_id);
239  int16_t rawAccX,
240  rawAccY,
241  rawAccZ,
242  rawTemp,
243  rawGyroX,
244  rawGyroY,
245  rawGyroZ,
246  rawMagX,
247  rawMagY,
248  rawMagZ;
249 
252  // virtual void _setBank(uint8_t bank_number);
253  void _setBank(uint8_t bank_number);
254 
255  uint8_t readAccelRange(void);
256  void writeAccelRange(uint8_t new_accel_range);
257 
258  uint8_t readGyroRange(void);
259  void writeGyroRange(uint8_t new_gyro_range);
260 
261 private:
263  friend class Adafruit_ICM20X_Gyro;
266  friend class Adafruit_ICM20X_Magnetometer;
268 
271  friend class Adafruit_ICM20X_Temp;
272 
274  void fillAccelEvent(sensors_event_t *accel, uint32_t timestamp);
275  void fillGyroEvent(sensors_event_t *gyro, uint32_t timestamp);
276  void fillTempEvent(sensors_event_t *temp, uint32_t timestamp);
277  void fillMagEvent(sensors_event_t *mag, uint32_t timestamp);
278  uint8_t auxillaryRegisterTransaction(bool read, uint8_t slv_addr,
279  uint8_t reg_addr, uint8_t value = -1);
280 };
281 
282 #endif
Definition: Adafruit_ICM20X.h:122
Adafruit_ICM20X_Accelerometer(Adafruit_ICM20X *parent)
Create an Adafruit_Sensor compatible object for the accelerometer sensor.
Definition: Adafruit_ICM20X.h:110
uint8_t current_gyro_range
gyro range cache
Definition: Adafruit_ICM20X.h:251
Definition: Adafruit_ICM20X.h:136
Class that stores state and functions for interacting with the ST ICM20X 6-DoF Accelerometer and Gyro...
Definition: Adafruit_ICM20X.h:168
int16_t rawTemp
temp variables
Definition: Adafruit_ICM20X.h:239
void getSensor(sensor_t *)
Gets the sensor_t data for the ICM20X&#39;s accelerometer.
Definition: Adafruit_ICM20X.cpp:845
float temperature
Last reading&#39;s temperature (C)
Definition: Adafruit_ICM20X.h:209
uint8_t current_accel_range
accelerometer range cache
Definition: Adafruit_ICM20X.h:250
icm20x_gyro_cutoff_t
Definition: Adafruit_ICM20X.h:90
Definition: Adafruit_ICM20X.h:151
Adafruit_ICM20X_Magnetometer(Adafruit_ICM20X *parent)
Create an Adafruit_Sensor compatible object for the magnetometer sensor.
Definition: Adafruit_ICM20X.h:141
uint16_t _sensorid_temp
ID number for temperature.
Definition: Adafruit_ICM20X.h:229
bool getEvent(sensors_event_t *)
Gets the accelerometer as a standard sensor event.
Definition: Adafruit_ICM20X.cpp:869
Definition: Adafruit_ICM20X.h:105
Adafruit_ICM20X_Gyro(Adafruit_ICM20X *parent)
Create an Adafruit_Sensor compatible object for the gyro sensor.
Definition: Adafruit_ICM20X.h:126
icm20x_accel_cutoff_t
Definition: Adafruit_ICM20X.h:79
Adafruit_ICM20X_Temp(Adafruit_ICM20X *parent)
Create an Adafruit_Sensor compatible object for the temp sensor.
Definition: Adafruit_ICM20X.h:155