Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IM the new U - new setup procedure #6

Merged
merged 11 commits into from
Dec 20, 2023
Prev Previous commit
Next Next commit
created BNO specific enum and updated sample code to utilize it
brainuser5705 committed Dec 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit c05579074ecbe702b6bf40995fa85de9998ceb82
13 changes: 12 additions & 1 deletion include/BNO055.hpp
Original file line number Diff line number Diff line change
@@ -64,6 +64,17 @@ namespace IMU {
*/
class BNO055 {
public:
/**
* Represents potential errors that may take place when using the I2C
* interface. Each method that interfaces over I2C could potentially
* return one of these errors, or OK if no error.
*/
enum class BNO055Status {
FAIL_INIT = 0,
FAIL_SELF_TEST = 1,
SUCCESS = 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nitpick, but generally, we have the successful state have a value of 0, and we call it "OK." It's not really an official standard, but it's pretty common across C code I've seen in my experience

};

/**
* Initializer for a BNO055 sensor.
* Takes in i2c to setup a connection with the board
@@ -78,7 +89,7 @@ class BNO055 {
*
* @return whether the setup succeeded.
*/
bool setup();
BNO055Status setup();

/**
* Fetch the euler angle data.
12 changes: 6 additions & 6 deletions src/BNO055.cpp
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ IMU::BNO055::BNO055(IO::I2C& i2C, uint8_t i2cSlaveAddress) : i2c(i2C) {
*
* @return a boolean indicating whether or not the device was successfully initialized
*/
bool IMU::BNO055::setup() {
IMU::BNO055::BNO055Status IMU::BNO055::setup() {

log::LOGGER.log(log::Logger::LogLevel::INFO, "Starting Initialization...\r\n");

@@ -40,7 +40,7 @@ bool IMU::BNO055::setup() {
uint8_t id = 0;
if (i2c.write(i2cAddress, 0x00) != IO::I2C::I2CStatus::OK) {
log::LOGGER.log(log::Logger::LogLevel::INFO, "Failed to detect IMU device with i2c and will quit initialization\r\n");
return
return BNO055::BNO055Status::FAIL_INIT;
}
log::LOGGER.log(log::Logger::LogLevel::INFO, "Device should be booted now... Checking if we can read...\r\n");
i2c.read(i2cAddress, &id);
@@ -54,7 +54,7 @@ bool IMU::BNO055::setup() {

if (id != BNO055_ID) {
log::LOGGER.log(log::Logger::LogLevel::ERROR, "Failed to initialize the IMU. Quitting initialization.\r\n");
return false;
return BNO055::BNO055Status::FAIL_INIT;
}
}

@@ -71,8 +71,8 @@ bool IMU::BNO055::setup() {
// All four LSB bits of result should be 1 for successful test
if ((result & 0x0F) != 0x0F){
log::LOGGER.log(log::Logger::LogLevel::ERROR, "Self-test failed. Quitting initialization.\r\n");
return false;
} else {
return BNO055::BNO055Status::FAIL_SELF_TEST;
}else{
log::LOGGER.log(log::Logger::LogLevel::INFO, "Self-test passed, all sensors and microcontroller are functioning.\r\n");
}

@@ -85,7 +85,7 @@ bool IMU::BNO055::setup() {

// If everything above worked, the device has successfully booted.
log::LOGGER.log(log::Logger::LogLevel::INFO, "System successfully booted!\r\n");
return true;
return BNO055::BNO055Status::SUCCESS;
}

IO::I2C::I2CStatus IMU::BNO055::getEuler(uint16_t& xBuffer, uint16_t& yBuffer, uint16_t& zBuffer) {
94 changes: 49 additions & 45 deletions targets/BNO055-Sample/main.cpp
Original file line number Diff line number Diff line change
@@ -32,65 +32,69 @@ int main() {

IMU::BNO055 bno055(i2c, 0x28);
// The bno055 has a lengthy boot sequence, so it needs a setup function to be called.
bno055.setup();
if (bno055.setup() == IMU::BNO055::BNO055Status::SUCCESS){
uart.printf("Starting BNO055 Testing...");
while (1) {
// Retrieve the Euler X, Y and Z values from the bno055
uint16_t eulerX;
uint16_t eulerY;
uint16_t eulerZ;

uart.printf("Starting BNO055 Testing...");
while (1) {
// Retrieve the Euler X, Y and Z values from the bno055
uint16_t eulerX;
uint16_t eulerY;
uint16_t eulerZ;
bno055.getEuler(eulerX, eulerY, eulerZ);

bno055.getEuler(eulerX, eulerY, eulerZ);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Euler x: %d.%d", eulerX / 16, eulerX % 16);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Euler y: %d.%d", eulerY / 16, eulerY % 16);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Euler z: %d.%d", eulerZ / 16, eulerZ % 16);

log::LOGGER.log(log::Logger::LogLevel::INFO, "Euler x: %d.%d", eulerX / 16, eulerX % 16);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Euler y: %d.%d", eulerY / 16, eulerY % 16);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Euler z: %d.%d", eulerZ / 16, eulerZ % 16);
// Retrieve the Gyroscope X, Y, and Z values from the bno055
uint16_t gyroX;
uint16_t gyroY;
uint16_t gyroZ;

// Retrieve the Gyroscope X, Y, and Z values from the bno055
uint16_t gyroX;
uint16_t gyroY;
uint16_t gyroZ;
bno055.getGyroscope(gyroX, gyroY, gyroZ);

bno055.getGyroscope(gyroX, gyroY, gyroZ);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gyroscope x: %d.%d", gyroX / 16, gyroX % 16);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gyroscope y: %d.%d", gyroY / 16, gyroY % 16);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gyroscope z: %d.%d", gyroZ / 16, gyroZ % 16);

log::LOGGER.log(log::Logger::LogLevel::INFO, "Gyroscope x: %d.%d", gyroX / 16, gyroX % 16);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gyroscope y: %d.%d", gyroY / 16, gyroY % 16);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gyroscope z: %d.%d", gyroZ / 16, gyroZ % 16);
// Retrieve the Linear Acceleration X, Y, and Z values from the bno055
uint16_t linearAccelX;
uint16_t linearAccelY;
uint16_t linearAccelZ;

// Retrieve the Linear Acceleration X, Y, and Z values from the bno055
uint16_t linearAccelX;
uint16_t linearAccelY;
uint16_t linearAccelZ;
bno055.getLinearAccel(linearAccelX, linearAccelY, linearAccelZ);

bno055.getLinearAccel(linearAccelX, linearAccelY, linearAccelZ);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Linear Acceleration x: %d.%d", linearAccelX / 100, linearAccelX % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Linear Acceleration y: %d.%d", linearAccelY / 100, linearAccelY % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Linear Acceleration z: %d.%d", linearAccelZ / 100, linearAccelZ % 100);

log::LOGGER.log(log::Logger::LogLevel::INFO, "Linear Acceleration x: %d.%d", linearAccelX / 100, linearAccelX % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Linear Acceleration y: %d.%d", linearAccelY / 100, linearAccelY % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Linear Acceleration z: %d.%d", linearAccelZ / 100, linearAccelZ % 100);
// Retrieve the Accelerometer X, Y, and Z values from the bno055
uint16_t accelerometerX;
uint16_t accelerometerY;
uint16_t accelerometerZ;

// Retrieve the Accelerometer X, Y, and Z values from the bno055
uint16_t accelerometerX;
uint16_t accelerometerY;
uint16_t accelerometerZ;
bno055.getAccelerometer(accelerometerX, accelerometerY, accelerometerZ);

bno055.getAccelerometer(accelerometerX, accelerometerY, accelerometerZ);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Accelerometer x: %d.%d", accelerometerX / 100, accelerometerX % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Accelerometer y: %d.%d", accelerometerY / 100, accelerometerY % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Accelerometer z: %d.%d", accelerometerZ / 100, accelerometerZ % 100);

log::LOGGER.log(log::Logger::LogLevel::INFO, "Accelerometer x: %d.%d", accelerometerX / 100, accelerometerX % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Accelerometer y: %d.%d", accelerometerY / 100, accelerometerY % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Accelerometer z: %d.%d", accelerometerZ / 100, accelerometerZ % 100);
// Retrieve the Gravity X, Y, and Z values from the bno055
uint16_t gravityX;
uint16_t gravityY;
uint16_t gravityZ;

// Retrieve the Gravity X, Y, and Z values from the bno055
uint16_t gravityX;
uint16_t gravityY;
uint16_t gravityZ;
bno055.getGravity(gravityX, gravityY, gravityZ);

bno055.getGravity(gravityX, gravityY, gravityZ);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gravity x: %d.%d", gravityX / 100, gravityX % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gravity y: %d.%d", gravityY / 100, gravityY % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gravity z: %d.%d", gravityZ / 100, gravityZ % 100);

log::LOGGER.log(log::Logger::LogLevel::INFO, "Gravity x: %d.%d", gravityX / 100, gravityX % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gravity y: %d.%d", gravityY / 100, gravityY % 100);
log::LOGGER.log(log::Logger::LogLevel::INFO, "Gravity z: %d.%d", gravityZ / 100, gravityZ % 100);

EVT::core::time::wait(500);
EVT::core::time::wait(500);
}
}else{
log::LOGGER.log(log::Logger::LogLevel::INFO, "Setup of BNO055 failed.");
}


}