Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 05b57936

History | View | Annotate | Download (26.09 KB)

1 5d8d461e Jan Hoffmann
/*!
2
 * @file Adafruit_BNO055.cpp
3
 *
4
 *  @mainpage Adafruit BNO055 Orientation Sensor
5 75f03d2e Szymon Kaliski
 *
6 5d8d461e Jan Hoffmann
 *  @section intro_sec Introduction
7
 *
8 75f03d2e Szymon Kaliski
 *  This is a library for the BNO055 orientation sensor
9 5d8d461e Jan Hoffmann
 *
10 75f03d2e Szymon Kaliski
 *  Designed specifically to work with the Adafruit BNO055 Breakout.
11 5d8d461e Jan Hoffmann
 *
12 75f03d2e Szymon Kaliski
 *  Pick one up today in the adafruit shop!
13
 *  ------> https://www.adafruit.com/product/2472
14 5d8d461e Jan Hoffmann
 *
15 75f03d2e Szymon Kaliski
 *  These sensors use I2C to communicate, 2 pins are required to interface.
16 5d8d461e Jan Hoffmann
 *
17 75f03d2e Szymon Kaliski
 *  Adafruit invests time and resources providing this open source code,
18
 *  please support Adafruit andopen-source hardware by purchasing products
19
 *  from Adafruit!
20 5d8d461e Jan Hoffmann
 *
21
 *  @section author Author
22
 *
23
 *  K.Townsend (Adafruit Industries)
24
 *
25 75f03d2e Szymon Kaliski
 *  @section license License
26
 *
27
 *  MIT license, all text above must be included in any redistribution
28 5d8d461e Jan Hoffmann
 */
29 4bc1c0c1 Kevin Townsend
30 55e2f6d1 Jan Hoffmann
#include "Arduino.h"
31 4bc1c0c1 Kevin Townsend
32
#include <limits.h>
33 55e2f6d1 Jan Hoffmann
#include <math.h>
34 4bc1c0c1 Kevin Townsend
35
#include "Adafruit_BNO055.h"
36
37
/*!
38 5d8d461e Jan Hoffmann
 *  @brief  Instantiates a new Adafruit_BNO055 class
39 9a8e8b26 Jan Hoffmann
 *  @param  sensorID
40
 *          sensor ID
41
 *  @param  address
42
 *          i2c address
43 2b95af9e ulysse314
 *  @param  theWire
44 dbc34b4d Jan Hoffmann
 *          Wire object
45 5d8d461e Jan Hoffmann
 */
46 9a8e8b26 Jan Hoffmann
Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address,
47
                                 TwoWire *theWire) {
48
  _sensorID = sensorID;
49
  _address = address;
50 83911fa6 Jan Hoffmann
  _wire = theWire;
51 9a8e8b26 Jan Hoffmann
}
52 f7556b0d Jan Hoffmann
53
/*!
54
 *  @brief  Sets up the HW
55
 *  @param  mode
56
 *          mode values
57
 *           [OPERATION_MODE_CONFIG,
58
 *            OPERATION_MODE_ACCONLY,
59
 *            OPERATION_MODE_MAGONLY,
60
 *            OPERATION_MODE_GYRONLY,
61
 *            OPERATION_MODE_ACCMAG,
62
 *            OPERATION_MODE_ACCGYRO,
63
 *            OPERATION_MODE_MAGGYRO,
64
 *            OPERATION_MODE_AMG,
65
 *            OPERATION_MODE_IMUPLUS,
66
 *            OPERATION_MODE_COMPASS,
67
 *            OPERATION_MODE_M4G,
68
 *            OPERATION_MODE_NDOF_FMC_OFF,
69
 *            OPERATION_MODE_NDOF]
70
 *  @return true if process is successful
71
 */
72 9a8e8b26 Jan Hoffmann
bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode) {
73
#if defined(ARDUINO_SAMD_ZERO) && (_address == BNO055_ADDRESS_A)
74 f7556b0d Jan Hoffmann
#error                                                                         \
75
    "On an arduino Zero, BNO055's ADR pin must be high. Fix that, then delete this line."
76
  _address = BNO055_ADDRESS_B;
77
#endif
78
79 463eabf7 Wetmelon
  /* Enable I2C */
80 f7556b0d Jan Hoffmann
  _wire->begin();
81 463eabf7 Wetmelon
82 9a8e8b26 Jan Hoffmann
  // BNO055 clock stretches for 500us or more!
83 78cc710f ladyada
#ifdef ESP8266
84 9a8e8b26 Jan Hoffmann
  _wire->setClockStretchLimit(1000); // Allow for 1000us of clock stretching
85 78cc710f ladyada
#endif
86
87 463eabf7 Wetmelon
  /* Make sure we have the right device */
88
  uint8_t id = read8(BNO055_CHIP_ID_ADDR);
89 55e2f6d1 Jan Hoffmann
  if (id != BNO055_ID) {
90 9a8e8b26 Jan Hoffmann
    delay(1000); // hold on for boot
91 463eabf7 Wetmelon
    id = read8(BNO055_CHIP_ID_ADDR);
92 55e2f6d1 Jan Hoffmann
    if (id != BNO055_ID) {
93 9a8e8b26 Jan Hoffmann
      return false; // still not? ok bail
94 463eabf7 Wetmelon
    }
95
  }
96
97
  /* Switch to config mode (just in case since this is the default) */
98
  setMode(OPERATION_MODE_CONFIG);
99
100
  /* Reset */
101
  write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
102 5f6b7a75 Jan Hoffmann
  /* Delay incrased to 30ms due to power issues https://tinyurl.com/y375z699 */
103
  delay(30);
104 55e2f6d1 Jan Hoffmann
  while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID) {
105 463eabf7 Wetmelon
    delay(10);
106
  }
107
  delay(50);
108
109
  /* Set to normal power mode */
110
  write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL);
111
  delay(10);
112
113
  write8(BNO055_PAGE_ID_ADDR, 0);
114
115 9a8e8b26 Jan Hoffmann
  /* Set the output units */
