Statistics
| Branch: | Revision:

adafruit_bno055 / Adafruit_BNO055.cpp @ 9a8e8b26

History | View | Annotate | Download (22.265 KB)

1
/*!
2
 * @file Adafruit_BNO055.cpp
3
 *
4
 *  @mainpage Adafruit BNO055 Orientation Sensor
5
 *
6
 *  @section intro_sec Introduction
7
 *
8
 *  This is a library for the BNO055 orientation sensor
9
 *
10
 *  Designed specifically to work with the Adafruit BNO055 Breakout.
11
 *
12
 *  Pick one up today in the adafruit shop!
13
 *  ------> https://www.adafruit.com/product/2472
14
 *
15
 *  These sensors use I2C to communicate, 2 pins are required to interface.
16
 *
17
 *  Adafruit invests time and resources providing this open source code,
18
 *  please support Adafruit andopen-source hardware by purchasing products
19
 *  from Adafruit!
20
 *
21
 *  @section author Author
22
 *
23
 *  K.Townsend (Adafruit Industries)
24
 *
25
 *  @section license License
26
 *
27
 *  MIT license, all text above must be included in any redistribution
28
 */
29

    
30
#include "Arduino.h"
31

    
32
#include <limits.h>
33
#include <math.h>
34

    
35
#include "Adafruit_BNO055.h"
36

    
37
/*!
38
 *  @brief  Instantiates a new Adafruit_BNO055 class
39
 *  @param  sensorID
40
 *          sensor ID
41
 *  @param  address
42
 *          i2c address
43
 */
44
Adafruit_BNO055::Adafruit_BNO055(int32_t sensorID, uint8_t address,
45
                                 TwoWire *theWire) {
46
  _sensorID = sensorID;
47
  _address = address;
48
}
49

    
50
/*!
51
 *  @brief  Sets up the HW
52
 *  @param  mode
53
 *          mode values
54
 *           [OPERATION_MODE_CONFIG,
55
 *            OPERATION_MODE_ACCONLY,
56
 *            OPERATION_MODE_MAGONLY,
57
 *            OPERATION_MODE_GYRONLY,
58
 *            OPERATION_MODE_ACCMAG,
59
 *            OPERATION_MODE_ACCGYRO,
60
 *            OPERATION_MODE_MAGGYRO,
61
 *            OPERATION_MODE_AMG,
62
 *            OPERATION_MODE_IMUPLUS,
63
 *            OPERATION_MODE_COMPASS,
64
 *            OPERATION_MODE_M4G,
65
 *            OPERATION_MODE_NDOF_FMC_OFF,
66
 *            OPERATION_MODE_NDOF]
67
 *  @return true if process is successful
68
 */
69
bool Adafruit_BNO055::begin(adafruit_bno055_opmode_t mode) {
70
#if defined(ARDUINO_SAMD_ZERO) && (_address == BNO055_ADDRESS_A)
71
#error                                                                         \
72
    "On an arduino Zero, BNO055's ADR pin must be high. Fix that, then delete this line."
73
  _address = BNO055_ADDRESS_B;
74
#endif
75

    
76
  /* Enable I2C */
77
  _wire->begin();
78

    
79
  // BNO055 clock stretches for 500us or more!
80
#ifdef ESP8266
81
  _wire->setClockStretchLimit(1000); // Allow for 1000us of clock stretching
82
#endif
83

    
84
  /* Make sure we have the right device */
85
  uint8_t id = read8(BNO055_CHIP_ID_ADDR);
86
  if (id != BNO055_ID) {
87
    delay(1000); // hold on for boot
88
    id = read8(BNO055_CHIP_ID_ADDR);
89
    if (id != BNO055_ID) {
90
      return false; // still not? ok bail
91
    }
92
  }
93

    
94
  /* Switch to config mode (just in case since this is the default) */
95
  setMode(OPERATION_MODE_CONFIG);
96

    
97
  /* Reset */
98
  write8(BNO055_SYS_TRIGGER_ADDR, 0x20);
99
  while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID) {
100
    delay(10);
101
  }
102
  delay(50);
103

    
104
  /* Set to normal power mode */
105
  write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL);
