Complementary filter with the MPU6050

HELP!!!!!

I think that my MPU-6050 is wrong... when it outputs the values flicker between two, even though I am vigorously shaking the IMU.
Here is some output values using just the MPU-6050 code from arduino playground:

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: 747, 512, 12545
temperature: 24.459 degrees Celsius
gyro x,y,z : 520, 4557, 396,

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: -5374, 2, 305
temperature: 37.212 degrees Celsius
gyro x,y,z : 2050, -13039, -29695,

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: 747, 512, 12545
temperature: 24.459 degrees Celsius
gyro x,y,z : 520, 4557, 396,

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: -5374, 2, 305
temperature: 37.212 degrees Celsius
gyro x,y,z : 2050, -13039, -29695,

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: 747, 512, 12545
temperature: 24.459 degrees Celsius
gyro x,y,z : 520, 4557, 396,

Can anybody help?

stratjnd:
I tried the complementary filter using the code that you provided. It looked quite good when I ran it but every time I moved the IMU around, the number increased and then stayed there when the IMU was still. Every time I moved the IMU, the number increased but would not go down. Can anybody help??

Thanks!
StratJnd

Please share your full code you used so we can check for possible errors.

stratjnd:
HELP!!!!!

I think that my MPU-6050 is wrong... when it outputs the values flicker between two, even though I am vigorously shaking the IMU. Here is some output values using just the MPU-6050 code from arduino playground:

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: 747, 512, 12545
temperature: 24.459 degrees Celsius
gyro x,y,z : 520, 4557, 396,

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: -5374, 2, 305
temperature: 37.212 degrees Celsius
gyro x,y,z : 2050, -13039, -29695,
....

It looks similar to an issue I had with one of my chips, it would return random values for both accelerometer and gyroscope regardless of being moved or not. Temperature however was returned just fine. Turned out it was a bad chip, replacing it fixed my issue.
But i'm not sure this is your issue since you state in the previous post that moving the mpu6050 did cause of change in angle in your possibly 'broken' complimentary filter.

Which MPU6050 breakout board are you using?
Are you supplying the correct voltage to your MPU6050? (depends on which breakout board you have, some have a voltage regulator which allows for 5V VCC, some others do not in which case you need to power it from 3.3V otherwise your MPU6050 will be damaged)
Are you always getting the same repeating values or do they change every time you run the sketch?
Are you still getting these repeating values when you change the MPU6050 address (0x68) to something not present on your I2C bus?
When you comment out the line "MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0);" in the setup routine does the sketch now return 0 for all the accelerometer and gyro axis?

Hi, I am using MPU6050 for self balancing unicycle. I am using single axis of sensor. I am getting my tilt angle from following code.

double accXangle=(atan2(AcY,AcZ)+PI)RAD_TO_DEG;
// Convert gyro raw vlues to degrees!
double gyroXrate=(double)GyX/131.0;
// Complementary Filter!
double compAngleX = (0.97 * (compAngleX + (gyroXrate * dt))) + (0.03 * accXangle);
//Serial.println("AcX = ");
float a=31.142
(compAngleX-5.35);
float ang=a-90;
return ang;

But the problem is that due to sudden movements of sensor, it gives random values which have nothing to do with sensor movement. For example when sensor is suddenly moved in positive direction of angle measurement, i gives -ve values for angle which then effect on pwm and thus direction of motor.

Can any one tell me where i am going wrong or why this is happening.

Always post all your code, using code tags ("<>" button).

where i am going wrong or why this is happening.

You are assuming that the accelerometer readings can be translated into the angle that gravity makes with respect to the Z axis. This works only when the accelerometer is not being subjected to other forces, and the gravity vector is in the Y-Z plane.

The following two lines are probably wrong. From the second line, it appears that "a" is supposed to be in degrees, but it is not.

float a=31.142*(compAngleX-5.35);
float ang=a-90;

I have been working on the MPU6050 Complementary filter as well. i have managed to get very stable values except for Roll angle which seems to drift due to gyro and takes time to return back to 0. does anybody know the reason and solution for this.

I have attached the graph after plotting the values on Excel

racquemis:
Yeah, you can just initialize the variables used for the complimentary filter to zero. It will automatically settle down to the correct angles.If your sampling fast enough you might not even notice it.

No, you don't need to add a line like.

Last_Filter_Angle = Filter_Angle

The complimentary filter already takes care of that.
Have another look at the code example i gave

float pitch=0;

float pitchAcc;
float P_CompCoeff= 0.98;
void ComplementaryFilter(int ax,int ay,int az,int gy,int gz) {
  long squaresum=(long)ayay+(long)azaz;
  pitch+=((-gy/32.8f)*(delta_t/1000000.0f));
  pitchAcc =atan(ax/sqrt(squaresum))RAD_TO_DEG;
  pitch =P_CompCoeff
pitch + (1.0f-P_CompCoeff)*pitchAcc;
}



First, notice that *pitch* is declared outside the filter function. It will keep it's value even after the filter has been executed. After the first filter execution *pitch* will contain your current Angle. However, when the filter is executed for the second time *pitch* actually becomes your Previous Angle. (pitch+= in line 6 of the code above)

Does this clear things up a bit? Let me know how it goes.

after applying this code I have got this error:

a function-definition is not allowed here before '{' token
expected ',' or '...' before '.' token

Here is my code:
void loop()
{
float pitch = 0;
float pitchAcc;
float coef = 0.98;
void ComplementaryFilter (int accel.readX_G(),int accel.readY_G(), int accel.readZ_G(), int gyro.readY_DegPerSec(),int gyro.readZ_DegPerSec())
long squaresum = (long)accel.readY_G()accel.readY_G() + (long)accel.readZ_G()accel.readZ_G();
pitch += ((-gyro.readY_DegPerSec())
dt);
pitchAcc = atan(accel.readX_G()/sqrt(squaresum))
180 / M_PI;
pitch = coef*pitch + (1.0-coef)*pitchAcc;

// вывод угловой скорости относительно оси Y
Serial.print(pitch);
Serial.println("");
delay(300);
}

You cannot embed one function definition inside another.
Move all the code from and including "void Complimentary" out of loop()

Hello, I am trying to build a four wheel drive robot and i want it to follow straight path. The robot is a prototype and i intend to drive it on rough surfaces (like offroads). I am thinking of using mpu 6050 and complimentary filter to fuse gyroscope and accelerometer to get accurate heading and compare it with reference (i.e 0 degree ) and accordingly supply power to the motors.
I also read a bit about the Jeff Rowberg library on mpu 6050 and I think the DMP6 example of the library gives accurate angles. Can i use it to get angles and then just get the difference between them and reference and then control motors.

I am using arduino uno.
I am a little new to arduino.
Will it work or will i need to do something more?

And one more thing.
Since my robot will be driving on pretty much flat surafces, i think i'll need to keep an eye on only one angle.
Am i right?