Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ fcd68623

History | View | Annotate | Download (13.466 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
  /* Orientation = Android (0 << )*/
83
  /* Temperature = Celsisu (0 << 4)
84
  uint8_t unitsel = 
85

86
  /* Set the requested operating mode (see section 3.3) */
87
  write8(BNO055_OPR_MODE_ADDR, mode);
88
  delay(20);
89

    
90
  return true;
91
}
92

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

    
106
/**************************************************************************/
107
/*!
108
    @brief  Gets the latest system status info
109
*/
110
/**************************************************************************/
111
void Adafruit_BNO055::getSystemStatus(adafruit_bno055_system_status_t * status)
112
{
113
  memset(status, 0, sizeof(adafruit_bno055_system_status_t));
114
  
115
  /* Read the system status register */
116
  status->system_status    = read8(BNO055_SYS_STAT_ADDR);
117
  status->self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR);
118
  status->system_error     = read8(BNO055_SYS_ERR_ADDR);
119
}
120

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

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

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

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

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

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

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

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

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

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

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

    
303
  /* Assign to Quaternion */
304
  imu::Quaternion quat((double)w, (double)x, (double)y, (double)z);
305
  return quat;
306
}
307

    
308
/**************************************************************************/
309
/*
310
    Prints a float or double with the specified number of decimal places.
311

312
    'precision' should be 1 followed by a zero for every decimal place
313
    desired, so '100' will produce two decimal places:
314

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

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

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

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

    
369
  event->version   = sizeof(sensors_event_t);
370
  event->sensor_id = _sensorID;
371
  event->type      = SENSOR_TYPE_ORIENTATION;
372
  event->timestamp = millis();
373

    
374
  /* Get a Euler angle sample for orientation */
375
  imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
376
  event->orientation.x = euler.x();
377
  event->orientation.y = euler.y();
378
  event->orientation.z = euler.z();
379

    
380
  return true;
381
}
382

    
383
/***************************************************************************
384
 PRIVATE FUNCTIONS
385
 ***************************************************************************/
386

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

    
404
  /* ToDo: Check for error! */
405
  return true;
406
}
407

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

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

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