summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/bmi160/bmi160.cxx
blob: d9f35b77fc92b1160f8189077ad2d6c5f1dbe0fe (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * 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.
 */

#include <unistd.h>
#include <iostream>
#include <stdexcept>
#include <string>

// we have to do it the old skool way
#include <mraa/i2c.h>

#include "bmi160.hpp"

extern "C" {
#include "bosch_bmi160.h"
}

// We do not need this define anyway.  It conflicts with mraa::SUCCESS.
#undef SUCCESS

using namespace upm;
using namespace std;

static mraa_i2c_context i2cContext = NULL;

// Our bmi160 info structure
struct bmi160_t s_bmi160;

// bus read and write functions for use with the bmi driver code
s8 bmi160_i2c_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
  if (!i2cContext)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": i2c context is NULL");
    }

  int retries = 10;

  // There seems to be some occasional flakyness with reads when
  // moving the sensor around
  while (retries >= 0)
    {
      int rv = mraa_i2c_read_bytes_data(i2cContext, reg_addr, reg_data, cnt);

      if (rv < 0)
        {
          usleep(100000);
          retries--;
        }
      else
        return 0;
    }

  throw std::runtime_error(std::string(__FUNCTION__) +
                           ": mraa_i2c_read_bytes_data() failed");

  return 0;
}

s8 bmi160_i2c_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
  if (!i2cContext)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": i2c context is NULL");
    }

  // FIXME  fprintf(stderr, "%s: %02x: cnt %d\n", __FUNCTION__, reg_addr, cnt);
  uint8_t buffer[cnt + 1];

  buffer[0] = reg_addr;
  for (int i=0; i<cnt; i++)
    buffer[i+1] = reg_data[i];

  mraa_result_t rv = mraa_i2c_write(i2cContext, buffer, cnt+1);

  if (rv != MRAA_SUCCESS)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": mraa_i2c_write() failed");
    }

  return 0;
}

// delay for some milliseconds
void bmi160_delay_ms(u32 msek)
{
  usleep(msek * 1000);
}


BMI160::BMI160(int bus, uint8_t address)
{
  m_addr = address;

  // We need to use the C MRAA interface to avoid issue with C++ <-> C
  // calling convention issues, also we need a global
  // mraa_i2c_context

  if (!(i2cContext = mraa_i2c_init(bus)))
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_i2c_init() failed");
    }

  if (mraa_i2c_address(i2cContext, m_addr) != MRAA_SUCCESS)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": mraa_i2c_address() failed");
      return;
    }

  // init the driver interface functions
  s_bmi160.bus_write = bmi160_i2c_bus_write;
  s_bmi160.bus_read = bmi160_i2c_bus_read;
  s_bmi160.delay_msec = bmi160_delay_ms;
  s_bmi160.dev_addr = m_addr;

  // Init our driver interface pointers
  bmi160_init(&s_bmi160);

  m_accelX = 0.0;
  m_accelY = 0.0;
  m_accelZ = 0.0;

  m_gyroX = 0.0;
  m_gyroY = 0.0;
  m_gyroZ = 0.0;

  m_magX = 0.0;
  m_magY = 0.0;
  m_magZ = 0.0;

  m_accelScale = 1.0;
  m_gyroScale = 1.0;

  m_magEnabled = false;

  if (!init())
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": init() failed");
    }
}

BMI160::~BMI160()
{
  mraa_i2c_stop(i2cContext);
  i2cContext = NULL;
}

bool BMI160::init()
{
  // This should be interesting...
  const u32 C_BMI160_THIRTY_U8X = 30;

  enableMagnetometer(true);

  /*Set the accel mode as Normal write in the register 0x7E*/
  bmi160_set_command_register(ACCEL_MODE_NORMAL);

  /* bmi160_delay_ms in ms*/
  bmi160_delay_ms(C_BMI160_THIRTY_U8X);

  /*Set the gyro mode as Normal write in the register 0x7E*/
  bmi160_set_command_register(GYRO_MODE_NORMAL);

  /* bmi160_delay_ms in ms*/
  bmi160_delay_ms(C_BMI160_THIRTY_U8X);

  /* Set the accel bandwidth as OSRS4 */
  bmi160_set_accel_bw(BMI160_ACCEL_OSR4_AVG1);
  bmi160_delay_ms(BMI160_GEN_READ_WRITE_DELAY);

  /* Set the gryo bandwidth as Normal */
  bmi160_set_gyro_bw(BMI160_GYRO_NORMAL_MODE);
  bmi160_delay_ms(BMI160_GEN_READ_WRITE_DELAY);

  /* set gyro data rate as 200Hz*/
  bmi160_set_gyro_output_data_rate(BMI160_GYRO_OUTPUT_DATA_RATE_200HZ);
  bmi160_delay_ms(BMI160_GEN_READ_WRITE_DELAY);

  /* set accel data rate as 200Hz*/
  bmi160_set_accel_output_data_rate(BMI160_ACCEL_OUTPUT_DATA_RATE_200HZ,
                                    BMI160_ACCEL_OSR4_AVG1);
  bmi160_delay_ms(BMI160_GEN_READ_WRITE_DELAY);

  setAccelerometerScale(ACCEL_RANGE_2G);
  setGyroscopeScale(GYRO_RANGE_125);

  return true;
}


