adafruit_bno055 / Adafruit_BNO055.cpp @ c4f272e1
History | View | Annotate | Download (14.298 KB)
1 | 4bc1c0c1 | Kevin Townsend | /***************************************************************************
|
---|---|---|---|
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 | c4f272e1 | ladyada | delay(1000); // hold on for boot |
65 | if(id != BNO055_ID) {
|
||
66 | return false; // still not? ok bail |
||
67 | } |
||
68 | 4bc1c0c1 | Kevin Townsend | } |
69 | |||
70 | /* Switch to config mode (just in case since this is the default) */
|
||
71 | setMode(OPERATION_MODE_CONFIG); |
||
72 | c4f272e1 | ladyada | |
73 | /* Reset */
|
||
74 | write8(BNO055_SYS_TRIGGER_ADDR, 0x20); //reset the sensor |
||
75 | while (read8(BNO055_CHIP_ID_ADDR) != BNO055_ID) //wait for boot |
||
76 | delay(10);
|
||
77 | 4bc1c0c1 | Kevin Townsend | |
78 | c4f272e1 | ladyada | delay(50);
|
79 | |||
80 | 4bc1c0c1 | Kevin Townsend | /* Set to normal power mode */
|
81 | write8(BNO055_PWR_MODE_ADDR, POWER_MODE_NORMAL); |
||
82 | delay(10);
|
||
83 | c4f272e1 | ladyada | |
84 | write8(BNO055_PAGE_ID_ADDR, 0);
|
||
85 | 26f98bcd | Kevin Townsend | |
86 | /* Set the output units */
|
||
87 | uint8_t unitsel = (0 << 7) | /* Orientation = Android */ |
||
88 | (0 << 4) | /* Temperature = Celsius */ |
||
89 | (0 << 2) | /* Euler = Degrees */ |
||
90 | (1 << 1) | /* Gyro = Rads */ |
||
91 | (0 << 0); /* Accelerometer = m/s^2 */ |
||
92 | write8(BNO055_UNIT_SEL_ADDR, unitsel); |
||
93 | |||
94 | c4f272e1 | ladyada | write8(BNO055_SYS_TRIGGER_ADDR, 0x0);
|
95 | delay(10);
|
||
96 | 4bc1c0c1 | Kevin Townsend | /* Set the requested operating mode (see section 3.3) */
|
97 | c4f272e1 | ladyada | setMode(mode); |
98 | 4bc1c0c1 | Kevin Townsend | delay(20);
|
99 | |||
100 | return true; |
||
101 | } |
||
102 | |||
103 | /**************************************************************************/
|
||
104 | /*!
|
||
105 | @brief Puts the chip in the specified operating mode
|
||
106 | */
|
||
107 | /**************************************************************************/
|
||
108 | void Adafruit_BNO055::setMode(adafruit_bno055_opmode_t mode)
|
||
109 | { |
||
110 | _mode = mode; |
||
111 | c4f272e1 | ladyada | //Serial.print("Mode: 0x"); Serial.println(mode, HEX);
|
112 | 4bc1c0c1 | Kevin Townsend | write8(BNO055_OPR_MODE_ADDR, _mode); |
113 | delay(30);
|
||
114 | } |
||
115 | |||
116 | /**************************************************************************/
|
||
117 | /*!
|
||
118 | c4f272e1 | ladyada | @brief Use the external 32.768KHz crystal
|
119 | */
|
||
120 | /**************************************************************************/
|
||
121 | void Adafruit_BNO055::setExtCrystalUse(boolean usextal)
|
||
122 | { |
||
123 | adafruit_bno055_opmode_t modeback = _mode; |
||
124 | |||
125 | /* Switch to config mode (just in case since this is the default) */
|
||
126 | setMode(OPERATION_MODE_CONFIG); |
||
127 | delay(25);
|
||
128 | write8(BNO055_PAGE_ID_ADDR, 0);
|
||
129 | if (usextal) {
|
||
130 | write8(BNO055_SYS_TRIGGER_ADDR, 0x80);
|
||
131 | } else {
|
||
132 | write8(BNO055_SYS_TRIGGER_ADDR, 0x00);
|
||
133 | } |
||
134 | delay(10);
|
||
135 | /* Set the requested operating mode (see section 3.3) */
|
||
136 | setMode(modeback); |
||
137 | delay(20);
|
||
138 | } |
||
139 | |||
140 | |||
141 | /**************************************************************************/
|
||
142 | /*!
|
||
143 | 4bc1c0c1 | Kevin Townsend | @brief Gets the latest system status info
|
144 | */
|
||
145 | /**************************************************************************/
|
||
146 | 3b2655dc | ladyada | void Adafruit_BNO055::getSystemStatus(uint8_t *system_status, uint8_t *self_test_result, uint8_t *system_error)
|
147 | 4bc1c0c1 | Kevin Townsend | { |
148 | c4f272e1 | ladyada | adafruit_bno055_opmode_t backupmode = _mode; |
149 | |||
150 | setMode(OPERATION_MODE_CONFIG); |
||
151 | delay(20);
|
||
152 | write8(BNO055_PAGE_ID_ADDR, 0);
|
||
153 | |||
154 | 3b2655dc | ladyada | write8(BNO055_SYS_TRIGGER_ADDR, read8(BNO055_SYS_TRIGGER_ADDR) | 0x1);
|
155 | c4f272e1 | ladyada | delay(1000);
|
156 | 4bc1c0c1 | Kevin Townsend | /* Read the system status register */
|
157 | 3b2655dc | ladyada | if (system_status != 0) |
158 | *system_status = read8(BNO055_SYS_STAT_ADDR); |
||
159 | if (self_test_result != 0) |
||
160 | *self_test_result = read8(BNO055_SELFTEST_RESULT_ADDR); |
||
161 | if (system_error != 0) |
||
162 | *system_error = read8(BNO055_SYS_ERR_ADDR); |
||
163 | c4f272e1 | ladyada | |
164 | setMode(backupmode); |
||
165 | delay(20);
|
||
166 | 4bc1c0c1 | Kevin Townsend | } |
167 | |||
168 | /**************************************************************************/
|
||
169 | /*!
|
||
170 | @brief Displays system status info via Serial.print
|
||
171 | */
|
||
172 | /**************************************************************************/
|
||
173 | void Adafruit_BNO055::displaySystemStatus(void) |
||
174 | { |
||
175 | 3b2655dc | ladyada | uint8_t system_status, self_test_result, system_error; |
176 | getSystemStatus(&system_status, &self_test_result, &system_error); |
||
177 | 4bc1c0c1 | Kevin Townsend | |
178 | /* System Status (see section 4.3.58)
|
||
179 | ---------------------------------
|
||
180 | 0 = Idle
|
||
181 | 1 = System Error
|
||
182 | 2 = Initializing Peripherals
|
||
183 | 3 = System Iniitalization
|
||
184 | 4 = Executing Self-Test
|
||
185 | 5 = Sensor fusio algorithm running
|
||
186 | 6 = System running without fusion algorithms */
|
||
187 | |||
188 | Serial.print("System Status: 0x");
|
||
189 | 3b2655dc | ladyada | Serial.println(system_status, HEX); |
190 | 4bc1c0c1 | Kevin Townsend | |
191 | /* Self Test Results (see section )
|
||
192 | --------------------------------
|
||
193 | 1 = test passed, 0 = test failed
|
||
194 |
|
||
195 | Bit 0 = Accelerometer self test
|
||
196 | Bit 1 = Magnetometer self test
|
||
197 | Bit 2 = Gyroscope self test
|
||
198 | Bit 3 = MCU self test
|
||
199 |
|
||
200 | 0x0F = all good! */
|
||
201 | |||
202 | Serial.print("Self Test Results: 0x");
|
||
203 | 3b2655dc | ladyada | Serial.println(self_test_result, HEX); |
204 | 4bc1c0c1 | Kevin Townsend | |
205 | /* System Error (see section 4.3.59)
|
||
206 | ---------------------------------
|
||
207 | 0 = No error
|
||
208 | 1 = Peripheral initialization error
|
||
209 | 2 = System initialization error
|
||
210 | 3 = Self test result failed
|
||
211 | 4 = Register map value out of range
|
||
212 | 5 = Register map address out of range
|
||
213 | 6 = Register map write error
|
||
214 | 7 = BNO low power mode not available for selected operat ion mode
|
||
215 | 8 = Accelerometer power mode not available
|
||
216 | 9 = Fusion algorithm configuration error
|
||
217 | A = Sensor configuration error */
|
||
218 | |||
219 | Serial.print("System Error: 0x");
|
||
220 | 3b2655dc | ladyada | Serial.println(system_error, HEX); |
221 | 4bc1c0c1 | Kevin Townsend | } |
222 | |||
223 | /**************************************************************************/
|
||
224 | /*!
|
||
225 | @brief Gets the chip revision numbers
|
||
226 | */
|
||
227 | /**************************************************************************/
|
||
228 | void Adafruit_BNO055::getRevInfo(adafruit_bno055_rev_info_t* info)
|
||
229 | { |
||
230 | uint8_t a, b; |
||
231 | |||
232 | memset(info, 0, sizeof(adafruit_bno055_rev_info_t)); |
||
233 | |||
234 | info->accel_rev = read8(BNO055_ACCEL_REV_ID_ADDR); |
||
235 | info->mag_rev = read8(BNO055_MAG_REV_ID_ADDR); |
||
236 | info->gyro_rev = read8(BNO055_GYRO_REV_ID_ADDR); |
||
237 | info->bl_rev = read8(BNO055_BL_REV_ID_ADDR); |
||
238 | |||
239 | a = read8(BNO055_SW_REV_ID_LSB_ADDR); |
||
240 | b = read8(BNO055_SW_REV_ID_MSB_ADDR); |
||
241 | info->sw_rev = (((uint16_t)b) << 8) | ((uint16_t)a);
|
||
242 | } |
||
243 | |||
244 | /**************************************************************************/
|
||
245 | /*!
|
||
246 | @brief Displays the chip revision numbers via Serial.print
|
||
247 | */
|
||
248 | /**************************************************************************/
|
||
249 | void Adafruit_BNO055::displayRevInfo(void) |
||
250 | { |
||
251 | adafruit_bno055_rev_info_t info; |
||
252 | getRevInfo(&info); |
||
253 | |||
254 | /* Check the accelerometer revision */
|
||
255 | Serial.print("Accelerometer Revision: 0x");
|
||
256 | Serial.println(info.accel_rev, HEX); |
||
257 | |||
258 | /* Check the magnetometer revision */
|
||
259 | Serial.print("Magnetometer Revision: 0x");
|
||
260 | Serial.println(info.mag_rev, HEX); |
||
261 | |||
262 | /* Check the gyroscope revision */
|
||
263 | Serial.print("Gyroscope Revision: 0x");
|
||
264 | Serial.println(info.gyro_rev, HEX); |
||
265 | |||
266 | /* Check the SW revision */
|
||
267 | Serial.print("SW Revision: 0x");
|
||
268 | Serial.println(info.sw_rev, HEX); |
||
269 | |||
270 | /* Check the bootloader revision */
|
||
271 | Serial.print("Bootloader Revision: 0x");
|
||
272 | Serial.println(info.bl_rev, HEX); |
||
273 | } |
||
274 | |||
275 | /**************************************************************************/
|
||
276 | /*!
|
||
277 | 0e2e2723 | Kevin Townsend | @brief Gets teh temperature in degrees celsius
|
278 | */
|
||
279 | /**************************************************************************/
|
||
280 | int8_t Adafruit_BNO055::getTemp(void)
|
||
281 | { |
||
282 | int8_t temp = (int8_t)(read8(BNO055_TEMP_ADDR)); |
||
283 | return temp;
|
||
284 | } |
||
285 | |||
286 | /**************************************************************************/
|
||
287 | /*!
|
||
288 | 48741e1f | Kevin Townsend | @brief Gets a vector reading from the specified source
|
289 | 4bc1c0c1 | Kevin Townsend | */
|
290 | /**************************************************************************/
|
||
291 | 48741e1f | Kevin Townsend | imu::Vector<3> Adafruit_BNO055::getVector(adafruit_vector_type_t vector_type)
|
292 | 4bc1c0c1 | Kevin Townsend | { |
293 | imu::Vector<3> xyz;
|
||
294 | uint8_t buffer[6];
|
||
295 | memset (buffer, 0, 6); |
||
296 | |||
297 | int16_t x, y, z; |
||
298 | x = y = z = 0;
|
||
299 | |||
300 | 48741e1f | Kevin Townsend | /* Read vector data (6 bytes) */
|
301 | readLen((adafruit_bno055_reg_t)vector_type, buffer, 6);
|
||
302 | 4bc1c0c1 | Kevin Townsend | x = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]); |
303 | y = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]); |
||
304 | z = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]); |
||
305 | 26f98bcd | Kevin Townsend | |
306 | /* Convert the value to an appropriate range (section 3.6.4) */
|
||
307 | /* and assign the value to the Vector type */
|
||
308 | switch(vector_type)
|
||
309 | { |
||
310 | case VECTOR_MAGNETOMETER:
|
||
311 | /* 1uT = 16 LSB */
|
||
312 | xyz[0] = ((double)x)/16.0; |
||
313 | xyz[1] = ((double)y)/16.0; |
||
314 | xyz[2] = ((double)z)/16.0; |
||
315 | break;
|
||
316 | case VECTOR_GYROSCOPE:
|
||
317 | /* 1rps = 900 LSB */
|
||
318 | xyz[0] = ((double)x)/900.0; |
||
319 | xyz[1] = ((double)y)/900.0; |
||
320 | xyz[2] = ((double)z)/900.0; |
||
321 | break;
|
||
322 | case VECTOR_EULER:
|
||
323 | /* 1 degree = 16 LSB */
|
||
324 | xyz[0] = ((double)x)/16.0; |
||
325 | xyz[1] = ((double)y)/16.0; |
||
326 | xyz[2] = ((double)z)/16.0; |
||
327 | break;
|
||
328 | case VECTOR_ACCELEROMETER:
|
||
329 | case VECTOR_LINEARACCEL:
|
||
330 | case VECTOR_GRAVITY:
|
||
331 | /* 1m/s^2 = 100 LSB */
|
||
332 | xyz[0] = ((double)x)/100.0; |
||
333 | xyz[1] = ((double)y)/100.0; |
||
334 | xyz[2] = ((double)z)/100.0; |
||
335 | break;
|
||
336 | } |
||
337 | 4bc1c0c1 | Kevin Townsend | |
338 | return xyz;
|
||
339 | } |
||
340 | |||
341 | /**************************************************************************/
|
||
342 | /*!
|
||
343 | 48741e1f | Kevin Townsend | @brief Gets a quaternion reading from the specified source
|
344 | */
|
||
345 | /**************************************************************************/
|
||
346 | imu::Quaternion Adafruit_BNO055::getQuat(void)
|
||
347 | { |
||
348 | uint8_t buffer[8];
|
||
349 | memset (buffer, 0, 8); |
||
350 | |||
351 | int16_t x, y, z, w; |
||
352 | x = y = z = w = 0;
|
||
353 | |||
354 | /* Read quat data (8 bytes) */
|
||
355 | readLen(BNO055_QUATERNION_DATA_W_LSB_ADDR, buffer, 8);
|
||
356 | w = (((uint16_t)buffer[1]) << 8) | ((uint16_t)buffer[0]); |
||
357 | x = (((uint16_t)buffer[3]) << 8) | ((uint16_t)buffer[2]); |
||
358 | y = (((uint16_t)buffer[5]) << 8) | ((uint16_t)buffer[4]); |
||
359 | z = (((uint16_t)buffer[7]) << 8) | ((uint16_t)buffer[6]); |
||
360 | |||
361 | /* Assign to Quaternion */
|
||
362 | imu::Quaternion quat((double)w, (double)x, (double)y, (double)z); |
||
363 | return quat;
|
||
364 | } |
||
365 | |||
366 | /**************************************************************************/
|
||
367 | /*!
|
||
368 | 4bc1c0c1 | Kevin Townsend | @brief Provides the sensor_t data for this sensor
|
369 | */
|
||
370 | /**************************************************************************/
|
||
371 | void Adafruit_BNO055::getSensor(sensor_t *sensor)
|
||
372 | { |
||
373 | /* Clear the sensor_t object */
|
||
374 | memset(sensor, 0, sizeof(sensor_t)); |
||
375 | |||
376 | /* Insert the sensor name in the fixed length char array */
|
||
377 | strncpy (sensor->name, "BNO055", sizeof(sensor->name) - 1); |
||
378 | sensor->name[sizeof(sensor->name)- 1] = 0; |
||
379 | sensor->version = 1;
|
||
380 | sensor->sensor_id = _sensorID; |
||
381 | sensor->type = SENSOR_TYPE_ORIENTATION; |
||
382 | sensor->min_delay = 0;
|
||
383 | sensor->max_value = 0.0F; |
||
384 | sensor->min_value = 0.0F; |
||
385 | sensor->resolution = 0.01F; |
||
386 | } |
||
387 | |||
388 | /**************************************************************************/
|
||
389 | /*!
|
||
390 | @brief Reads the sensor and returns the data as a sensors_event_t
|
||
391 | */
|
||
392 | /**************************************************************************/
|
||
393 | bool Adafruit_BNO055::getEvent(sensors_event_t *event)
|
||
394 | { |
||
395 | /* Clear the event */
|
||
396 | memset(event, 0, sizeof(sensors_event_t)); |
||
397 | |||
398 | event->version = sizeof(sensors_event_t);
|
||
399 | event->sensor_id = _sensorID; |
||
400 | event->type = SENSOR_TYPE_ORIENTATION; |
||
401 | fcd68623 | Kevin Townsend | event->timestamp = millis(); |
402 | |||
403 | /* Get a Euler angle sample for orientation */
|
||
404 | imu::Vector<3> euler = getVector(Adafruit_BNO055::VECTOR_EULER);
|
||
405 | event->orientation.x = euler.x(); |
||
406 | event->orientation.y = euler.y(); |
||
407 | event->orientation.z = euler.z(); |
||
408 | |||
409 | 4bc1c0c1 | Kevin Townsend | return true; |
410 | } |
||
411 | |||
412 | /***************************************************************************
|
||
413 | PRIVATE FUNCTIONS
|
||
414 | ***************************************************************************/
|
||
415 | |||
416 | /**************************************************************************/
|
||
417 | /*!
|
||
418 | @brief Writes an 8 bit value over I2C
|
||
419 | */
|
||
420 | /**************************************************************************/
|
||
421 | bool Adafruit_BNO055::write8(adafruit_bno055_reg_t reg, byte value)
|
||
422 | { |
||
423 | Wire.beginTransmission(_address); |
||
424 | #if ARDUINO >= 100 |
||
425 | Wire.write((uint8_t)reg); |
||
426 | Wire.write((uint8_t)value); |
||
427 | #else
|
||
428 | Wire.send(reg); |
||
429 | Wire.send(value); |
||
430 | #endif
|
||
431 | Wire.endTransmission(); |
||
432 | |||
433 | /* ToDo: Check for error! */
|
||
434 | return true; |
||
435 | } |
||
436 | |||
437 | /**************************************************************************/
|
||
438 | /*!
|
||
439 | @brief Reads an 8 bit value over I2C
|
||
440 | */
|
||
441 | /**************************************************************************/
|
||
442 | byte Adafruit_BNO055::read8(adafruit_bno055_reg_t reg ) |
||
443 | { |
||
444 | byte value = 0;
|
||
445 | |||
446 | Wire.beginTransmission(_address); |
||
447 | #if ARDUINO >= 100 |
||
448 | Wire.write((uint8_t)reg); |
||
449 | #else
|
||
450 | Wire.send(reg); |
||
451 | #endif
|
||
452 | Wire.endTransmission(); |
||
453 | Wire.requestFrom(_address, (byte)1);
|
||
454 | #if ARDUINO >= 100 |
||
455 | value = Wire.read(); |
||
456 | #else
|
||
457 | value = Wire.receive(); |
||
458 | #endif
|
||
459 | |||
460 | return value;
|
||
461 | } |
||
462 | |||
463 | /**************************************************************************/
|
||
464 | /*!
|
||
465 | @brief Reads the specified number of bytes over I2C
|
||
466 | */
|
||
467 | /**************************************************************************/
|
||
468 | bool Adafruit_BNO055::readLen(adafruit_bno055_reg_t reg, byte * buffer, uint8_t len)
|
||
469 | { |
||
470 | Wire.beginTransmission(_address); |
||
471 | #if ARDUINO >= 100 |
||
472 | Wire.write((uint8_t)reg); |
||
473 | #else
|
||
474 | Wire.send(reg); |
||
475 | #endif
|
||
476 | Wire.endTransmission(); |
||
477 | Wire.requestFrom(_address, (byte)len); |
||
478 | |||
479 | /* Wait until data is available */
|
||
480 | while (Wire.available() < len);
|
||
481 | |||
482 | for (uint8_t i = 0; i < len; i++) |
||
483 | { |
||
484 | #if ARDUINO >= 100 |
||
485 | buffer[i] = Wire.read(); |
||
486 | #else
|
||
487 | buffer[i] = Wire.receive(); |
||
488 | #endif
|
||
489 | } |
||
490 | |||
491 | /* ToDo: Check for errors! */
|
||
492 | return true; |
||
493 | } |