Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 378858ec

History | View | Annotate | Download (13.805 KB)

1 4bc1c0c1 Kevin Townsend
/***************************************************************************
2
  This is a library for the BNO055 orientation sensor
3

4
  Designed specifically to work with the Adafruit BNO055 Breakout.
5

6
  Pick one up today in the adafruit shop!
7
  ------> http://www.adafruit.com/products
8

9
  These sensors use I2C to communicate, 2 pins are required to interface.
10

11
  Adafruit invests time and resources providing this open source code,
12
  please support Adafruit andopen-source hardware by purchasing products
13
  from Adafruit!
14

15
  Written by KTOWN for Adafruit Industries.
16

17
  MIT license, all text above must be included in any redistribution
18
 ***************************************************************************/
19
20
#if ARDUINO >= 100
21
 #include "Arduino.h"
22
#else
23
 #include "WProgram.h"
24
#endif
25
26
#include <math.h>
27
#include <limits.h>
28
29
#include "Adafruit_BNO055.h"
30
31
/***************************************************************************
32
 CONSTRUCTOR
33
 ***************************************************************************/
34 40f91f6f Tony DiCola
35 4bc1c0c1 Kevin Townsend
/**************************************************************************/
36
/*!
37
    @brief  Instantiates a new Adafruit_BNO055 class
38
*/
39
/**************************************************************************/
40
Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address)
41
{
42
  _sensorID = sensorID;
43
  _address = address;
44
}
45
46
/***************************************************************************
47
 PUBLIC FUNCTIONS
48
 ***************************************************************************/
49
50
/**************************************************************************/
51
/*!
52
    @brief  Sets up the HW
53
*/
54
/**************************************************************************/
55
bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode)
56
{
57
  /* Enable I2C */
58
  Wire.begin();
59
60
  /* Make sure we have the right device */
61
  uint8_t id = read8(BNO055_CHIP_ID_ADDR);
62
  if(id != BNO055_ID)
63
  {
64 c4f272e1 ladyada
    delay(1000); // hold on for boot
65 8cee904f Paul Du Bois (laptop)
    id = read8(BNO055_CHIP_ID_ADDR);
66 c4f272e1 ladyada
    if(id != BNO055_ID) {
67
      return false;  // still not? ok bail
68
    }
69 4bc1c0c1 Kevin Townsend
  }
70
71
  /* Switch to config mode (just in case since this is the default) */
72
  setMode(OPERATION_MODE_CONFIG);
73 c4f272e1 ladyada
74
  /* Reset */
75 dd57d4fa Kevin Townsend
  write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
76
  while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID)
77
  {
78 c4f272e1 ladyada
    delay(10);
79 dd57d4fa Kevin Townsend
  }
80 c4f272e1 ladyada
  delay(50);
81 40f91f6f Tony DiCola
82 4bc1c0c1 Kevin Townsend
  /* Set to normal power mode */
83
  write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL);
84
  delay(10);
85 c4f272e1 ladyada
86
  write8(BNO055_PAGE_ID_ADDR, 0);
87 40f91f6f Tony DiCola
88 26f98bcd Kevin Townsend
  /* Set the output units */
89 dd57d4fa Kevin Townsend
  /*
90
  uint8_t unitsel = (0 << 7) | // Orientation = Android
91
                    (0 << 4) | // Temperature = Celsius
92
                    (0 << 2) | // Euler = Degrees
93
                    (1 << 1) | // Gyro = Rads
94
                    (0 << 0);  // Accelerometer = m/s^2
95 26f98bcd Kevin Townsend
  write8(BNO055_UNIT_SEL_ADDR, unitsel);
96 dd57d4fa Kevin Townsend
  */
97 26f98bcd Kevin Townsend
98 378858ec Shunya Sato
  /* Configure axis mapping (see section 3.4) */
