Adafruit LSM6DS Sensors Library
Adafruit_LSM6DS.h
Go to the documentation of this file.
1 
14 #ifndef _ADAFRUIT_LSM6DS_H
15 #define _ADAFRUIT_LSM6DS_H
16 
17 #include "Arduino.h"
18 #include <Adafruit_BusIO_Register.h>
19 #include <Adafruit_I2CDevice.h>
20 #include <Adafruit_Sensor.h>
21 #include <Wire.h>
22 
23 #define LSM6DS_I2CADDR_DEFAULT 0x6A
24 
25 #define LSM6DS_FUNC_CFG_ACCESS 0x1
26 #define LSM6DS_INT1_CTRL 0x0D
27 #define LSM6DS_INT2_CTRL 0x0E
28 #define LSM6DS_WHOAMI 0x0F
29 #define LSM6DS_CTRL1_XL 0x10
30 #define LSM6DS_CTRL2_G 0x11
31 #define LSM6DS_CTRL3_C 0x12
32 #define LSM6DS_CTRL8_XL 0x17
33 #define LSM6DS_CTRL10_C 0x19
34 #define LSM6DS_WAKEUP_SRC 0x1B
35 #define LSM6DS_STATUS_REG 0X1E
36 #define LSM6DS_OUT_TEMP_L 0x20
37 #define LSM6DS_OUTX_L_G 0x22
38 #define LSM6DS_OUTX_L_A 0x28
39 #define LSM6DS_STEPCOUNTER 0x4B
40 #define LSM6DS_TAP_CFG 0x58
41 #define LSM6DS_WAKEUP_THS \
42  0x5B
43 #define LSM6DS_WAKEUP_DUR \
44  0x5C
45 #define LSM6DS_MD1_CFG 0x5E
46 
47 
48 typedef enum data_rate {
49  LSM6DS_RATE_SHUTDOWN,
50  LSM6DS_RATE_12_5_HZ,
51  LSM6DS_RATE_26_HZ,
52  LSM6DS_RATE_52_HZ,
53  LSM6DS_RATE_104_HZ,
54  LSM6DS_RATE_208_HZ,
55  LSM6DS_RATE_416_HZ,
56  LSM6DS_RATE_833_HZ,
57  LSM6DS_RATE_1_66K_HZ,
58  LSM6DS_RATE_3_33K_HZ,
59  LSM6DS_RATE_6_66K_HZ,
61 
63 typedef enum accel_range {
64  LSM6DS_ACCEL_RANGE_2_G,
65  LSM6DS_ACCEL_RANGE_16_G,
66  LSM6DS_ACCEL_RANGE_4_G,
67  LSM6DS_ACCEL_RANGE_8_G
69 
71 typedef enum gyro_range {
72  LSM6DS_GYRO_RANGE_125_DPS = 0b0010,
73  LSM6DS_GYRO_RANGE_250_DPS = 0b0000,
74  LSM6DS_GYRO_RANGE_500_DPS = 0b0100,
75  LSM6DS_GYRO_RANGE_1000_DPS = 0b1000,
76  LSM6DS_GYRO_RANGE_2000_DPS = 0b1100,
77  ISM330DHCX_GYRO_RANGE_4000_DPS = 0b0001
79 
81 typedef enum hpf_range {
82  LSM6DS_HPF_ODR_DIV_50 = 0,
83  LSM6DS_HPF_ODR_DIV_100 = 1,
84  LSM6DS_HPF_ODR_DIV_9 = 2,
85  LSM6DS_HPF_ODR_DIV_400 = 3,
87 
88 class Adafruit_LSM6DS;
89 
91 class Adafruit_LSM6DS_Temp : public Adafruit_Sensor {
92 public:
95  Adafruit_LSM6DS_Temp(Adafruit_LSM6DS *parent) { _theLSM6DS = parent; }
96  bool getEvent(sensors_event_t *);
97  void getSensor(sensor_t *);
98 
99 private:
100  int _sensorID = 0x6D0;
101  Adafruit_LSM6DS *_theLSM6DS = NULL;
102 };
103 
105 class Adafruit_LSM6DS_Accelerometer : public Adafruit_Sensor {
106 public:
111  _theLSM6DS = parent;
112  }
113  bool getEvent(sensors_event_t *);
114  void getSensor(sensor_t *);
115 
116 private:
117  int _sensorID = 0x6D1;
118  Adafruit_LSM6DS *_theLSM6DS = NULL;
119 };
120 
122 class Adafruit_LSM6DS_Gyro : public Adafruit_Sensor {
123 public:
126  Adafruit_LSM6DS_Gyro(Adafruit_LSM6DS *parent) { _theLSM6DS = parent; }
127  bool getEvent(sensors_event_t *);
128  void getSensor(sensor_t *);
129 
130 private:
131  int _sensorID = 0x6D2;
132  Adafruit_LSM6DS *_theLSM6DS = NULL;
133 };
134 
140 public:
141  Adafruit_LSM6DS();
142  virtual ~Adafruit_LSM6DS();
143 
144  bool begin_I2C(uint8_t i2c_addr = LSM6DS_I2CADDR_DEFAULT,
145  TwoWire *wire = &Wire, int32_t sensorID = 0);
146 
147  bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI, int32_t sensorID = 0,
148  uint32_t frequency = 1000000);
149  bool begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
150  int8_t mosi_pin, int32_t sensorID = 0,
151  uint32_t frequency = 1000000);
152 
153  bool getEvent(sensors_event_t *accel, sensors_event_t *gyro,
154  sensors_event_t *temp);
155 
156  lsm6ds_data_rate_t getAccelDataRate(void);
157  void setAccelDataRate(lsm6ds_data_rate_t data_rate);
158 
159  lsm6ds_accel_range_t getAccelRange(void);
160  void setAccelRange(lsm6ds_accel_range_t new_range);
161 
162  lsm6ds_data_rate_t getGyroDataRate(void);
163  void setGyroDataRate(lsm6ds_data_rate_t data_rate);
164 
165  lsm6ds_gyro_range_t getGyroRange(void);
166  void setGyroRange(lsm6ds_gyro_range_t new_range);
167 
168  void reset(void);
169  void configIntOutputs(bool active_low, bool open_drain);
170  void configInt1(bool drdy_temp, bool drdy_g, bool drdy_xl,
171  bool step_detect = false, bool wakeup = false);
172  void configInt2(bool drdy_temp, bool drdy_g, bool drdy_xl);
173  void highPassFilter(bool enabled, lsm6ds_hp_filter_t filter);
174 
175  void enableWakeup(bool enable, uint8_t duration = 0, uint8_t thresh = 20);
176  bool awake(void);
177  bool shake(void);
178 
179  void enablePedometer(bool enable);
180  void resetPedometer(void);
181  uint16_t readPedometer(void);
182 
183  // Arduino compatible API
184  int readAcceleration(float &x, float &y, float &z);
185  float accelerationSampleRate(void);
186  int accelerationAvailable(void);
187 
188  int readGyroscope(float &x, float &y, float &z);
189  float gyroscopeSampleRate(void);
190  int gyroscopeAvailable(void);
191 
192  int16_t rawAccX,
193  rawAccY,
194  rawAccZ,
195  rawTemp,
196  rawGyroX,
197  rawGyroY,
198  rawGyroZ;
199 
200  float temperature,
201  accX,
202  accY,
203  accZ,
204  gyroX,
205  gyroY,
206  gyroZ;
207 
208  Adafruit_Sensor *getTemperatureSensor(void);
209  Adafruit_Sensor *getAccelerometerSensor(void);
210  Adafruit_Sensor *getGyroSensor(void);
211 
212 protected:
213  uint8_t chipID(void);
214  uint8_t status(void);
215  virtual void _read(void);
216  virtual bool _init(int32_t sensor_id);
217 
218  uint16_t _sensorid_accel,
219  _sensorid_gyro,
221 
222  Adafruit_I2CDevice *i2c_dev = NULL;
223  Adafruit_SPIDevice *spi_dev = NULL;
224 
225  float temperature_sensitivity =
226  256.0;
227  Adafruit_LSM6DS_Temp *temp_sensor = NULL;
229  NULL;
230  Adafruit_LSM6DS_Gyro *gyro_sensor = NULL;
231 
233  lsm6ds_accel_range_t accelRangeBuffered = LSM6DS_ACCEL_RANGE_2_G;
235  lsm6ds_gyro_range_t gyroRangeBuffered = LSM6DS_GYRO_RANGE_250_DPS;
236 
237 private:
238  friend class Adafruit_LSM6DS_Temp;
239  friend class Adafruit_LSM6DS_Accelerometer;
241  friend class Adafruit_LSM6DS_Gyro;
244 
246  void fillTempEvent(sensors_event_t *temp, uint32_t timestamp);
247  void fillAccelEvent(sensors_event_t *accel, uint32_t timestamp);
248  void fillGyroEvent(sensors_event_t *gyro, uint32_t timestamp);
249 };
250 
251 #endif
gyro_range
Definition: Adafruit_LSM6DS.h:71
enum data_rate lsm6ds_data_rate_t
float temperature
Last reading&#39;s temperature (C)
Definition: Adafruit_LSM6DS.h:200
uint16_t _sensorid_temp
ID number for temperature.
Definition: Adafruit_LSM6DS.h:218
Definition: Adafruit_LSM6DS.h:105
bool getEvent(sensors_event_t *)
Gets the temperature as a standard sensor event.
Definition: Adafruit_LSM6DS.cpp:682
enum gyro_range lsm6ds_gyro_range_t
enum accel_range lsm6ds_accel_range_t
int16_t rawTemp
Last reading&#39;s raw temperature reading.
Definition: Adafruit_LSM6DS.h:192
accel_range
Definition: Adafruit_LSM6DS.h:63
enum hpf_range lsm6ds_hp_filter_t
Adafruit_LSM6DS_Gyro(Adafruit_LSM6DS *parent)
Create an Adafruit_Sensor compatible object for the gyro sensor.
Definition: Adafruit_LSM6DS.h:126
Definition: Adafruit_LSM6DS.h:122
void getSensor(sensor_t *)
Gets the sensor_t data for the LSM6DS&#39;s tenperature.
Definition: Adafruit_LSM6DS.cpp:659
Definition: Adafruit_LSM6DS.h:91
Class that stores state and functions for interacting with the LSM6DS I2C Accel/Gyro.
Definition: Adafruit_LSM6DS.h:139
hpf_range
Definition: Adafruit_LSM6DS.h:81
Adafruit_LSM6DS_Temp(Adafruit_LSM6DS *parent)
Create an Adafruit_Sensor compatible object for the temp sensor.
Definition: Adafruit_LSM6DS.h:95
Adafruit_LSM6DS_Accelerometer(Adafruit_LSM6DS *parent)
Create an Adafruit_Sensor compatible object for the accelerometer sensor.
Definition: Adafruit_LSM6DS.h:110
data_rate
Definition: Adafruit_LSM6DS.h:48
#define LSM6DS_I2CADDR_DEFAULT
LSM6DS default i2c address.
Definition: Adafruit_LSM6DS.h:23