Skip to content

Commit 905084e

Browse files
committed
Added support for newData handling from outside + reading multiple variable at the same time
1 parent 263c274 commit 905084e

File tree

2 files changed

+166
-3
lines changed

2 files changed

+166
-3
lines changed

src/SparkFun_BNO080_Arduino_Library.cpp

+145-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/*
2-
This is a library written for the BNO080
2+
This is a modified version of the library written for the BNO080
33
SparkFun sells these at its website: www.sparkfun.com
44
Do you like this library? Help support SparkFun. Buy a board!
55
https://www.sparkfun.com/products/14686
66
7+
Modified by Guillaume Walck August 2019
78
Written by Nathan Seidle @ SparkFun Electronics, December 28th, 2017
89
910
The BNO080 IMU is a powerful triple axis gyro/accel/magnetometer coupled with an ARM processor
@@ -13,6 +14,8 @@
1314
This library handles the initialization of the BNO080 and is able to query the sensor
1415
for different readings.
1516
17+
This version of the library provides a way to know if the data was updated
18+
1619
https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library
1720
1821
Development environment specifics:
@@ -27,7 +30,7 @@
2730
along with this program. If not, see <http://www.gnu.org/licenses/>.
2831
*/
2932

30-
#include "SparkFun_BNO080_Arduino_Library.h"
33+
#include "BNO08x_Library.h"
3134