116
  /*
117
  uint8_t unitsel = (0 << 7) | // Orientation = Android
118
                    (0 << 4) | // Temperature = Celsius
119
                    (0 << 2) | // Euler = Degrees
120
                    (1 << 1) | // Gyro = Rads
121
                    (0 << 0);  // Accelerometer = m/s^2
122
  write8(BNO055_UNIT_SEL_ADDR, unitsel);
123
  */
124 463eabf7 Wetmelon
125 378858ec Shunya Sato
  /* Configure axis mapping (see section 3.4) */
126 9a8e8b26 Jan Hoffmann
  /*
127
  write8(BNO055_AXIS_MAP_CONFIG_ADDR, REMAP_CONFIG_P2); // P0-P7, Default is P1
128
  delay(10);
129
  write8(BNO055_AXIS_MAP_SIGN_ADDR, REMAP_SIGN_P2); // P0-P7, Default is P1
130
  delay(10);
131
  */
132
133 463eabf7 Wetmelon
  write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
134
  delay(10);
135
  /* Set the requested operating mode (see section 3.3) */
136
  setMode(mode);
137
  delay(20);
138
139
  return true;
140 4bc1c0c1 Kevin Townsend
}
141
142
/*!
143 5d8d461e Jan Hoffmann
 *  @brief  Puts the chip in the specified operating mode
144
 *  @param  mode
145 f7556b0d Jan Hoffmann
 *          mode values
146
 *           [OPERATION_MODE_CONFIG,
147
 *            OPERATION_MODE_ACCONLY,
148
 *            OPERATION_MODE_MAGONLY,
149
 *            OPERATION_MODE_GYRONLY,
150
 *            OPERATION_MODE_ACCMAG,
151
 *            OPERATION_MODE_ACCGYRO,
152
 *            OPERATION_MODE_MAGGYRO,
153
 *            OPERATION_MODE_AMG,
154
 *            OPERATION_MODE_IMUPLUS,
155
 *            OPERATION_MODE_COMPASS,
156
 *            OPERATION_MODE_M4G,
157
 *            OPERATION_MODE_NDOF_FMC_OFF,
158
 *            OPERATION_MODE_NDOF]
159 5d8d461e Jan Hoffmann
 */
160 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode) {
161 463eabf7 Wetmelon
  _mode = mode;
162
  write8(BNO055_OPR_MODE_ADDR, _mode);
163
  delay(30);
164 4bc1c0c1 Kevin Townsend
}
165
166
/*!
167 5d8d461e Jan Hoffmann
 *  @brief  Changes the chip's axis remap
168
 *  @param  remapcode
169 f7556b0d Jan Hoffmann
 *          remap code possible values
170
 *          [REMAP_CONFIG_P0
171
 *           REMAP_CONFIG_P1 (default)
172
 *           REMAP_CONFIG_P2
173
 *           REMAP_CONFIG_P3
174
 *           REMAP_CONFIG_P4
175
 *           REMAP_CONFIG_P5
176
 *           REMAP_CONFIG_P6
177
 *           REMAP_CONFIG_P7]
178 5d8d461e Jan Hoffmann
 */
179 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setAxisRemap(
180
    adafruit_bno055_axis_remap_config_t remapcode) {
181 a97e0fe1 kA®0šhî
  adafruit_bno055_opmode_t modeback = _mode;
182
183
  setMode(OPERATION_MODE_CONFIG);
184
  delay(25);
185
  write8(BNO055_AXIS_MAP_CONFIG_ADDR, remapcode);
186
  delay(10);
187
  /* Set the requested operating mode (see section 3.3) */
188
  setMode(modeback);
189
  delay(20);
190
}
191
192
/*!
193 5d8d461e Jan Hoffmann
 *  @brief  Changes the chip's axis signs
194
 *  @param  remapsign
195 f7556b0d Jan Hoffmann
 *          remap sign possible values
196
 *          [REMAP_SIGN_P0
197
 *           REMAP_SIGN_P1 (default)
198
 *           REMAP_SIGN_P2
199
 *           REMAP_SIGN_P3
200
 *           REMAP_SIGN_P4
201
 *           REMAP_SIGN_P5
202
 *           REMAP_SIGN_P6
203
 *           REMAP_SIGN_P7]
204 5d8d461e Jan Hoffmann
 */
205 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setAxisSign(adafruit_bno055_axis_remap_sign_t remapsign) {
206 a97e0fe1 kA®0šhî
  adafruit_bno055_opmode_t modeback = _mode;
207
208
  setMode(OPERATION_MODE_CONFIG);
209
  delay(25);
210
  write8(BNO055_AXIS_MAP_SIGN_ADDR, remapsign);
211
  delay(10);
212
  /* Set the requested operating mode (see section 3.3) */
213
  setMode(modeback);
214
  delay(20);
215
}
216
217
/*!
218 5d8d461e Jan Hoffmann
 *  @brief  Use the external 32.768KHz crystal
219 f7556b0d Jan Hoffmann
 *  @param  usextal
220
 *          use external crystal boolean
221 5d8d461e Jan Hoffmann
 */
222 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setExtCrystalUse(boolean usextal) {
223 463eabf7 Wetmelon
  adafruit_bno055_opmode_t modeback = _mode;
224
225
  /* Switch to config mode (just in case since this is the default) */
226
  setMode(OPERATION_MODE_CONFIG);
227
  delay(25);
228
  write8(BNO055_PAGE_ID_ADDR, 0);
229
  if (usextal) {
230
    write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
231
  } else {
232
    write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
233
  }
234
  delay(10);
235
  /* Set the requested operating mode (see section 3.3) */
236
  setMode(modeback);
237
  delay(20);
238 c4f272e1 ladyada
}
239
240
/*!
241 5d8d461e Jan Hoffmann
 *   @brief  Gets the latest system status info
242
 *   @param  system_status
243 f7556b0d Jan Hoffmann
 *           system status info
244 5d8d461e Jan Hoffmann
 *   @param  self_test_result
245 f7556b0d Jan Hoffmann
 *           self test result
246 5d8d461e Jan Hoffmann
 *   @param  system_error
247 f7556b0d Jan Hoffmann
 *           system error info
248 5d8d461e Jan Hoffmann
 */