106
  delay(10);
107

    
108
  write8(BNO055_PAGE_ID_ADDR, 0);
109

    
110
  /* Set the output units */
111
  /*
112
  uint8_t unitsel = (0 << 7) | // Orientation = Android
113
                    (0 << 4) | // Temperature = Celsius
114
                    (0 << 2) | // Euler = Degrees
115
                    (1 << 1) | // Gyro = Rads
116
                    (0 << 0);  // Accelerometer = m/s^2
117
  write8(BNO055_UNIT_SEL_ADDR, unitsel);
118
  */
119

    
120
  /* Configure axis mapping (see section 3.4) */
121
  /*
122
  write8(BNO055_AXIS_MAP_CONFIG_ADDR, REMAP_CONFIG_P2); // P0-P7, Default is P1
123
  delay(10);
124
  write8(BNO055_AXIS_MAP_SIGN_ADDR, REMAP_SIGN_P2); // P0-P7, Default is P1
125
  delay(10);
126
  */
127

    
128
  write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
129
  delay(10);
130
  /* Set the requested operating mode (see section 3.3) */
131
  setMode(mode);
132
  delay(20);
133

    
134
  return true;
135
}
136

    
137
/*!
138
 *  @brief  Puts the chip in the specified operating mode
139
 *  @param  mode
140
 *          mode values
141
 *           [OPERATION_MODE_CONFIG,
142
 *            OPERATION_MODE_ACCONLY,
143
 *            OPERATION_MODE_MAGONLY,
144
 *            OPERATION_MODE_GYRONLY,
145
 *            OPERATION_MODE_ACCMAG,
146
 *            OPERATION_MODE_ACCGYRO,
147
 *            OPERATION_MODE_MAGGYRO,
148
 *            OPERATION_MODE_AMG,
149
 *            OPERATION_MODE_IMUPLUS,
150
 *            OPERATION_MODE_COMPASS,
151
 *            OPERATION_MODE_M4G,
152
 *            OPERATION_MODE_NDOF_FMC_OFF,
153
 *            OPERATION_MODE_NDOF]
154
 */
155
void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode) {
156
  _mode = mode;
157
  write8(BNO055_OPR_MODE_ADDR, _mode);
158
  delay(30);
159
}
160

    
161
/*!
162
 *  @brief  Changes the chip's axis remap
163
 *  @param  remapcode
164
 *          remap code possible values
165
 *          [REMAP_CONFIG_P0
166
 *           REMAP_CONFIG_P1 (default)
167
 *           REMAP_CONFIG_P2
168
 *           REMAP_CONFIG_P3
169
 *           REMAP_CONFIG_P4
170
 *           REMAP_CONFIG_P5
171
 *           REMAP_CONFIG_P6
172
 *           REMAP_CONFIG_P7]
173
 */
174
void Adafruit_BNO055::setAxisRemap(
175
    adafruit_bno055_axis_remap_config_t remapcode) {
176
  adafruit_bno055_opmode_t modeback = _mode;
177

    
178
  setMode(OPERATION_MODE_CONFIG);
179
  delay(25);
180
  write8(BNO055_AXIS_MAP_CONFIG_ADDR, remapcode);
181
  delay(10);
182
  /* Set the requested operating mode (see section 3.3) */
183
  setMode(modeback);
184
  delay(20);
185
}
186

    
187
/*!
188
 *  @brief  Changes the chip's axis signs
189
 *  @param  remapsign
190
 *          remap sign possible values
191
 *          [REMAP_SIGN_P0
192
 *           REMAP_SIGN_P1 (default)
193
 *           REMAP_SIGN_P2
194
 *           REMAP_SIGN_P3
195
 *           REMAP_SIGN_P4
196
 *           REMAP_SIGN_P5
197
 *           REMAP_SIGN_P6
198
 *           REMAP_SIGN_P7]
199
 */
