maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Maemo 5 / Fremantle (https://talk.maemo.org/forumdisplay.php?f=40)
-   -   Python script for GPS location help appreciated (https://talk.maemo.org/showthread.php?t=52609)

jedi 2010-05-13 21:01

Python script for GPS location help appreciated
 
I've been using the example script from here:http://wiki.maemo.org/PyMaemo/Using_Location_API. It works fine - switching the GPS on momentarily and returning the long/lat of the GPS signal.

But the signal is a long way off - and I was under the impression that if the GPS was on for longer, there was more chance of the location being accurate.

So my question is, how could I adapt this script to make it wait 1 minute? or 5 minutes etc before returning the location? Would this make the location more accurate?

I'm code a bit in PHP but am finding Python a bit baffling...

Any help appreciated.
Thanks

Hukka 2010-05-14 07:21

Re: Python script for GPS location help appreciated
 
In the on_change function, do not call data.stop(). Then you will still get updates on the location. What I do is to compare the new location to the old one, and if it's close enough determine that the GPS has given as accurate location as it can and then stop.

jedi 2010-05-14 18:15

Re: Python script for GPS location help appreciated
 
Quote:

Originally Posted by Hukka (Post 657941)
In the on_change function, do not call data.stop(). Then you will still get updates on the location. What I do is to compare the new location to the old one, and if it's close enough determine that the GPS has given as accurate location as it can and then stop.

Hi Hukka

Thanks for your tantilizing clue :) I guess we're talking about this block of code:

Code:

def on_changed(device, data):
    if not device:
        return
    if device.fix:
        if device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
            print "%f %f" % device.fix[4:6]
            data.stop()

so can you help me and tell me how I can wait for a given amount of time before quitting? Or indeed, your idea of comparing the last result with current sounds good too.

I'm still finding Python mind boggling!

Thanks for your help
:)

jedi 2010-05-14 20:05

Re: Python script for GPS location help appreciated
 
ah, so I needed to add 'import time' at the top, then 'time.sleep(30)' before the 'print %f...' line to make it wait 30 seconds...

That *seems* to give me a more accurate reading...

i think..?

Hukka 2010-05-15 11:02

Re: Python script for GPS location help appreciated
 
Well, the problem with hard timed reads is that the gps might not give you the data. What I mean is that you want to wait for 30 seconds for an update, but the gps gives you one after 25 seconds and 90 seconds (it happens if the device isn't moving at all).

So instead you might need to just save all the updates to a variable and then have a separate thread counting the timeouts and stopping the data loop, because after running loop.run() you are fully in asynchronous mode (that is, you couldn't say loop.run() then wait(30) and then data.stop()).

But if the possibly longer timeouts don't matter to you (or you are constantly on the move) you could just save the current time when your program starts and then in "if device.fix" do "if datetime.datetime.now() > savedtime + datetime.timedelta(seconds=30)".

rcastberg 2010-05-15 12:06

Re: Python script for GPS location help appreciated
 
Alternativly check device.fix[6]
This gives eph, Horizontal position accuracy

And wait for this to fall below a specified number. (Not sure what is a sensible limit)

jedi 2010-05-17 21:16

Re: Python script for GPS location help appreciated
 
Cool - I think I've made progress - thanks Hukka/rcastberg :)

For anyone interested, here's my script. It waits for the horizontal accuracy to be less than 500 meters, then exits printing the latitude, longitude and the horizontal accuracy.

Code:

#!/usr/bin/python
import location
import gobject

def on_error(control, error, data):
    print "location error: %d... quitting" % error
    data.quit()

def on_changed(device, data):
    if not device:
        return

    #Uncomment line below to show progress...
    #print "Accuracy: ",device.fix[6]," Lat/Long: ",device.fix[4:6] 
 
    if not device.fix[6] == device.fix[6]:
        return

    if device.fix[6] > 50000:
        return

    if device.fix:
        if device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
            print device.fix[4]," ",device.fix[5]," ",device.fix[6]
            data.stop()

def on_stop(control, data):
    data.quit()

def start_location(data):
    data.start()
    return False

loop = gobject.MainLoop()
control = location.GPSDControl.get_default()
device = location.GPSDevice()
control.set_properties(preferred_method=location.METHOD_USER_SELECTED,
                      preferred_interval=location.INTERVAL_DEFAULT)

control.connect("error-verbose", on_error, loop)
device.connect("changed", on_changed, control)
control.connect("gpsd-stopped", on_stop, loop)

gobject.idle_add(start_location, control)

loop.run()

This is part of some remote tracking software I'm writing in case my phone gets stolen.

Next question - is it possible to stop the GPS icon appearing in the notification area?

Hukka 2010-05-24 09:39

Re: Python script for GPS location help appreciated
 
I certainly hope there is not. I don't want any apps draining the battery without me knowing about it.

jedi 2010-05-25 07:15

Re: Python script for GPS location help appreciated
 
Quote:

Originally Posted by Hukka (Post 672960)
I certainly hope there is not. I don't want any apps draining the battery without me knowing about it.

If you're the one that's stolen my phone, I don't care!

Anyone else with any constructive comments?

nicolai 2010-05-25 07:23

Re: Python script for GPS location help appreciated
 
Quote:

Originally Posted by jedi (Post 663098)
Next question - is it possible to stop the GPS icon appearing in the notification area?

You can try to find .desktop file for this statusmenu plugin
and move this file temporarily somewhere else.

nicolai


All times are GMT. The time now is 22:48.

vBulletin® Version 3.8.8