Script
Script
//===============================================
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
/* =========================================================================
NOTE: In addition to connection 3.3v, GND, SDA, and SCL, this sketch
depends on the MPU-6050's INT pin being connected to the Arduino's
external interrupt #0 pin. On the Arduino Uno and Mega 2560, this is
digital I/O pin 2.
* ========================================================================= */
// orientation/motion vars
Quaternion q1; // [w, x, y, z] quaternion container
Quaternion q2;
VectorInt16 aa; // [x, y, z] accel sensor measurements
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor
measurements
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor
measurements
VectorFloat gravity; // [x, y, z] gravity vector
float euler[3]; // [psi, theta, phi] Euler angle container
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and
gravity vector
// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has
gone high
void dmpDataReady() {
mpuInterrupt = true;
}
// ================================================================
// === INITIAL SETUP ===
// ================================================================
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
//*byte rc;
Wire.begin();
/*
/==================================================================================
=======================
// I2C address CONTROL
if (addr<16) Serial.print("0");
Serial.print(addr,HEX);
if (rc==0) {
Serial.print(" found!");
} else {
Serial.print(" "); Serial.print(rc); Serial.print(" ");
}
Serial.print( (addr%8)==7 ? "\n":" ");
}
Serial.println("\n-------------------------------\nPossible devices:");
Serial.println("\ndone");
}
===================================================================================
====================== */
// initialize device
Serial.println(F("Initializing I2C devices..."));
mpu1.initialize();
mpu2.initialize();
// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu1.testConnection() ? F("MPU6050 1 connection successful") :
F("MPU6050 1 connection failed"));
Serial.println(mpu2.testConnection() ? F("MPU6050 2 connection successful") :
F("MPU6050 2 connection failed"));
// set our DMP Ready flag so the main loop() function knows it's okay to use it
Serial.println(F("DMP ready! Waiting for first interrupt..."));
dmpReady = true;
// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================
void loop() {
// if programming failed, don't try to do anything
if (!dmpReady) return;
// check for overflow (this should never happen unless our code is too
inefficient)
if ((mpuIntStatus1 & 0x10) || (mpuIntStatus2 & 0x10) || fifoCount1 == 1024 ||
fifoCount2 == 1024) {
// reset so we can continue cleanly
mpu1.resetFIFO();
mpu2.resetFIFO();
Serial.println(F("FIFO overflow!"));
// otherwise, check for DMP data ready interrupt (this should happen
frequently)
}
else if ((mpuIntStatus1 & 0x02) || (mpuIntStatus2 & 0x02)) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount1 < packetSize1 && fifoCount2 < packetSize2)
{
fifoCount1 = mpu1.getFIFOCount();
fifoCount2 = mpu2.getFIFOCount();
Serial.print("\npacchetto incompleto\n");
}
#ifdef OUTPUT_READABLE_QUATERNION
// display quaternion values in easy matrix form: w x y z
mpu1.dmpGetQuaternion(&q1, fifoBuffer1);
Serial.print("a2 1 0 0 0 ");
Serial.print(q1.w);
Serial.print(" ");
Serial.print(q1.x);
Serial.print(" ");
Serial.print(q1.y);
Serial.print(" ");
Serial.print(q1.z);
Serial.print("\n");
mpu2.dmpGetQuaternion(&q2, fifoBuffer2);
Serial.print("2 0 0 0 ");
Serial.print(q2.w);
Serial.print(" ");
Serial.print(q2.x);
Serial.print(" ");
Serial.print(q2.y);
Serial.print(" ");
Serial.print(q2.z);
Serial.println("b");
Serial.print("\n");
#endif
}
}