99
  /*
100
  write8(BNO055_AXIS_MAP_CONFIG_ADDR, REMAP_CONFIG_P2); // P0-P7, Default is P1
101
  delay(10);
102
  write8(BNO055_AXIS_MAP_SIGN_ADDR, REMAP_SIGN_P2); // P0-P7, Default is P1
103
  delay(10);
104
  */
105
  
106 c4f272e1 ladyada
  write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
107
  delay(10);
108 4bc1c0c1 Kevin Townsend
  /* Set the requested operating mode (see section 3.3) */
109 c4f272e1 ladyada
  setMode(mode);
110 4bc1c0c1 Kevin Townsend
  delay(20);
111
112
  return true;
113
}
114
115
/**************************************************************************/
116
/*!
117
    @brief  Puts the chip in the specified operating mode
118
*/
119
/**************************************************************************/
120
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode)
121
{
122
  _mode = mode;
123
  write8(BNO055_OPR_MODE_ADDR, _mode);
124
  delay(30);
125
}
126
127
/**************************************************************************/
128
/*!
129 c4f272e1 ladyada
    @brief  Use the external 32.768KHz crystal
130
*/
131
/**************************************************************************/
132
void Adafruit_BNO055::setExtCrystalUse(boolean usextal)
133
{
134
  adafruit_bno055_opmode_t modeback = _mode;
135
136
  /* Switch to config mode (just in case since this is the default) */
137
  setMode(OPERATION_MODE_CONFIG);
138
  delay(25);
139
  write8(BNO055_PAGE_ID_ADDR, 0);
140
  if (usextal) {
141
    write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
142
  } else {
143
    write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
144
  }
145
  delay(10);
146
  /* Set the requested operating mode (see section 3.3) */
147
  setMode(modeback);
148
  delay(20);
149
}
150
151
152
/**************************************************************************/
153
/*!
154 4bc1c0c1 Kevin Townsend
    @brief  Gets the latest system status info
155
*/
156
/**************************************************************************/
157 3b2655dc ladyada
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error)
158 4bc1c0c1 Kevin Townsend
{
159 c4f272e1 ladyada
  adafruit_bno055_opmode_t backupmode = _mode;
160
161
  setMode(OPERATION_MODE_CONFIG);
162
  delay(20);
163
  write8(BNO055_PAGE_ID_ADDR, 0);
164
165 3b2655dc ladyada
  write8(BNO055_SYS_TRIGGER_ADDR, read8(BNO055_SYS_TRIGGER_ADDR) | 0x1);
166 c4f272e1 ladyada
  delay(1000);
167 40f91f6f Tony DiCola
168 4bc1c0c1 Kevin Townsend
  /* System Status (see section 4.3.58)
169
     ---------------------------------
170
     0 = Idle
171
     1 = System Error
172
     2 = Initializing Peripherals
173
     3 = System Iniitalization
174
     4 = Executing Self-Test
175
     5 = Sensor fusio algorithm running
176
     6 = System running without fusion algorithms */
177 40f91f6f Tony DiCola
178 67f3cff5 Kevin Townsend
  if (system_status != 0)
179
    *system_status    = read8(BNO055_SYS_STAT_ADDR);
180 40f91f6f Tony DiCola
181 4bc1c0c1 Kevin Townsend
  /* Self Test Results (see section )
182
     --------------------------------
183
     1 = test passed, 0 = test failed
184 40f91f6f Tony DiCola

185 4bc1c0c1 Kevin Townsend
     Bit 0 = Accelerometer self test
186
     Bit 1 = Magnetometer self test
187
     Bit 2 = Gyroscope self test
188
     Bit 3 = MCU self test
189 67f3cff5 Kevin Townsend

190 4bc1c0c1 Kevin Townsend
     0x0F = all good! */
191 40f91f6f Tony DiCola
192 67f3cff5 Kevin Townsend
  if (self_test_result != 0)
193
    *self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR);