200
void Adafruit_BNO055::setAxisSign(adafruit_bno055_axis_remap_sign_t remapsign) {
201
  adafruit_bno055_opmode_t modeback = _mode;
202

    
203
  setMode(OPERATION_MODE_CONFIG);
204
  delay(25);
205
  write8(BNO055_AXIS_MAP_SIGN_ADDR, remapsign);
206
  delay(10);
207
  /* Set the requested operating mode (see section 3.3) */
208
  setMode(modeback);
209
  delay(20);
210
}
211

    
212
/*!
213
 *  @brief  Use the external 32.768KHz crystal
214
 *  @param  usextal
215
 *          use external crystal boolean
216
 */
217
void Adafruit_BNO055::setExtCrystalUse(boolean usextal) {
218
  adafruit_bno055_opmode_t modeback = _mode;
219

    
220
  /* Switch to config mode (just in case since this is the default) */
221
  setMode(OPERATION_MODE_CONFIG);
222
  delay(25);
223
  write8(BNO055_PAGE_ID_ADDR, 0);
224
  if (usextal) {
225
    write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
226
  } else {
227
    write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
228
  }
229
  delay(10);
230
  /* Set the requested operating mode (see section 3.3) */
231
  setMode(modeback);
232
  delay(20);
233
}
234

    
235
/*!
236
 *   @brief  Gets the latest system status info
237
 *   @param  system_status
238
 *           system status info
239
 *   @param  self_test_result
240
 *           self test result
241
 *   @param  system_error
242
 *           system error info
243
 */
244
void Adafruit_BNO055::getSystemStatus(uint8_t *system_status,
245
                                      uint8_t *self_test_result,
246
                                      uint8_t *system_error) {
247
  write8(BNO055_PAGE_ID_ADDR, 0);
248

    
249
  /* System Status (see section 4.3.58)
250
     0 = Idle
251
     1 = System Error
252
     2 = Initializing Peripherals
253
     3 = System Iniitalization
254
     4 = Executing Self-Test
255
     5 = Sensor fusio algorithm running
256
     6 = System running without fusion algorithms
257
   */
258

    
259
  if (system_status != 0)
260
    *system_status = read8(BNO055_SYS_STAT_ADDR);
261

    
262
  /* Self Test Results
263
     1 = test passed, 0 = test failed
264

265
     Bit 0 = Accelerometer self test
266
     Bit 1 = Magnetometer self test
267
     Bit 2 = Gyroscope self test
268
     Bit 3 = MCU self test
269

270
     0x0F = all good!
271
   */
272

    
273
  if (self_test_result != 0)
274
    *self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR);
275

    
276
  /* System Error (see section 4.3.59)
277
     0 = No error
278
     1 = Peripheral initialization error
279
     2 = System initialization error
280
     3 = Self test result failed
281
     4 = Register map value out of range
282
     5 = Register map address out of range
283
     6 = Register map write error
284
     7 = BNO low power mode not available for selected operat ion mode
285
     8 = Accelerometer power mode not available
286
     9 = Fusion algorithm configuration error
287
     A = Sensor configuration error
288
   */
289

    
290
  if (system_error != 0)
291
    *system_error = read8(BNO055_SYS_ERR_ADDR);
292

    
293
  delay(200);
294
}
295

    
296
/*!
297
 *  @brief  Gets the chip revision numbers
298
 *  @param  info
299
 *          revision info
300
 */
301
void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t *info) {
302
  uint8_t a, b;
303

    
304
  memset(info, 0, sizeof(adafruit_bno055_rev_info_t));
305

    
306
  /* Check the accelerometer revision */
307
  info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR);
308

    
309
  /* Check the magnetometer revision */
310
  info->mag_rev = read8(BNO055_MAG_REV_ID_ADDR);
311

    
312
  /* Check the gyroscope revision */
313
  info->gyro_rev = read8(BNO055_GYRO_REV_ID_ADDR);
314

    
315
  /* Check the SW revision */
316
  info->bl_rev = read8(BNO055_BL_REV_ID_ADDR);
317

    
318
  a = read8(BNO055_SW_REV_ID_LSB_ADDR);
319
  b = read8(BNO055_SW_REV_ID_MSB_ADDR);
320
  info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