249 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status,
250
                                      uint8_t *self_test_result,
251
                                      uint8_t *system_error) {
252 463eabf7 Wetmelon
  write8(BNO055_PAGE_ID_ADDR, 0);
253
254
  /* System Status (see section 4.3.58)
255
     0 = Idle
256
     1 = System Error
257
     2 = Initializing Peripherals
258
     3 = System Iniitalization
259
     4 = Executing Self-Test
260
     5 = Sensor fusio algorithm running
261 75f03d2e Szymon Kaliski
     6 = System running without fusion algorithms
262 5d8d461e Jan Hoffmann
   */
263 463eabf7 Wetmelon
264
  if (system_status != 0)
265 55e2f6d1 Jan Hoffmann
    *system_status = read8(BNO055_SYS_STAT_ADDR);
266 463eabf7 Wetmelon
267 5d8d461e Jan Hoffmann
  /* Self Test Results
268 463eabf7 Wetmelon
     1 = test passed, 0 = test failed
269 75f03d2e Szymon Kaliski

270 463eabf7 Wetmelon
     Bit 0 = Accelerometer self test
271
     Bit 1 = Magnetometer self test
272
     Bit 2 = Gyroscope self test
273
     Bit 3 = MCU self test
274 75f03d2e Szymon Kaliski

275
     0x0F = all good!
276 5d8d461e Jan Hoffmann
   */
277 463eabf7 Wetmelon
278
  if (self_test_result != 0)
279
    *self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR);
280
281
  /* System Error (see section 4.3.59)
282
     0 = No error
283
     1 = Peripheral initialization error
284
     2 = System initialization error
285
     3 = Self test result failed
286
     4 = Register map value out of range
287
     5 = Register map address out of range
288
     6 = Register map write error
289
     7 = BNO low power mode not available for selected operat ion mode
290
     8 = Accelerometer power mode not available
291
     9 = Fusion algorithm configuration error
292 75f03d2e Szymon Kaliski
     A = Sensor configuration error
293 5d8d461e Jan Hoffmann
   */
294 463eabf7 Wetmelon
295
  if (system_error != 0)
296 55e2f6d1 Jan Hoffmann
    *system_error = read8(BNO055_SYS_ERR_ADDR);
297 463eabf7 Wetmelon
298
  delay(200);
299 4bc1c0c1 Kevin Townsend
}
300
301
/*!
302 5d8d461e Jan Hoffmann
 *  @brief  Gets the chip revision numbers
303 f7556b0d Jan Hoffmann
 *  @param  info
304
 *          revision info
305 5d8d461e Jan Hoffmann
 */
306 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t *info) {
307 463eabf7 Wetmelon
  uint8_t a, b;
308 4bc1c0c1 Kevin Townsend
309 463eabf7 Wetmelon
  memset(info, 0, sizeof(adafruit_bno055_rev_info_t));
310 4bc1c0c1 Kevin Townsend
311 463eabf7 Wetmelon
  /* Check the accelerometer revision */
312
  info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR);
313 67f3cff5 Kevin Townsend
314 463eabf7 Wetmelon
  /* Check the magnetometer revision */
315 55e2f6d1 Jan Hoffmann
  info->mag_rev = read8(BNO055_MAG_REV_ID_ADDR);
316 67f3cff5 Kevin Townsend
317 463eabf7 Wetmelon
  /* Check the gyroscope revision */
318 55e2f6d1 Jan Hoffmann
  info->gyro_rev = read8(BNO055_GYRO_REV_ID_ADDR);
319 67f3cff5 Kevin Townsend
320 463eabf7 Wetmelon
  /* Check the SW revision */
321 55e2f6d1 Jan Hoffmann
  info->bl_rev = read8(BNO055_BL_REV_ID_ADDR);
322 40f91f6f Tony DiCola
323 463eabf7 Wetmelon
  a = read8(BNO055_SW_REV_ID_LSB_ADDR);
324
  b = read8(BNO055_SW_REV_ID_MSB_ADDR);
325
  info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
326 4bc1c0c1 Kevin Townsend
}
327
328
/*!
329 5d8d461e Jan Hoffmann
 *  @brief  Gets current calibration state.  Each value should be a uint8_t
330
 *          pointer and it will be set to 0 if not calibrated and 3 if
331
 *          fully calibrated.
332 f7556b0d Jan Hoffmann
 *          See section 34.3.54
333 5d8d461e Jan Hoffmann
 *  @param  sys
334 f7556b0d Jan Hoffmann
 *          Current system calibration status, depends on status of all sensors,
335
 * read-only
336 5d8d461e Jan Hoffmann
 *  @param  gyro
337 f7556b0d Jan Hoffmann
 *          Current calibration status of Gyroscope, read-only
338 5d8d461e Jan Hoffmann
 *  @param  accel
339 f7556b0d Jan Hoffmann
 *          Current calibration status of Accelerometer, read-only
340 5d8d461e Jan Hoffmann
 *  @param  mag
341 f7556b0d Jan Hoffmann
 *          Current calibration status of Magnetometer, read-only
342 5d8d461e Jan Hoffmann
 */
343 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::getCalibration(uint8_t *sys, uint8_t *gyro,
344
                                     uint8_t *accel, uint8_t *mag) {
345 463eabf7 Wetmelon
  uint8_t calData = read8(BNO055_CALIB_STAT_ADDR);
346
  if (sys != NULL) {
347
    *sys = (calData >> 6) & 0x03;
348
  }
349
  if (gyro != NULL) {
350
    *gyro = (calData >> 4) & 0x03;
351
  }
352
  if (accel != NULL) {
353
    *accel = (calData >> 2) & 0x03;
354
  }
355
  if (mag != NULL) {
356
    *mag = calData & 0x03;
357
  }
358 40f91f6f Tony DiCola
}
359
360
/*!
361 5d8d461e Jan Hoffmann
 *  @brief  Gets the temperature in degrees celsius
362
 *  @return temperature in degrees celsius
363
 */
