/* This program is an abridged version of Adafruit BNO055 rawdata.ino available after installing the Adafruit BNO055 library File→Examples→Adafruit BNO055→Raw Data Connections on Arduino Uno ========================================================================= SCL to analog 5 | SDA to analog 4 | VDD to 3.3V DC | GND to common ground */
#define BNO055_SAMPLERATE_DELAY_MS (100) // Delay between data requests
Adafruit_BNO055 bno = Adafruit_BNO055(); // Create sensor object bno based on Adafruit_BNO055 library
voidsetup(void) { Serial.begin(115200); // Begin serial port communication if(!bno.begin()) // Initialize sensor communication { Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); } delay(1000); bno.setExtCrystalUse(true); // Use the crystal on the development board }
voidloop(void) { imu::Quaternion quat = bno.getQuat(); // Request quaternion data from BNO055
Serial.print(quat.w(), 4); Serial.print("\t"); // Print quaternion w Serial.print(quat.x(), 4); Serial.print("\t"); // Print quaternion x Serial.print(quat.y(), 4); Serial.print("\t"); // Print quaternion y Serial.print(quat.z(), 4); Serial.println(); // Print quaternion z
delay(BNO055_SAMPLERATE_DELAY_MS); // Pause before capturing new data }
//---- Included Libraries ----// #include// I²C library #include// trig functions #include// Base library for sensors #include// BNO055 specific library #include// Vector, Matrix, and IMUMath library //#include // Standard Servo library #include// XLR8 servo library #include// XLR8 accelerated floating point math
#define BNO055_SAMPLERATE_DELAY_MS (50) // Set pause between samples
//---- Variable Declaration ----//
boolean debug = true; // true/false = extra/no information over serial
int rollPin = 9; // Digital pin for roll int yawPin = 10; // Digital pin for yaw int pitchPin = 11; // Digital pin for pitch
float roll, pitch, yaw; // Variable to hold roll, pitch, yaw information
Adafruit_BNO055 bno = Adafruit_BNO055(); // Use object bno to hold information
rollServo.attach(rollPin); // The rollServo is connected at rollPin pitchServo.attach(pitchPin); // The pitchServo is connected at pitchPin yawServo.attach(yawPin); // The yawServo is connected at yawPin
Serial.begin(115200); // Create serial connection at 115,000 Baud
if (!bno.begin()) // Attempt communication with sensor { Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); }
delay(100); // Wait 0.1 seconds to allow it to initialize bno.setExtCrystalUse(true); // Tell sensor to use external crystal }
if (debug) { // If debug is true, send information over serial Serial.print("Measured Euler Roll-Pitch-Yaw"); Serial.print("\t yaw: "); Serial.print(euler.x()); Serial.print("\t"); Serial.print("\t pitch: "); Serial.print(euler.z()); Serial.print("\t"); Serial.print("\t roll: "); Serial.print(euler.y()); Serial.println(); } /* Remap information from the sensor over the 0° - 180° range of the servo The Yaw values are between 0° to +360° The Roll values are between -90° and +90° The Pitch values are between -180° and +180° */ int servoYaw = map(euler.x(), 0, 360, 0, 180); int servoRoll = map(euler.y(), -90, 90, 0, 180); int servoPitch = map(euler.z(), -180, 180, 0, 180);
if (debug) { // If debug is true, send information over serial Serial.print("Measured Euler Roll-Pitch-Yaw"); Serial.print("\t Yaw Servo: "); Serial.print(servoYaw); Serial.print("\t"); Serial.print("\t Pitch Servo: "); Serial.print(servoPitch); Serial.print("\t"); Serial.print("\t Roll Servo: "); Serial.print(servoRoll); Serial.println(); } // If debug is true, send information over serial if (debug) { Serial.println("Calculated Servo Roll-Pitch-Yaw"); Serial.print("\t roll:"); Serial.print(servoRoll, DEC); Serial.print("\t"); Serial.print("\t pitch:"); Serial.print(servoPitch, DEC); Serial.print("\t"); Serial.print("\t yaw:"); Serial.print(servoYaw, DEC); Serial.println(); }
rollServo.write(servoRoll); // Send mapped value to rollServo pitchServo.write(servoPitch); // Send mapped value to rollServo yawServo.write(servoYaw); // Send mapped value to rollServo
delay(BNO055_SAMPLERATE_DELAY_MS); // Wait before rerunning loop }