321
}
322

    
323
/*!
324
 *  @brief  Gets current calibration state.  Each value should be a uint8_t
325
 *          pointer and it will be set to 0 if not calibrated and 3 if
326
 *          fully calibrated.
327
 *          See section 34.3.54
328
 *  @param  sys
329
 *          Current system calibration status, depends on status of all sensors,
330
 * read-only
331
 *  @param  gyro
332
 *          Current calibration status of Gyroscope, read-only
333
 *  @param  accel
334
 *          Current calibration status of Accelerometer, read-only
335
 *  @param  mag
336
 *          Current calibration status of Magnetometer, read-only
337
 */
338
void Adafruit_BNO055::getCalibration(uint8_t *sys, uint8_t *gyro,
339
                                     uint8_t *accel, uint8_t *mag) {
340
  uint8_t calData = read8(BNO055_CALIB_STAT_ADDR);
341
  if (sys != NULL) {
342
    *sys = (calData >> 6) & 0x03;
343
  }
344
  if (gyro != NULL) {
345
    *gyro = (calData >> 4) & 0x03;
346
  }
347
  if (accel != NULL) {
348
    *accel = (calData >> 2) & 0x03;
349
  }
350
  if (mag != NULL) {
351
    *mag = calData & 0x03;
352
  }
353
}
354

    
355
/*!
356
 *  @brief  Gets the temperature in degrees celsius
357
 *  @return temperature in degrees celsius
358
 */
359
int8_t Adafruit_BNO055::getTemp() {
360
  int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR));
361
  return temp;
362
}
363

    
364
/*!
365
 *  @brief   Gets a vector reading from the specified source
366
 *  @param   vector_type
367
 *           possible vector type values
368
 *           [VECTOR_ACCELEROMETER
369
 *            VECTOR_MAGNETOMETER
370
 *            VECTOR_GYROSCOPE
371
 *            VECTOR_EULER
372
 *            VECTOR_LINEARACCEL
373
 *            VECTOR_GRAVITY]
374
 *  @return  vector from specified source
375
 */
376
imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type) {
377
  imu::Vector<3> xyz;
378
  uint8_t buffer[6];
379
  memset(buffer, 0, 6);
380

    
381
  int16_t x, y, z;
382
  x = y = z = 0;
383

    
384
  /* Read vector data (6 bytes) */
385
  readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
386

    
387
  x = ((int16_t)buffer[0]) | (((int16_t)buffer[1]) << 8);
388
  y = ((int16_t)buffer[2]) | (((int16_t)buffer[3]) << 8);
389
  z = ((int16_t)buffer[4]) | (((int16_t)buffer[5]) << 8);
390

    
391
  /*!
392
   * Convert the value to an appropriate range (section 3.6.4)
393
   * and assign the value to the Vector type
394
   */
395
  switch (vector_type) {
396
  case VECTOR_MAGNETOMETER:
397
    /* 1uT = 16 LSB */
398
    xyz[0] = ((double)x) / 16.0;
399
    xyz[1] = ((double)y) / 16.0;
400
    xyz[2] = ((double)z) / 16.0;
401
    break;
402
  case VECTOR_GYROSCOPE:
403
    /* 1dps = 16 LSB */
404
    xyz[0] = ((double)x) / 16.0;
405
    xyz[1] = ((double)y) / 16.0;
406
    xyz[2] = ((double)z) / 16.0;
407
    break;
408
  case VECTOR_EULER:
409
    /* 1 degree = 16 LSB */
410
    xyz[0] = ((double)x) / 16.0;
411
    xyz[1] = ((double)y) / 16.0;
412
    xyz[2] = ((double)z) / 16.0;
413
    break;
414
  case VECTOR_ACCELEROMETER:
415
  case VECTOR_LINEARACCEL:
416
  case VECTOR_GRAVITY:
417
    /* 1m/s^2 = 100 LSB */
418
    xyz[0] = ((double)x) / 100.0;
419
    xyz[1] = ((double)y) / 100.0;
420
    xyz[2] = ((double)z) / 100.0;
421
    break;
422
  }
423

    
424
  return xyz;
425
}
426

    
427
/*!
428
 *  @brief  Gets a quaternion reading from the specified source
429
 *  @return quaternion reading
430
 */
