maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   [Solved] Maemo GPS Location with Qt (https://talk.maemo.org/showthread.php?t=71093)

hezillion 2011-03-16 08:37

[Solved] Maemo GPS Location with Qt
 
Hi,

I am developing a GPS tracking application.
I have been following the location API on this page:
http://wiki.maemo.org/PyMaemo/Using_Location_API

The problem is, I couldn't run it in a simple Qt application

Code:

import location as gps
import logging

class myGPS:

    def __init__(self):
        self.control = gps.GPSDControl.get_default()
        self.device = gps.GPSDevice()
        self.control.set_properties(preferred_method=gps.METHOD_CWP,
                                    preferred_interval=gps.INTERVAL_5S)
       
        self.control.connect("error-verbose", self.onGPSError)
        self.device.connect("changed", self.onGPSData, self.control)
        self.control.connect("gpsd-stopped", self.onGPSStop)
   
    def start(self):
        logging.debug("GPS started")
        try:
            self.control.start()
        except Exception, err:
            logging.exception("start:")
   
    def stop(self):
        self.control.stop()
   
    def onGPSError(self, control, error, data):
        logging.debug("GPS Error: " + str(error))
   
    def onGPSData(self, device, data):
        if not device:
            return
       
        lat = device.fix[4]
        lng = device.fix[5]
        alt = device.fix[7]
        speed = float(device.fix[11])
        accuracy = float(device.fix[12])
       
        logging.info("Input: received")
        logging.info(lat, lng, alt, speed, accuracy)
   
    def onGPSStop(self, control):
        logging.debug("GPS Stopped")

When I trigger using start, the GPS symbol appears for a moment (blinking for 1~2s) then the application terminates.
In the log, I could only find "GPS started"

Any help is appreciated, Thank You.

hezillion 2011-03-19 04:40

Re: Maemo GPS Location with Qt
 
I have found the solution.
Here's the sample code for any of you who are still wondering.

Code:

import location as gps
import gobject

class myGPS():
   
    def __init__(self):
        try:
            self.boolLoop = False
            self.loop = gobject.MainLoop()
           
            self.control = gps.GPSDControl.get_default()
            self.device = gps.GPSDevice()
            self.control.set_properties(preferred_method=gps.METHOD_USER_SELECTED,
                                        preferred_interval=gps.INTERVAL_5S)
           
            self.control.connect("error-verbose", self.on_error, self.loop)
            self.device.connect("changed", self.on_changed, self.control)
            self.control.connect("gpsd-stopped", self.on_stop, self.loop)
            print "GPS object created"
        except Exception, err:
            print "Cannot Init:"
   
    def on_error(self, control, error, data):
        print ("location error: %d... quitting" % error)
        data.quit()
   
    def on_changed(self, device, data):
        if not device:
            return
        if device.fix:
            # get all the data needed from device.fix
            if device.fix[1] & gps.GPS_DEVICE_LATLONG_SET:
                print ("lat = %f, long = %f" % device.fix[4:6])
                print ("acc = %f" % device.fix[12])
   
    def on_stop(self, control, data):
        print "GPS stop"
   
    def start_location(self, data):
        data.start()
   
    def start(self):
        print "GPS start"
        if self.boolLoop:
            self.control.start()
        else:
            gobject.idle_add(self.start_location, self.control)
            self.loop.run()
            self.boolLoop = True
       
    def stop(self):
        self.control.stop()

On the other hand, I'm still not sure why I need to use gobject, without it it won't work.
To my understanding, the control.start runs the GPS thread by itself.
If someone could explain about gobject and its mainloop, it'll be appreciated.

Thanks


All times are GMT. The time now is 16:27.

vBulletin® Version 3.8.8