Shop OBEX P1 Docs P2 Docs Learn Events
Accelerometer Wrap Around — Parallax Forums

Accelerometer Wrap Around

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

Comments

  • Heater.Heater. Posts: 21,230
    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?
  • @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 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
  • kwinnkwinn Posts: 8,697
    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.
  • 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
  • kwinnkwinn Posts: 8,697
    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.
  • 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.
  • 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.
  • kwinnkwinn Posts: 8,697
    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.
Sign In or Register to comment.