Skip to content

Commit

Permalink
Merge pull request #73 from sparkfun/release-candidate
Browse files Browse the repository at this point in the history
v1.1.10
  • Loading branch information
PaulZC authored Feb 18, 2021
2 parents 314ba5d + e74a234 commit a0316a0
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 2 deletions.
105 changes: 105 additions & 0 deletions examples/Example20-Sleep/Example20-Sleep.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Using the BNO080 IMU
By: Nathan Seidle
SparkFun Electronics
Date: December 21st, 2017
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/14586
This example shows how to output the i/j/k/real parts of the rotation vector.
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
It takes about 1ms at 400kHz I2C to read a record from the sensor, but we are polling the sensor continually
between updates from the sensor. Use the interrupt pin on the BNO080 breakout to avoid polling.
Hardware Connections:
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
Plug the sensor onto the shield
Serial.print it out at 9600 baud to serial monitor.
*/

#include <Wire.h>

#include "SparkFun_BNO080_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO080
BNO080 myIMU;

void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("BNO080 Sleep Example");

Wire.begin();

//Are you using a ESP? Check this issue for more information: https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/issues/16
// //=================================
// delay(100); // Wait for BNO to boot
// // Start i2c and BNO080
// Wire.flush(); // Reset I2C
// IMU.begin(BNO080_DEFAULT_ADDRESS, Wire);
// Wire.begin(4, 5);
// Wire.setClockStretchLimit(4000);
// //=================================

if (myIMU.begin() == false)
{
Serial.println("BNO080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
while (1);
}

Wire.setClock(400000); //Increase I2C data rate to 400kHz

myIMU.enableRotationVector(50); //Send data update every 50ms

Serial.println(F("Rotation vector enabled"));
Serial.println(F("Output in form i, j, k, real, accuracy"));
}

unsigned long lastMillis = 0; // Keep track of time
bool lastPowerState = true; // Toggle between "On" and "Sleep"

void loop()
{
//Look for reports from the IMU
if (myIMU.dataAvailable() == true)
{
float quatI = myIMU.getQuatI();
float quatJ = myIMU.getQuatJ();
float quatK = myIMU.getQuatK();
float quatReal = myIMU.getQuatReal();
float quatRadianAccuracy = myIMU.getQuatRadianAccuracy();

Serial.print(quatI, 2);
Serial.print(F(","));
Serial.print(quatJ, 2);
Serial.print(F(","));
Serial.print(quatK, 2);
Serial.print(F(","));
Serial.print(quatReal, 2);
Serial.print(F(","));
Serial.print(quatRadianAccuracy, 2);
Serial.print(F(","));

Serial.println();
}

//Check if it is time to change the power state
if (millis() > (lastMillis + 5000)) // Change state every 5 seconds
{
lastMillis = millis(); // Keep track of time

if (lastPowerState) // Are we "On"?
{
myIMU.modeSleep(); // Put BNO to sleep
}
else
{
myIMU.modeOn(); // Turn BNO back on
}

lastPowerState ^= 1; // Invert lastPowerState (using ex-or)
}
}
4 changes: 4 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enableDebugging KEYWORD2

softReset KEYWORD2
resetReason KEYWORD2
modeOn KEYWORD2
modeSleep KEYWORD2

qToFloat KEYWORD2

Expand All @@ -37,6 +39,7 @@ enableARVRStabilizedGameRotationVector KEYWORD2
enableAccelerometer KEYWORD2
enableGyro KEYWORD2
enableMagnetometer KEYWORD2
enableTapDetector KEYWORD2
enableStepCounter KEYWORD2
enableStabilityClassifier KEYWORD2
enableActivityClassifier KEYWORD2
Expand Down Expand Up @@ -98,6 +101,7 @@ saveCalibration KEYWORD2
requestCalibrationStatus KEYWORD2
calibrationComplete KEYWORD2

getTapDetector KEYWORD2
getTimeStamp KEYWORD2
getStepCount KEYWORD2
getStabilityClassifier KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SparkFun BNO080 Cortex Based IMU
version=1.1.9
version=1.1.10
author=SparkFun Electronics <[email protected]>
maintainer=SparkFun Electronics <sparkfun.com>
sentence=Library for the SparkFun Qwiic VR IMU - BNO080/BNO085
Expand Down
56 changes: 55 additions & 1 deletion src/SparkFun_BNO080_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ uint16_t BNO080::parseInputReport(void)
// not game rot vector and not ar/vr stabilized rotation vector
rawQuatRadianAccuracy = data5;
}
else if (shtpData[5] == SENSOR_REPORTID_TAP_DETECTOR)
{
tapDetector = shtpData[5 + 4]; //Byte 4 only
}
else if (shtpData[5] == SENSOR_REPORTID_STEP_COUNTER)
{
stepCount = data3; //Bytes 8/9
Expand Down Expand Up @@ -717,6 +721,14 @@ float BNO080::getFastGyroZ()
return (gyro);
}

