views:

232

answers:

2

Hi,

I want some help on UIAccelerometer class. I want to find some way to find out or distinguish when I wave iphone device from right-to-left and the left-to-right. I am getting x,y,z values as well as interval value. I am also getting velocity and distance calculated from normal physics rule.

Distance = (prevDist + sqrt(pow((prevx - acceleration.x), 2) + pow((prevy - acceleration.y), 2) + pow((prevz - acceleration.z), 2)))

Velocity = (Distance * timeintercal) { here distance means newdistance-prevdistance and timeinterval for that distance }

When I am moving iphone device right-to-left then left-to-right I am getting total distance traveld in both direction and velocity at regular intervals. Can you help me how I can find out that I am moving iphone device left-to-right or right-to-left?

Help would be appreciated. Thanks.

A: 

First, you have a few physics errors. Your distance is not a 'distance' variable because you are summing acceleration values from the accelerometer. Also, your velocity is actually a 'speed' variable because you are multiplying acceleration by a time interval.

What you really want to do is monitor acceleration.x for values that exceed a certain pre-determined magnitude. This magnitude should be low enough that the user does not have to swing the iPhone violenty, but high enough that simple movements will not cross your threshold. If acceleration.x is greater than 0 by your pre-determined threshold or more, then you are accelerating right. If acceleration.y is less than 0 by your pre-determined amount or more, then you are accelerating left.

It is far more difficult to determine the velocity of the phone over a period of time, because you will have to integrate the acceleration values continuously. The iPhone accelerometer is not incredibly precise and can be noisy, so after a small period of time your application will probably be convinced your phone is drifting in random directions. Also, remember that gravity will always show up in your acceleration values.

Tom S
acceleration vector * scalar = another vector. Speed is a scalar, and velocity is speed * direction, no?
dash-tom-bang
That is correct, but the original post has no vectors, nor does it multiply acceleration by a scalar.
Tom S
A: 

One thing to keep in mind is that gravity is always affecting the accelerometers, so that needs to be taken into account. Unfortunately, you can't count on straight forward methods to compute distances and actual velocity due to not knowing at any instant where "down" is.

To note when "swing left" or "swing right" happens, also consider that the movement is likely to be in an arc, and you'll get opposite acceleration when the swinging stops. To know that the swinging is underway, take a look at the "out" acceleration vector, e.g. the one that points away from the center of rotation. Using that you can distinguish between a "start moving" state and a "stop moving state" which otherwise will have equal-but-opposite acceleration values.

dash-tom-bang