1
1
/*
2
- This is a library written for the BNO080
2
+ This is a modified version of the library written for the BNO080
3
3
SparkFun sells these at its website: www.sparkfun.com
4
4
Do you like this library? Help support SparkFun. Buy a board!
5
5
https://www.sparkfun.com/products/14686
6
6
7
+ Modified by Guillaume Walck August 2019
7
8
Written by Nathan Seidle @ SparkFun Electronics, December 28th, 2017
8
9
9
10
The BNO080 IMU is a powerful triple axis gyro/accel/magnetometer coupled with an ARM processor
13
14
This library handles the initialization of the BNO080 and is able to query the sensor
14
15
for different readings.
15
16
17
+ This version of the library provides a way to know if the data was updated
18
+
16
19
https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library
17
20
18
21
Development environment specifics:
27
30
along with this program. If not, see <http://www.gnu.org/licenses/>.
28
31
*/
29
32
30
- #include " SparkFun_BNO080_Arduino_Library .h"
33
+ #include " BNO08x_Library .h"
31
34
32
35
// Attempt communication with the device
33
36
// Return true if we got a 'Polo' back from Marco
@@ -255,27 +258,31 @@ void BNO080::parseInputReport(void)
255
258
rawAccelX = data1;
256
259
rawAccelY = data2;
257
260
rawAccelZ = data3;
261
+ newData[SENSOR_REPORTID_ACCELEROMETER] = true ;
258
262
}
259
263
else if (shtpData[5 ] == SENSOR_REPORTID_LINEAR_ACCELERATION)
260
264
{
261
265
accelLinAccuracy = status;
262
266
rawLinAccelX = data1;
263
267
rawLinAccelY = data2;
264
268
rawLinAccelZ = data3;
269
+ newData[SENSOR_REPORTID_LINEAR_ACCELERATION] = true ;
265
270
}
266
271
else if (shtpData[5 ] == SENSOR_REPORTID_GYROSCOPE)
267
272
{
268
273
gyroAccuracy = status;
269
274
rawGyroX = data1;
270
275
rawGyroY = data2;
271
276
rawGyroZ = data3;
277
+ newData[SENSOR_REPORTID_GYROSCOPE] = true ;
272
278
}
273
279
else if (shtpData[5 ] == SENSOR_REPORTID_MAGNETIC_FIELD)
274
280
{
275
281
magAccuracy = status;
276
282
rawMagX = data1;
277
283
rawMagY = data2;
278
284
rawMagZ = data3;
285
+ newData[SENSOR_REPORTID_MAGNETIC_FIELD] = true ;
279
286
}
280
287
else if (shtpData[5 ] == SENSOR_REPORTID_ROTATION_VECTOR || shtpData[5 ] == SENSOR_REPORTID_GAME_ROTATION_VECTOR)
281
288
{
@@ -285,18 +292,23 @@ void BNO080::parseInputReport(void)
285
292
rawQuatK = data3;
286
293
rawQuatReal = data4;
287
294
rawQuatRadianAccuracy = data5; // Only available on rotation vector, not game rot vector
295
+ newData[SENSOR_REPORTID_ROTATION_VECTOR] = true ;
296
+ newData[SENSOR_REPORTID_GAME_ROTATION_VECTOR] = true ;
288
297
}
289
298
else if (shtpData[5 ] == SENSOR_REPORTID_STEP_COUNTER)
290
299
{
291
300
stepCount = data3; // Bytes 8/9
301
+ newData[SENSOR_REPORTID_STEP_COUNTER] = true ;
292
302
}
293
303
else if (shtpData[5 ] == SENSOR_REPORTID_STABILITY_CLASSIFIER)
294
304
{
295
305
stabilityClassifier = shtpData[5 + 4 ]; // Byte 4 only
306
+ newData[SENSOR_REPORTID_STABILITY_CLASSIFIER] = true ;
296
307
}
297
308
else if (shtpData[5 ] == SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER)
298
309
{
299
310
activityClassifier = shtpData[5 + 5 ]; // Most likely state
311
+ newData[SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER] = true ;
300
312
301
313
// Load activity classification confidences into the array
302
314
for (uint8_t x = 0 ; x < 9 ; x++) // Hardcoded to max of 9. TODO - bring in array size
@@ -307,18 +319,21 @@ void BNO080::parseInputReport(void)
307
319
memsRawAccelX = data1;
308
320
memsRawAccelY = data2;
309
321
memsRawAccelZ = data3;
322
+ newData[SENSOR_REPORTID_RAW_ACCELEROMETER] = true ;
310
323
}
311
324
else if (shtpData[5 ] == SENSOR_REPORTID_RAW_GYROSCOPE)
312
325
{
313
326
memsRawGyroX = data1;
314
327
memsRawGyroY = data2;
315
328
memsRawGyroZ = data3;
329
+ newData[SENSOR_REPORTID_RAW_GYROSCOPE] = true ;
316
330
}
317
331
else if (shtpData[5 ] == SENSOR_REPORTID_RAW_MAGNETOMETER)
318
332
{
319
333
memsRawMagX = data1;
320
334
memsRawMagY = data2;
321
335
memsRawMagZ = data3;
336
+ newData[SENSOR_REPORTID_RAW_MAGNETOMETER] = true ;
322
337
}
323
338
else if (shtpData[5 ] == SHTP_REPORT_COMMAND_RESPONSE)
324
339
{
@@ -341,6 +356,54 @@ void BNO080::parseInputReport(void)
341
356
// TODO additional feature reports may be strung together. Parse them all.
342
357
}
343
358
359
+ bool BNO080::isNewData (uint8_t type)
360
+ {
361
+ if (type <= SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER)
362
+ return newData[type];
363
+ else
364
+ return false ;
365
+ }
366
+
367
+ void BNO080::resetAllNewData ()
368
+ {
369
+ for (uint8_t i = 0 ; i <= SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER; i++)
370
+ newData[i] = false ;
371
+ }
372
+ void BNO080::resetNewData (uint8_t type)
373
+ {
374
+ if (type <= SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER)
375
+ newData[type] = false ;
376
+ }
377
+
378
+ // Gets the full quaternion only if there is new data
379
+ // i,j,k,real output floats
380
+ // reset_new_data (default=true) on newdata, erase the flag
381
+ bool BNO080::getOnNewQuat (float &i, float &j, float &k, float &real, float radaccuracy, uint8_t &accuracy, bool reset_new_data)
382
+ {
383
+ if (newData[SENSOR_REPORTID_ROTATION_VECTOR]) // if new data only
384
+ {
385
+ getQuat (i, j, k, real, radaccuracy, accuracy, reset_new_data);
386
+ return true ;
387
+ }
388
+ return false ;
389
+ }
390
+
391
+ // Gets the full quaternion
392
+ // i,j,k,real output floats
393
+ // reset_new_data (default=true) on newdata, erase the flag
394
+ void BNO080::getQuat (float &i, float &j, float &k, float &real, float radaccuracy, uint8_t &accuracy, bool reset_new_data)
395
+ {
396
+ i = qToFloat (rawQuatI, rotationVector_Q1);
397
+ j = qToFloat (rawQuatJ, rotationVector_Q1);
398
+ k = qToFloat (rawQuatK, rotationVector_Q1);
399
+ real = qToFloat (rawQuatReal, rotationVector_Q1);
400
+ radaccuracy = qToFloat (rawQuatRadianAccuracy, rotationVector_Q1);
401
+ accuracy = quatAccuracy;
402
+
403
+ if (reset_new_data) // reset the new data flag
404
+ newData[SENSOR_REPORTID_ROTATION_VECTOR] = false ;
405
+ }
406
+
344
407
// Return the rotation vector quaternion I
345
408
float BNO080::getQuatI ()
346
409
{
@@ -382,6 +445,33 @@ uint8_t BNO080::getQuatAccuracy()
382
445
return (quatAccuracy);
383
446
}
384
447
448
+
449
+ // Gets the full acceleration only if there is new data
450
+ // x,y,z output floats
451
+ // reset_new_data (default=true) on newdata, erase the flag
452
+ bool BNO080::getOnNewAccel (float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data)
453
+ {
454
+ if (newData[SENSOR_REPORTID_ACCELEROMETER]) // if new data only
455
+ {
456
+ getAccel (x, y, z, accuracy, reset_new_data);
457
+ return true ;
458
+ }
459
+ return false ;
460
+ }
461
+
462
+ // Gets the full acceleration
463
+ // x,y,z output floats
464
+ // reset_new_data (default=true) on newdata, erase the flag
465
+ void BNO080::getAccel (float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data)
466
+ {
467
+ x = qToFloat (rawAccelX, accelerometer_Q1);
468
+ y = qToFloat (rawAccelY, accelerometer_Q1);
469
+ z = qToFloat (rawAccelZ, accelerometer_Q1);
470
+ accuracy = accelAccuracy;
471
+ if (reset_new_data) // reset the new data flag
472
+ newData[SENSOR_REPORTID_ACCELEROMETER] = false ;
473
+ }
474
+
385
475
// Return the acceleration component
386
476
float BNO080::getAccelX ()
387
477
{
@@ -411,6 +501,33 @@ uint8_t BNO080::getAccelAccuracy()
411
501
412
502
// linear acceleration, i.e. minus gravity
413
503
504
+ // Gets the full lin acceleration only if there is new data
505
+ // x,y,z output floats
506
+ // reset_new_data (default=true) on newdata, erase the flag
507
+ bool BNO080::getOnNewLinAccel (float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data)
508
+ {
509
+ if (newData[SENSOR_REPORTID_LINEAR_ACCELERATION]) // if new data only
510
+ {
511
+ getLinAccel (x, y, z, accuracy, reset_new_data);
512
+ return true ;
513
+ }
514
+ return false ;
515
+ }
516
+
517
+ // Gets the full lin acceleration
518
+ // x,y,z output floats
519
+ // reset_new_data (default=true) on newdata, erase the flag
520
+ void BNO080::getLinAccel (float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data)
521
+ {
522
+ x = qToFloat (rawLinAccelX, linear_accelerometer_Q1);
523
+ y = qToFloat (rawLinAccelY, linear_accelerometer_Q1);
524
+ z = qToFloat (rawLinAccelZ, linear_accelerometer_Q1);
525
+ accuracy = accelLinAccuracy;
526
+
527
+ if (reset_new_data) // reset the new data flag
528
+ newData[SENSOR_REPORTID_LINEAR_ACCELERATION] = false ;
529
+ }
530
+
414
531
// Return the acceleration component
415
532
float BNO080::getLinAccelX ()
416
533
{
@@ -438,6 +555,32 @@ uint8_t BNO080::getLinAccelAccuracy()
438
555
return (accelLinAccuracy);
439
556
}
440
557
558
+ // Gets the full gyro vector only if there is new data
559
+ // x,y,z output floats
560
+ // reset_new_data (default=true) on newdata, erase the flag
561
+ bool BNO080::getOnNewGyro (float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data)
562
+ {
563
+ if (newData[SENSOR_REPORTID_GYROSCOPE]) // if new data only
564
+ {
565
+ getGyro (x, y, z, accuracy, reset_new_data);
566
+ return true ;
567
+ }
568
+ return false ;
569
+ }
570
+
571
+ // Gets the full gyro vector
572
+ // x,y,z output floats
573
+ // reset_new_data (default=true) on newdata, erase the flag
574
+ void BNO080::getGyro (float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data)
575
+ {
576
+ x = qToFloat (rawGyroX, gyro_Q1);
577
+ y = qToFloat (rawGyroY, gyro_Q1);
578
+ z = qToFloat (rawGyroZ, gyro_Q1);
579
+ accuracy = gyroAccuracy;
580
+ if (reset_new_data) // reset the new data flag
581
+ newData[SENSOR_REPORTID_GYROSCOPE] = false ;
582
+ }
583
+
441
584
// Return the gyro component
442
585
float BNO080::getGyroX ()
443
586
{
0 commit comments