431
imu::Quaternion Adafruit_BNO055::getQuat() {
432
  uint8_t buffer[8];
433
  memset(buffer, 0, 8);
434

    
435
  int16_t x, y, z, w;
436
  x = y = z = w = 0;
437

    
438
  /* Read quat data (8 bytes) */
439
  readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
440
  w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]);
441
  x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]);
442
  y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]);
443
  z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]);
444

    
445
  /*!
446
   * Assign to Quaternion
447
   * See
448
   * http://ae-bst.resource.bosch.com/media/products/dokumente/bno055/BST_BNO055_DS000_12~1.pdf
449
   * 3.6.5.5 Orientation (Quaternion)
450
   */
451
  const double scale = (1.0 / (1 << 14));
452
  imu::Quaternion quat(scale * w, scale * x, scale * y, scale * z);
453
  return quat;
454
}
455

    
456
/*!
457
 *  @brief  Provides the sensor_t data for this sensor
458
 *  @param  sensor
459
 */
460
void Adafruit_BNO055::getSensor(sensor_t *sensor) {
461
  /* Clear the sensor_t object */
462
  memset(sensor, 0, sizeof(sensor_t));
463

    
464
  /* Insert the sensor name in the fixed length char array */
465
  strncpy(sensor->name, "BNO055", sizeof(sensor->name) - 1);
466
  sensor->name[sizeof(sensor->name) - 1] = 0;
467
  sensor->version = 1;
468
  sensor->sensor_id = _sensorID;
469
  sensor->type = SENSOR_TYPE_ORIENTATION;
470
  sensor->min_delay = 0;
471
  sensor->max_value = 0.0F;
472
  sensor->min_value = 0.0F;
473
  sensor->resolution = 0.01F;
474
}
475

    
476
/*!
477
 *  @brief  Reads the sensor and returns the data as a sensors_event_t
478
 *  @param  event
479
 *  @return always returns true
480
 */
481
bool Adafruit_BNO055::getEvent(sensors_event_t *event) {
482
  /* Clear the event */
483
  memset(event, 0, sizeof(sensors_event_t));
484

    
485
  event->version = sizeof(sensors_event_t);
486
  event->sensor_id = _sensorID;
487
  event->type = SENSOR_TYPE_ORIENTATION;
488
  event->timestamp = millis();
489

    
490
  /* Get a Euler angle sample for orientation */
491
  imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
492
  event->orientation.x = euler.x();
493
  event->orientation.y = euler.y();
494
  event->orientation.z = euler.z();
495

    
496
  return true;
497
}
498

    
499
/*!
500
 *  @brief  Reads the sensor's offset registers into a byte array
501
 *  @param  calibData
502
 *  @return true if read is successful
503
 */
504
bool Adafruit_BNO055::getSensorOffsets(uint8_t *calibData) {
505
  if (isFullyCalibrated()) {
506
    adafruit_bno055_opmode_t lastMode = _mode;
507
    setMode(OPERATION_MODE_CONFIG);
508

    
509
    readLen(ACCEL_OFFSET_X_LSB_ADDR, calibData, NUM_BNO055_OFFSET_REGISTERS);
510

    
511
    setMode(lastMode);
512
    return true;
513
  }
514
  return false;
515
}
516

    
517
/*!
518
 *  @brief  Reads the sensor's offset registers into an offset struct
519
 *  @param  offsets_type
520
 *  @return true if read is successful
521
 */