364 55e2f6d1 Jan Hoffmann
int8_t Adafruit_BNO055::getTemp() {
365 463eabf7 Wetmelon
  int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR));
366
  return temp;
367 0e2e2723 Kevin Townsend
}
368
369
/*!
370 5d8d461e Jan Hoffmann
 *  @brief   Gets a vector reading from the specified source
371
 *  @param   vector_type
372 f7556b0d Jan Hoffmann
 *           possible vector type values
373
 *           [VECTOR_ACCELEROMETER
374
 *            VECTOR_MAGNETOMETER
375
 *            VECTOR_GYROSCOPE
376
 *            VECTOR_EULER
377
 *            VECTOR_LINEARACCEL
378
 *            VECTOR_GRAVITY]
379 5d8d461e Jan Hoffmann
 *  @return  vector from specified source
380
 */
381 55e2f6d1 Jan Hoffmann
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type) {
382 463eabf7 Wetmelon
  imu::Vector<3> xyz;
383
  uint8_t buffer[6];
384 55e2f6d1 Jan Hoffmann
  memset(buffer, 0, 6);
385 463eabf7 Wetmelon
386
  int16_t x, y, z;
387
  x = y = z = 0;
388
389
  /* Read vector data (6 bytes) */
390
  readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
391
392
  x = ((int16_t)buffer[0]) | (((int16_t)buffer[1]) << 8);
393
  y = ((int16_t)buffer[2]) | (((int16_t)buffer[3]) << 8);
394
  z = ((int16_t)buffer[4]) | (((int16_t)buffer[5]) << 8);
395
396 75f03d2e Szymon Kaliski
  /*!
397 5d8d461e Jan Hoffmann
   * Convert the value to an appropriate range (section 3.6.4)
398 75f03d2e Szymon Kaliski
   * and assign the value to the Vector type
399 5d8d461e Jan Hoffmann
   */
400 55e2f6d1 Jan Hoffmann
  switch (vector_type) {
401
  case VECTOR_MAGNETOMETER:
402
    /* 1uT = 16 LSB */
403
    xyz[0] = ((double)x) / 16.0;
404
    xyz[1] = ((double)y) / 16.0;
405
    xyz[2] = ((double)z) / 16.0;
406
    break;
407
  case VECTOR_GYROSCOPE:
408
    /* 1dps = 16 LSB */
409
    xyz[0] = ((double)x) / 16.0;
410
    xyz[1] = ((double)y) / 16.0;
411
    xyz[2] = ((double)z) / 16.0;
412
    break;
413
  case VECTOR_EULER:
414
    /* 1 degree = 16 LSB */
415
    xyz[0] = ((double)x) / 16.0;
416
    xyz[1] = ((double)y) / 16.0;
417
    xyz[2] = ((double)z) / 16.0;
418
    break;
419
  case VECTOR_ACCELEROMETER:
420 e223568a Jan Hoffmann
    /* 1m/s^2 = 100 LSB */
421
    xyz[0] = ((double)x) / 100.0;
422
    xyz[1] = ((double)y) / 100.0;
423
    xyz[2] = ((double)z) / 100.0;
424
    break;
425 55e2f6d1 Jan Hoffmann
  case VECTOR_LINEARACCEL:
426 e223568a Jan Hoffmann
    /* 1m/s^2 = 100 LSB */
427
    xyz[0] = ((double)x) / 100.0;
428
    xyz[1] = ((double)y) / 100.0;
429
    xyz[2] = ((double)z) / 100.0;
430
    break;
431 55e2f6d1 Jan Hoffmann
  case VECTOR_GRAVITY:
432
    /* 1m/s^2 = 100 LSB */
433
    xyz[0] = ((double)x) / 100.0;
434
    xyz[1] = ((double)y) / 100.0;
435
    xyz[2] = ((double)z) / 100.0;
436
    break;
437 463eabf7 Wetmelon
  }
438
439
  return xyz;
440 4bc1c0c1 Kevin Townsend
}
441
442
/*!
443 5d8d461e Jan Hoffmann
 *  @brief  Gets a quaternion reading from the specified source
444
 *  @return quaternion reading
445
 */
446 55e2f6d1 Jan Hoffmann
imu::Quaternion Adafruit_BNO055::getQuat() {
447 463eabf7 Wetmelon
  uint8_t buffer[8];
448 55e2f6d1 Jan Hoffmann
  memset(buffer, 0, 8);
449 463eabf7 Wetmelon
450
  int16_t x, y, z, w;
451
  x = y = z = w = 0;
452
453
  /* Read quat data (8 bytes) */
454
  readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
455
  w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
456
  x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
457
  y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
458
  z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]);
459
460 5d8d461e Jan Hoffmann
  /*!
461
   * Assign to Quaternion
462
   * See
463
   * http://ae-bst.resource.bosch.com/media/products/dokumente/bno055/BST_BNO055_DS000_12~1.pdf
464 75f03d2e Szymon Kaliski
   * 3.6.5.5 Orientation (Quaternion)
465 5d8d461e Jan Hoffmann
   */
466 55e2f6d1 Jan Hoffmann
  const double scale = (1.0 / (1 << 14));
467 463eabf7 Wetmelon
  imu::Quaternion quat(scale * w, scale * x, scale * y, scale * z);
468
  return quat;
469 48741e1f Kevin Townsend
}
470
471
/*!
472 5d8d461e Jan Hoffmann
 *  @brief  Provides the sensor_t data for this sensor
473
 *  @param  sensor
474 2b95af9e ulysse314
 *          Sensor description
475 75f03d2e Szymon Kaliski
 */
