Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 3b2655dc

History | View | Annotate | Download (13.837 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
    return false;
65
  }
66

    
67
  /* Switch to config mode (just in case since this is the default) */
68
  setMode(OPERATION_MODE_CONFIG);
69
  
70
  /* Set to normal power mode */
71
  write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL);
72
  delay(10);
73
  
74
  /* Set the output units */
75
  uint8_t unitsel = (0 << 7) | /* Orientation = Android */
76
                    (0 << 4) | /* Temperature = Celsius */
77
                    (0 << 2) | /* Euler = Degrees */
78
                    (1 << 1) | /* Gyro = Rads */
79
                    (0 << 0);  /* Accelerometer = m/s^2 */
80
  write8(BNO055_UNIT_SEL_ADDR, unitsel);
81

    
82
  /* Set the requested operating mode (see section 3.3) */
83
  write8(BNO055_OPR_MODE_ADDR, mode);
84
  delay(20);
85

    
86
  return true;
87
}
88

    
89
/**************************************************************************/
90
/*!
91
    @brief  Puts the chip in the specified operating mode
92
*/
93
/**************************************************************************/
94
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode)
95
{
96
  _mode = mode;
97
  
98
  write8(BNO055_OPR_MODE_ADDR, _mode);
99
  delay(30);
100
}
101

    
102
/**************************************************************************/
103
/*!
104
    @brief  Gets the latest system status info
105
*/
106
/**************************************************************************/
107
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error)
108
{
109
  write8(BNO055_SYS_TRIGGER_ADDR, read8(BNO055_SYS_TRIGGER_ADDR) | 0x1);
110
  delay(10);
111
  /* Read the system status register */
112
  if (system_status != 0)
113
    *system_status    = read8(BNO055_SYS_STAT_ADDR);
114
  if (self_test_result != 0)
115
    *self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR);
116
  if (system_error != 0)
117
    *system_error     = read8(BNO055_SYS_ERR_ADDR);
118
}
119

    
120
/**************************************************************************/
121
/*!
122
    @brief  Displays system status info via Serial.print
123
*/
124
/**************************************************************************/
125
void Adafruit_BNO055::displaySystemStatus(void)
126
{
127
  uint8_t system_status, self_test_result, system_error;
128
  getSystemStatus(&system_status, &self_test_result, &system_error);
129
  
130
  /* System Status (see section 4.3.58)
131
     ---------------------------------
132
     0 = Idle
133
     1 = System Error
134
     2 = Initializing Peripherals
135
     3 = System Iniitalization
136
     4 = Executing Self-Test
137
     5 = Sensor fusio algorithm running
138
     6 = System running without fusion algorithms */
139
  
140
  Serial.print("System Status:          0x");
141
  Serial.println(system_status, HEX);
142

    
143
  /* Self Test Results (see section )
144
     --------------------------------
145
     1 = test passed, 0 = test failed
146
    
147
     Bit 0 = Accelerometer self test
148
     Bit 1 = Magnetometer self test
149
     Bit 2 = Gyroscope self test
150
     Bit 3 = MCU self test
151
  
152
     0x0F = all good! */
153
  
154
  Serial.print("Self Test Results:      0x");
155
  Serial.println(self_test_result, HEX);
156

    
157
  /* System Error (see section 4.3.59)
158
     ---------------------------------
159
     0 = No error
160
     1 = Peripheral initialization error
161
     2 = System initialization error
162
     3 = Self test result failed
163
     4 = Register map value out of range
164
     5 = Register map address out of range
165
     6 = Register map write error
166
     7 = BNO low power mode not available for selected operat ion mode
167
     8 = Accelerometer power mode not available
168
     9 = Fusion algorithm configuration error
169
     A = Sensor configuration error */
170
  
171
  Serial.print("System Error:           0x");
172
  Serial.println(system_error, HEX);
173
}
174

    
175
/**************************************************************************/
176
/*!
177
    @brief  Gets the chip revision numbers
178
*/
179
/**************************************************************************/
180
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t* info)
181
{
182
  uint8_t a, b;
183

    
184
  memset(info, 0, sizeof(adafruit_bno055_rev_info_t));
185

    
186
  info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR);
187
  info->mag_rev   = read8(BNO055_MAG_REV_ID_ADDR);
188
  info->gyro_rev  = read8(BNO055_GYRO_REV_ID_ADDR);
189
  info->bl_rev    = read8(BNO055_BL_REV_ID_ADDR);
190
  
191
  a = read8(BNO055_SW_REV_ID_LSB_ADDR);
