summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/bmi160/bmi160.hpp
blob: 84448c041633a27b2a863a385fd8d8d0dccf26dc (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 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 <stdint.h>

#define BMI160_I2C_BUS 0
#define BMI160_DEFAULT_I2C_ADDR 0x69

namespace upm {

  /**
   * @brief BMI160 3-axis Accelerometer, Gyroscope and Magnetometer
   * @defgroup bmi160 libupm-bmi160
   * @ingroup i2c accelerometer compass
   */


  /**
   * @library bmi160
   * @sensor bmi160
   * @comname UPM API for the BMI160 3-axis Accelerometer, Gyroscope
   * and Magnetometer
   * @type accelerometer compass
   * @man mouser
   * @con i2c
   * @web http://www.mouser.com/ProductDetail/Bosch-Sensortec/0330SB2187/?qs=sGAEpiMZZMvi6wO7nhr1L9JELKA6cYRX60mAGNTn0fQ%3d
   *
   * @brief UPM API for the BMI160 3-axis Accelerometer, Gyroscope and
   * Magnetometer
   *
   * The Bosch BMI160 is a 3-axis Accelerometer and Gyroscope.
   * Additionally it supports an external Magnetometer, accessed
   * through the BMI160's register interface.  This driver was
   * developed with a BMI160 "Shuttle" board, which included a BMM150
   * Magnetometer.
   *
   * The device is driven by either 1.8v or 3.3vdc.  This driver
   * incorporates the Bosch BMI160 driver code at
   * https://github.com/BoschSensortec/BMI160_driver .
   *
   * While not all of the functionality of this device is supported
   * initially, the inclusion of the Bosch driver in the source code
   * makes it possible to support whatever features are required that
   * the driver can support.
   *
   * @snippet bmi160.cxx Interesting
   */
  class BMI160 {
  public:

    typedef enum {
      ACCEL_RANGE_2G                      = 0, // 2 Gravities
      ACCEL_RANGE_4G,
      ACCEL_RANGE_8G,
      ACCEL_RANGE_16G
    } ACCEL_RANGE_T;

    typedef enum {
      GYRO_RANGE_125                      = 0, // 125 degrees/sec
      GYRO_RANGE_250,
      GYRO_RANGE_500,
      GYRO_RANGE_1000,
      GYRO_RANGE_2000
    } GYRO_RANGE_T;

    /**
     * bmi160 constructor
     *
     * @param bus i2c bus to use
     * @param address the address for this device
     */
    BMI160(int bus=BMI160_I2C_BUS, uint8_t address=BMI160_DEFAULT_I2C_ADDR);

    /**
     * BMI160 Destructor
     */
    ~BMI160();

    /**
     * Take a measurement and store the current sensor values
     * internally.  This function must be called prior to retrieving
     * any sensor values, for example getAccelerometer().
     *
     */
    void update();

    /**
     * set the scaling mode of the accelerometer
     *
     * @param scale one of the ACCEL_RANGE_T values
     */
    void setAccelerometerScale(ACCEL_RANGE_T scale);

    /**
     * set the scaling mode of the gyroscope
     *
     * @param scale one of the GYRO_RANGE_T values
     */
    void setGyroscopeScale(GYRO_RANGE_T scale);

    /**
     * Get the Accelerometer values.  This function returns a pointer
     * to 3 floating point values: X, Y, and Z, in that order.  The
     * values returned are in gravities.  update() must have been
     * called prior to calling this method.
     *
     * The caller is reponsible for freeing the returned pointer.
     *
     * @return Pointer to 3 floating point values: X, Y, and Z in
     * gravities.
     */
    float *getAccelerometer();

    /**
     * Get the Accelerometer values.  The values returned are in
     * gravities.  update() must have been called prior to calling
     * this method.
     *
     * @param x A pointer into which the X value will be returned
     * @param y A pointer into which the Y value will be returned
     * @param z A pointer into which the Z value will be returned
     */
    void getAccelerometer(float *x, float *y, float *z);

    /**
     * Get the Gyroscope values.  This function returns a pointer to 3
     * floating point values: X, Y, and Z, in that order.  The values
     * values returned are in degrees per second.  update() must have
     * been called prior to calling this method.
     *
     * The caller is reponsible for freeing the returned pointer.
     *
     * @return Pointer to 3 floating point values: X, Y, and Z in
     * degrees per second.
     */
    float *getGyroscope();

    /**
     * Get the Gyroscope values.  The values returned are in degrees
     * per second.  update() must have been called prior to calling
     * this method.
     *
     * @param x A pointer into which the X value will be returned
     * @param y A pointer into which the Y value will be returned
     * @param z A pointer into which the Z value will be returned
     */
    void getGyroscope(float *x, float *y, float *z);

    /**
     * Get the Magnetometer values.  This function returns a pointer
     * to 3 floating point values: X, Y, and Z, in that order.  The
     * values values returned are in micro Teslas.  update() must have
     * been called prior to calling this method.  If the Magnetometer
     * has been disabled, the return values will always be 0, 0, and
     * 0.
     *
     * The caller is reponsible for freeing the returned pointer.
     *
     * @return Pointer to 3 floating point values: X, Y, and Z in
     * micro Teslas.
     */
    float *getMagnetometer();

    /**
     * Get the Magnetometer values.  The values returned are in micro
     * Teslas.  update() must have been called prior to calling this
     * method.
     *
     * @param x A pointer into which the X value will be returned
     * @param y A pointer into which the Y value will be returned
     * @param z A pointer into which the Z value will be returned
     */
    void getMagnetometer(float *x, float *y, float *z);

    /**
     * Enable or disable the Magnetometer.  By default, the
     * magnetometer is enabled.
     *
     * @param enable true to enable the magnetometer, false to disable.
     */
    void enableMagnetometer(bool enable);

    /**
     * Return the sensor time.  This is a 24bit value that increments
     * every 39us.  It will wrap around once the 24b resolution is
     * exceeded.
     *
     * @return The current sensor time.
     */
    unsigned int getSensorTime();

  protected:
    // uncompensated accelerometer and gyroscope values
    float m_accelX;
    float m_accelY;
    float m_accelZ;

    float m_gyroX;
    float m_gyroY;
    float m_gyroZ;

    float m_magX;
    float m_magY;
    float m_magZ;

    unsigned int m_sensorTime;

    // accelerometer and gyro scaling factors, depending on their Full
    // Scale (Range) settings.
    float m_accelScale;
    float m_gyroScale;

    // is the magnetometer enabled?
    bool m_magEnabled;

    /**
     * set up initial values and start operation
     *
     * @return true if successful
     */
    virtual bool init();

  private:
    // due to the way we need to 'hook' into the bmi driver, the i2c
    // context is a static variable defined in the .cxx implmentation.

    uint8_t m_addr;
  };
}