476 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::getSensor(sensor_t *sensor) {
477 463eabf7 Wetmelon
  /* Clear the sensor_t object */
478
  memset(sensor, 0, sizeof(sensor_t));
479
480
  /* Insert the sensor name in the fixed length char array */
481 55e2f6d1 Jan Hoffmann
  strncpy(sensor->name, "BNO055", sizeof(sensor->name) - 1);
482
  sensor->name[sizeof(sensor->name) - 1] = 0;
483
  sensor->version = 1;
484
  sensor->sensor_id = _sensorID;
485
  sensor->type = SENSOR_TYPE_ORIENTATION;
486
  sensor->min_delay = 0;
487
  sensor->max_value = 0.0F;
488
  sensor->min_value = 0.0F;
489
  sensor->resolution = 0.01F;
490 4bc1c0c1 Kevin Townsend
}
491
492
/*!
493 5d8d461e Jan Hoffmann
 *  @brief  Reads the sensor and returns the data as a sensors_event_t
494
 *  @param  event
495 2b95af9e ulysse314
 *          Event description
496 5d8d461e Jan Hoffmann
 *  @return always returns true
497
 */
498 55e2f6d1 Jan Hoffmann
bool Adafruit_BNO055::getEvent(sensors_event_t *event) {
499 463eabf7 Wetmelon
  /* Clear the event */
500
  memset(event, 0, sizeof(sensors_event_t));
501 4bc1c0c1 Kevin Townsend
502 55e2f6d1 Jan Hoffmann
  event->version = sizeof(sensors_event_t);
503 463eabf7 Wetmelon
  event->sensor_id = _sensorID;
504 55e2f6d1 Jan Hoffmann
  event->type = SENSOR_TYPE_ORIENTATION;
505 463eabf7 Wetmelon
  event->timestamp = millis();
506 fcd68623 Kevin Townsend
507 463eabf7 Wetmelon
  /* Get a Euler angle sample for orientation */
508
  imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
509
  event->orientation.x = euler.x();
510
  event->orientation.y = euler.y();
511
  event->orientation.z = euler.z();
512 312a5b9e Wetmelon
513 463eabf7 Wetmelon
  return true;
514 312a5b9e Wetmelon
}
515 fcd68623 Kevin Townsend
516 312a5b9e Wetmelon
/*!
517 f0ebdf46 Jan Hoffmann
 *  @brief  Reads the sensor and returns the data as a sensors_event_t
518
 *  @param  event
519 2b95af9e ulysse314
 *          Event description
520 f0ebdf46 Jan Hoffmann
 *  @param  vec_type
521
 *          specify the type of reading
522
 *  @return always returns true
523
 */
524
bool Adafruit_BNO055::getEvent(sensors_event_t *event, adafruit_vector_type_t vec_type)
525
{
526
  /* Clear the event */
527
  memset(event, 0, sizeof(sensors_event_t));
528
529
  event->version = sizeof(sensors_event_t);
530
  event->sensor_id = _sensorID;
531
  event->timestamp = millis();
532
533
  //read the data according to vec_type
534
  imu::Vector<3> vec;
535
  if (vec_type == Adafruit_BNO055::VECTOR_LINEARACCEL)
536
  {
537 7bc8249a Paul Otto
    event->type = SENSOR_TYPE_LINEAR_ACCELERATION;
538 f0ebdf46 Jan Hoffmann
    vec = getVector(Adafruit_BNO055::VECTOR_LINEARACCEL);
539
540
    event->acceleration.x = vec.x();
541
    event->acceleration.y = vec.y();
542
    event->acceleration.z = vec.z();
543
  }
544
  else if (vec_type == Adafruit_BNO055::VECTOR_ACCELEROMETER)
545
  {
546
    event->type = SENSOR_TYPE_ACCELEROMETER;
547
    vec = getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
548
549
    event->acceleration.x = vec.x();
550
    event->acceleration.y = vec.y();
551
    event->acceleration.z = vec.z();
552
  }
553
  else if (vec_type == Adafruit_BNO055::VECTOR_GRAVITY)
554
  {
555
    event->type = SENSOR_TYPE_ACCELEROMETER;
556
    vec = getVector(Adafruit_BNO055::VECTOR_GRAVITY);
557
558
    event->acceleration.x = vec.x();
559
    event->acceleration.y = vec.y();
560
    event->acceleration.z = vec.z();
561
  }
562
  else if (vec_type == Adafruit_BNO055::VECTOR_EULER)
563
  {
564
    event->type = SENSOR_TYPE_ORIENTATION;
565
    vec = getVector(Adafruit_BNO055::VECTOR_EULER);
566
567
    event->orientation.x = vec.x();
568
    event->orientation.y = vec.y();
569
    event->orientation.z = vec.z();
570
  }
571
  else if (vec_type == Adafruit_BNO055::VECTOR_GYROSCOPE)
572
  {
573
    event->type = SENSOR_TYPE_ROTATION_VECTOR;
574
    vec = getVector(Adafruit_BNO055::VECTOR_GYROSCOPE);
575
576
    event->gyro.x = vec.x();
577
    event->gyro.y = vec.y();
578
    event->gyro.z = vec.z();
579
  }
580
  else if (vec_type == Adafruit_BNO055::VECTOR_MAGNETOMETER)
581
  {
582
    event->type = SENSOR_TYPE_MAGNETIC_FIELD;
583
    vec = getVector(Adafruit_BNO055::VECTOR_MAGNETOMETER);
584
585
    event->magnetic.x = vec.x();
586
    event->magnetic.y = vec.y();
587
    event->magnetic.z = vec.z();
588
  }
589
  
590
591
  return true;
592
}
593
594
595
/*!
596 5d8d461e Jan Hoffmann
 *  @brief  Reads the sensor's offset registers into a byte array
597
 *  @param  calibData
598 2b95af9e ulysse314
 *          Calibration offset (buffer size should be 22)
599 5d8d461e Jan Hoffmann
 *  @return true if read is successful
600
 */
