summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/adxrs610/adxrs610.h
blob: 07bc3897837290f822fe23984acb3278b43cf7fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#pragma once

#include <iostream>
#include <string>
#include <mraa/aio.hpp>

// volts per degree / second (typ)
#define m_degreeCoeff 0.006

// volts per degree C (typ)
#define m_temperatureCoeff 0.009

// nominal temperature at m_centerVolts
#define m_temperatureNom 25.0

namespace upm {
  /**
   * @brief DFRobot ADXRS610 Gyro Breakout Board
   * @defgroup adxrs610 libupm-adxrs610
   * @ingroup dfrobot analog compass
   */

  /**
   * @library adxrs610
   * @sensor adxrs610
   * @comname DFRobot ADXRS610 Gyro Breakout Board
   * @altname ADXRS610
   * @type compass
   * @man dfrobot
   * @web http://www.dfrobot.com/index.php?route=product/product&product_id=642
   * @con analog
   *
   * @brief DFRobot ADXRS610 Gyro Beakout board
   *
   * The ADXRS610 is a MEMS based single axis gyroscope with a range
   * of +/- 300 degrees/sec.  It also incorporates a temperature
   * sensing unit that can be used for advanced calibration.
   *
   * This sensor returns an analog voltage proportional to the
   * rotation about the Z-axis in degrees/sec.  The temperature
   * component returns a proportional analog values in degrees C.
   *
   * This driver was developed using the DFRobot ADXRS610 Gyro Beakout board.
   *
   * @image html adxrs610.jpg
   * @snippet adxrs610.cxx Interesting
   */

  class ADXRS610 {
  public:

    /**
     * ADXRS610 constructor
     *
     * @param dPin Analog pin to use for DATAOUT
     * @param tPin Analog pin to use for temperature measurement
     * @param aref Analog reference voltage; default is 5.0 V
     */
    ADXRS610(int dPin, int tPin, float aref=5.0);

    /**
     * ADXRS610 destructor
     */
    ~ADXRS610();

    /**
     * Returns the voltage detected on the DATA analog pin
     *
     * @return The detected voltage
     */
    float getDataVolts();

    /**
     * Returns the voltage detected on the TEMP analog pin
     *
     * @return The detected voltage
     */
    float getTemperatureVolts();

    /**
     * This method allows you to specify a deadband region around the
     * zero point of the gyro (at rest).  This can be used as a
     * primitive filter to ignore movment around the zero point.
     *
     * @param deadband The voltage around the zero point which will be
     * ignored
     */
    void setDeadband(float deadband) { m_deadband = deadband; };

    /**
     * Set the zero point.  This is the point measured and averaged
     * when the sensor is not moving.  It is set at construction time
     * (averaged over a number of samples), but can be overriden here.
     *
     * @param zeroPoint The averaged zero point of the sensor at rest
     */
    void setZeroPoint(float zeroPoint) { m_zeroPoint = zeroPoint; };

    /**
     * This method samples the data pin samples times to produce an
     * average.  This value can then be used as the zero point
     * (setZeroPoint()).
     *
     * @param samples the number of samples to take an average over.
     * The default is 50.  
     * @return the average of the reading over samples times.
     */
    float calibrateZeroPoint(unsigned int samples=50);

    /**
     * Return the zero point value.
     *
     * @return the current zero point value
     */
    float getZeroPoint() { return m_zeroPoint; };

    /**
     * Return the measured temperature in Celcius.  Note, the
     * datasheet says that this value is very repeatable, but is not
     * an accurate absolute temperature.
     *
     * @return the current temperature in C
     */
    float getTemperature();

    /**
     * Return the measured angular velocity in degrees/sec.
     *
     * @return the current angular velocity in degrees/sec
     */
    float getAngularVelocity();

  protected:
    mraa::Aio m_aioData;
    mraa::Aio m_aioTemp;

  private:
    float m_aref;
    // ADC resolution
    int m_aRes;

    // measured or set zero point value
    float m_zeroPoint;
    // desired deadband, default is 0.0
    float m_deadband;

    // aref / 2
    float m_centerVolts;
  };
}