Active Topics

 


Reply
Thread Tools
jedi's Avatar
Posts: 1,411 | Thanked: 1,330 times | Joined on Jan 2010 @ Tatooine
#1
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
 
Posts: 13 | Thanked: 9 times | Joined on Dec 2009 @ Espoo, Finland
#2
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's Avatar
Posts: 1,411 | Thanked: 1,330 times | Joined on Jan 2010 @ Tatooine
#3
Originally Posted by Hukka View Post
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's Avatar
Posts: 1,411 | Thanked: 1,330 times | Joined on Jan 2010 @ Tatooine
#4
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..?
 
Posts: 13 | Thanked: 9 times | Joined on Dec 2009 @ Espoo, Finland
#5
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)".
 

The Following User Says Thank You to Hukka For This Useful Post:
Posts: 20 | Thanked: 16 times | Joined on Jan 2010 @ Norway
#6
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)
 

The Following User Says Thank You to rcastberg For This Useful Post:
jedi's Avatar
Posts: 1,411 | Thanked: 1,330 times | Joined on Jan 2010 @ Tatooine
#7
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?
 
Posts: 13 | Thanked: 9 times | Joined on Dec 2009 @ Espoo, Finland
#8
I certainly hope there is not. I don't want any apps draining the battery without me knowing about it.
 
jedi's Avatar
Posts: 1,411 | Thanked: 1,330 times | Joined on Jan 2010 @ Tatooine
#9
Originally Posted by Hukka View Post
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's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#10
Originally Posted by jedi View Post
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
 

The Following User Says Thank You to nicolai For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 20:12.