//Return the tap detector
uint8_t BNO080::getTapDetector()
{
uint8_t previousTapDetector = tapDetector;
tapDetector = 0; //Reset so user code sees exactly one tap
return (previousTapDetector);
}

//Return the step count
uint16_t BNO080::getStepCount()
{
Expand Down Expand Up @@ -897,7 +909,7 @@ bool BNO080::readFRSdata(uint16_t recordID, uint8_t startLocation, uint8_t words
//We have the packet, inspect it for the right contents
//See page 40. Report ID should be 0xF3 and the FRS types should match the thing we requested
if (shtpData[0] == SHTP_REPORT_FRS_READ_RESPONSE)
if (((uint16_t)shtpData[13] << 8 | shtpData[12]) == recordID)
if (((((uint16_t)shtpData[13]) << 8) | shtpData[12]) == recordID)
break; //This packet is one we are looking for
}

Expand Down Expand Up @@ -951,6 +963,42 @@ void BNO080::softReset(void)
; //delay(1);
}

//Set the operating mode to "On"
//(This one is for @jerabaul29)
void BNO080::modeOn(void)
{
shtpData[0] = 2; //On

//Attempt to start communication with sensor
sendPacket(CHANNEL_EXECUTABLE, 1); //Transmit packet on channel 1, 1 byte

//Read all incoming data and flush it
delay(50);
while (receivePacket() == true)
; //delay(1);
delay(50);
while (receivePacket() == true)
; //delay(1);
}

//Set the operating mode to "Sleep"
//(This one is for @jerabaul29)
void BNO080::modeSleep(void)
{
shtpData[0] = 3; //Sleep

//Attempt to start communication with sensor
sendPacket(CHANNEL_EXECUTABLE, 1); //Transmit packet on channel 1, 1 byte

//Read all incoming data and flush it
delay(50);
while (receivePacket() == true)
; //delay(1);
delay(50);
while (receivePacket() == true)
; //delay(1);
}

//Get the reason for the last reset
//1 = POR, 2 = Internal reset, 3 = Watchdog, 4 = External reset, 5 = Other
uint8_t BNO080::resetReason()
Expand Down Expand Up @@ -1036,6 +1084,12 @@ void BNO080::enableGyroIntegratedRotationVector(uint16_t timeBetweenReports)
setFeatureCommand(SENSOR_REPORTID_GYRO_INTEGRATED_ROTATION_VECTOR, timeBetweenReports);
}

//Sends the packet to enable the tap detector
void BNO080::enableTapDetector(uint16_t timeBetweenReports)
{
setFeatureCommand(SENSOR_REPORTID_TAP_DETECTOR, timeBetweenReports);
}

//Sends the packet to enable the step counter
void BNO080::enableStepCounter(uint16_t timeBetweenReports)
{
Expand Down
5 changes: 5 additions & 0 deletions src/SparkFun_BNO080_Arduino_Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class BNO080

void softReset(); //Try to reset the IMU via software
uint8_t resetReason(); //Query the IMU for the reason it last reset
void modeOn(); //Use the executable channel to turn the BNO on
void modeSleep(); //Use the executable channel to put the BNO to sleep

float qToFloat(int16_t fixedPointValue, uint8_t qPoint); //Given a Q value, converts fixed point floating to regular floating point number

Expand All @@ -151,6 +153,7 @@ class BNO080
void enableLinearAccelerometer(uint16_t timeBetweenReports);
void enableGyro(uint16_t timeBetweenReports);
void enableMagnetometer(uint16_t timeBetweenReports);
void enableTapDetector(uint16_t timeBetweenReports);
void enableStepCounter(uint16_t timeBetweenReports);
void enableStabilityClassifier(uint16_t timeBetweenReports);
void enableActivityClassifier(uint16_t timeBetweenReports, uint32_t activitiesToEnable, uint8_t (&activityConfidences)[9]);
Expand Down Expand Up @@ -211,6 +214,7 @@ class BNO080
void requestCalibrationStatus(); //Sends command to get status
boolean calibrationComplete(); //Checks ME Cal response for byte 5, R0 - Status

uint8_t getTapDetector();
uint32_t getTimeStamp();
uint16_t getStepCount();
uint8_t getStabilityClassifier();
Expand Down Expand Up @@ -276,6 +280,7 @@ class BNO080
uint16_t rawMagX, rawMagY, rawMagZ, magAccuracy;
uint16_t rawQuatI, rawQuatJ, rawQuatK, rawQuatReal, rawQuatRadianAccuracy, quatAccuracy;
uint16_t rawFastGyroX, rawFastGyroY, rawFastGyroZ;
uint8_t tapDetector;
uint16_t stepCount;
uint32_t timeStamp;
uint8_t stabilityClassifier;
Expand Down

0 comments on commit a0316a0

Please sign in to comment.