The personal wiki of ...

Using Phone GPS Services

Now we are going explore the GPS functionality offered by your phone. When doing an engineering project, one part of the process might involve doing preliminary testing of functionality you might be going to use in the system you are building.

In this exercise you "write" a program that collects location data, stores the data and exports it for analysis. Finally you get to reflect on what you have learnt about the limits of the GPS functionality and how it might be used to write programs and build systems that can be of benefit to people.

GPS is also interesting because it uses a combination of satellites and wireless networks to work out where you are located as quickly as possible (Roma and Elf would be good to talk to about this).

This exercise also also involves a walk down to Te Puni village, onto Boyd Wilson field and back up to the University via the Recreation Centre. We're hoping for fine weather.

Latitude and longitude

Here is the code, have a read through (we'll also go through the basic ideas on the screen).

# this tells python which extra libraries to use
import android, time, os

# start talking to the phone
droid = android.Android()

# start the phone's GPS system
droid.startLocating()

# need to wait until GPS turns on, will only work outside and can take several minutes the first time 
# lets try and read a location
loc = droid.readLocation()

# loc[1].has_key('gps') checks if any gps data is available
# if it is available we want to skip the while loop 
# otherwise we will continue reading and checking
while not loc[1].has_key('gps') :
    # start while loop

    # tell the user that the GPS is trying to lock on to satellites (might take sometime)
    print("Waiting for GPS lock")
    # sleep for 2 seconds
    time.sleep(2)

    # try and read our location again to see if it is available now
    loc = droid.readLocation()

    # end while loop and go back to start

# now create the file that is going to hold the data (replace any files with the same name)
f = open('/sdcard/sl4a/scripts/logfile.txt','w')

# write the latitude and longitude header required by the graphing program
f.write('altitude, latitude, longitude\n')

# close the file (this makes sure what we have written is saved)
f.close()

# we also want to count how many times we take a reading 
count = 0

# now we'll loop until the user closes the application, reading latitude and longitude
# "while True" means repeat everything indented under the while forever 
# this means you will have to manually stop the program 
while True:
     # start while loop

    # read the location
    loc = droid.readLocation()

    # increment number of times we have gone around this loop
    count = count + 1

    # retrieve the data that you need and convert to a string
    # the [gps][altitude] means please tell me the altitude associated with the gps reading
    # the [gps][latitude] means please tell me the latitude associated with the gps reading
    # the [gps][longitude] means please tell me the latitude associated with the gps reading
    alt = str(loc.result['gps']['altitude'])
    lat = str(loc.result['gps']['latitude'])
    lon = str(loc.result['gps']['longitude'])

    # open the file for adding new data
    f = open('/sdcard/sl4a/scripts/logfile.txt','a')

    # write the data to the file
    f.write(alt + ',' + lat + ',' + lon + '\n')

    # print on the console
    print(str(count) + ',' + alt + ',' + lat + ',' + lon + '\n')

    # close the file (this makes sure what we have written is saved)
    f.close()

    # sleep for 5 seconds
    time.sleep(5)

    # end while loop and go back to start

Loading the Script

You are all very new to Python so we don't expect you to understand all of the code above but we hope you get the general idea. Neither are we going to make you type it all in. Instead use the QR Code below to add the script to your phone.

To download the script to your phone:

  1. Launch SL4A or return the scripts list.
  2. Press the Menu button.
  3. Tap Add.
  4. Tap Scan Barcode.
  5. Scan the barcode and SL4A will add the script to your list.

cyber-gps-py.png

Before You Run the Code

Check your GPS is turned on (Settings -> Location & security make sure that Use wireless networks (agree when it says that Google will collect anonymous data) and Use GPS satellites are both turned on.

When you have done this, the settings page should look like this.

gps-settings.png

Doing the Experiment

You can start the script inside but I won't be able to lock onto the satellites until you are outside. I recommend starting it inside and going outside with a mentor for a good walk around the campus so you have something to graph later.

Tap the script (it is called cyber-gps.py).

Start the script by choosing the Run (with Terminal) option (highlighted in red below).

sl4a quickaction.png

We run it in a terminal because the script will print out useful information that lets you know if it has managed to lock on and get GPS information.

You should see something like the following appear in the terminal window. You will see many more "Waiting" messages than shown below!

Waiting for GPS lock
Waiting for GPS lock
Waiting for GPS lock
Waiting for GPS lock
Waiting for GPS lock
1,134.699996948,-41.28982212,174.76778432
2,134.699996948,-41.28982212,174.76778432
3,134.699996948,-41.28982212,174.76778432
4,134.699996948,-41.28982212,174.76778432
5,134.699996948,-41.28982212,174.76778432
6,134.699996948,-41.28982212,174.76778432
7,134.699996948,-41.28982212,174.76778432
8,134.699996948,-41.28982212,174.76778432
9,134.699996948,-41.28982212,174.76778432

When you come back inside you will need to stop the running script. You can do this in a couple of ways. The easiest is to tap the BACK button on the phone.

back-button.jpg

This will bring up the dialog below, choose YES to close the program (the data won't be lost because it is being stored on the phone's SDCARD).

kill-process.png

Troubleshooting

Not seeing any of the above? Check that the GPS is working by running the application (its on the home page of the phone) called GPS Status. It displays a compass and other sensor data. This is what it looks like. I am running this inside so it doesn't display latitude and longitude, instead it has a spinning circle. Wait until it stops spinning and displays a lattitude and longitude.

img-gps-page.png

Graphing the Data on a Map

We will use a website called GPS Visualizer (shown below) to graph the latitude and longitude data that you have collected.

img-gps-page.png

1. You need to copy across the data that you have collected. Connect the phone to the PC using the USB cable, on the PC open the terminal window and enter the following command to copy the data file across.

getlog

2. Open the data file in gedit by entering the following command:

gedit logfile.txt

3. Select and copy the contents of the data file into the clipboard.

4. In a web browser, go to http://www.gpsvisualizer.com/map_input?form=google. Find the box shown below on the GPS visualizer webpage. Paste your data into the box. You should replace what is there (i.e. the "name, description, latitude, longitude" line).

box.png

5. Graph your path by clicking on Draw your map. Compare your path with the other people who went out with you in your group.

Below is the results of us running the program.

tracks.png

6. Now we want you to generate a line diagram showing altitude changes. For example, here is a graph we made of altitude changes recorded in a walk.

altitude.png

We suggest you use a spreadsheet and create a line graph. These machines don't run Excel but we have an open source (i.e. free) spreadsheet that has many of the same features called OpenOffice. Start it by typing the command below into the PC's Terminal window.

scalc

To import the data just copy and paste from the text file, this will start an import process.

After that you can find Chart making under Insert -> Chart.

Reflection

See if you can answer the following questions. Discuss them with the mentors:

  1. Why does it take a while to fix on the GPS?
  2. How often does the latitude and longitude change in logfile.txt? What do you think makes it change at the rate you find there?
  3. Why do we bother making the phone sleep between doing GPS readings?
  4. Does the path look like the one that you expected? How accurate do you feel the GPS is? What could cause this?
  5. How far did you walk? Can you work this out from just the start and end points? How could you do a more accurate measurement?
  6. Can you think of a computer application that could make use of location information? This could be a game or something to help people?
I Attachment Action SizeSorted ascending Date Who Comment
back-button.jpgjpg back-button.jpg manage 1 K 19 Jan 2012 - 22:08 Main.ian  
cyber-gps-py.pngpng cyber-gps-py.png manage 5 K 19 Jan 2012 - 19:57 Main.ian  
box.pngpng box.png manage 6 K 19 Jan 2012 - 16:02 Main.ian  
kill-process.pngpng kill-process.png manage 6 K 19 Jan 2012 - 22:07 Main.ian  
altitude.pngpng altitude.png manage 16 K 19 Jan 2012 - 21:53 Main.ian  
sl4a_quickaction.pngpng sl4a_quickaction.png manage 16 K 19 Jan 2012 - 21:25 Main.ian  
gps-settings.pngpng gps-settings.png manage 55 K 19 Jan 2012 - 16:44 Main.ian  
gps-track.pngpng gps-track.png manage 131 K 19 Jan 2012 - 16:56 Main.ian  
img-gps-page.pngpng img-gps-page.png manage 182 K 19 Jan 2012 - 15:30 Main.ian  
tracks.pngpng tracks.png manage 817 K 19 Jan 2012 - 21:43 Main.ian  
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 19 Jan 2012 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated