Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 3528bff2

History | View | Annotate | Download (13.566 KB)

1
/***************************************************************************
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

    
35
/**************************************************************************/
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
    delay(1000); // hold on for boot
65
    id = read8(BNO055_CHIP_ID_ADDR);
66
    if(id != BNO055_ID) {
67
      return false;  // still not? ok bail
68
    }
69
  }
70

    
71
  /* Switch to config mode (just in case since this is the default) */
72
  setMode(OPERATION_MODE_CONFIG);
73

    
74
  /* Reset */
75
  write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
76
  while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID)
77
  {
78
    delay(10);
79
  }
80
  delay(50);
81

    
82
  /* Set to normal power mode */
83
  write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL);
84
  delay(10);
85

    
86
  write8(BNO055_PAGE_ID_ADDR, 0);
87

    
88
  /* Set the output units */
89
  /*
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
  write8(BNO055_UNIT_SEL_ADDR, unitsel);
96
  */
97

    
98
  write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
99
  delay(10);
100
  /* Set the requested operating mode (see section 3.3) */
101
  setMode(mode);
102
  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
    @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
    @brief  Gets the latest system status info
147
*/
148
/**************************************************************************/
149
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error)
150
{
151
  adafruit_bno055_opmode_t backupmode = _mode;
152

    
153
  setMode(OPERATION_MODE_CONFIG);
154
  delay(20);
155
  write8(BNO055_PAGE_ID_ADDR, 0);
156

    
157
  write8(BNO055_SYS_TRIGGER_ADDR, read8(BNO055_SYS_TRIGGER_ADDR) | 0x1);
158
  delay(1000);
159

    
160
  /* 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

    
170
  if (system_status != 0)
171
    *system_status    = read8(BNO055_SYS_STAT_ADDR);
172

    
173
  /* Self Test Results (see section )
174
     --------------------------------
175
     1 = test passed, 0 = test failed
176

177
     Bit 0 = Accelerometer self test
178
     Bit 1 = Magnetometer self test
179
     Bit 2 = Gyroscope self test
180
     Bit 3 = MCU self test
181

182
     0x0F = all good! */
183

    
184
  if (self_test_result != 0)
185
    *self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR);
186

    
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

    
201
  if (system_error != 0)
202
    *system_error     = read8(BNO055_SYS_ERR_ADDR);
203

    
204
  setMode(backupmode);
205
  delay(20);
206
}
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
  /* Check the accelerometer revision */
220
  info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR);
221

    
222
  /* Check the magnetometer revision */
223
  info->mag_rev   = read8(BNO055_MAG_REV_ID_ADDR);
224

    
225
  /* Check the gyroscope revision */
226
  info->gyro_rev  = read8(BNO055_GYRO_REV_ID_ADDR);
227

    
228
  /* Check the SW revision */
229
  info->bl_rev    = read8(BNO055_BL_REV_ID_ADDR);
230

    
231
  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
    @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
*/
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
    @brief  Gets a vector reading from the specified source
273
*/
274
/**************************************************************************/
275
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type)
276
{
277
  imu::Vector<3> xyz;
278
  uint8_t buffer[6];
279
  memset (buffer, 0, 6);
280

    
281
  int16_t x, y, z;
282
  x = y = z = 0;
283

    
284
  /* Read vector data (6 bytes) */
285
  readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
286

    
287
  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

    
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

    
323
  return xyz;
324
}
325

    
326
/**************************************************************************/
327
/*!
328
    @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

    
336
  int16_t x, y, z, w;
337
  x = y = z = w = 0;
338

    
339
  /* 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
  /* 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
  return quat;
352
}
353

    
354
/**************************************************************************/
355
/*!
356
    @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
  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
  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

    
434
  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

    
448
  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
  for (uint8_t i = 0; i < len; i++)
468
  {
469
    #if ARDUINO >= 100
470
      buffer[i] = Wire.read();
471
    #else
472
      buffer[i] = Wire.receive();
473
    #endif
474
  }
475

    
476
  /* ToDo: Check for errors! */
477
  return true;
478
}