android - How to calculate distance based on phone acceleration -


i want build using android phone: http://www.youtube.com/watch?v=wot9mb5qqrs

i've built app sends sensor information via socket (still looking websocket implementation android). intend use information interact web app, example able move image based on phone movement. problem tried calculate distance based on accelerometer data results bad. wonder if me correct equation, first of all, possible this?

till i'm using following equations:

velocity = acceleration * time;

distance = velocity * time + (acceleration * time^2) / 2;

then translate distance meters per second pixels based on monitor screen resolution.

that's calculated javascript in browser every time receive sensor data, every ~80ms.

the basics simple. in analog world use continuous math is:

velocity = integrate(acceleration) distance = integrate(velocity) 

and in digital world easier, use discrete math integration becomes summation:

velocity = sum(acceleration) distance = sum(velocity) 

just keep adding values of acceleration read , distance.

the big problem on planet earth there constant acceleration downwards of approximately 10m/s/s due gravity. figuring out part of vector gravity hard part.

btw, gravity how accelerometers can detect tilt. it, unless can calculate tilt independently of accelerometers (for example of gyros) code measuring tilt instead of distance.


ha! realized last statement lot of iphone apps won't work in space :-p


additional answer:

based on "comment" posted op (as answer either below or above answer) looks need provide further explanation. implementation really-really simple people not familiar maths think must more complicated that. pseudocode follows:

// set distance 0 @ start-up: var distance_x = 0 var velocity_x = 0  function update_acceleration_x (acceleration_x) {     velocity_x = velocity_x + acceleration_x     distance_x = distance_x + velocity_x }  // use distance value read distance_x variable: function get_distance_x_and_reset () {     x = distance_x     distance_x = 0     return x } 

distance measured software first starts unless reset distance variable zero. accelerometer must read (preferably @ rate accelerometer measures forces) , values of velocity , distance updated accordingly. when want know distance starting point read distance variable.

several things: any amount of tilt, no matter how slight, add drift. meaning there small amount of constant acceleration in 1 direction or other unless angle of tilt tracked. nuclear submarines, equipped high precision accelerometers , gyros because gps doesn't work under water, need periodically surface , sync gps correct drift.

second, accelerometer measures force, not movement. kind of force measured. mentioned gravity measures bumps caused friction table, pulse heartbeat , breathing cause hand shake slightly, anything. news on long run these forces average out , formula still correct. in short run means reading going noisy. there lot of tricks people have come minimize noise using things weiner , kalman filters.

third, may have noticed, accelerometer reading not constant. don't mean values different each time read them, obvious, changes values in-between readings. every value miss affects our accuracy important read values possible. now, news in long run these errors caused missing values should average out caused jerky movements or vibrations , our formula again still correct. means again in short run adds noise our system. if use perdictive filter kalman filter should able account weaker filters may need help. 1 way of doing average out each acceleration reading previous reading. note must previous "real" reading, not previous averaged reading.

more accuracy goes realm of inertial measurement units (imu) , inertial guidance , lot of hairy vector , matrix maths. there open source projects doing though (less 10 years ago stuff strictly military since, know, submarines , cruise missiles use them).

these sparkfun articles have nice links @ bottom , reference code:

http://www.sparkfun.com/products/9268

http://www.sparkfun.com/products/8454

hope helps. , if else have links article may please comment.


example

of course if want real units need scale sample rate. example accelerating @ 9m/s/s 80ms means velocity (9m/s/s * 0.08s) = 0.72m/s. above pseudocode simplified assuming don't care units. final values still represent distance number it's number has little relation real world unit of measurement. can apply scaling function @ end calibrated pixel valus. anyway, here's example real-world units clarify what's happening:

given following acceleration readings: 9m/s/s 3m/s/s 0m/s/s 0m/s/s 0m/s/s -5m/s/s -7m/s/s  assuming 80ms sample rate can derive following velocities: 0.72m/s (what accelerating 9m/s 80ms) 0.96m/s 0.96m/s 0.96m/s 0.96m/s 0.56m/s 0m/s  can derive following distances: 57.6mm (what moving @ 0.72m/s 80ms) 134.4mm 211.2mm 288mm 364.8mm 409.6mm 

now, if take derived distances , reverse calculation per usual (v = (s2-s1)/t , a = (v2-v1)/t) should acceleration readings back.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -