Active Topics

 



Notices


Reply
Thread Tools
Posts: 27 | Thanked: 17 times | Joined on Jun 2010
#1
Hello!

I've followed the guide on http://wiki.maemo.org/PyMaemo/Using_Location_API. The purpose is to every 3rd hour connect with the GPS, fetch 10 coordinates and send them to my server. If I lose my phone I can always go to my server and find the coordinates of the phone. The script works fine, but the problem occurs when I enter a closed building without GPS-reception, then my phone tries forever until it finds GPS-reception which ofcourse drains the battery. So I'd like to implement a function that cancels the update if the script takes longer than 5 minutes. How can this be made?

Thanks!

Code:
import location
import gobject


x=0
 
def on_error(control, error, data):
    print "location error: %d... quitting" % error
    data.quit()
 
def on_changed(device, data):
    global x

    if not device:
        return
    if device.fix:
        if device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
	    #http://maps.google.com/maps?q=62.62991%2C17.040443
            print "http://maps.google.com/maps?q=%f,%f" % device.fix[4:6]
            if x>8:
		#commented out to allow continuous loop for a reliable fix - press ctrl c to break the loop, or program your own way of exiting)
            	data.stop() 
            else:
		x=x+1
		
 
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_5S)
 
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()
 
Saturn's Avatar
Posts: 1,648 | Thanked: 2,122 times | Joined on Mar 2007 @ UNKLE's Never Never Land
#2
Just a rough explanation.

When you start the GPS register the time
Code:
global StartTime
StartTime = time.time()
Then in the function you listen for the GPS changes add a check
Code:
PastTime = time.time() - StartTime
if PastTime >= GPSTIMEOUT:
   data.stop()
ofcourse you need to import time.

I think this is all you need to make it work, this is also how the smscon_daemon does it.

Last edited by Saturn; 2011-05-30 at 20:55. Reason: better tabs in the code
 
Posts: 27 | Thanked: 17 times | Joined on Jun 2010
#3
Yes, I've tried similar like that. But the problem is that when the GPS doesn't find any coordinates (i.e. doesnt have any reception) it doesn't change and therefore that function doesn't update. So I need to put it somewhere else
 
Posts: 141 | Thanked: 41 times | Joined on Apr 2011 @ Ahmedabad, India
#4
a suggestion...
invoke a shell script that checks for the GPS update and run the shell script in background mode using & before calling this script put a counter (timer) and if it exceeds limit (say 5 mins) , kill the shell script using PID from ps -ef <shell script name> and the kill -9 command.
 
Posts: 214 | Thanked: 140 times | Joined on Aug 2010
#5
You know, you could use Google Latitude and ZapLoc to do this...

But anyway, in ZapLoc, I have two timers.

One timer in the main loop, which is the polling interval. Then, when that timeout calls its function, that function setts up a second timeout, which turns the GPS back off.

Note that timeouts using the gobject.timeout_add() function, if the function you call return True, it will loop and repeat your timeout, if it returns False, it will only happen once.

So roughtly, I have something like this:

Code:
def turn_off_gps(control):
     control.stop()
     return False

def start_gps(control):
    control.start()
    gobject.timeout_add(30000, turn_off_gps, control)
    return True

def init ():
   control =  ...
   gobject.timeout_add(120000, start_gps, control)
Roughly, this will start the GPS every two minutes, and keep it running for 30 seconds.

/Z
 
Reply


 
Forum Jump


All times are GMT. The time now is 23:02.