522
bool Adafruit_BNO055::getSensorOffsets(
523
    adafruit_bno055_offsets_t &offsets_type) {
524
  if (isFullyCalibrated()) {
525
    adafruit_bno055_opmode_t lastMode = _mode;
526
    setMode(OPERATION_MODE_CONFIG);
527
    delay(25);
528

    
529
    /* Accel offset range depends on the G-range:
530
       +/-2g  = +/- 2000 mg
531
       +/-4g  = +/- 4000 mg
532
       +/-8g  = +/- 8000 mg
533
       +/-1§g = +/- 16000 mg */
534
    offsets_type.accel_offset_x = (read8(ACCEL_OFFSET_X_MSB_ADDR) << 8) |
535
                                  (read8(ACCEL_OFFSET_X_LSB_ADDR));
536
    offsets_type.accel_offset_y = (read8(ACCEL_OFFSET_Y_MSB_ADDR) << 8) |
537
                                  (read8(ACCEL_OFFSET_Y_LSB_ADDR));
538
    offsets_type.accel_offset_z = (read8(ACCEL_OFFSET_Z_MSB_ADDR) << 8) |
539
                                  (read8(ACCEL_OFFSET_Z_LSB_ADDR));
540

    
541
    /* Magnetometer offset range = +/- 6400 LSB where 1uT = 16 LSB */
542
    offsets_type.mag_offset_x =
543
        (read8(MAG_OFFSET_X_MSB_ADDR) << 8) | (read8(MAG_OFFSET_X_LSB_ADDR));
544
    offsets_type.mag_offset_y =
545
        (read8(MAG_OFFSET_Y_MSB_ADDR) << 8) | (read8(MAG_OFFSET_Y_LSB_ADDR));
546
    offsets_type.mag_offset_z =
547
        (read8(MAG_OFFSET_Z_MSB_ADDR) << 8) | (read8(MAG_OFFSET_Z_LSB_ADDR));
548

    
549
    /* Gyro offset range depends on the DPS range:
550
      2000 dps = +/- 32000 LSB
551
      1000 dps = +/- 16000 LSB
552
       500 dps = +/- 8000 LSB
553
       250 dps = +/- 4000 LSB
554
       125 dps = +/- 2000 LSB
555
       ... where 1 DPS = 16 LSB */
556
    offsets_type.gyro_offset_x =
557
        (read8(GYRO_OFFSET_X_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_X_LSB_ADDR));
558
    offsets_type.gyro_offset_y =
559
        (read8(GYRO_OFFSET_Y_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_Y_LSB_ADDR));
560
    offsets_type.gyro_offset_z =
561
        (read8(GYRO_OFFSET_Z_MSB_ADDR) << 8) | (read8(GYRO_OFFSET_Z_LSB_ADDR));
562

    
563
    /* Accelerometer radius = +/- 1000 LSB */
564
    offsets_type.accel_radius =
565
        (read8(ACCEL_RADIUS_MSB_ADDR) << 8) | (read8(ACCEL_RADIUS_LSB_ADDR));
566

    
567
    /* Magnetometer radius = +/- 960 LSB */
568
    offsets_type.mag_radius =
569
        (read8(MAG_RADIUS_MSB_ADDR) << 8) | (read8(MAG_RADIUS_LSB_ADDR));
570

    
571
    setMode(lastMode);
572
    return true;
573
  }
574
  return false;
575
}
576

    
577
/*!
578
 *  @brief  Writes an array of calibration values to the sensor's offset
579
<<<<<<< HEAD
580
 * registers
581
=======
582
 *          registers
583
>>>>>>> 75f03d2e1fd440e7febc0de8c47a52bab09c51b0
584
 *  @param  calibData
585
 *          calibration data
586
 */
