Welcome to the Parallax Discussion Forums, sign-up to participate.

# Accelerometer Wrap Around

Posts: 126
Hello,

I am using a Parallax accelerometer module and would like to keep the outputted values between 0 and 180 deg such that when rotated upside down it reads something like: ...177, 178, 179, 0, 1, 2, 3,... I was able to get this to work for the gyroscope module using the fmod(gyro,PI) function, but no such luck with the accelerometer.

Currently, the accelerometer is outputting ...177, 178, 179,-179,-178,-177...

Could someone kindly show me a routine to output the values I want?

Many thanks,
David

• Posts: 21,213
I have no idea what you want.

An accelerometer does not output values in degrees.

I'm curious, why do you call yourself "amalfiCoast" and "David" at the same time?
• Posts: 2,394
@amalfiCoast,

comparing your numbers you showed as example you could substract the number you got from 179 if negative.

but like @Heater. said, why you want to do that?

An accelerometer measures the change of movement, not some fixed position. holding it still at any degree should read zero.

Enjoy!

Mike
I am just another Code Monkey.
A determined coder can write COBOL programs in any language. -- Author unknown.
Press any key to continue, any other key to quit

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this post are to be interpreted as described in RFC 2119.
• Posts: 126
I am doing following:
```accelAngle = atan2(ax,az);
gyroAngle = gyroAngle+gx*Ts;
gyroAngleMod = fmod(gyroAngle,PI);
```

Think of how an aircraft's attitude and heading reference system display is always been 0 and 180 deg. When I fuse the accelAngle and gyroAngle together using a complementary filter I need both angles to be "on the same page" so to speak. When the sensors are inverted for example, gyroAngleMod goes like 178, 179, 0, 1, 2, etc. while the accelAngle does 178, 179, -179, -178, which is not what I want. I hope this helps explain what I mean.

Heater,

My name is David but I chose amalfiCoast as my profile name because it's one of the most beautiful places I have ever visited.

Thanks,
David
• Posts: 8,178
From a strictly math perspective the simplest thing to do would be to multiply the accelerometer reading by a factor that results in a value between -180 and +180 and then add 180 to the result if it is negative. Could even use one factor for positive readings and another for negative ones if there was a difference between the + and - maximum readings.
In science there is no authority. There is only experiment.
Life is unpredictable. Eat dessert first.
• Posts: 126
Kwinn,

I'm still confused. Could you explain more what you mean by multiplying the accelerometer value by a factor that results in a value between -180 and +180?

Thank you.

David
• Posts: 8,178
Are you trying to determine the angle the accelerometer module is at in relation to the ground, and which module are you using (part number please)? If that is the case it will require a more complex calculation than a simple multiply.
In science there is no authority. There is only experiment.
Life is unpredictable. Eat dessert first.
• Posts: 126
Correct, I am trying to determine the angle of the accelerometer relative to ground. The accelerometer module I'm using is the MMA7455 3 axis sensor.
• Posts: 1,885
The angle from vertical is calculated as: atan2(sqrt(x^2+y^2), z) // in radians, given a vector (x,y,z)

atan2 is the standard function for converting cartesian to polar coords in 2 dimensions.
• Posts: 8,178
If you want both the x and y angle the math is a bit more involved. See:

https://en.wikipedia.org/wiki/Spherical_coordinate_system
https://en.wikipedia.org/wiki/Vector_fields_in_cylindrical_and_spherical_coordinates

for a more in depth explanation.
In science there is no authority. There is only experiment.
Life is unpredictable. Eat dessert first.