# Arduino Forum

## Using Arduino => Sensors => Topic started by: DragoSupremo on Jan 17, 2019, 10:21 pm

Title: Can i use an MPU-6050 upside-down?
Post by: DragoSupremo on Jan 17, 2019, 10:21 pm
I'm trying to use my MPU-6050 upside-down since it's easier to place on the circuit. However it returns weird values: when it is horizontal it returns 0 degrees as it should be but when i try to rotate it i get weird angles.

When it is placed head up it works perfectly, but isn't there a way to use it upside down?

Here's the code i use to get the angles and filter them:
Code: [Select]
`  //Acceleration Angles (0 = X, 1 = Y);  Acceleration_angle[0] = atan((Acc_rawY/16384.0)/sqrt(pow((Acc_rawX/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;  Acceleration_angle[1] = atan(-1*(Acc_rawX/16384.0)/sqrt(pow((Acc_rawY/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;  //Gyroscope Anlges  Gyro_angle[0] = Gyr_rawX/131.0;   Gyro_angle[1] = Gyr_rawY/131.0;  //Resulting Angles  Total_angle[0] = 0.98 *(Total_angle[0] + Gyro_angle[0]*elapsedTime) + 0.02*Acceleration_angle[0];`

Title: Re: Can i use an MPU-6050 upside-down?
Post by: jremington on Jan 17, 2019, 10:59 pm
The acceleration and gyro values are measured relative to the (X,Y,Z) coordinate axes defined by the chip itself. See the device data sheet for the details.

Your program needs a transformation that relates those chip axes to whatever real world (X, Y, Z) axes you are using, and that depends on how the IMU module is mounted.

Many people use a general transformation matrix.
Title: Re: Can i use an MPU-6050 upside-down?
Post by: DragoSupremo on Jan 18, 2019, 10:01 am
So for example in this case the Z axe is upside down and I should transform it with that axe inverted?

Sorry If I didn't understand correctly but I don't know much about transformation matrix
Title: Re: Can i use an MPU-6050 upside-down?
Post by: MarkT on Jan 18, 2019, 12:36 pm
You must use atan2, not atan, for angle calculations.
Title: Re: Can i use an MPU-6050 upside-down?
Post by: DragoSupremo on Jan 18, 2019, 03:54 pm
Oh i see! But in this case what should i use as second parameter of the function? Atan2 looks like it needs 2 parameters.
Title: Re: Can i use an MPU-6050 upside-down?
Post by: jremington on Jan 18, 2019, 05:37 pm
Quote
So for example in this case the Z axe is upside down and I should transform it with that axe inverted?
You can't invert the Z axis without changing the direction of X or Y as well.

The sensor axes are fixed with respect to each other, and you have to know which direction all of them are pointed to work out the transformation.

You need trigonometry and linear algebra to do this. If you are interested in learning about these topics, google "coordinate system transformations". One place to start is here (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-837-computer-graphics-fall-2012/lecture-notes/MIT6_837F12_Lec03.pdf).
Title: Re: Can i use an MPU-6050 upside-down?
Post by: MarkT on Jan 20, 2019, 12:31 am
Oh i see! But in this case what should i use as second parameter of the function? Atan2 looks like it needs 2 parameters.
Yes, that's why you need it, you are performing a catesian to polar coordinate transform and you need to
know the signs of both axes to get the angle right for all 360 degrees.

So for instance on the xy-plane you'd call atan2 (y, x) to get the mathematical angle.

Review coordinate transforms:

polar coords (r, theta).   x = r cos(theta), y = r sin(theta), thus tan(theta) = y/x.

But note that tan(theta) repeats every 180 degrees, not every 360, so it loses vital information.

cartesian  coords (x, y).   r = sqrt(x^2+y^2), theta = atan2(y, x)

Note that atan2 sees the signs of both x and y and can reconstruct the correct angle always,
and doesn't blow up if x = 0, which atan(y/x) would.