upm  0.7.2
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  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Thanks to https://github.com/01org/android-iio-sensors-hal for gyroscope
25  * calibration and denoise algorithm.
26  */
27 #pragma once
28 
29 #include <string>
30 #include <mraa/iio.h>
31 
32 namespace upm
33 {
55 class L3GD20
56 {
57  public:
58  typedef struct {
59  float bias_x, bias_y, bias_z;
60  int count;
61  float min_x, min_y, min_z;
62  float max_x, max_y, max_z;
63  } gyro_cal_t;
64 
65  typedef struct {
66  float* buff;
67  unsigned int idx;
68  unsigned int count;
69  unsigned int sample_size;
76  L3GD20(int device);
77 
81  ~L3GD20();
82 
92  void installISR(void (*isr)(char*), void* arg);
93 
99  int64_t getChannelValue(unsigned char* input, mraa_iio_channel* chan);
100 
105  bool enableBuffer(int length);
106 
110  bool disableBuffer();
111 
118  bool setScale(const float scale);
119 
126  bool setSamplingFrequency(const float sampling_frequency);
127 
131  bool enable3AxisChannel();
132 
140  bool extract3Axis(char* data, float* x, float* y, float* z);
141 
145  void initCalibrate();
146 
150  bool getCalibratedStatus();
151 
155  void getCalibratedData(float* bias_x, float* bias_y, float* bias_z);
156 
160  void loadCalibratedData(float bias_x, float bias_y, float bias_z);
161 
168  bool gyroCollect(float x, float y, float z);
169 
176  void gyroDenoiseMedian(float* x, float* y, float* z);
177 
183  float median(float* queue, unsigned int size);
184 
192  unsigned int
193  partition(float* list, unsigned int left, unsigned int right, unsigned int pivot_index);
194 
201  void clampGyroReadingsToZero(float* x, float* y, float* z);
202 
203  private:
204  mraa_iio_context m_iio;
205  int m_iio_device_num;
206  bool m_mount_matrix_exist; // is mount matrix exist
207  float m_mount_matrix[9]; // mount matrix
208  float m_scale; // gyroscope data scale
209  int m_event_count; // sample data arrive
210  bool m_calibrated; // calibrate state
211  gyro_cal_t m_cal_data; // calibrate data
212  filter_median_t m_filter; // filter data
213 };
214 }
int64_t getChannelValue(unsigned char *input, mraa_iio_channel *chan)
Definition: l3gd20.cxx:102
void initCalibrate()
Definition: l3gd20.cxx:253
~L3GD20()
Definition: l3gd20.cxx:85
bool setSamplingFrequency(const float sampling_frequency)
Definition: l3gd20.cxx:181
bool disableBuffer()
Definition: l3gd20.cxx:163
L3GD20(int device)
Definition: l3gd20.cxx:42
bool enableBuffer(int length)
Definition: l3gd20.cxx:154
bool setScale(const float scale)
Definition: l3gd20.cxx:170
void gyroDenoiseMedian(float *x, float *y, float *z)
Definition: l3gd20.cxx:345
bool getCalibratedStatus()
Definition: l3gd20.cxx:263
void getCalibratedData(float *bias_x, float *bias_y, float *bias_z)
Definition: l3gd20.cxx:269
Definition: l3gd20.hpp:58
float median(float *queue, unsigned int size)
Definition: l3gd20.cxx:375
void loadCalibratedData(float bias_x, float bias_y, float bias_z)
Definition: l3gd20.cxx:277
bool gyroCollect(float x, float y, float z)
Definition: l3gd20.cxx:286
bool extract3Axis(char *data, float *x, float *y, float *z)
Definition: l3gd20.cxx:204
bool enable3AxisChannel()
Definition: l3gd20.cxx:188
void clampGyroReadingsToZero(float *x, float *y, float *z)
Definition: l3gd20.cxx:436
unsigned int partition(float *list, unsigned int left, unsigned int right, unsigned int pivot_index)
Definition: l3gd20.cxx:406
Definition: l3gd20.hpp:65
L3GD20 Tri-axis Digital Gyroscope API.
Definition: l3gd20.hpp:55
void installISR(void(*isr)(char *), void *arg)
Definition: l3gd20.cxx:96