194 4bc1c0c1 Kevin Townsend
195
  /* System Error (see section 4.3.59)
196
     ---------------------------------
197
     0 = No error
198
     1 = Peripheral initialization error
199
     2 = System initialization error
200
     3 = Self test result failed
201
     4 = Register map value out of range
202
     5 = Register map address out of range
203
     6 = Register map write error
204
     7 = BNO low power mode not available for selected operat ion mode
205
     8 = Accelerometer power mode not available
206
     9 = Fusion algorithm configuration error
207
     A = Sensor configuration error */
208 40f91f6f Tony DiCola
209 67f3cff5 Kevin Townsend
  if (system_error != 0)
210
    *system_error     = read8(BNO055_SYS_ERR_ADDR);
211
212
  setMode(backupmode);
213
  delay(20);
214 4bc1c0c1 Kevin Townsend
}
215
216
/**************************************************************************/
217
/*!
218
    @brief  Gets the chip revision numbers
219
*/
220
/**************************************************************************/
221
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t* info)
222
{
223
  uint8_t a, b;
224
225
  memset(info, 0, sizeof(adafruit_bno055_rev_info_t));
226
227 67f3cff5 Kevin Townsend
  /* Check the accelerometer revision */
228 4bc1c0c1 Kevin Townsend
  info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR);
229 67f3cff5 Kevin Townsend
230
  /* Check the magnetometer revision */
231 4bc1c0c1 Kevin Townsend
  info->mag_rev   = read8(BNO055_MAG_REV_ID_ADDR);
232 67f3cff5 Kevin Townsend
233
  /* Check the gyroscope revision */
234 4bc1c0c1 Kevin Townsend
  info->gyro_rev  = read8(BNO055_GYRO_REV_ID_ADDR);
235 67f3cff5 Kevin Townsend
236
  /* Check the SW revision */
237 4bc1c0c1 Kevin Townsend
  info->bl_rev    = read8(BNO055_BL_REV_ID_ADDR);
238 40f91f6f Tony DiCola
239 4bc1c0c1 Kevin Townsend
  a = read8(BNO055_SW_REV_ID_LSB_ADDR);
240
  b = read8(BNO055_SW_REV_ID_MSB_ADDR);
241
  info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
242
}
243
244
/**************************************************************************/
245
/*!
246 40f91f6f Tony DiCola
    @brief  Gets current calibration state.  Each value should be a uint8_t
247
            pointer and it will be set to 0 if not calibrated and 3 if
248
            fully calibrated.
249
*/
250
/**************************************************************************/
251
void Adafruit_BNO055::getCalibration(uint8_t* sys, uint8_t* gyro, uint8_t* accel, uint8_t* mag) {
252
  uint8_t calData = read8(BNO055_CALIB_STAT_ADDR);
253
  if (sys != NULL) {
254
    *sys = (calData >> 6) & 0x03;
255
  }
256
  if (gyro != NULL) {
257
    *gyro = (calData >> 4) & 0x03;
258
  }
259
  if (accel != NULL) {
260
    *accel = (calData >> 2) & 0x03;
261
  }
262
  if (mag != NULL) {
263
    *mag = calData & 0x03;
264
  }
265
}
266
267
/**************************************************************************/
268
/*!
269
    @brief  Gets the temperature in degrees celsius
270 0e2e2723 Kevin Townsend
*/
271
/**************************************************************************/
272
int8_t Adafruit_BNO055::getTemp(void)
273
{
274
  int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR));
275
  return temp;
