Thursday, July 28, 2011

Python scripting to access Android GPS

One of my long-term ideas has been to record myself playing Ultimate frisbee. I think visualizing a player's movement around the field (as well as the analytics you could get from the movement) would be awesome.

I thought my old G1 would be the perfect device for the job. It is a small, self contained unit with a GPS. Using SL4A it even runs Python- what more could I want? I could poll the GPS for the player position and record their lat/lon at every tick. From that, it would be easy enough to make a graph of movement.

Android is very straightforward with getting Location data from the GPS, and the Python scripting layer is easy to use.

This code sets up our GPS listener and records the first event it spits back at us. From there, it's easy to record a bunch of events, parse out the lat/lon, and plot the points with matplotlib.

That's a lat/lon graph of me running around a block of houses- ouch! I made some zig-zags (the waves on the top) and even backtracked (the big bump on the bottom) for a half block, but the frequency is nowhere near being good enough to be useful. Even though it is easy to access, the G1's GPS just doesn't update fast enough (with enough precision) to record a player's movement.

I had a small amount of hope that the accelerometer would be viable option, but as this stackoverflow answer (and the accompanying video) shows, this isn't possible. The error introduced by going from acceleration to position is too great for navigation the navigation I want to do.

Even though I failed in my planned goal, the code to record the GPS is very nice. It's available on github.

As for next steps, I asked around and got some good ideas. First (and easiest), I plan to try to use Network location data along with the GPS data on the phone to try get a more accurate location. I don't think it will improve the update frequency, but it may. Long term, the best way to get my desired data (track someone running around a field) probably rests with short range triangulation. Something like putting radio transmitters on the sides of the field and determine the position of a device in the middle, or perhaps using cameras (Kinect style) to triangulate a person's position visually and with high frequency.


  1. Thnaks for sharing this code snippet!!

  2. Hi just a quick one about your Blog is it on Blogger?
    If so how did you insert the gist file?

    1. Since I can't put HTML tags in the comments here, I put the relevant snippet in a pastebin.

      Go into the HTML view and add a link to your gist like that. If you need more guidance let me know!