Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ b1f69f2e

History | View | Annotate | Download (13.373 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(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
    @brief  Gets a vector reading from the specified source
227
*/
228
/**************************************************************************/
229
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type)
230
{
231
  imu::Vector<3> xyz;
232
  uint8_t buffer[6];
233
  memset (buffer, 0, 6);
234
  
235
  int16_t x, y, z;
236
  x = y = z = 0;
237
  
238
  /* Read vector data (6 bytes) */
239
  readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
240
  x = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
241
  y = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
242
  z = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
243

    
244
  /* Convert the value to an appropriate range (section 3.6.4) */
245
  /* and assign the value to the Vector type */
246
  switch(vector_type)
247
  {
248
    case VECTOR_MAGNETOMETER:
249
      /* 1uT = 16 LSB */
250
      xyz[0] = ((double)x)/16.0;
251
      xyz[1] = ((double)y)/16.0;
252
      xyz[2] = ((double)z)/16.0;
253
      break;
254
    case VECTOR_GYROSCOPE:
255
      /* 1rps = 900 LSB */
256
      xyz[0] = ((double)x)/900.0;
257
      xyz[1] = ((double)y)/900.0;
258
      xyz[2] = ((double)z)/900.0;
259
      break;
260
    case VECTOR_EULER:
261
      /* 1 degree = 16 LSB */
262
      xyz[0] = ((double)x)/16.0;
263
      xyz[1] = ((double)y)/16.0;
264
      xyz[2] = ((double)z)/16.0;
265
      break;
266
    case VECTOR_ACCELEROMETER:
267
    case VECTOR_LINEARACCEL:
268
    case VECTOR_GRAVITY:
269
      /* 1m/s^2 = 100 LSB */
270
      xyz[0] = ((double)x)/100.0;
271
      xyz[1] = ((double)y)/100.0;
272
      xyz[2] = ((double)z)/100.0;
273
      break;
274
  }
275
  
276
  return xyz;
277
}
278

    
279
/**************************************************************************/
280
/*!
281
    @brief  Gets a quaternion reading from the specified source
282
*/
283
/**************************************************************************/
284
imu::Quaternion Adafruit_BNO055::getQuat(void)
285
{
286
  uint8_t buffer[8];
287
  memset (buffer, 0, 8);
288
  
289
  int16_t x, y, z, w;
290
  x = y = z = w = 0;
291
  
292
  /* Read quat data (8 bytes) */
293
  readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
294
  w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
295
  x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
296
  y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
297
  z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]);
298

    
299
  /* Assign to Quaternion */
300
  imu::Quaternion quat((double)w, (double)x, (double)y, (double)z);
301
  return quat;
302
}
303

    
304
/**************************************************************************/
305
/*
306
    Prints a float or double with the specified number of decimal places.
307

308
    'precision' should be 1 followed by a zero for every decimal place
309
    desired, so '100' will produce two decimal places:
310

311
    print_double(3.1415, 100); // Output = 3.14
312
*/
313
/**************************************************************************/
314
void Adafruit_BNO055::printDouble(double val, unsigned int precision)
315
{
316
  /* Print the integer portion */
317
  Serial.print (int(val));
318
  Serial.print(".");
319
  
320
  /* Print the fraction portion */
321
  unsigned int frac;
322
  if(val >= 0)
323
  {
324
    frac = (val - int(val)) * precision;
325
  }
326
  else
327
  {
328
    frac = (int(val)- val ) * precision;
329
  }
330
  Serial.println(frac,DEC) ;
331
}
332

    
333
/**************************************************************************/
334
/*!
335
    @brief  Provides the sensor_t data for this sensor
336
*/
337
/**************************************************************************/
338
void Adafruit_BNO055::getSensor(sensor_t *sensor)
339
{
340
  /* Clear the sensor_t object */
341
  memset(sensor, 0, sizeof(sensor_t));
342

    
343
  /* Insert the sensor name in the fixed length char array */
344
  strncpy (sensor->name, "BNO055", sizeof(sensor->name) - 1);
345
  sensor->name[sizeof(sensor->name)- 1] = 0;
346
  sensor->version     = 1;
347
  sensor->sensor_id   = _sensorID;
348
  sensor->type        = SENSOR_TYPE_ORIENTATION;
349
  sensor->min_delay   = 0;
350
  sensor->max_value   = 0.0F;
351
  sensor->min_value   = 0.0F;
352
  sensor->resolution  = 0.01F;
353
}
354

    
355
/**************************************************************************/
356
/*!
357
    @brief  Reads the sensor and returns the data as a sensors_event_t
358
*/
359
/**************************************************************************/
360
bool Adafruit_BNO055::getEvent(sensors_event_t *event)
361
{
362
  /* Clear the event */
363
  memset(event, 0, sizeof(sensors_event_t));
364

    
365
  event->version   = sizeof(sensors_event_t);
366
  event->sensor_id = _sensorID;
367
  event->type      = SENSOR_TYPE_ORIENTATION;
368
  event->timestamp = millis();
369

    
370
  /* Get a Euler angle sample for orientation */
371
  imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
372
  event->orientation.x = euler.x();
373
  event->orientation.y = euler.y();
374
  event->orientation.z = euler.z();
375

    
376
  return true;
377
}
378

    
379
/***************************************************************************
380
 PRIVATE FUNCTIONS
381
 ***************************************************************************/
382

    
383
/**************************************************************************/
384
/*!
385
    @brief  Writes an 8 bit value over I2C
386
*/
387
/**************************************************************************/
388
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value)
389
{
390
  Wire.beginTransmission(_address);
391
  #if ARDUINO >= 100
392
    Wire.write((uint8_t)reg);
393
    Wire.write((uint8_t)value);
394
  #else
395
    Wire.send(reg);
396
    Wire.send(value);
397
  #endif
398
  Wire.endTransmission();
399

    
400
  /* ToDo: Check for error! */
401
  return true;
402
}
403

    
404
/**************************************************************************/
405
/*!
406
    @brief  Reads an 8 bit value over I2C
407
*/
408
/**************************************************************************/
409
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg )
410
{
411
  byte value = 0;
412
  
413
  Wire.beginTransmission(_address);
414
  #if ARDUINO >= 100
415
    Wire.write((uint8_t)reg);
416
  #else
417
    Wire.send(reg);
418
  #endif
419
  Wire.endTransmission();
420
  Wire.requestFrom(_address, (byte)1);
421
  #if ARDUINO >= 100
422
    value = Wire.read();
423
  #else
424
    value = Wire.receive();
425
  #endif
426
  
427
  return value;
428
}
429

    
430
/**************************************************************************/
431
/*!
432
    @brief  Reads the specified number of bytes over I2C
433
*/
434
/**************************************************************************/
435
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte * buffer, uint8_t len)
436
{
437
  Wire.beginTransmission(_address);
438
  #if ARDUINO >= 100
439
    Wire.write((uint8_t)reg);
440
  #else
441
    Wire.send(reg);
442
  #endif
443
  Wire.endTransmission();
444
  Wire.requestFrom(_address, (byte)len);
445

    
446
  /* Wait until data is available */
447
  while (Wire.available() < len);
448
    
449
  for (uint8_t i = 0; i < len; i++)
450
  {
451
    #if ARDUINO >= 100
452
      buffer[i] = Wire.read();
453
    #else
454
      buffer[i] = Wire.receive();
455
    #endif
456
  }
457
  
458
  /* ToDo: Check for errors! */
459
  return true;
460
}