192
  b = read8(BNO055_SW_REV_ID_MSB_ADDR);
193
  info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
194
}
195

    
196
/**************************************************************************/
197
/*!
198
    @brief  Displays the chip revision numbers via Serial.print
199
*/
200
/**************************************************************************/
201
void Adafruit_BNO055::displayRevInfo(void)
202
{
203
  adafruit_bno055_rev_info_t info;
204
  getRevInfo(&info);
205

    
206
  /* Check the accelerometer revision */
207
  Serial.print("Accelerometer Revision: 0x");
208
  Serial.println(info.accel_rev, HEX);
209
  
210
  /* Check the magnetometer revision */
211
  Serial.print("Magnetometer Revision:  0x");
212
  Serial.println(info.mag_rev, HEX);
213
  
214
  /* Check the gyroscope revision */
215
  Serial.print("Gyroscope Revision:     0x");
216
  Serial.println(info.gyro_rev, HEX);
217
  
218
  /* Check the SW revision */
219
  Serial.print("SW Revision:            0x");
220
  Serial.println(info.sw_rev, HEX);
221
  
222
  /* Check the bootloader revision */
223
  Serial.print("Bootloader Revision:    0x");
224
  Serial.println(info.bl_rev, HEX);
225
}
226

    
227
/**************************************************************************/
228
/*!
229
    @brief  Gets teh temperature in degrees celsius
230
*/
231
/**************************************************************************/
232
int8_t Adafruit_BNO055::getTemp(void)
233
{
234
  int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR));
235
  return temp;
236
}
237

    
238
/**************************************************************************/
239
/*!
240
    @brief  Gets a vector reading from the specified source
241
*/
242
/**************************************************************************/
243
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type)
244
{
245
  imu::Vector<3> xyz;
246
  uint8_t buffer[6];
247
  memset (buffer, 0, 6);
248
  
249
  int16_t x, y, z;
250
  x = y = z = 0;
251
  
252
  /* Read vector data (6 bytes) */
253
  readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
254
  x = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
255
  y = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
256
  z = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
257

    
258
  /* Convert the value to an appropriate range (section 3.6.4) */
259
  /* and assign the value to the Vector type */
260
  switch(vector_type)
261
  {
262
    case VECTOR_MAGNETOMETER:
263
      /* 1uT = 16 LSB */
264
      xyz[0] = ((double)x)/16.0;
265
      xyz[1] = ((double)y)/16.0;
266
      xyz[2] = ((double)z)/16.0;
267
      break;
268
    case VECTOR_GYROSCOPE:
269
      /* 1rps = 900 LSB */
270
      xyz[0] = ((double)x)/900.0;
271
      xyz[1] = ((double)y)/900.0;
272
      xyz[2] = ((double)z)/900.0;
273
      break;
274
    case VECTOR_EULER:
275
      /* 1 degree = 16 LSB */
276
      xyz[0] = ((double)x)/16.0;
277
      xyz[1] = ((double)y)/16.0;
278
      xyz[2] = ((double)z)/16.0;
279
      break;
280
    case VECTOR_ACCELEROMETER:
281
    case VECTOR_LINEARACCEL:
282
    case VECTOR_GRAVITY:
283
      /* 1m/s^2 = 100 LSB */
284
      xyz[0] = ((double)x)/100.0;
285
      xyz[1] = ((double)y)/100.0;
286
      xyz[2] = ((double)z)/100.0;
287
      break;
288
  }
289
  
290
  return xyz;
291
}
292

    
293
/**************************************************************************/
294
/*!
295
    @brief  Gets a quaternion reading from the specified source
296
*/
297
/**************************************************************************/
298
imu::Quaternion Adafruit_BNO055::getQuat(void)
299
{
300
  uint8_t buffer[8];
301
  memset (buffer, 0, 8);
302
  
303
  int16_t x, y, z, w;
304
  x = y = z = w = 0;
305
  
306
  /* Read quat data (8 bytes) */
307
  readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
308
  w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
309
  x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
310
  y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
311
  z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]);
312

    
313
  /* Assign to Quaternion */
314
  imu::Quaternion quat((double)w, (double)x, (double)y, (double)z);
315
  return quat;