601 55e2f6d1 Jan Hoffmann
bool Adafruit_BNO055::getSensorOffsets(uint8_t *calibData) {
602
  if (isFullyCalibrated()) {
603
    adafruit_bno055_opmode_t lastMode = _mode;
604
    setMode(OPERATION_MODE_CONFIG);
605 312a5b9e Wetmelon
606 55e2f6d1 Jan Hoffmann
    readLen(ACCEL_OFFSET_X_LSB_ADDR, calibData, NUM_BNO055_OFFSET_REGISTERS);
607 312a5b9e Wetmelon
608 55e2f6d1 Jan Hoffmann
    setMode(lastMode);
609
    return true;
610
  }
611
  return false;
612 312a5b9e Wetmelon
}
613
614
/*!
615 5d8d461e Jan Hoffmann
 *  @brief  Reads the sensor's offset registers into an offset struct
616
 *  @param  offsets_type
617 dbc34b4d Jan Hoffmann
 *          type of offsets
618 5d8d461e Jan Hoffmann
 *  @return true if read is successful
619
 */
620 55e2f6d1 Jan Hoffmann
bool Adafruit_BNO055::getSensorOffsets(
621
    adafruit_bno055_offsets_t &offsets_type) {
622
  if (isFullyCalibrated()) {
623
    adafruit_bno055_opmode_t lastMode = _mode;
624
    setMode(OPERATION_MODE_CONFIG);
625
    delay(25);
626
627
    /* Accel offset range depends on the G-range:
628
       +/-2g  = +/- 2000 mg
629
       +/-4g  = +/- 4000 mg
630
       +/-8g  = +/- 8000 mg
631
       +/-1§g = +/- 16000 mg */
632
    offsets_type.accel_offset_x = (read8(ACCEL_OFFSET_X_MSB_ADDR) << 8) |
633
                                  (read8(ACCEL_OFFSET_X_LSB_ADDR));
634
    offsets_type.accel_offset_y = (read8(ACCEL_OFFSET_Y_MSB_ADDR) << 8) |
635
                                  (read8(ACCEL_OFFSET_Y_LSB_ADDR));
636
    offsets_type.accel_offset_z = (read8(ACCEL_OFFSET_Z_MSB_ADDR) << 8) |
637
                                  (read8(ACCEL_OFFSET_Z_LSB_ADDR));
638
639
    /* Magnetometer offset range = +/- 6400 LSB where 1uT = 16 LSB */
640
    offsets_type.mag_offset_x =
641
        (read8(MAG_OFFSET_X_MSB_ADDR) << 8) | (read8(MAG_OFFSET_X_LSB_ADDR));
642
    offsets_type.mag_offset_y =
643
        (read8(MAG_OFFSET_Y_MSB_ADDR) << 8) | (read8(MAG_OFFSET_Y_LSB_ADDR));
644
    offsets_type.mag_offset_z =
645
        (read8(MAG_OFFSET_Z_MSB_ADDR) << 8) | (read8(MAG_OFFSET_Z_LSB_ADDR));
646
647
    /* Gyro offset range depends on the DPS range:
648
      2000 dps = +/- 32000 LSB
649
      1000 dps = +/- 16000 LSB
650
       500 dps = +/- 8000 LSB
651
       250 dps = +/- 4000 LSB
652
       125 dps = +/- 2000 LSB
653
       ... where 1 DPS = 16 LSB */
654
    offsets_type.gyro_offset_x =
655
        (read8(GYRO_OFFSET_X_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_X_LSB_ADDR));
656
    offsets_type.gyro_offset_y =
657
        (read8(GYRO_OFFSET_Y_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_Y_LSB_ADDR));
658
    offsets_type.gyro_offset_z =
659
        (read8(GYRO_OFFSET_Z_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_Z_LSB_ADDR));
660
661
    /* Accelerometer radius = +/- 1000 LSB */
662
    offsets_type.accel_radius =
663
        (read8(ACCEL_RADIUS_MSB_ADDR) << 8) | (read8(ACCEL_RADIUS_LSB_ADDR));
664
665
    /* Magnetometer radius = +/- 960 LSB */
666
    offsets_type.mag_radius =
667
        (read8(MAG_RADIUS_MSB_ADDR) << 8) | (read8(MAG_RADIUS_LSB_ADDR));
668 312a5b9e Wetmelon
669 55e2f6d1 Jan Hoffmann
    setMode(lastMode);
670
    return true;
671
  }
672
  return false;
673
}
674 312a5b9e Wetmelon
675
/*!
676 75f03d2e Szymon Kaliski
 *  @brief  Writes an array of calibration values to the sensor's offset
677 2b95af9e ulysse314
 *  @param  calibData
678 f7556b0d Jan Hoffmann
 *          calibration data
679 5d8d461e Jan Hoffmann
 */
