upm  0.7.0
Sensor/Actuator repository for libmraa (v1.0.0)
 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) 2015 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 
116  bool setScale(const float scale);
117 
122  bool setSamplingFrequency(const float sampling_frequency);
123 
127  bool enable3AxisChannel();
128 
136  bool extract3Axis(char* data, float* x, float* y, float* z);
137 
141  void initCalibrate();
142 
146  bool getCalibratedStatus();
147 
151  void getCalibratedData(float* bias_x, float* bias_y, float* bias_z);
152 
156  void loadCalibratedData(float bias_x, float bias_y, float bias_z);
157 
164  bool gyroCollect(float x, float y, float z);
165 
172  void gyroDenoiseMedian(float* x, float* y, float* z);
173 
179  float median(float* queue, unsigned int size);
180 
188  unsigned int
189  partition(float* list, unsigned int left, unsigned int right, unsigned int pivot_index);
190 
197  void clampGyroReadingsToZero(float* x, float* y, float* z);
198 
199  private:
200  mraa_iio_context m_iio;
201  int m_iio_device_num;
202  bool m_mount_matrix_exist; // is mount matrix exist
203  float m_mount_matrix[9]; // mount matrix
204  float m_scale; // gyroscope data scale
205  int m_event_count; // sample data arrive
206  bool m_calibrated; // calibrate state
207  gyro_cal_t m_cal_data; // calibrate data
208  filter_median_t m_filter; // filter data
209 };
210 }
int64_t getChannelValue(unsigned char *input, mraa_iio_channel *chan)
Definition: l3gd20.cxx:110
void initCalibrate()
Definition: l3gd20.cxx:257
~L3GD20()
Definition: l3gd20.cxx:85
bool setSamplingFrequency(const float sampling_frequency)
Definition: l3gd20.cxx:187
bool disableBuffer()
Definition: l3gd20.cxx:171
L3GD20(int device)
Definition: l3gd20.cxx:42
bool enableBuffer(int length)
Definition: l3gd20.cxx:162
bool setScale(const float scale)
Definition: l3gd20.cxx:178
void gyroDenoiseMedian(float *x, float *y, float *z)
Definition: l3gd20.cxx:349
bool getCalibratedStatus()
Definition: l3gd20.cxx:267
void getCalibratedData(float *bias_x, float *bias_y, float *bias_z)
Definition: l3gd20.cxx:273
Definition: l3gd20.hpp:58
float median(float *queue, unsigned int size)
Definition: l3gd20.cxx:379
void loadCalibratedData(float bias_x, float bias_y, float bias_z)
Definition: l3gd20.cxx:281
bool gyroCollect(float x, float y, float z)
Definition: l3gd20.cxx:290
bool extract3Axis(char *data, float *x, float *y, float *z)
Definition: l3gd20.cxx:210
bool enable3AxisChannel()
Definition: l3gd20.cxx:194
void clampGyroReadingsToZero(float *x, float *y, float *z)
Definition: l3gd20.cxx:440
unsigned int partition(float *list, unsigned int left, unsigned int right, unsigned int pivot_index)
Definition: l3gd20.cxx:410
Definition: l3gd20.hpp:65
L3GD20 Tri-axis Digital Gyroscope API.
Definition: l3gd20.hpp:55
void installISR(void(*isr)(char *), void *arg)
Definition: l3gd20.cxx:104