Hello!
for the last week i had been trying to get my arduino gyroscope ( MPU-6050 ) to work, all attempts to do it on my self have failed, so i decided to follow a tutorial, to be precise this one now i have followed it and did everything as said, but the gyro still outputs values that are random. Shaking, moving or rotating the device has no effect on the output ( the output values range from ~-1,3 to ~1,3. I pinned the code below, if anyone would just take a look and see if i did everything correctly i would really appreciate it.
Dmajster
#define accSens 0 // 0 = 2g, 1 = 4g, 2 = 8g, 3 = 16g
#define gyroSens 0 // 0 = 250rad/s, 1 = 500rad/s, 2 1000rad/s, 3 = 2000rad/s
#include<Wire.h>
#include<math.h>
const int MPU=0x68; // I2C address of the MPU-6050 int16_t
int gyroState = 0;
int calibrationStep = 0;
int temp;
float axCal = 0 , ayCal = 0, azCal = 0;
float gxCal = 0, gyCal = 0, gzCal = 0;
float axRaw = 0, ayRaw = 0, azRaw = 0;
float gxRaw = 0, gyRaw = 0, gzRaw = 0;
float xTilt = 0, yTilt = 0, zTilt = 0;
float pitch = 0, roll = 0, yaw = 0;
float samplingRate = 0.04;
float timeConstant = 1.00;
float alpha = 0;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
switch(gyroState){
case 0:
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); //EDIT1
if (calibrationStep < 10){
axCal += Wire.read()<<8|Wire.read();
ayCal += Wire.read()<<8|Wire.read();
azCal += Wire.read()<<8|Wire.read();
temp = Wire.read()<<8|Wire.read();
gxCal += Wire.read()<<8|Wire.read();
gyCal += Wire.read()<<8|Wire.read();
gzCal += Wire.read()<<8|Wire.read();
calibrationStep ++;
}
else{
axCal /= 10;
ayCal /= 10;
azCal /= 10;
gxCal /= 10;
gyCal /= 10;
gzCal /= 10;
gyroState = 1;
}
break;
case 1:
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); //EDIT1
axRaw = float(Wire.read()<<8|Wire.read()) - axCal;
ayRaw = float(Wire.read()<<8|Wire.read()) - ayCal;
azRaw = float(Wire.read()<<8|Wire.read()) - azCal;
temp = float(Wire.read()<<8|Wire.read()) /340.00 + 36.53;
gxRaw = float(Wire.read()<<8|Wire.read()) - gxCal;
gyRaw = float(Wire.read()<<8|Wire.read()) - gyCal;
gzRaw = float(Wire.read()<<8|Wire.read()) - gzCal;
xTilt = atan( axRaw / sqrt( pow( gyRaw, 2 ) + pow( gzRaw, 2 ) ) );
yTilt = atan( ayRaw / sqrt( pow( gxRaw, 2 ) + pow( gzRaw, 2 ) ) );
zTilt = atan( sqrt( pow( axRaw, 2 ) + pow( ayRaw, 2 ) ) / azRaw );
alpha = timeConstant / (timeConstant + samplingRate);
pitch = alpha * ( pitch + (gxRaw/131) * millis()) + (1 - alpha) * (xTilt);
roll = alpha * ( roll + (gyRaw/131) * millis()) + (1 - alpha) * (yTilt);
yaw = alpha * (yaw + (gzRaw/131) * millis()) + (1 - alpha) * (zTilt);
Serial.print("\n>>Pitch is: ");Serial.print(pitch);
Serial.print(", Roll is: ");Serial.print(roll);
Serial.print(", Yaw is: ");Serial.print(yaw);
break;
}
}