class and script creation

This commit is contained in:
2024-05-08 14:09:03 +02:00
parent 21f1a844ba
commit 6a20c8f16d
4 changed files with 96 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
#include "SensorManager.h"
SensorManager sensorManager;
void setup() {
Wire.setClockStretchLimit(150000L); // Default stretch limit 150mS
Serial.begin(9600);
Serial.println("startup");
delay(10000);
sensorManager.sensorSetup();
}
void loop() {
SensorManager::Rotation rotation = sensorManager.readLoop();
Serial.print(rotation.x);
Serial.print(" ");
Serial.print(rotation.y);
Serial.print(" ");
Serial.print(rotation.z);
Serial.println();
}

View File

@@ -0,0 +1,50 @@
#include "SensorManager.h"
#include <Wire.h>
SensorManager::SensorManager() {}
void SensorManager::sensorSetup() {
Serial.println();
Serial.println("BNO080 Read Example");
delay(1000); // Wait for BNO to boot
Wire.begin();
if (myIMU.begin() == false) {
delay(1000);
Serial.println(".");
}
Wire.setClock(400000); //Increase I2C data rate to 400kHz
myIMU.calibrateAll(); //Turn on cal for Accel, Gyro, and Mag
myIMU.enableGameRotationVector(100);
myIMU.enableMagnetometer(100); //Send data update every 100ms
myIMU.saveCalibration(); //Saves the current dynamic calibration data (DCD) to memory
myIMU.requestCalibrationStatus(); //Sends command to get the latest calibration status
if (myIMU.calibrationComplete() == true) {
Serial.println("Calibration data successfully stored");
}
Serial.println(F("magnetometer rotation enabled"));
}
SensorManager::Rotation SensorManager::readLoop() {
if (myIMU.dataAvailable() == true) {
float x = myIMU.getMagX();
float y = myIMU.getMagY();
float z = myIMU.getMagZ();
Rotation rotation = { x, y, z };
return rotation;
}
else {
float x = myIMU.getMagX();
float y = myIMU.getMagY();
float z = myIMU.getMagZ();
Rotation rotation = { x, y, z };
return rotation;
}
}

View File

@@ -0,0 +1,21 @@
#ifndef SensorManager_h
#define SensorManager_h
#include "Arduino.h"
#include "SparkFun_BNO080_Arduino_Library.h"
class SensorManager {
public:
SensorManager();
void sensorSetup();
struct Rotation {
float x;
float y;
float z;
};
Rotation readLoop();
private:
BNO080 myIMU;
};
#endif

View File

@@ -0,0 +1 @@
#include "SensorManager.h"