316
}
317

    
318
/**************************************************************************/
319
/*
320
    Prints a float or double with the specified number of decimal places.
321

322
    'precision' should be 1 followed by a zero for every decimal place
323
    desired, so '100' will produce two decimal places:
324

325
    print_double(3.1415, 100); // Output = 3.14
326
*/
327
/**************************************************************************/
328
void Adafruit_BNO055::printDouble(double val, unsigned int precision)
329
{
330
  /* Print the integer portion */
331
  Serial.print (int(val));
332
  Serial.print(".");
333
  
334
  /* Print the fraction portion */
335
  unsigned int frac;
336
  if(val >= 0)
337
  {
338
    frac = (val - int(val)) * precision;
339
  }
340
  else
341
  {
342
    frac = (int(val)- val ) * precision;
343
  }
344
  Serial.println(frac,DEC) ;
345
}
346

    
347
/**************************************************************************/
348
/*!
349
    @brief  Provides the sensor_t data for this sensor
350
*/
351
/**************************************************************************/
352
void Adafruit_BNO055::getSensor(sensor_t *sensor)
353
{
354
  /* Clear the sensor_t object */
355
  memset(sensor, 0, sizeof(sensor_t));
356

    
357
  /* Insert the sensor name in the fixed length char array */
358
  strncpy (sensor->name, "BNO055", sizeof(sensor->name) - 1);
359
  sensor->name[sizeof(sensor->name)- 1] = 0;
360
  sensor->version     = 1;
361
  sensor->sensor_id   = _sensorID;
362
  sensor->type        = SENSOR_TYPE_ORIENTATION;
363
  sensor->min_delay   = 0;
364
  sensor->max_value   = 0.0F;
365
  sensor->min_value   = 0.0F;
366
  sensor->resolution  = 0.01F;
367
}
368

    
369
/**************************************************************************/
370
/*!
371
    @brief  Reads the sensor and returns the data as a sensors_event_t
372
*/
373
/**************************************************************************/
374
bool Adafruit_BNO055::getEvent(sensors_event_t *event)
375
{
376
  /* Clear the event */
377
  memset(event, 0, sizeof(sensors_event_t));
378

    
379
  event->version   = sizeof(sensors_event_t);
380
  event->sensor_id = _sensorID;
381
  event->type      = SENSOR_TYPE_ORIENTATION;
382
  event->timestamp = millis();
383

    
384
  /* Get a Euler angle sample for orientation */
385
  imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
386
  event->orientation.x = euler.x();
387
  event->orientation.y = euler.y();
388
  event->orientation.z = euler.z();
389

    
390
  return true;
391
}
392

    
393
/***************************************************************************
394
 PRIVATE FUNCTIONS
395
 ***************************************************************************/
396

    
397
/**************************************************************************/
398
/*!
399
    @brief  Writes an 8 bit value over I2C
400
*/
401
/**************************************************************************/
402
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value)
403
{
404
  Wire.beginTransmission(_address);
405
  #if ARDUINO >= 100
406
    Wire.write((uint8_t)reg);
407
    Wire.write((uint8_t)value);
408
  #else
409
    Wire.send(reg);
410
    Wire.send(value);
411
  #endif
412
  Wire.endTransmission();
413

    
414
  /* ToDo: Check for error! */
415
  return true;
416
}
417

    
418
/**************************************************************************/
419
/*!
420
    @brief  Reads an 8 bit value over I2C
421
*/
422
/**************************************************************************/
423
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg )
424
{
425
  byte value = 0;
426
  
427
  Wire.beginTransmission(_address);
428
  #if ARDUINO >= 100
429
    Wire.write((uint8_t)reg);
430
  #else
431
    Wire.send(reg);
432
  #endif
433
  Wire.endTransmission();
434
  Wire.requestFrom(_address, (byte)1);
435
  #if ARDUINO >= 100
436
    value = Wire.read();
437
  #else
438
    value = Wire.receive();
439
  #endif
440
  
441
  return value;
442
}
443

    
444
/**************************************************************************/
445
/*!
446
    @brief  Reads the specified number of bytes over I2C
447
*/
448
/**************************************************************************/
449
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte * buffer, uint8_t len)
450
{
451
  Wire.beginTransmission(_address);
452
  #if ARDUINO >= 100
453
    Wire.write((uint8_t)reg);
454
  #else
455
    Wire.send(reg);
456
  #endif
457
  Wire.endTransmission();
458
  Wire.requestFrom(_address, (byte)len);
459

    
460
  /* Wait until data is available */
461
  while (Wire.available() < len);
462
    
463
  for (uint8_t i = 0; i < len; i++)
464
  {
465
    #if ARDUINO >= 100
466
      buffer[i] = Wire.read();
467
    #else
468
      buffer[i] = Wire.receive();
469
    #endif
470
  }
471
  
472
  /* ToDo: Check for errors! */
473
  return true;
474
}