1

I'm playing with the "AccelerometerDemo" in SDK7.0 and have a question regarding the calculation of "rotation" extracted from the XYZ data.

What I want to accomplish is have a virtual "pendulum" that points straight down. However, the thing is gyrating around and does not move as I would expect it.

Here is part of my code:

_accChannel.getLastAccelerationData(_xyz);
double roll = MathUtilities.atan2(X, Z) * 180.0 / Math.PI;

graphics.setColor(Color.BLACK);
int xcenter = 240;
int ycenter = 400;

int length = 220;

int newx1 = (int)(Math.cos( roll ) * (double)length) - xcenter;
int newy1 = (int)(Math.sin( roll ) * (double)length) - ycenter;

graphics.drawLine(xcenter, ycenter, newx1, newy1);

Any clues what I am doing wrong?

Thanks in advance!

user386093
  • 67
  • 8
  • 1
    the sensors naturally have "noise" in the readings. you should filter the readings down to an acceptable range to suppress the noise. – Marc B May 02 '13 at 20:22
  • Do you mean that if a roll "sample" is more than say 5 degrees to discard it? That way only readings that are close to the CURRENT angle are used? – user386093 May 02 '13 at 20:33
  • 1
    depends on what you mean by "gyrating around". if it's swinging around like it was in a hurricane-level breeze, that'd mean you're reading wrong or calculating wrong. if it's staying in relatively the same place but "shivering", then that's sensor noise. – Marc B May 02 '13 at 20:57
  • It's not "shivering" ~ more like a tornado driven by red-bull. . . . Although it appears to be "random", meaning not any apparent sequence. – user386093 May 02 '13 at 21:02
  • @user386093, I don't think the +/- 5 degrees would make a very good filter. It would throw out legitimate data, and still be very noisy under 5 degrees. Ideally, noisy sensor data is filtered with a Kalman Filter. That may be a bit much to implement for your purposes. Perhaps start with something simple like a moving average of the last X data points. – Nate May 02 '13 at 21:07
  • @Nate, thanks for taking the time to respond ~ I was hoping you would show up :-) I'll try averaging the last "roll" readings and see if that's what is needed. – user386093 May 02 '13 at 21:13
  • Ok, it seems like my problem is probably in the "math". I've changed my code and made it sequence between 0 and 359 in order to make my "line" simply rotate around the center-point. However, it doesn't work as expected but rather does the "gyrating" around as before. Any clues on what I have wrong with calculating the X and Y? – user386093 May 02 '13 at 21:53

1 Answers1

1

I see at least two problems:

1. Math.cos() and Math.sin() expect angle inputs in radians, not degrees. By using this code:

double roll = MathUtilities.atan2(X, Z) * 180.0 / Math.PI;

you have converted roll into degrees.

2. Secondly, you are subtracting the center coordinates from your vector coordinates. I believe you should be adding them, like this:

int newx1 = (int)(Math.cos( roll ) * (double)length) + xcenter;
int newy1 = (int)(Math.sin( roll ) * (double)length) + ycenter;
Nate
  • 31,017
  • 13
  • 83
  • 207