Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 0e2e2723

History | View | Annotate | Download (13.688 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
 
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 26f98bcd Kevin Townsend
  
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 4bc1c0c1 Kevin Townsend
  /* 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(adafruit_bno055_system_status_t * status)
108
{
109
  memset(status, 0, sizeof(adafruit_bno055_system_status_t));
110
  
111
  /* Read the system status register */
112
  status->system_status    = read8(BNO055_SYS_STAT_ADDR);
113
  status->self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR);
114
  status->system_error     = read8(BNO055_SYS_ERR_ADDR);
115
}
116
117
/**************************************************************************/
118
/*!
119
    @brief  Displays system status info via Serial.print
120
*/
121
/**************************************************************************/
122
void Adafruit_BNO055::displaySystemStatus(void)
123
{
124
  adafruit_bno055_system_status_t status;
125
  getSystemStatus(&status);
126
  
127
  /* System Status (see section 4.3.58)
128
     ---------------------------------
129
     0 = Idle
130
     1 = System Error
131
     2 = Initializing Peripherals
132
     3 = System Iniitalization
133
     4 = Executing Self-Test
134
     5 = Sensor fusio algorithm running
135
     6 = System running without fusion algorithms */
136
  
137
  Serial.print("System Status:          0x");
138
  Serial.println(status.system_status, HEX);
139
140
  /* Self Test Results (see section )
141
     --------------------------------
142
     1 = test passed, 0 = test failed
143
    
144
     Bit 0 = Accelerometer self test
145
     Bit 1 = Magnetometer self test
146
     Bit 2 = Gyroscope self test
147
     Bit 3 = MCU self test
148
  
149
     0x0F = all good! */
150
  
151
  Serial.print("Self Test Results:      0x");
152
  Serial.println(status.self_test_result, HEX);
153
154
  /* System Error (see section 4.3.59)
155
     ---------------------------------
156
     0 = No error
157
     1 = Peripheral initialization error
158
     2 = System initialization error
159
     3 = Self test result failed
160
     4 = Register map value out of range
161
     5 = Register map address out of range
162
     6 = Register map write error
163
     7 = BNO low power mode not available for selected operat ion mode
164
     8 = Accelerometer power mode not available
165
     9 = Fusion algorithm configuration error
166
     A = Sensor configuration error */
167
  
168
  Serial.print("System Error:           0x");
169
  Serial.println(status.system_error, HEX);
170
}
171
172
/**************************************************************************/
173
/*!
174
    @brief  Gets the chip revision numbers
175
*/
176
/**************************************************************************/
177
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t* info)
178
{
179
  uint8_t a, b;
180
181
  memset(info, 0, sizeof(adafruit_bno055_rev_info_t));
182
183
  info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR);
184
  info->mag_rev   = read8(BNO055_MAG_REV_ID_ADDR);
185
  info->gyro_rev  = read8(BNO055_GYRO_REV_ID_ADDR);
186
  info->bl_rev    = read8(BNO055_BL_REV_ID_ADDR);
187
  
188
  a = read8(BNO055_SW_REV_ID_LSB_ADDR);
189
  b = read8(BNO055_SW_REV_ID_MSB_ADDR);
190
  info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
191
}
192
193
/**************************************************************************/
194
/*!
195
    @brief  Displays the chip revision numbers via Serial.print
196
*/
197
/**************************************************************************/
198
void Adafruit_BNO055::displayRevInfo(void)
199
{
200
  adafruit_bno055_rev_info_t info;
201
  getRevInfo(&info);
202
203
  /* Check the accelerometer revision */
204
  Serial.print("Accelerometer Revision: 0x");
205
  Serial.println(info.accel_rev, HEX);
206
  
207
  /* Check the magnetometer revision */
208
  Serial.print("Magnetometer Revision:  0x");
209
  Serial.println(info.mag_rev, HEX);
210
  
211
  /* Check the gyroscope revision */
212
  Serial.print("Gyroscope Revision:     0x");
213
  Serial.println(info.gyro_rev, HEX);
214
  
215
  /* Check the SW revision */
216
  Serial.print("SW Revision:            0x");
217
  Serial.println(info.sw_rev, HEX);
218
  
219
  /* Check the bootloader revision */
220
  Serial.print("Bootloader Revision:    0x");
221
  Serial.println(info.bl_rev, HEX);
222
}
223
224
/**************************************************************************/
225
/*!
226 0e2e2723 Kevin Townsend
    @brief  Gets teh temperature in degrees celsius
227
*/
228
/**************************************************************************/
229
int8_t Adafruit_BNO055::getTemp(void)
230
{
231
  int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR));
232
  return temp;
233
}
234
235
/**************************************************************************/
236
/*!
237 48741e1f Kevin Townsend
    @brief  Gets a vector reading from the specified source
238 4bc1c0c1 Kevin Townsend
*/
239
/**************************************************************************/
240 48741e1f Kevin Townsend
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type)
241 4bc1c0c1 Kevin Townsend
{
242
  imu::Vector<3> xyz;
243
  uint8_t buffer[6];
244
  memset (buffer, 0, 6);
245
  
246
  int16_t x, y, z;
247
  x = y = z = 0;
248
  
249 48741e1f Kevin Townsend
  /* Read vector data (6 bytes) */
250
  readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
251 4bc1c0c1 Kevin Townsend
  x = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
252
  y = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
253
  z = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
254 26f98bcd Kevin Townsend
255
  /* Convert the value to an appropriate range (section 3.6.4) */
256
  /* and assign the value to the Vector type */
257
  switch(vector_type)
258
  {
259
    case VECTOR_MAGNETOMETER:
260
      /* 1uT = 16 LSB */
261
      xyz[0] = ((double)x)/16.0;
262
      xyz[1] = ((double)y)/16.0;
263
      xyz[2] = ((double)z)/16.0;
264
      break;
265
    case VECTOR_GYROSCOPE:
266
      /* 1rps = 900 LSB */
267
      xyz[0] = ((double)x)/900.0;
268
      xyz[1] = ((double)y)/900.0;
269
      xyz[2] = ((double)z)/900.0;
270
      break;
271
    case VECTOR_EULER:
272
      /* 1 degree = 16 LSB */
273
      xyz[0] = ((double)x)/16.0;
274
      xyz[1] = ((double)y)/16.0;
275
      xyz[2] = ((double)z)/16.0;
276
      break;
277
    case VECTOR_ACCELEROMETER:
278
    case VECTOR_LINEARACCEL:
279
    case VECTOR_GRAVITY:
280
      /* 1m/s^2 = 100 LSB */
281
      xyz[0] = ((double)x)/100.0;
282
      xyz[1] = ((double)y)/100.0;
283
      xyz[2] = ((double)z)/100.0;
284
      break;
285
  }
286 4bc1c0c1 Kevin Townsend
  
287
  return xyz;
288
}
289
290
/**************************************************************************/
291
/*!
292 48741e1f Kevin Townsend
    @brief  Gets a quaternion reading from the specified source
293
*/
294
/**************************************************************************/
295
imu::Quaternion Adafruit_BNO055::getQuat(void)
296
{
297
  uint8_t buffer[8];
298
  memset (buffer, 0, 8);
299
  
300
  int16_t x, y, z, w;
301
  x = y = z = w = 0;
302
  
303
  /* Read quat data (8 bytes) */
304
  readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
305
  w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
306
  x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
307
  y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
308
  z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]);
309
310
  /* Assign to Quaternion */
311
  imu::Quaternion quat((double)w, (double)x, (double)y, (double)z);
312
  return quat;
313
}
314
315
/**************************************************************************/
316 fcd68623 Kevin Townsend
/*
317
    Prints a float or double with the specified number of decimal places.
318

319
    'precision' should be 1 followed by a zero for every decimal place
320
    desired, so '100' will produce two decimal places:
321

322
    print_double(3.1415, 100); // Output = 3.14
323
*/
324
/**************************************************************************/
325
void Adafruit_BNO055::printDouble(double val, unsigned int precision)
326
{
327
  /* Print the integer portion */
328
  Serial.print (int(val));
329
  Serial.print(".");
330
  
331
  /* Print the fraction portion */
332
  unsigned int frac;
333
  if(val >= 0)
334
  {
335
    frac = (val - int(val)) * precision;
336
  }
337
  else
338
  {
339
    frac = (int(val)- val ) * precision;
340
  }
341
  Serial.println(frac,DEC) ;
342
}
343
344
/**************************************************************************/
345 48741e1f Kevin Townsend
/*!
346 4bc1c0c1 Kevin Townsend
    @brief  Provides the sensor_t data for this sensor
347
*/
348
/**************************************************************************/
349
void Adafruit_BNO055::getSensor(sensor_t *sensor)
350
{
351
  /* Clear the sensor_t object */
352
  memset(sensor, 0, sizeof(sensor_t));
353
354
  /* Insert the sensor name in the fixed length char array */
355
  strncpy (sensor->name, "BNO055", sizeof(sensor->name) - 1);
356
  sensor->name[sizeof(sensor->name)- 1] = 0;
357
  sensor->version     = 1;
358
  sensor->sensor_id   = _sensorID;
359
  sensor->type        = SENSOR_TYPE_ORIENTATION;
360
  sensor->min_delay   = 0;
361
  sensor->max_value   = 0.0F;
362
  sensor->min_value   = 0.0F;
363
  sensor->resolution  = 0.01F;
364
}
365
366
/**************************************************************************/
367
/*!
368
    @brief  Reads the sensor and returns the data as a sensors_event_t
369
*/
370
/**************************************************************************/
371
bool Adafruit_BNO055::getEvent(sensors_event_t *event)
372
{
373
  /* Clear the event */
374
  memset(event, 0, sizeof(sensors_event_t));
375
376
  event->version   = sizeof(sensors_event_t);
377
  event->sensor_id = _sensorID;
378
  event->type      = SENSOR_TYPE_ORIENTATION;
379 fcd68623 Kevin Townsend
  event->timestamp = millis();
380
381
  /* Get a Euler angle sample for orientation */
382
  imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
383
  event->orientation.x = euler.x();
384
  event->orientation.y = euler.y();
385
  event->orientation.z = euler.z();
386
387 4bc1c0c1 Kevin Townsend
  return true;
388
}
389
390
/***************************************************************************
391
 PRIVATE FUNCTIONS
392
 ***************************************************************************/
393
394
/**************************************************************************/
395
/*!
396
    @brief  Writes an 8 bit value over I2C
397
*/
398
/**************************************************************************/
399
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value)
400
{
401
  Wire.beginTransmission(_address);
402
  #if ARDUINO >= 100
403
    Wire.write((uint8_t)reg);
404
    Wire.write((uint8_t)value);
405
  #else
406
    Wire.send(reg);
407
    Wire.send(value);
408
  #endif
409
  Wire.endTransmission();
410
411
  /* ToDo: Check for error! */
412
  return true;
413
}
414
415
/**************************************************************************/
416
/*!
417
    @brief  Reads an 8 bit value over I2C
418
*/
419
/**************************************************************************/
420
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg )
421
{
422
  byte value = 0;
423
  
424
  Wire.beginTransmission(_address);
425
  #if ARDUINO >= 100
426
    Wire.write((uint8_t)reg);
427
  #else
428
    Wire.send(reg);
429
  #endif
430
  Wire.endTransmission();
431
  Wire.requestFrom(_address, (byte)1);
432
  #if ARDUINO >= 100
433
    value = Wire.read();
434
  #else
435
    value = Wire.receive();
436
  #endif
437
  
438
  return value;
439
}
440
441
/**************************************************************************/
442
/*!
443
    @brief  Reads the specified number of bytes over I2C
444
*/
445
/**************************************************************************/
446
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte * buffer, uint8_t len)
447
{
448
  Wire.beginTransmission(_address);
449
  #if ARDUINO >= 100
450
    Wire.write((uint8_t)reg);
451
  #else
452
    Wire.send(reg);
453
  #endif
454
  Wire.endTransmission();
455
  Wire.requestFrom(_address, (byte)len);
456
457
  /* Wait until data is available */
458
  while (Wire.available() < len);
459
    
460
  for (uint8_t i = 0; i < len; i++)
461
  {
462
    #if ARDUINO >= 100
463
      buffer[i] = Wire.read();
464
    #else
465
      buffer[i] = Wire.receive();
466
    #endif
467
  }
468
  
469
  /* ToDo: Check for errors! */
470
  return true;
471
}