Hi
This is my first time playing around with a 6DOF sensor. I am looking to determine the angle of the sensor along a single axis.
The below code did the job nicely but I notices that whenever the board is moves erratically/vigorously, it gives me strange angle values. I am not really sure why this is, do all MPU6050's do this, is the sensor faulty or is my code causing the issue?
Any help would be much appreciated!
//Written by Ahmet Burkay KIRNIK
//TR_CapaFenLisesi
//Measure Angle with a MPU-6050(GY-521)
#include<Wire.h>
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
boolean standStatus;
int standDuration;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
standStatus = false;
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
//Serial.print("AngleX= ");
//Serial.println(x);
//Serial.print("AngleY= ");
//Serial.print(y);
//Serial.print("AngleZ= ");
//Serial.println(z);
//Serial.println(standStatus);
/**/
if (((x > 90) && (x < 220)) && (standStatus == false)){
Serial.print(x);
Serial.print(" upside down ");
standStatus = true;
Serial.println(standStatus);
}
else if (((x < 20) || (x > 300)) && (standStatus == true)){
Serial.print(x);
Serial.print(" right way up ");
standStatus = false;
Serial.println(standStatus);
}
}