Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 26f98bcd

History | View | Annotate | Download (12.588 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
    @brief  Provides the sensor_t data for this sensor
311
*/
312
/**************************************************************************/
313
void Adafruit_BNO055::getSensor(sensor_t *sensor)
314
{
315
  /* Clear the sensor_t object */
316
  memset(sensor, 0, sizeof(sensor_t));
317

    
318
  /* Insert the sensor name in the fixed length char array */
319
  strncpy (sensor->name, "BNO055", sizeof(sensor->name) - 1);
320
  sensor->name[sizeof(sensor->name)- 1] = 0;
321
  sensor->version     = 1;
322
  sensor->sensor_id   = _sensorID;
323
  sensor->type        = SENSOR_TYPE_ORIENTATION;
324
  sensor->min_delay   = 0;
325
  sensor->max_value   = 0.0F;
326
  sensor->min_value   = 0.0F;
327
  sensor->resolution  = 0.01F;
328
}
329

    
330
/**************************************************************************/
331
/*!
332
    @brief  Reads the sensor and returns the data as a sensors_event_t
333
*/
334
/**************************************************************************/
335
bool Adafruit_BNO055::getEvent(sensors_event_t *event)
336
{
337
  float orientation;
338

    
339
  /* Clear the event */
340
  memset(event, 0, sizeof(sensors_event_t));
341

    
342
  event->version   = sizeof(sensors_event_t);
343
  event->sensor_id = _sensorID;
344
  event->type      = SENSOR_TYPE_ORIENTATION;
345
  event->timestamp = 0;
346
  /* 
347
  getPressure(&pressure_kPa);
348
  event->pressure = pressure_kPa / 100.0F;
349
  */
350
  
351
  return true;
352
}
353

    
354
/***************************************************************************
355
 PRIVATE FUNCTIONS
356
 ***************************************************************************/
357

    
358
/**************************************************************************/
359
/*!
360
    @brief  Writes an 8 bit value over I2C
361
*/
362
/**************************************************************************/
363
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value)
364
{
365
  Wire.beginTransmission(_address);
366
  #if ARDUINO >= 100
367
    Wire.write((uint8_t)reg);
368
    Wire.write((uint8_t)value);
369
  #else
370
    Wire.send(reg);
371
    Wire.send(value);
372
  #endif
373
  Wire.endTransmission();
374

    
375
  /* ToDo: Check for error! */
376
  return true;
377
}
378

    
379
/**************************************************************************/
380
/*!
381
    @brief  Reads an 8 bit value over I2C
382
*/
383
/**************************************************************************/
384
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg )
385
{
386
  byte value = 0;
387
  
388
  Wire.beginTransmission(_address);
389
  #if ARDUINO >= 100
390
    Wire.write((uint8_t)reg);
391
  #else
392
    Wire.send(reg);
393
  #endif
394
  Wire.endTransmission();
395
  Wire.requestFrom(_address, (byte)1);
396
  #if ARDUINO >= 100
397
    value = Wire.read();
398
  #else
399
    value = Wire.receive();
400
  #endif
401
  
402
  return value;
403
}
404

    
405
/**************************************************************************/
406
/*!
407
    @brief  Reads the specified number of bytes over I2C
408
*/
409
/**************************************************************************/
410
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte * buffer, uint8_t len)
411
{
412
  Wire.beginTransmission(_address);
413
  #if ARDUINO >= 100
414
    Wire.write((uint8_t)reg);
415
  #else
416
    Wire.send(reg);
417
  #endif
418
  Wire.endTransmission();
419
  Wire.requestFrom(_address, (byte)len);
420

    
421
  /* Wait until data is available */
422
  while (Wire.available() < len);
423
    
424
  for (uint8_t i = 0; i < len; i++)
425
  {
426
    #if ARDUINO >= 100
427
      buffer[i] = Wire.read();
428
    #else
429
      buffer[i] = Wire.receive();
430
    #endif
431
  }
432
  
433
  /* ToDo: Check for errors! */
434
  return true;
435
}