Quick Start

This guide will get you up and running with the BNO08x driver in just a few steps.

Prerequisites

Minimal Example

Here’s a complete working example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "bno08x.hpp"

// 1. Implement the communication interface
class MyComm : public bno08x::CommInterface<MyComm> {
public:
    bool Open() override {
        // Your I²C/SPI/UART initialization
        return true;
    }
    
    void Close() override {
        // Your cleanup code
    }
    
    int Write(const uint8_t* data, uint32_t length) override {
        // Your write implementation
        return length;
    }
    
    int Read(uint8_t* data, uint32_t length) override {
        // Your read implementation
        return length;
    }
    
    bool DataAvailable() override {
        // Check if data is available
        return true;
    }
    
    void Delay(uint32_t ms) override {
        // Your delay implementation
    }
    
    uint32_t GetTimeUs() override {
        // Return current time in microseconds
        return 0;
    }
};

// 2. Create instances
MyComm comm;
bno08x::BNO085 imu(comm);

// 3. Initialize
if (!imu.Begin()) {
    printf("Initialization failed\n");
    return;
}

// 4. Enable sensors
imu.EnableSensor(bno08x::BNO085Sensor::RotationVector, 10);  // 100 Hz
imu.EnableSensor(bno08x::BNO085Sensor::Accelerometer, 50); // 20 Hz

// 5. Set callback
imu.SetCallback([](const bno08x::SensorEvent& e) {
    if (e.sensor == bno08x::BNO085Sensor::RotationVector) {
        auto euler = e.toEuler();
        printf("Yaw: %.1f°, Pitch: %.1f°, Roll: %.1f°\n", 
               euler.yaw, euler.pitch, euler.roll);
    }
});

// 6. Update loop
while (true) {
    imu.Update();  // Call as often as possible
    delay(5);
}

Step-by-Step Explanation

Step 1: Include the Header

1
#include "bno08x.hpp"

This includes the main driver class and all necessary types.

Step 2: Implement the Communication Interface

You need to implement the CommInterface for your platform. See Platform Integration for detailed examples.

1
2
3
class MyComm : public bno08x::CommInterface<MyComm> {
    // Implement all required methods
};

Step 3: Create Driver Instance

1
2
MyComm comm;
bno08x::BNO085 imu(comm);

The constructor takes a reference to your communication interface implementation.

Step 4: Initialize

1
2
3
4
if (!imu.Begin()) {
    // Handle error
    return;
}

Step 5: Enable Sensors

1
imu.EnableSensor(bno08x::BNO085Sensor::RotationVector, 10);  // 100 Hz

The interval is in milliseconds. Use 0 for on-change sensors.

Step 6: Set Callback

1
2
3
imu.SetCallback([](const bno08x::SensorEvent& e) {
    // Handle sensor events
});

Step 7: Update Loop

1
2
3
4
while (true) {
    imu.Update();  // Must be called frequently
    delay(5);
}

Complete Example with Error Handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "bno08x.hpp"

class MyComm : public bno08x::CommInterface<MyComm> {
    // ... communication implementation
};

void app_main() {
    MyComm comm;
    bno08x::BNO085 imu(comm);
    
    // Initialize
    if (!imu.Begin()) {
        printf("Initialization failed\n");
        return;
    }
    
    // Enable sensors
    if (!imu.EnableSensor(bno08x::BNO085Sensor::RotationVector, 10)) {
        printf("Failed to enable rotation vector\n");
        return;
    }
    
    // Set callback
    imu.SetCallback([](const bno08x::SensorEvent& e) {
        if (e.sensor == bno08x::BNO085Sensor::RotationVector) {
            auto euler = e.toEuler();
            printf("Yaw: %.1f°\n", euler.yaw);
        }
    });
    
    // Main loop
    while (true) {
        imu.Update();
        vTaskDelay(pdMS_TO_TICKS(5));
    }
}

Expected Output

When running this example, you should see:

1
2
3
Yaw: 45.2°
Yaw: 45.3°
Yaw: 45.5°

Troubleshooting

If you encounter issues:

  • Compilation errors: Check that you’ve implemented all required methods in your communication interface
  • Initialization fails: Verify I²C/SPI connections and hardware setup
  • No data: Ensure Update() is called frequently
  • See: Troubleshooting for common issues

Next Steps


Navigation ⬅️ Installation | Next: Hardware Setup ➡️ | Back to Index