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 
141 public:
142  Adafruit_LSM6DS();
143  virtual ~Adafruit_LSM6DS();
144 
145  bool begin_I2C(uint8_t i2c_addr = LSM6DS_I2CADDR_DEFAULT,
146  TwoWire *wire = &Wire, int32_t sensorID = 0);
147 
148  bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI, int32_t sensorID = 0,
149  uint32_t frequency = 1000000);
150  bool begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
151  int8_t mosi_pin, int32_t sensorID = 0,
152  uint32_t frequency = 1000000);
153 
154  bool getEvent(sensors_event_t *accel, sensors_event_t *gyro,
155  sensors_event_t *temp);
156 
157  lsm6ds_data_rate_t getAccelDataRate(void);
158  void setAccelDataRate(lsm6ds_data_rate_t data_rate);
159 
160  lsm6ds_accel_range_t getAccelRange(void);
161  void setAccelRange(lsm6ds_accel_range_t new_range);
162 
163  lsm6ds_data_rate_t getGyroDataRate(void);
164  void setGyroDataRate(lsm6ds_data_rate_t data_rate);
165 
166  lsm6ds_gyro_range_t getGyroRange(void);
167  void setGyroRange(lsm6ds_gyro_range_t new_range);
168 
169  void reset(void);
170  void configIntOutputs(bool active_low, bool open_drain);
171  void configInt1(bool drdy_temp, bool drdy_g, bool drdy_xl,
172  bool step_detect = false, bool wakeup = false);
173  void configInt2(bool drdy_temp, bool drdy_g, bool drdy_xl);
174  void highPassFilter(bool enabled, lsm6ds_hp_filter_t filter);
175 
176  void enableWakeup(bool enable, uint8_t duration = 0, uint8_t thresh = 20);
177  bool awake(void);
178  bool shake(void);
179 
180  void enablePedometer(bool enable);
181  void resetPedometer(void);
182  uint16_t readPedometer(void);
183 
184  // Arduino compatible API
185  int readAcceleration(float &x, float &y, float &z);
186  float accelerationSampleRate(void);
187  int accelerationAvailable(void);
188 
189  int readGyroscope(float &x, float &y, float &z);
190  float gyroscopeSampleRate(void);
191  int gyroscopeAvailable(void);
192 
193  int16_t rawAccX,
194  rawAccY,
195  rawAccZ,
196  rawTemp,
197  rawGyroX,
198  rawGyroY,
199  rawGyroZ;
200 
201  float temperature,
202  accX,
203  accY,
204  accZ,
205  gyroX,
206  gyroY,
207  gyroZ;
208 
209  Adafruit_Sensor *getTemperatureSensor(void);
210  Adafruit_Sensor *getAccelerometerSensor(void);
211  Adafruit_Sensor *getGyroSensor(void);
212 
213 protected:
214  uint8_t chipID(void);
215  uint8_t status(void);
216  virtual void _read(void);
217  virtual bool _init(int32_t sensor_id);
218 
219  uint16_t _sensorid_accel,
220  _sensorid_gyro,
222 
223  Adafruit_I2CDevice *i2c_dev = NULL;
224  Adafruit_SPIDevice *spi_dev = NULL;
225 
226  float temperature_sensitivity =
227  256.0;
228  Adafruit_LSM6DS_Temp *temp_sensor = NULL;
230  NULL;
231  Adafruit_LSM6DS_Gyro *gyro_sensor = NULL;
232 
234  lsm6ds_accel_range_t accelRangeBuffered = LSM6DS_ACCEL_RANGE_2_G;
236  lsm6ds_gyro_range_t gyroRangeBuffered = LSM6DS_GYRO_RANGE_250_DPS;
237 
238 private:
239  friend class Adafruit_LSM6DS_Temp;
240  friend class Adafruit_LSM6DS_Accelerometer;
242  friend class Adafruit_LSM6DS_Gyro;
245 
247  void fillTempEvent(sensors_event_t *temp, uint32_t timestamp);
248  void fillAccelEvent(sensors_event_t *accel, uint32_t timestamp);
249  void fillGyroEvent(sensors_event_t *gyro, uint32_t timestamp);
250 };
251 
252 #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:201
uint16_t _sensorid_temp
ID number for temperature.
Definition: Adafruit_LSM6DS.h:219
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:193
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
Base class for use with LSM6DS series acclerometer gyro sensors from STMicroelectronics. DO NOT USE DIRECTLY. Specific sensor variants should be subclassed as needed.
Definition: Adafruit_LSM6DS.h:140
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