276
}
277
278
/**************************************************************************/
279
/*!
280 48741e1f Kevin Townsend
    @brief  Gets a vector reading from the specified source
281 4bc1c0c1 Kevin Townsend
*/
282
/**************************************************************************/
283 48741e1f Kevin Townsend
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type)
284 4bc1c0c1 Kevin Townsend
{
285
  imu::Vector<3> xyz;
286
  uint8_t buffer[6];
287
  memset (buffer, 0, 6);
288 40f91f6f Tony DiCola
289 4bc1c0c1 Kevin Townsend
  int16_t x, y, z;
290
  x = y = z = 0;
291 40f91f6f Tony DiCola
292 48741e1f Kevin Townsend
  /* Read vector data (6 bytes) */
293
  readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
294 40f91f6f Tony DiCola
295 dd57d4fa Kevin Townsend
  x = ((int16_t)buffer[0]) | (((int16_t)buffer[1]) << 8);
296
  y = ((int16_t)buffer[2]) | (((int16_t)buffer[3]) << 8);
297
  z = ((int16_t)buffer[4]) | (((int16_t)buffer[5]) << 8);
298 26f98bcd Kevin Townsend
299
  /* Convert the value to an appropriate range (section 3.6.4) */
300
  /* and assign the value to the Vector type */
301
  switch(vector_type)
302
  {
303
    case VECTOR_MAGNETOMETER:
304
      /* 1uT = 16 LSB */
305
      xyz[0] = ((double)x)/16.0;
306
      xyz[1] = ((double)y)/16.0;
307
      xyz[2] = ((double)z)/16.0;
308
      break;
309
    case VECTOR_GYROSCOPE:
310
      /* 1rps = 900 LSB */
311
      xyz[0] = ((double)x)/900.0;
312
      xyz[1] = ((double)y)/900.0;
313
      xyz[2] = ((double)z)/900.0;
314
      break;
315
    case VECTOR_EULER:
316
      /* 1 degree = 16 LSB */
317
      xyz[0] = ((double)x)/16.0;
318
      xyz[1] = ((double)y)/16.0;
319
      xyz[2] = ((double)z)/16.0;
320
      break;
321
    case VECTOR_ACCELEROMETER:
322
    case VECTOR_LINEARACCEL:
323
    case VECTOR_GRAVITY:
324
      /* 1m/s^2 = 100 LSB */
325
      xyz[0] = ((double)x)/100.0;
326
      xyz[1] = ((double)y)/100.0;
327
      xyz[2] = ((double)z)/100.0;
328
      break;
329
  }
330 40f91f6f Tony DiCola
331 4bc1c0c1 Kevin Townsend
  return xyz;
332
}
333
334
/**************************************************************************/
335
/*!
336 48741e1f Kevin Townsend
    @brief  Gets a quaternion reading from the specified source
337
*/
338
/**************************************************************************/
339
imu::Quaternion Adafruit_BNO055::getQuat(void)
340
{
341
  uint8_t buffer[8];
342
  memset (buffer, 0, 8);
343 40f91f6f Tony DiCola
344 48741e1f Kevin Townsend
  int16_t x, y, z, w;
345
  x = y = z = w = 0;
346 40f91f6f Tony DiCola
347 48741e1f Kevin Townsend
  /* Read quat data (8 bytes) */
348
  readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
349
  w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
350
  x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
351
  y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
352
  z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]);
353
354
  /* Assign to Quaternion */
355 8cee904f Paul Du Bois (laptop)
  /* See http://ae-bst.resource.bosch.com/media/products/dokumente/bno055/BST_BNO055_DS000_12~1.pdf
356
     3.6.5.5 Orientation (Quaternion)  */
357
  const double scale = (1.0 / (1<<14));
358
  imu::Quaternion quat(scale * w, scale * x, scale * y, scale * z);
359 48741e1f Kevin Townsend
  return quat;
