Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 40f91f6f

History | View | Annotate | Download (13.637 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 c4f272e1 ladyada
  write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
99
  delay(10);
100 4bc1c0c1 Kevin Townsend
  /* Set the requested operating mode (see section 3.3) */
101 c4f272e1 ladyada
  setMode(mode);
102 4bc1c0c1 Kevin Townsend
  delay(20);
103
104
  return true;
105
}
106
107
/**************************************************************************/
108
/*!
109
    @brief  Puts the chip in the specified operating mode
110
*/
111
/**************************************************************************/
112
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode)
113
{
114
  _mode = mode;
115
  write8(BNO055_OPR_MODE_ADDR, _mode);
116
  delay(30);
117
}
118
119
/**************************************************************************/
120
/*!
121 c4f272e1 ladyada
    @brief  Use the external 32.768KHz crystal
122
*/
123
/**************************************************************************/
124
void Adafruit_BNO055::setExtCrystalUse(boolean usextal)
125
{
126
  adafruit_bno055_opmode_t modeback = _mode;
127
128
  /* Switch to config mode (just in case since this is the default) */
129
  setMode(OPERATION_MODE_CONFIG);
130
  delay(25);
131
  write8(BNO055_PAGE_ID_ADDR, 0);
132
  if (usextal) {
133
    write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
134
  } else {
135
    write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
136
  }
137
  delay(10);
138
  /* Set the requested operating mode (see section 3.3) */
139
  setMode(modeback);
140
  delay(20);
141
}
142
143
144
/**************************************************************************/
145
/*!
146 4bc1c0c1 Kevin Townsend
    @brief  Gets the latest system status info
147
*/
148
/**************************************************************************/
149 3b2655dc ladyada
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error)
150 4bc1c0c1 Kevin Townsend
{
151 c4f272e1 ladyada
  adafruit_bno055_opmode_t backupmode = _mode;
152
153
  setMode(OPERATION_MODE_CONFIG);
154
  delay(20);
155
  write8(BNO055_PAGE_ID_ADDR, 0);
156
157 3b2655dc ladyada
  write8(BNO055_SYS_TRIGGER_ADDR, read8(BNO055_SYS_TRIGGER_ADDR) | 0x1);
158 c4f272e1 ladyada
  delay(1000);
159 40f91f6f Tony DiCola
160 4bc1c0c1 Kevin Townsend
  /* System Status (see section 4.3.58)
161
     ---------------------------------
162
     0 = Idle
163
     1 = System Error
164
     2 = Initializing Peripherals
165
     3 = System Iniitalization
166
     4 = Executing Self-Test
167
     5 = Sensor fusio algorithm running
168
     6 = System running without fusion algorithms */
169 40f91f6f Tony DiCola
170 67f3cff5 Kevin Townsend
  if (system_status != 0)
171
    *system_status    = read8(BNO055_SYS_STAT_ADDR);
172 40f91f6f Tony DiCola
173 4bc1c0c1 Kevin Townsend
  /* Self Test Results (see section )
174
     --------------------------------
175
     1 = test passed, 0 = test failed
176 40f91f6f Tony DiCola

177 4bc1c0c1 Kevin Townsend
     Bit 0 = Accelerometer self test
178
     Bit 1 = Magnetometer self test
179
     Bit 2 = Gyroscope self test
180
     Bit 3 = MCU self test
181 67f3cff5 Kevin Townsend

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