#include "Wire.h" // This library allows you to communicate with I2C devices.
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
float accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
float gyro_x, gyro_y, gyro_z; // variables for gyro raw data
float temperature; // variables for temperature data
float accAngleX,accAngleY,previousTime,currentTime,elapsedTime;
float roll_moment,pitch_moment,yaw_moment;
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
}
void loop() {
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers
// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
accelerometer_x = (Wire.read()<<8 | Wire.read())/ 16384.0;; // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = (Wire.read()<<8 | Wire.read())/ 16384.0;; // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = (Wire.read()<<8 | Wire.read())/ 16384.0;; // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
gyro_x = (Wire.read()<<8 | Wire.read())/ 131.0;; // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
gyro_y = (Wire.read()<<8 | Wire.read())/ 131.0;; // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
gyro_z = (Wire.read()<<8 | Wire.read())/131.0; // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
//calculate roll and pitch
accAngleX = (atan(accelerometer_y/sqrt(pow(accelerometer_x,2) + pow(accelerometer_z,2))) * 180 / PI);
accAngleY = (atan(-1 * accelerometer_x / sqrt(pow(accelerometer_y, 2) + pow(accelerometer_z, 2))) * 180 / PI) ;
//TIME
previousTime = currentTime;
currentTime = millis();
elapsedTime = (currentTime - previousTime) / 1000;
//GyroX = GyroX + 0.56; // GyroErrorX ~(-0.56)
//GyroY = GyroY - 2; // GyroErrorY ~(2)
//GyroZ = GyroZ + 0.79; // GyroErrorZ ~ (-0.8)
roll_moment=gyro_x;// deg/s
pitch_moment=gyro_y;// deg/s
yaw_moment=gyro_z;// deg/s
// print out data
Serial.print("aX = "); Serial.print(accelerometer_x);
Serial.print(" | aY = "); Serial.print(accelerometer_y);
Serial.print(" | aZ = "); Serial.print(accelerometer_z);
// the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53);
Serial.print(" | gX = "); Serial.print(gyro_x);
Serial.print(" | gY = "); Serial.print(gyro_y);
Serial.print(" | gZ = "); Serial.print(gyro_z);
Serial.print(" | yaw momrnt = "); Serial.print(yaw_moment);
Serial.print(" |roll moment = "); Serial.print(roll_moment);
Serial.print(" | pitch moment = "); Serial.print(pitch_moment);
Serial.println();
// delay
delay(1000);
}