360
}
361
362
/**************************************************************************/
363
/*!
364 4bc1c0c1 Kevin Townsend
    @brief  Provides the sensor_t data for this sensor
365
*/
366
/**************************************************************************/
367
void Adafruit_BNO055::getSensor(sensor_t *sensor)
368
{
369
  /* Clear the sensor_t object */
370
  memset(sensor, 0, sizeof(sensor_t));
371
372
  /* Insert the sensor name in the fixed length char array */
373
  strncpy (sensor->name, "BNO055", sizeof(sensor->name) - 1);
374
  sensor->name[sizeof(sensor->name)- 1] = 0;
375
  sensor->version     = 1;
376
  sensor->sensor_id   = _sensorID;
377
  sensor->type        = SENSOR_TYPE_ORIENTATION;
378
  sensor->min_delay   = 0;
379
  sensor->max_value   = 0.0F;
380
  sensor->min_value   = 0.0F;
381
  sensor->resolution  = 0.01F;
382
}
383
384
/**************************************************************************/
385
/*!
386
    @brief  Reads the sensor and returns the data as a sensors_event_t
387
*/
388
/**************************************************************************/
389
bool Adafruit_BNO055::getEvent(sensors_event_t *event)
390
{
391
  /* Clear the event */
392
  memset(event, 0, sizeof(sensors_event_t));
393
394
  event->version   = sizeof(sensors_event_t);
395
  event->sensor_id = _sensorID;
396
  event->type      = SENSOR_TYPE_ORIENTATION;
397 fcd68623 Kevin Townsend
  event->timestamp = millis();
398
399
  /* Get a Euler angle sample for orientation */
400
  imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
401
  event->orientation.x = euler.x();
402
  event->orientation.y = euler.y();
403
  event->orientation.z = euler.z();
404
405 4bc1c0c1 Kevin Townsend
  return true;
406
}
407
408
/***************************************************************************
409
 PRIVATE FUNCTIONS
410
 ***************************************************************************/
411
412
/**************************************************************************/
413
/*!
414
    @brief  Writes an 8 bit value over I2C
415
*/
416
/**************************************************************************/
417
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value)
418
{
419
  Wire.beginTransmission(_address);
420
  #if ARDUINO >= 100
421
    Wire.write((uint8_t)reg);
422
    Wire.write((uint8_t)value);
423
  #else
424
    Wire.send(reg);
425
    Wire.send(value);
426
  #endif
427
  Wire.endTransmission();
428
429
  /* ToDo: Check for error! */
430
  return true;
431
}
432
433
/**************************************************************************/
434
/*!
435
    @brief  Reads an 8 bit value over I2C
436
*/
437
/**************************************************************************/
438
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg )
439
{
440
  byte value = 0;
441 40f91f6f Tony DiCola
442 4bc1c0c1 Kevin Townsend
  Wire.beginTransmission(_address);
443
  #if ARDUINO >= 100
444
    Wire.write((uint8_t)reg);
445
  #else
446
    Wire.send(reg);
447
  #endif
448
  Wire.endTransmission();
449
  Wire.requestFrom(_address, (byte)1);
450
  #if ARDUINO >= 100
451
    value = Wire.read();
452
  #else
453
    value = Wire.receive();
454
  #endif
455 40f91f6f Tony DiCola
456 4bc1c0c1 Kevin Townsend
  return value;
457
}
458
459
/**************************************************************************/
460
/*!
461
    @brief  Reads the specified number of bytes over I2C
462
*/
463
/**************************************************************************/
464
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte * buffer, uint8_t len)
465
{
466
  Wire.beginTransmission(_address);
467
  #if ARDUINO >= 100
468
    Wire.write((uint8_t)reg);
469
  #else
470
    Wire.send(reg);
471
  #endif
472
  Wire.endTransmission();
473
  Wire.requestFrom(_address, (byte)len);
474
475
  for (uint8_t i = 0; i < len; i++)
476
  {
477
    #if ARDUINO >= 100
478
      buffer[i] = Wire.read();
479
    #else
480
      buffer[i] = Wire.receive();
481
    #endif
482
  }
483 40f91f6f Tony DiCola
484 4bc1c0c1 Kevin Townsend
  /* ToDo: Check for errors! */
485
  return true;
486
}