void BMI160::update()
{
  struct bmi160_gyro_t gyroxyz;
  struct bmi160_accel_t accelxyz;
  struct bmi160_mag_xyz_s32_t magxyz;

  // read gyro data
  bmi160_read_gyro_xyz(&gyroxyz);

  // read accel data
  bmi160_read_accel_xyz(&accelxyz);

  // read mag data
  if (m_magEnabled)
    bmi160_bmm150_mag_compensate_xyz(&magxyz);

  // read the sensor time
  u32 v_sensor_time;
  bmi160_get_sensor_time(&v_sensor_time);
  m_sensorTime = (unsigned int)v_sensor_time;

  m_accelX = float(accelxyz.x);
  m_accelY = float(accelxyz.y);
  m_accelZ = float(accelxyz.z);

  m_gyroX = float(gyroxyz.x);
  m_gyroY = float(gyroxyz.y);
  m_gyroZ = float(gyroxyz.z);

  if (m_magEnabled)
    {
      m_magX = float(magxyz.x);
      m_magY = float(magxyz.y);
      m_magZ = float(magxyz.z);
    }
}

void BMI160::setAccelerometerScale(ACCEL_RANGE_T scale)
{
  s8 v_range = BMI160_ACCEL_RANGE_2G;
  // store scaling factor

  switch (scale)
    {
    case ACCEL_RANGE_2G:
      v_range = BMI160_ACCEL_RANGE_2G;
      m_accelScale = 16384.0;
      break;

    case ACCEL_RANGE_4G:
      v_range = BMI160_ACCEL_RANGE_4G;
      m_accelScale = 8192.0;
      break;

    case ACCEL_RANGE_8G:
      v_range = BMI160_ACCEL_RANGE_8G;
      m_accelScale = 4096.0;
      break;

    case ACCEL_RANGE_16G:
      v_range = BMI160_ACCEL_RANGE_16G;
      m_accelScale = 2048.0;
      break;

    default: // should never occur, but...
      m_accelScale = 1.0;        // set a safe, though incorrect value
      throw std::logic_error(string(__FUNCTION__) +
                             ": internal error, unsupported scale");
      break;
    }

  bmi160_set_accel_range(v_range);

  return;
}

void BMI160::setGyroscopeScale(GYRO_RANGE_T scale)
{
  u8 v_range = BMI160_GYRO_RANGE_2000_DEG_SEC;

  // store scaling factor

  switch (scale)
    {
    case GYRO_RANGE_125:
      v_range = BMI160_GYRO_RANGE_125_DEG_SEC;
      m_gyroScale = 262.4;
      break;

    case GYRO_RANGE_250:
      v_range = BMI160_GYRO_RANGE_250_DEG_SEC;
      m_gyroScale = 131.2;
      break;

    case GYRO_RANGE_500:
      v_range = BMI160_GYRO_RANGE_500_DEG_SEC;
      m_gyroScale = 65.6;
      break;

    case GYRO_RANGE_1000:
      v_range = BMI160_GYRO_RANGE_1000_DEG_SEC;
      m_gyroScale = 32.8;
      break;

    case GYRO_RANGE_2000:
      v_range = BMI160_GYRO_RANGE_2000_DEG_SEC;
      m_gyroScale = 16.4;
      break;

    default: // should never occur, but...
      m_gyroScale = 1.0;        // set a safe, though incorrect value
      throw std::logic_error(string(__FUNCTION__) +
                             ": internal error, unsupported scale");
      break;
    }

  bmi160_set_gyro_range(v_range);

  return;
}

void BMI160::getAccelerometer(float *x, float *y, float *z)
{
  if (x)
    *x = m_accelX / m_accelScale;

  if (y)
    *y = m_accelY / m_accelScale;

  if (z)
    *z = m_accelZ / m_accelScale;
}

void BMI160::getGyroscope(float *x, float *y, float *z)
{
  if (x)
    *x = m_gyroX / m_gyroScale;

  if (y)
    *y = m_gyroY / m_gyroScale;

  if (z)
    *z = m_gyroZ / m_gyroScale;
}

void BMI160::getMagnetometer(float *x, float *y, float *z)
{
  if (x)
    *x = m_magX;

  if (y)
    *y = m_magY;

  if (z)
    *z = m_magZ;
}

float *BMI160::getAccelerometer()
{
  float *values = new float[3]; // x, y, and then z

  getAccelerometer(&values[0], &values[1], &values[2]);

  return values;
}

float *BMI160::getGyroscope()
{
  float *values = new float[3]; // x, y, and then z

  getGyroscope(&values[0], &values[1], &values[2]);

  return values;
}

float *BMI160::getMagnetometer()
{
  float *values = new float[3]; // x, y, and then z

  getMagnetometer(&values[0], &values[1], &values[2]);

  return values;
}

void BMI160::enableMagnetometer(bool enable)
{
  // butchered from support example
  if (!enable)
    {
      bmi160_set_bmm150_mag_and_secondary_if_power_mode(MAG_SUSPEND_MODE);
      bmi160_delay_ms(BMI160_GEN_READ_WRITE_DELAY);
      bmi160_set_if_mode(0x00);
      bmi160_delay_ms(BMI160_GEN_READ_WRITE_DELAY);

      m_magEnabled = false;
      m_magX = 0;
      m_magY = 0;
      m_magZ = 0;
    }
  else
    {
      u8 v_bmm_chip_id_u8 = BMI160_INIT_VALUE;
      /* Init the magnetometer */
      bmi160_bmm150_mag_interface_init(&v_bmm_chip_id_u8);

      /* bmi160_delay_ms in ms*/
      bmi160_delay_ms(BMI160_GEN_READ_WRITE_DELAY);

      m_magEnabled = true;
    }
}

unsigned int BMI160::getSensorTime()
{
  return m_sensorTime;
}