680 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setSensorOffsets(const uint8_t *calibData) {
681
  adafruit_bno055_opmode_t lastMode = _mode;
682
  setMode(OPERATION_MODE_CONFIG);
683
  delay(25);
684 463eabf7 Wetmelon
685 55e2f6d1 Jan Hoffmann
  /* Note: Configuration will take place only when user writes to the last
686
     byte of each config data pair (ex. ACCEL_OFFSET_Z_MSB_ADDR, etc.).
687
     Therefore the last byte must be written whenever the user wants to
688
     changes the configuration. */
689
690
  /* A writeLen() would make this much cleaner */
691
  write8(ACCEL_OFFSET_X_LSB_ADDR, calibData[0]);
692
  write8(ACCEL_OFFSET_X_MSB_ADDR, calibData[1]);
693
  write8(ACCEL_OFFSET_Y_LSB_ADDR, calibData[2]);
694
  write8(ACCEL_OFFSET_Y_MSB_ADDR, calibData[3]);
695
  write8(ACCEL_OFFSET_Z_LSB_ADDR, calibData[4]);
696
  write8(ACCEL_OFFSET_Z_MSB_ADDR, calibData[5]);
697
698
  write8(MAG_OFFSET_X_LSB_ADDR, calibData[6]);
699
  write8(MAG_OFFSET_X_MSB_ADDR, calibData[7]);
700
  write8(MAG_OFFSET_Y_LSB_ADDR, calibData[8]);
701
  write8(MAG_OFFSET_Y_MSB_ADDR, calibData[9]);
702
  write8(MAG_OFFSET_Z_LSB_ADDR, calibData[10]);
703
  write8(MAG_OFFSET_Z_MSB_ADDR, calibData[11]);
704
705
  write8(GYRO_OFFSET_X_LSB_ADDR, calibData[12]);
706
  write8(GYRO_OFFSET_X_MSB_ADDR, calibData[13]);
707
  write8(GYRO_OFFSET_Y_LSB_ADDR, calibData[14]);
708
  write8(GYRO_OFFSET_Y_MSB_ADDR, calibData[15]);
709
  write8(GYRO_OFFSET_Z_LSB_ADDR, calibData[16]);
710
  write8(GYRO_OFFSET_Z_MSB_ADDR, calibData[17]);
711
712
  write8(ACCEL_RADIUS_LSB_ADDR, calibData[18]);
713
  write8(ACCEL_RADIUS_MSB_ADDR, calibData[19]);
714
715
  write8(MAG_RADIUS_LSB_ADDR, calibData[20]);
716
  write8(MAG_RADIUS_MSB_ADDR, calibData[21]);
717
718
  setMode(lastMode);
719 4bc1c0c1 Kevin Townsend
}
720
721 312a5b9e Wetmelon
/*!
722 5d8d461e Jan Hoffmann
 *  @brief  Writes to the sensor's offset registers from an offset struct
723
 *  @param  offsets_type
724 f7556b0d Jan Hoffmann
 *          accel_offset_x = acceleration offset x
725
 *          accel_offset_y = acceleration offset y
726
 *          accel_offset_z = acceleration offset z
727
 *
728
 *          mag_offset_x   = magnetometer offset x
729
 *          mag_offset_y   = magnetometer offset y
730
 *          mag_offset_z   = magnetometer offset z
731
 *
732
 *          gyro_offset_x  = gyroscrope offset x
733
 *          gyro_offset_y  = gyroscrope offset y
734
 *          gyro_offset_z  = gyroscrope offset z
735 75f03d2e Szymon Kaliski
 */
736 55e2f6d1 Jan Hoffmann
void Adafruit_BNO055::setSensorOffsets(
737
    const adafruit_bno055_offsets_t &offsets_type) {
738
  adafruit_bno055_opmode_t lastMode = _mode;
739
  setMode(OPERATION_MODE_CONFIG);
740
  delay(25);
741 463eabf7 Wetmelon
742 55e2f6d1 Jan Hoffmann
  /* Note: Configuration will take place only when user writes to the last
743
     byte of each config data pair (ex. ACCEL_OFFSET_Z_MSB_ADDR, etc.).
744
     Therefore the last byte must be written whenever the user wants to
745
     changes the configuration. */
746
747
  write8(ACCEL_OFFSET_X_LSB_ADDR, (offsets_type.accel_offset_x) & 0x0FF);
748
  write8(ACCEL_OFFSET_X_MSB_ADDR, (offsets_type.accel_offset_x >> 8) & 0x0FF);
749
  write8(ACCEL_OFFSET_Y_LSB_ADDR, (offsets_type.accel_offset_y) & 0x0FF);
750
  write8(ACCEL_OFFSET_Y_MSB_ADDR, (offsets_type.accel_offset_y >> 8) & 0x0FF);
751
  write8(ACCEL_OFFSET_Z_LSB_ADDR, (offsets_type.accel_offset_z) & 0x0FF);
752
  write8(ACCEL_OFFSET_Z_MSB_ADDR, (offsets_type.accel_offset_z >> 8) & 0x0FF);
753
754
  write8(MAG_OFFSET_X_LSB_ADDR, (offsets_type.mag_offset_x) & 0x0FF);
755
  write8(MAG_OFFSET_X_MSB_ADDR, (offsets_type.mag_offset_x >> 8) & 0x0FF);
756
  write8(MAG_OFFSET_Y_LSB_ADDR, (offsets_type.mag_offset_y) & 0x0FF);
757
  write8(MAG_OFFSET_Y_MSB_ADDR, (offsets_type.mag_offset_y >> 8) & 0x0FF);
758
  write8(MAG_OFFSET_Z_LSB_ADDR, (offsets_type.mag_offset_z) & 0x0FF);
759
  write8(MAG_OFFSET_Z_MSB_ADDR, (offsets_type.mag_offset_z >> 8) & 0x0FF);
760
761
  write8(GYRO_OFFSET_X_LSB_ADDR, (offsets_type.gyro_offset_x) & 0x0FF);
762
  write8(GYRO_OFFSET_X_MSB_ADDR, (offsets_type.gyro_offset_x >> 8) & 0x0FF);
763
  write8(GYRO_OFFSET_Y_LSB_ADDR, (offsets_type.gyro_offset_y) & 0x0FF);
764
  write8(GYRO_OFFSET_Y_MSB_ADDR, (offsets_type.gyro_offset_y >> 8) & 0x0FF);
765
  write8(GYRO_OFFSET_Z_LSB_ADDR, (offsets_type.gyro_offset_z) & 0x0FF);
766
  write8(GYRO_OFFSET_Z_MSB_ADDR, (offsets_type.gyro_offset_z >> 8) & 0x0FF);
767
768
  write8(ACCEL_RADIUS_LSB_ADDR, (offsets_type.accel_radius) & 0x0FF);
769
  write8(ACCEL_RADIUS_MSB_ADDR, (offsets_type.accel_radius >> 8) & 0x0FF);
770
771
  write8(MAG_RADIUS_LSB_ADDR, (offsets_type.mag_radius) & 0x0FF);
772
  write8(MAG_RADIUS_MSB_ADDR, (offsets_type.mag_radius >> 8) & 0x0FF);
773
774
  setMode(lastMode);
775 312a5b9e Wetmelon
}
776
777 741a95a7 Kevin Townsend
/*!
778 5d8d461e Jan Hoffmann
 *  @brief  Checks of all cal status values are set to 3 (fully calibrated)
779
 *  @return status of calibration
780
 */
