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

  • 9 Comments sorted by Date Added Votes
  • 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 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.
  • 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
  • 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.
  • 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
  • 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.
  • 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.
  • 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.
Sign In or Register to comment.