587
void Adafruit_BNO055::setSensorOffsets(const uint8_t *calibData) {
588
  adafruit_bno055_opmode_t lastMode = _mode;
589
  setMode(OPERATION_MODE_CONFIG);
590
  delay(25);
591

    
592
  /* Note: Configuration will take place only when user writes to the last
593
     byte of each config data pair (ex. ACCEL_OFFSET_Z_MSB_ADDR, etc.).
594
     Therefore the last byte must be written whenever the user wants to
595
     changes the configuration. */
596

    
597
  /* A writeLen() would make this much cleaner */
598
  write8(ACCEL_OFFSET_X_LSB_ADDR, calibData[0]);
599
  write8(ACCEL_OFFSET_X_MSB_ADDR, calibData[1]);
600
  write8(ACCEL_OFFSET_Y_LSB_ADDR, calibData[2]);
601
  write8(ACCEL_OFFSET_Y_MSB_ADDR, calibData[3]);
602
  write8(ACCEL_OFFSET_Z_LSB_ADDR, calibData[4]);
603
  write8(ACCEL_OFFSET_Z_MSB_ADDR, calibData[5]);
604

    
605
  write8(MAG_OFFSET_X_LSB_ADDR, calibData[6]);
606
  write8(MAG_OFFSET_X_MSB_ADDR, calibData[7]);
607
  write8(MAG_OFFSET_Y_LSB_ADDR, calibData[8]);
608
  write8(MAG_OFFSET_Y_MSB_ADDR, calibData[9]);
609
  write8(MAG_OFFSET_Z_LSB_ADDR, calibData[10]);
610
  write8(MAG_OFFSET_Z_MSB_ADDR, calibData[11]);
611

    
612
  write8(GYRO_OFFSET_X_LSB_ADDR, calibData[12]);
613
  write8(GYRO_OFFSET_X_MSB_ADDR, calibData[13]);
614
  write8(GYRO_OFFSET_Y_LSB_ADDR, calibData[14]);
615
  write8(GYRO_OFFSET_Y_MSB_ADDR, calibData[15]);
616
  write8(GYRO_OFFSET_Z_LSB_ADDR, calibData[16]);
617
  write8(GYRO_OFFSET_Z_MSB_ADDR, calibData[17]);
618

    
619
  write8(ACCEL_RADIUS_LSB_ADDR, calibData[18]);
620
  write8(ACCEL_RADIUS_MSB_ADDR, calibData[19]);
621

    
622
  write8(MAG_RADIUS_LSB_ADDR, calibData[20]);
623
  write8(MAG_RADIUS_MSB_ADDR, calibData[21]);
624

    
625
  setMode(lastMode);
626
}
627

    
628
/*!
629
 *  @brief  Writes to the sensor's offset registers from an offset struct
630
 *  @param  offsets_type
631
<<<<<<< HEAD
632
 *          accel_offset_x = acceleration offset x
633
 *          accel_offset_y = acceleration offset y
634
 *          accel_offset_z = acceleration offset z
635
 *
636
 *          mag_offset_x   = magnetometer offset x
637
 *          mag_offset_y   = magnetometer offset y
638
 *          mag_offset_z   = magnetometer offset z
639
 *
640
 *          gyro_offset_x  = gyroscrope offset x
641
 *          gyro_offset_y  = gyroscrope offset y
642
 *          gyro_offset_z  = gyroscrope offset z
643
 *
644
 *
645
=======
646
>>>>>>> 75f03d2e1fd440e7febc0de8c47a52bab09c51b0
647
 */