3235
//Attempt communication with the device
3336
//Return true if we got a 'Polo' back from Marco
@@ -255,27 +258,31 @@ void BNO080::parseInputReport(void)
255258
rawAccelX = data1;
256259
rawAccelY = data2;
257260
rawAccelZ = data3;
261+
newData[SENSOR_REPORTID_ACCELEROMETER] = true;
258262
}
259263
else if (shtpData[5] == SENSOR_REPORTID_LINEAR_ACCELERATION)
260264
{
261265
accelLinAccuracy = status;
262266
rawLinAccelX = data1;
263267
rawLinAccelY = data2;
264268
rawLinAccelZ = data3;
269+
newData[SENSOR_REPORTID_LINEAR_ACCELERATION] = true;
265270
}
266271
else if (shtpData[5] == SENSOR_REPORTID_GYROSCOPE)
267272
{
268273
gyroAccuracy = status;
269274
rawGyroX = data1;
270275
rawGyroY = data2;
271276
rawGyroZ = data3;
277+
newData[SENSOR_REPORTID_GYROSCOPE] = true;
272278
}
273279
else if (shtpData[5] == SENSOR_REPORTID_MAGNETIC_FIELD)
274280
{
275281
magAccuracy = status;
276282
rawMagX = data1;
277283
rawMagY = data2;
278284
rawMagZ = data3;
285+
newData[SENSOR_REPORTID_MAGNETIC_FIELD] = true;
279286
}
280287
else if (shtpData[5] == SENSOR_REPORTID_ROTATION_VECTOR || shtpData[5] == SENSOR_REPORTID_GAME_ROTATION_VECTOR)
281288
{
@@ -285,18 +292,23 @@ void BNO080::parseInputReport(void)
285292
rawQuatK = data3;
286293
rawQuatReal = data4;
287294
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;
288297
}
289298
else if (shtpData[5] == SENSOR_REPORTID_STEP_COUNTER)
290299
{
291300
stepCount = data3; //Bytes 8/9
301+
newData[SENSOR_REPORTID_STEP_COUNTER] = true;
292302
}
293303
else if (shtpData[5] == SENSOR_REPORTID_STABILITY_CLASSIFIER)
294304
{
295305
stabilityClassifier = shtpData[5 + 4]; //Byte 4 only
306+
newData[SENSOR_REPORTID_STABILITY_CLASSIFIER] = true;
296307
}
297308
else if (shtpData[5] == SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER)
298309
{
299310
activityClassifier = shtpData[5 + 5]; //Most likely state
311+
newData[SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER] = true;
300312

301313
//Load activity classification confidences into the array
302314
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)
307319
memsRawAccelX = data1;
308320
memsRawAccelY = data2;
309321
memsRawAccelZ = data3;
322+
newData[SENSOR_REPORTID_RAW_ACCELEROMETER] = true;
310323
}
311324
else if (shtpData[5] == SENSOR_REPORTID_RAW_GYROSCOPE)
312325
{
313326
memsRawGyroX = data1;
314327
memsRawGyroY = data2;
315328
memsRawGyroZ = data3;
329+
newData[SENSOR_REPORTID_RAW_GYROSCOPE] = true;
316330
}
317331
else if (shtpData[5] == SENSOR_REPORTID_RAW_MAGNETOMETER)
318332
{
319333
memsRawMagX = data1;
320334
memsRawMagY = data2;
321335
memsRawMagZ = data3;
336+
newData[SENSOR_REPORTID_RAW_MAGNETOMETER] = true;
322337
}
323338
else if (shtpData[5] == SHTP_REPORT_COMMAND_RESPONSE)
324339
{
@@ -341,6 +356,54 @@ void BNO080::parseInputReport(void)
341356
//TODO additional feature reports may be strung together. Parse them all.
342357
}
343358

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+
344407
//Return the rotation vector quaternion I
345408
float BNO080::getQuatI()
346409
{
@@ -382,6 +445,33 @@ uint8_t BNO080::getQuatAccuracy()
382445
return (quatAccuracy);
383446
}
384447

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+
385475
//Return the acceleration component
386476
float BNO080::getAccelX()
387477
{
@@ -411,6 +501,33 @@ uint8_t BNO080::getAccelAccuracy()
411501

412502
// linear acceleration, i.e. minus gravity
413503

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+
414531
//Return the acceleration component
415532
float BNO080::getLinAccelX()
416533
{
@@ -438,6 +555,32 @@ uint8_t BNO080::getLinAccelAccuracy()
438555
return (accelLinAccuracy);
439556
}
440557

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+
441584
//Return the gyro component
442585
float BNO080::getGyroX()
443586
{

src/SparkFun_BNO080_Arduino_Library.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/*
2-
This is a library written for the BNO080
2+
This is a modified version of the library written for the BNO080
33
SparkFun sells these at its website: www.sparkfun.com
44
Do you like this library? Help support SparkFun. Buy a board!
55
https://www.sparkfun.com/products/14686
66
7+
Modified by Guillaume Walck August 2019
78
Written by Nathan Seidle @ SparkFun Electronics, December 28th, 2017
89
910
The BNO080 IMU is a powerful triple axis gyro/accel/magnetometer coupled with an ARM processor
@@ -13,6 +14,8 @@
1314
This library handles the initialization of the BNO080 and is able to query the sensor
1415
for different readings.
1516
17+
This version of the library provides a way to know if the data was updated
18+
1619
https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library
1720
1821
Development environment specifics:
@@ -161,26 +164,39 @@ class BNO080
161164
void parseInputReport(void); //Parse sensor readings out of report
162165
void parseCommandReport(void); //Parse command responses out of report
163166

167+
void resetAllNewData();
168+
bool isNewData(uint8_t type);
169+
void resetNewData(uint8_t type);
170+
164171
float getQuatI();
165172
float getQuatJ();
166173
float getQuatK();
167174
float getQuatReal();
168175
float getQuatRadianAccuracy();
176+
void getQuat(float &i, float &j, float &k, float &real, float radaccuracy, uint8_t &accuracy, bool reset_new_data=true);
177+
bool getOnNewQuat(float &i, float &j, float &k, float &real, float radaccuracy, uint8_t &accuracy, bool reset_new_data=true);
169178
uint8_t getQuatAccuracy();
170179

171180
float getAccelX();
172181
float getAccelY();
173182
float getAccelZ();
174183
uint8_t getAccelAccuracy();
184+
void getAccel(float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data=true);
185+
bool getOnNewAccel(float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data=true);
175186

176187
float getLinAccelX();
177188
float getLinAccelY();
178189
float getLinAccelZ();
190+
void getLinAccel(float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data=true);
191+
bool getOnNewLinAccel(float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data=true);
192+
179193
uint8_t getLinAccelAccuracy();
180194

181195
float getGyroX();
182196
float getGyroY();
183197
float getGyroZ();
198+
void getGyro(float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data=true);
199+
bool getOnNewGyro(float &x, float &y, float &z, uint8_t &accuracy, bool reset_new_data=true);
184200
uint8_t getGyroAccuracy();
185201

186202
float getMagX();
@@ -236,6 +252,8 @@ class BNO080
236252
uint8_t sequenceNumber[6] = {0, 0, 0, 0, 0, 0}; //There are 6 com channels. Each channel has its own seqnum
237253
uint8_t commandSequenceNumber = 0; //Commands have a seqNum as well. These are inside command packet, the header uses its own seqNum per channel
238254
uint32_t metaData[MAX_METADATA_SIZE]; //There is more than 10 words in a metadata record but we'll stop at Q point 3
255+
256+
bool newData[SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER+1]; //Vector of flags to report on newdata for a given report
239257

240258
private:
241259
//Variables
@@ -255,9 +273,11 @@ class BNO080
255273
//These are the raw sensor values (without Q applied) pulled from the user requested Input Report
256274
uint16_t rawAccelX, rawAccelY, rawAccelZ, accelAccuracy;
257275
uint16_t rawLinAccelX, rawLinAccelY, rawLinAccelZ, accelLinAccuracy;
276+
bool linaccel_newdata;
258277
uint16_t rawGyroX, rawGyroY, rawGyroZ, gyroAccuracy;
259278
uint16_t rawMagX, rawMagY, rawMagZ, magAccuracy;
260279
uint16_t rawQuatI, rawQuatJ, rawQuatK, rawQuatReal, rawQuatRadianAccuracy, quatAccuracy;
280+
bool quat_newdata;
261281
uint16_t stepCount;
262282
uint32_t timeStamp;
263283
uint8_t stabilityClassifier;

0 commit comments

Comments
 (0)