781 55e2f6d1 Jan Hoffmann
bool Adafruit_BNO055::isFullyCalibrated() {
782
  uint8_t system, gyro, accel, mag;
783
  getCalibration(&system, &gyro, &accel, &mag);
784 510d0f10 Jan Hoffmann
785
  switch (_mode) {
786
  case OPERATION_MODE_ACCONLY:
787
    return (accel == 3);
788
  case OPERATION_MODE_MAGONLY:
789
    return (mag == 3);
790
  case OPERATION_MODE_GYRONLY:
791
  case OPERATION_MODE_M4G: /* No magnetometer calibration required. */
792
    return (gyro == 3);
793
  case OPERATION_MODE_ACCMAG:
794
  case OPERATION_MODE_COMPASS:
795
    return (accel == 3 && mag == 3);
796
  case OPERATION_MODE_ACCGYRO:
797
  case OPERATION_MODE_IMUPLUS:
798
    return (accel == 3 && gyro == 3);
799
  case OPERATION_MODE_MAGGYRO:
800
    return (mag == 3 && gyro == 3);
801
  default:
802
    return (system == 3 && gyro == 3 && accel == 3 && mag == 3);
803
  }
804 312a5b9e Wetmelon
}
805
806 4bc1c0c1 Kevin Townsend
/*!
807 a7b2a1c9 Jan Hoffmann
 *  @brief  Enter Suspend mode (i.e., sleep)
808
 */
809
void Adafruit_BNO055::enterSuspendMode() {
810
  adafruit_bno055_opmode_t modeback = _mode;
811
812
  /* Switch to config mode (just in case since this is the default) */
813
  setMode(OPERATION_MODE_CONFIG);
814
  delay(25);
815
  write8(BNO055_PWR_MODE_ADDR, 0x02);
816
  /* Set the requested operating mode (see section 3.3) */
817
  setMode(modeback);
818
  delay(20);
819
}
820
821
/*!
822
 *  @brief  Enter Normal mode (i.e., wake)
823
 */
824
void Adafruit_BNO055::enterNormalMode() {
825
  adafruit_bno055_opmode_t modeback = _mode;
826
827
  /* Switch to config mode (just in case since this is the default) */
828
  setMode(OPERATION_MODE_CONFIG);
829
  delay(25);
830
  write8(BNO055_PWR_MODE_ADDR, 0x00);
831
  /* Set the requested operating mode (see section 3.3) */
832
  setMode(modeback);
833
  delay(20);
834
}
835
836
/*!
837 5d8d461e Jan Hoffmann
 *  @brief  Writes an 8 bit value over I2C
838
 */
839 55e2f6d1 Jan Hoffmann
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value) {
840 f7556b0d Jan Hoffmann
  _wire->beginTransmission(_address);
841 55e2f6d1 Jan Hoffmann
#if ARDUINO >= 100
842 f7556b0d Jan Hoffmann
  _wire->write((uint8_t)reg);
843
  _wire->write((uint8_t)value);
844 55e2f6d1 Jan Hoffmann
#else
845 f7556b0d Jan Hoffmann
  _wire->send(reg);
846
  _wire->send(value);
847 55e2f6d1 Jan Hoffmann
#endif
848 f7556b0d Jan Hoffmann
  _wire->endTransmission();
849 463eabf7 Wetmelon
850
  /* ToDo: Check for error! */
851
  return true;
852 4bc1c0c1 Kevin Townsend
}
853
854
/*!
855 5d8d461e Jan Hoffmann
 *  @brief  Reads an 8 bit value over I2C
856
 */
857 55e2f6d1 Jan Hoffmann
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg) {
858 463eabf7 Wetmelon
  byte value = 0;
859
860 f7556b0d Jan Hoffmann
  _wire->beginTransmission(_address);
861 55e2f6d1 Jan Hoffmann
#if ARDUINO >= 100
862 f7556b0d Jan Hoffmann
  _wire->write((uint8_t)reg);
863 55e2f6d1 Jan Hoffmann
#else
864 f7556b0d Jan Hoffmann
  _wire->send(reg);
865 55e2f6d1 Jan Hoffmann
#endif
866 f7556b0d Jan Hoffmann
  _wire->endTransmission();
867
  _wire->requestFrom(_address, (byte)1);
868 55e2f6d1 Jan Hoffmann
#if ARDUINO >= 100
869 f7556b0d Jan Hoffmann
  value = _wire->read();
870 55e2f6d1 Jan Hoffmann
#else
871 f7556b0d Jan Hoffmann
  value = _wire->receive();
872 55e2f6d1 Jan Hoffmann
#endif
873 463eabf7 Wetmelon
874
  return value;
875 4bc1c0c1 Kevin Townsend
}
876
877
/*!
878 5d8d461e Jan Hoffmann
 *  @brief  Reads the specified number of bytes over I2C
879
 */
880 55e2f6d1 Jan Hoffmann
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte *buffer,
881
                              uint8_t len) {
882 f7556b0d Jan Hoffmann
  _wire->beginTransmission(_address);
883 55e2f6d1 Jan Hoffmann
#if ARDUINO >= 100
884 f7556b0d Jan Hoffmann
  _wire->write((uint8_t)reg);
885 55e2f6d1 Jan Hoffmann
#else
886 f7556b0d Jan Hoffmann
  _wire->send(reg);
887 55e2f6d1 Jan Hoffmann
#endif
888 f7556b0d Jan Hoffmann
  _wire->endTransmission();
889
  _wire->requestFrom(_address, (byte)len);
890 463eabf7 Wetmelon
891 55e2f6d1 Jan Hoffmann
  for (uint8_t i = 0; i < len; i++) {
892
#if ARDUINO >= 100
893 f7556b0d Jan Hoffmann
    buffer[i] = _wire->read();
894 55e2f6d1 Jan Hoffmann
#else
895 f7556b0d Jan Hoffmann
    buffer[i] = _wire->receive();
896 55e2f6d1 Jan Hoffmann
#endif
897 463eabf7 Wetmelon
  }
898
899
  /* ToDo: Check for errors! */
900
  return true;
901
}