648
void Adafruit_BNO055::setSensorOffsets(
649
    const adafruit_bno055_offsets_t &offsets_type) {
650
  adafruit_bno055_opmode_t lastMode = _mode;
651
  setMode(OPERATION_MODE_CONFIG);
652
  delay(25);
653

    
654
  /* Note: Configuration will take place only when user writes to the last
655
     byte of each config data pair (ex. ACCEL_OFFSET_Z_MSB_ADDR, etc.).
656
     Therefore the last byte must be written whenever the user wants to
657
     changes the configuration. */
658

    
659
  write8(ACCEL_OFFSET_X_LSB_ADDR, (offsets_type.accel_offset_x) & 0x0FF);
660
  write8(ACCEL_OFFSET_X_MSB_ADDR, (offsets_type.accel_offset_x >> 8) & 0x0FF);
661
  write8(ACCEL_OFFSET_Y_LSB_ADDR, (offsets_type.accel_offset_y) & 0x0FF);
662
  write8(ACCEL_OFFSET_Y_MSB_ADDR, (offsets_type.accel_offset_y >> 8) & 0x0FF);
663
  write8(ACCEL_OFFSET_Z_LSB_ADDR, (offsets_type.accel_offset_z) & 0x0FF);
664
  write8(ACCEL_OFFSET_Z_MSB_ADDR, (offsets_type.accel_offset_z >> 8) & 0x0FF);
665

    
666
  write8(MAG_OFFSET_X_LSB_ADDR, (offsets_type.mag_offset_x) & 0x0FF);
667
  write8(MAG_OFFSET_X_MSB_ADDR, (offsets_type.mag_offset_x >> 8) & 0x0FF);
668
  write8(MAG_OFFSET_Y_LSB_ADDR, (offsets_type.mag_offset_y) & 0x0FF);
669
  write8(MAG_OFFSET_Y_MSB_ADDR, (offsets_type.mag_offset_y >> 8) & 0x0FF);
670
  write8(MAG_OFFSET_Z_LSB_ADDR, (offsets_type.mag_offset_z) & 0x0FF);
671
  write8(MAG_OFFSET_Z_MSB_ADDR, (offsets_type.mag_offset_z >> 8) & 0x0FF);
672

    
673
  write8(GYRO_OFFSET_X_LSB_ADDR, (offsets_type.gyro_offset_x) & 0x0FF);
674
  write8(GYRO_OFFSET_X_MSB_ADDR, (offsets_type.gyro_offset_x >> 8) & 0x0FF);
675
  write8(GYRO_OFFSET_Y_LSB_ADDR, (offsets_type.gyro_offset_y) & 0x0FF);
676
  write8(GYRO_OFFSET_Y_MSB_ADDR, (offsets_type.gyro_offset_y >> 8) & 0x0FF);
677
  write8(GYRO_OFFSET_Z_LSB_ADDR, (offsets_type.gyro_offset_z) & 0x0FF);
678
  write8(GYRO_OFFSET_Z_MSB_ADDR, (offsets_type.gyro_offset_z >> 8) & 0x0FF);
679

    
680
  write8(ACCEL_RADIUS_LSB_ADDR, (offsets_type.accel_radius) & 0x0FF);
681
  write8(ACCEL_RADIUS_MSB_ADDR, (offsets_type.accel_radius >> 8) & 0x0FF);
682

    
683
  write8(MAG_RADIUS_LSB_ADDR, (offsets_type.mag_radius) & 0x0FF);
684
  write8(MAG_RADIUS_MSB_ADDR, (offsets_type.mag_radius >> 8) & 0x0FF);
685

    
686
  setMode(lastMode);
687
}
688

    
689
/*!
690
 *  @brief  Checks of all cal status values are set to 3 (fully calibrated)
691
 *  @return status of calibration
692
 */
693
bool Adafruit_BNO055::isFullyCalibrated() {
694
  uint8_t system, gyro, accel, mag;
695
  getCalibration(&system, &gyro, &accel, &mag);
696
  if (system < 3 || gyro < 3 || accel < 3 || mag < 3)
697
    return false;
698
  return true;
699
}
700

    
701
/*!
702
 *  @brief  Writes an 8 bit value over I2C
703
 */
704
bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value) {
705
  _wire->beginTransmission(_address);
706
#if ARDUINO >= 100
707
  _wire->write((uint8_t)reg);
708
  _wire->write((uint8_t)value);
709
#else
710
  _wire->send(reg);
711
  _wire->send(value);
712
#endif
713
  _wire->endTransmission();
714

    
715
  /* ToDo: Check for error! */
716
  return true;
717
}
718

    
719
/*!
720
 *  @brief  Reads an 8 bit value over I2C
721
 */
722
byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg) {
723
  byte value = 0;
724

    
725
  _wire->beginTransmission(_address);
726
#if ARDUINO >= 100
727
  _wire->write((uint8_t)reg);
728
#else
729
  _wire->send(reg);
730
#endif
731
  _wire->endTransmission();
732
  _wire->requestFrom(_address, (byte)1);
733
#if ARDUINO >= 100
734
  value = _wire->read();
735
#else
736
  value = _wire->receive();
737
#endif
738

    
739
  return value;
740
}
741

    
742
/*!
743
 *  @brief  Reads the specified number of bytes over I2C
744
 */
745
bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte *buffer,
746
                              uint8_t len) {
747
  _wire->beginTransmission(_address);
748
#if ARDUINO >= 100
749
  _wire->write((uint8_t)reg);
750
#else
751
  _wire->send(reg);
752
#endif
753
  _wire->endTransmission();
754
  _wire->requestFrom(_address, (byte)len);
755

    
756
  for (uint8_t i = 0; i < len; i++) {
757
#if ARDUINO >= 100
758
    buffer[i] = _wire->read();
759
#else
760
    buffer[i] = _wire->receive();
761
#endif
762
  }
763

    
764
  /* ToDo: Check for errors! */
765
  return true;
766
}