upm  0.8.0
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
l3gd20.hpp
1 /*
2  * Author: Lay, Kuan Loon <kuan.loon.lay@intel.com>
3  * Jon Trulson <jtrulson@ics.com>
4  * Copyright (c) 2016 Intel Corporation.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Thanks to https://github.com/01org/android-iio-sensors-hal for gyroscope
26  * calibration and denoise algorithm.
27  */
28 #pragma once
29 
30 #include <string>
31 #include <mraa/iio.h>
32 #include <mraa/i2c.hpp>
33 
34 #define L3GD20_DEFAULT_I2C_BUS 0
35 // if SDO tied to GND
36 #define L3GD20_DEFAULT_I2C_ADDR 0x6a
37 #define L3GD20_DEFAULT_CHIP_ID 0xd4
38 // the 'H' variant uses a different chip id
39 #define L3GD20H_DEFAULT_CHIP_ID 0xd7
40 
41 namespace upm
42 {
76 class L3GD20
77 {
78  public:
79  typedef struct {
80  float bias_x, bias_y, bias_z;
81  int count;
82  float min_x, min_y, min_z;
83  float max_x, max_y, max_z;
84  } gyro_cal_t;
85 
86  typedef struct {
87  float* buff;
88  unsigned int idx;
89  unsigned int count;
90  unsigned int sample_size;
92 
93  // NOTE: Reserved registers must not be written into or permanent
94  // device damage can result. Reading from them may return
95  // indeterminate values. Registers containing reserved bitfields
96  // must be written as 0. Reading reserved bitfields may return
97  // indeterminate values.
98 
102  typedef enum {
103  // 0x00-0x0e reserved
104 
105  REG_WHO_AM_I = 0x0f,
106 
107  // 0x10-0x1f reserved
108 
109  REG_CTRL_REG1 = 0x20,
110  REG_CTRL_REG2 = 0x21,
111  REG_CTRL_REG3 = 0x22,
112  REG_CTRL_REG4 = 0x23,
113  REG_CTRL_REG5 = 0x24,
114 
115  REG_REFERENCE = 0x25,
116 
117  REG_OUT_TEMPERATURE = 0x26,
118 
119  REG_STATUS_REG = 0x27,
120 
121  // output registers (also for FIFO output)
122  REG_OUT_X_L = 0x28,
123  REG_OUT_X_H = 0x29,
124 
125  REG_OUT_Y_L = 0x2a,
126  REG_OUT_Y_H = 0x2b,
127 
128  REG_OUT_Z_L = 0x2c,
129  REG_OUT_Z_H = 0x2d,
130 
131  REG_FIFO_CTRL_REG = 0x2e,
132  REG_FIFO_SRC_REG = 0x2f,
133 
134  REG_INT1_CFG = 0x30,
135  REG_INT1_SRC = 0x31,
136 
137  REG_INT1_TSH_XH = 0x32,
138  REG_INT1_TSH_XL = 0x33,
139 
140  REG_INT1_TSH_YH = 0x34,
141  REG_INT1_TSH_YL = 0x35,
142 
143  REG_INT1_TSH_ZH = 0x36,
144  REG_INT1_TSH_ZL = 0x37,
145  REG_INT1_DURATION = 0x38
146  } L3GD20_REGS_T;
147 
151  typedef enum {
152  CTRL_REG1_YEN = 0x01,
153  CTRL_REG1_XEN = 0x02,
154  CTRL_REG1_ZEN = 0x04,
155  CTRL_REG1_PD = 0x08,
156 
157  CTRL_REG1_BW0 = 0x10, // bandwidth
158  CTRL_REG1_BW1 = 0x20,
159  _CTRL_REG1_BW_MASK = 3,
160  _CTRL_REG1_BW_SHIFT = 4,
161 
162  CTRL_REG1_DR0 = 0x40, // data rate
163  CTRL_REG1_DR1 = 0x80,
164  _CTRL_REG1_DR_MASK = 3,
165  _CTRL_REG1_DR_SHIFT = 6,
166 
167  // together the BW and DR modes represent an output data rate
168  // (ODR) and a filter cut-off. So here, we will create a 'fake'
169  // bitfield that can be used directly with the ODR_CUTOFF enum
170  _CTRL_REG1_ODR_CUTOFF0 = 0x10,
171  _CTRL_REG1_ODR_CUTOFF1 = 0x20,
172  _CTRL_REG1_ODR_CUTOFF2 = 0x40,
173  _CTRL_REG1_ODR_CUTOFF3 = 0x80,
174  _CTRL_REG1_ODR_CUTOFF_MASK = 15,
175  _CTRL_REG1_ODR_CUTOFF_SHIFT = 4
176 
178 
182  typedef enum {
183  ODR_CUTOFF_95_12_5 = 0, // ODR 95Hz, CO 12.5
184  ODR_CUTOFF_95_25 = 1, // ODR 95Hz, CO 25
185  // 2 and 3 same as 1
186 
187  ODR_CUTOFF_190_12_5 = 4,
188  ODR_CUTOFF_190_25 = 5,
189  ODR_CUTOFF_190_50 = 6,
190  ODR_CUTOFF_190_70 = 7,
191 
192  ODR_CUTOFF_380_20 = 8,
193  ODR_CUTOFF_380_25 = 9,
194  ODR_CUTOFF_380_50 = 10,
195  ODR_CUTOFF_380_100 = 11,
196 
197  ODR_CUTOFF_760_30 = 12,
198  ODR_CUTOFF_760_35 = 13,
199  ODR_CUTOFF_760_50 = 14,
200  ODR_CUTOFF_760_100 = 15
201  } ODR_CUTOFF_T;
202 
207  typedef enum {
208  POWER_DOWN,
209  POWER_SLEEP,
210  POWER_NORMAL
211  } POWER_MODES_T;
212 
216  typedef enum {
217  _CTRL_REG2_RESERVED_BITS = 0x40 | 0x80,
218 
219  CTRL_REG2_HPCF0 = 0x01, // highpass filter cutoff
220  CTRL_REG2_HPCF1 = 0x02,
221  CTRL_REG2_HPCF2 = 0x04,
222  CTRL_REG2_HPCF3 = 0x08,
223  _CTRL_REG2_HPCF_MASK = 15,
224  _CTRL_REG2_HPCF_SHIFT = 0,
225 
226  CTRL_REG2_HPM0 = 0x10, // highpass filter mode
227  CTRL_REG2_HPM1 = 0x20,
228  _CTRL_REG2_HPM_MASK = 3,
229  _CTRL_REG2_HPM_SHIFT = 4
230 
231  // 0x40-0x80 reserved
233 
234 
238  typedef enum {
239  HPCF_7_2 = 0, // 7.2Hz CO (w/ ODR@95Hz)
240  HPCF_3_5 = 1,
241  HPCF_1_8 = 2,
242  HPCF_0_9 = 3,
243  HPCF_0_45 = 4,
244  HPCF_0_18 = 5,
245  HPCF_0_09 = 6,
246  HPCF_0_045 = 7,
247  HPCF_0_018 = 8,
248  HPCF_0_009 = 9
249  } HPCF_T;
250 
254  typedef enum {
255  HPM_NORMAL_RESET_FILTER = 0,
256  HPM_REFERENCE_SIGNAL = 1,
257  HPM_NORMAL = 2,
258  HPM_AUTORESET_ON_INT = 3
259  } HPM_T;
260 
264  typedef enum {
265  CTRL_REG3_I2_EMPTY = 0x01,
266  CTRL_REG3_I2_ORUN = 0x02,
267  CTRL_REG3_I2_WTM = 0x04,
268  CTRL_REG3_I2_DRDY = 0x08,
269  CTRL_REG3_PP_OD = 0x10,
270  CTRL_REG3_H_LACTIVE = 0x20,
271  CTRL_REG3_I1_BOOT = 0x40,
272  CTRL_REG3_I1_INT1 = 0x80
274 
278  typedef enum {
279  _CTRL_REG4_RESERVED_BITS = 0x02 | 0x04 | 0x08,
280 
281  CTRL_REG4_SIM = 0x01, // SPI 3 or 4 wire
282  // 0x02-0x08 reserved
283 
284  CTRL_REG4_FS0 = 0x10, // full scale select
285  CTRL_REG4_FS1 = 0x20,
286  _CTRL_REG4_FS_MASK = 3,
287  _CTRL_REG4_FS_SHIFT = 4,
288 
289  CTRL_REG4_BLE = 0x40, // endian selection
290  CTRL_REG4_BDU = 0x80 // block updating
292 
296  typedef enum {
297  FS_250 = 0, // 250 deg/s
298  FS_500 = 1,
299  FS_2000 = 2
300  } FS_T;
301 
305  typedef enum {
306  _CTRL_REG5_RESERVED_BITS = 0x20,
307 
308  CTRL_REG5_OUT_SEL0 = 0x01,
309  CTRL_REG5_OUT_SEL1 = 0x02,
310  _CTRL_REG5_OUT_SEL_MASK = 3,
311  _CTRL_REG5_OUT_SEL_SHIFT = 0,
312 
313  CTRL_REG5_INT1_SEL0 = 0x04,
314  CTRL_REG5_INT1_SEL1 = 0x08,
315  _CTRL_REG5_INT1_SEL_MASK = 3,
316  _CTRL_REG5_INT1_SEL_SHIFT = 2,
317 
318  CTRL_REG5_HPEN = 0x10,
319 
320  // 0x20 reserved
321  CTRL_REG5_FIFO_EN = 0x40,
322  CTRL_REG5_BOOT = 0x80
324 
328  typedef enum {
329  STATUS_REG_XDA = 0x01, // axis data avail
330  STATUS_REG_YDA = 0x02,
331  STATUS_REG_ZDA = 0x04,
332  STATUS_REG_ZYXDA = 0x08,
333 
334  STATUS_REG_XOR = 0x10, // axis data overrun
335  STATUS_REG_YOR = 0x20,
336  STATUS_REG_ZOR = 0x40,
337  STATUS_REG_ZYXOR = 0x80
339 
343  typedef enum {
344  FIFO_CTRL_REG_WTM0 = 0x01, // FIFO watermark
345  FIFO_CTRL_REG_WTM1 = 0x02,
346  FIFO_CTRL_REG_WTM2 = 0x04,
347  FIFO_CTRL_REG_WTM3 = 0x08,
348  FIFO_CTRL_REG_WTM4 = 0x10,
349  _FIFO_CTRL_REG_WTM_MASK = 31,
350  _FIFO_CTRL_REG_WTM_SHIFT = 0,
351 
352  FIFO_CTRL_REG_FM0 = 0x20, // FIFO mode
353  FIFO_CTRL_REG_FM1 = 0x40,
354  FIFO_CTRL_REG_FM2 = 0x80,
355  _FIFO_CTRL_REG_FM_MASK = 7,
356  _FIFO_CTRL_REG_FM_SHIFT = 5
358 
362  typedef enum {
363  FIFO_MODE_BYPASS = 0,
364  FIFO_MODE_FIFO = 1,
365  FIFO_MODE_STREAM = 2,
366  FIFO_MODE_STREAM_TO_FIFO = 3,
367  FIFO_MODE_BYPASS_TO_STREAM = 4
368  } FIFO_MODE_T;
369 
370 
374  typedef enum {
375  FIFO_SRC_REG_FSS0 = 0x01, // FIFO stored data level
376  FIFO_SRC_REG_FSS1 = 0x02,
377  FIFO_SRC_REG_FSS2 = 0x04,
378  FIFO_SRC_REG_FSS3 = 0x08,
379  FIFO_SRC_REG_FSS4 = 0x10,
380  _FIFO_SRC_REG_FSS_MASK = 31,
381  _FIFO_SRC_REG_FSS_SHIFT = 0,
382 
383  FIFO_SRC_REG_EMPTY = 0x20,
384  FIFO_SRC_REG_OVRN = 0x40,
385  FIFO_SRC_REG_WTM = 0x80
386  } FIFO_SRC_BITS_T;
387 
391  typedef enum {
392  INT1_CFG_XLIE = 0x01, // low intr en
393  INT1_CFG_XHIE = 0x02, // high intr en
394 
395  INT1_CFG_YLIE = 0x04,
396  INT1_CFG_YHIE = 0x08,
397 
398  INT1_CFG_ZLIE = 0x10,
399  INT1_CFG_ZHIE = 0x20,
400 
401  INT1_CFG_LIR = 0x40,
402  INT1_CFG_AND_OR = 0x80
403  } INT1_CFG_BITS_T;
404 
408  typedef enum {
409  _INT1_SRC_RESERVED_BITS = 0x80,
410 
411  INT1_SRC_XL = 0x01, // X low intr
412  INT1_SRC_XH = 0x02, // X high intr
413 
414  INT1_SRC_YL = 0x04,
415  INT1_SRC_YH = 0x08,
416 
417  INT1_SRC_ZL = 0x10,
418  INT1_SRC_ZH = 0x20,
419 
420  INT1_SRC_IA = 0x40 // intr active
421 
422  // 0x80 reserved
423  } INT1_SRC_BITS_T;
424 
428  typedef enum {
429  INT1_DURATION_D0 = 0x01,
430  INT1_DURATION_D1 = 0x02,
431  INT1_DURATION_D2 = 0x04,
432  INT1_DURATION_D3 = 0x08,
433  INT1_DURATION_D4 = 0x10,
434  INT1_DURATION_D5 = 0x20,
435  INT1_DURATION_D6 = 0x40,
436 
437  INT1_DURATION_WAIT = 0x80
439 
440 
446  L3GD20(int device);
447 
454  L3GD20(int bus, int addr);
455 
459  ~L3GD20();
460 
466  uint8_t getChipID();
467 
479  void getGyroscope(float *x, float *y, float *z);
480 
486  void setPowerMode(POWER_MODES_T mode);
487 
494  void setRange(FS_T range);
495 
501  void update();
502 
512  float getTemperature(bool fahrenheit=false);
513 
520  void setODR(ODR_CUTOFF_T odr);
521 
530  void enableBDU(bool enable);
531 
539  uint8_t getStatusBits();
540 
550  void installISR(void (*isr)(char*), void* arg);
551 
558  int64_t getChannelValue(unsigned char* input, mraa_iio_channel* chan);
559 
565  bool enableBuffer(int length);
566 
570  bool disableBuffer();
571 
580  bool setScale(const float scale);
581 
590  bool setSamplingFrequency(const float sampling_frequency);
591 
595  bool enable3AxisChannel();
596 
604  bool extract3Axis(char* data, float* x, float* y, float* z);
605 
609  void initCalibrate();
610 
614  bool getCalibratedStatus();
615 
619  void getCalibratedData(float* bias_x, float* bias_y, float* bias_z);
620 
624  void loadCalibratedData(float bias_x, float bias_y, float bias_z);
625 
632  uint8_t readReg(uint8_t reg);
633 
641  int readRegs(uint8_t reg, uint8_t *buffer, int len);
642 
649  void writeReg(uint8_t reg, uint8_t val);
650 
657  bool gyroCollect(float x, float y, float z);
658 
665  void gyroDenoiseMedian(float* x, float* y, float* z);
666 
672  float median(float* queue, unsigned int size);
673 
681  unsigned int
682  partition(float* list, unsigned int left, unsigned int right, unsigned int pivot_index);
683 
690  void clampGyroReadingsToZero(float* x, float* y, float* z);
691 
692  protected:
693  mraa::I2c *m_i2c;
694  float m_gyrScale;
695  float m_gyrX;
696  float m_gyrY;
697  float m_gyrZ;
698  float m_temperature;
699 
700  private:
701  mraa_iio_context m_iio;
702 
703  int m_iio_device_num;
704  bool m_mount_matrix_exist; // is mount matrix exist
705  float m_mount_matrix[9]; // mount matrix
706  float m_scale; // gyroscope data scale
707  int m_event_count; // sample data arrive
708  bool m_calibrated; // calibrate state
709  gyro_cal_t m_cal_data; // calibrate data
710  filter_median_t m_filter; // filter data
711 };
712 }
uint8_t getStatusBits()
Definition: l3gd20.cxx:340
int64_t getChannelValue(unsigned char *input, mraa_iio_channel *chan)
Definition: l3gd20.cxx:353
ODR_CUTOFF_T
Definition: l3gd20.hpp:182
void writeReg(uint8_t reg, uint8_t val)
Definition: l3gd20.cxx:183
void update()
Definition: l3gd20.cxx:276
void initCalibrate()
Definition: l3gd20.cxx:504
~L3GD20()
Definition: l3gd20.cxx:162
bool setSamplingFrequency(const float sampling_frequency)
Definition: l3gd20.cxx:432
bool disableBuffer()
Definition: l3gd20.cxx:414
L3GD20(int device)
Definition: l3gd20.cxx:49
int readRegs(uint8_t reg, uint8_t *buffer, int len)
Definition: l3gd20.cxx:177
uint8_t readReg(uint8_t reg)
Definition: l3gd20.cxx:172
void setODR(ODR_CUTOFF_T odr)
Definition: l3gd20.cxx:330
bool enableBuffer(int length)
Definition: l3gd20.cxx:405
L3GD20_REGS_T
Definition: l3gd20.hpp:102
STATUS_REG_BITS_T
Definition: l3gd20.hpp:328
bool setScale(const float scale)
Definition: l3gd20.cxx:421
void gyroDenoiseMedian(float *x, float *y, float *z)
Definition: l3gd20.cxx:596
uint8_t getChipID()
Definition: l3gd20.cxx:192
bool getCalibratedStatus()
Definition: l3gd20.cxx:514
CTRL_REG3_BITS_T
Definition: l3gd20.hpp:264
float getTemperature(bool fahrenheit=false)
Definition: l3gd20.cxx:322
void enableBDU(bool enable)
Definition: l3gd20.cxx:252
INT1_CFG_BITS_T
Definition: l3gd20.hpp:391
FS_T
Definition: l3gd20.hpp:296
void getCalibratedData(float *bias_x, float *bias_y, float *bias_z)
Definition: l3gd20.cxx:520
void setRange(FS_T range)
Definition: l3gd20.cxx:226
HPM_T
Definition: l3gd20.hpp:254
Definition: l3gd20.hpp:79
FIFO_MODE_T
Definition: l3gd20.hpp:362
float median(float *queue, unsigned int size)
Definition: l3gd20.cxx:626
void setPowerMode(POWER_MODES_T mode)
Definition: l3gd20.cxx:197
void loadCalibratedData(float bias_x, float bias_y, float bias_z)
Definition: l3gd20.cxx:528
POWER_MODES_T
Definition: l3gd20.hpp:207
FIFO_SRC_BITS_T
Definition: l3gd20.hpp:374
bool gyroCollect(float x, float y, float z)
Definition: l3gd20.cxx:537
bool extract3Axis(char *data, float *x, float *y, float *z)
Definition: l3gd20.cxx:455
bool enable3AxisChannel()
Definition: l3gd20.cxx:439
FIFO_CTRL_REG_BITS_T
Definition: l3gd20.hpp:343
void clampGyroReadingsToZero(float *x, float *y, float *z)
Definition: l3gd20.cxx:687
CTRL_REG2_BITS_T
Definition: l3gd20.hpp:216
unsigned int partition(float *list, unsigned int left, unsigned int right, unsigned int pivot_index)
Definition: l3gd20.cxx:657
CTRL_REG4_BITS_T
Definition: l3gd20.hpp:278
Definition: l3gd20.hpp:86
L3GD20 Tri-axis Digital Gyroscope API.
Definition: l3gd20.hpp:76
HPCF_T
Definition: l3gd20.hpp:238
void installISR(void(*isr)(char *), void *arg)
Definition: l3gd20.cxx:347
INT1_DURATION_BITS_T
Definition: l3gd20.hpp:428
CTRL_REG5_BITS_T
Definition: l3gd20.hpp:305
void getGyroscope(float *x, float *y, float *z)
Definition: l3gd20.cxx:264
INT1_SRC_BITS_T
Definition: l3gd20.hpp:408
CTRL_REG1_BITS_T
Definition: l3gd20.hpp:151