Python GPS模块:读取最新的GPS数据

时间:2011-05-26 23:09:01

标签: python gps gpsd

我一直在尝试使用python 2.6中的标准 GPS(gps.py)模块。这应该充当客户端并从Ubuntu中运行的gpsd读取GPS数据。

根据GPSD网页上关于客户端设计的文档(GPSD Client Howto),我应该可以使用以下代码(稍微修改一下)获取最新的GPS读数(lat long是我主要是感兴趣的)

from gps import *
session = gps() # assuming gpsd running with default options on port 2947
session.stream(WATCH_ENABLE|WATCH_NEWSTYLE)
report = session.next()
print report

如果我重复使用next(),它会从队列底部(从会话开始时)开始给出缓冲值,而不是LATEST Gps读数。有没有办法使用这个库获取更新的值?在某种程度上,寻求流到最新的价值?

是否有人使用此库获取代码示例来轮询gps并获取我正在寻找的值?

以下是我要做的事情:

  1. 开始会话
  2. 等待用户在我的代码中调用gps_poll()方法
  3. 在此方法内阅读最新的TPV(时间位置速度)报告并返回lat long
  4. 返回等待用户拨打gps_poll()

4 个答案:

答案 0 :(得分:17)

您需要做的是定期轮询'session.next()' - 这里的问题是您正在处理串行接口 - 您按照收到的顺序获得结果。由您来维护具有最新检索值的'current_value'。

如果您不轮询会话对象,最终您的UART FIFO将填满,无论如何您都不会获得任何新值。

考虑使用一个线程,不要等待用户调用gps_poll(),你应该进行轮询,当用户想要一个新值时,他们使用'get_current_value()'返回current_value。

在我的脑海中,它可能就像这样简单:

import threading
import time
from gps import *

class GpsPoller(threading.Thread):

   def __init__(self):
       threading.Thread.__init__(self)
       self.session = gps(mode=WATCH_ENABLE)
       self.current_value = None

   def get_current_value(self):
       return self.current_value

   def run(self):
       try:
            while True:
                self.current_value = self.session.next()
                time.sleep(0.2) # tune this, you might not get values that quickly
       except StopIteration:
            pass

if __name__ == '__main__':

   gpsp = GpsPoller()
   gpsp.start()
   # gpsp now polls every .2 seconds for new data, storing it in self.current_value
   while 1:
       # In the main thread, every 5 seconds print the current value
       time.sleep(5)
       print gpsp.get_current_value() 

答案 1 :(得分:3)

对于使用现代版本的gpsd且仅在特定时间需要数据而不是流式传输的人来说,上述答案效率非常低且过于复杂。

大多数GPS每秒至少发送一次位置信息。据推测,由于许多基于GPS的应用程序需要实时更新,我见过的绝大多数gpsd客户端示例都使用上述方法来观看来自gpsd的流并接收实时更新(与gps发送的频率相同或多或少)它们)。

但是,如果(如在OP的情况下)你不需要需要流媒体信息,但只要它被请求就需要最后报告的位置(即通过用户交互或其他一些事件),有一种更高效,更简单的方法:让gpsd缓存最新的位置信息,并在需要时进行查询。

gpsd JSON protocol有一个?WATCH={"enable":true}请求,该请求会返回gpsd看到的最新GPS信息。您可以在gpsd会话开始时发送?POLL;消息,然后在需要时查询最新的位置信息,而不必迭代积压的gps消息,并不断读取新消息以避免使用完整缓冲区。它与import gpsd gpsd.connect() packet = gpsd.get_current() print(packet.position()) 。响应是一个JSON对象,包含gpsd从GPS中看到的最新信息。

如果您使用的是Python3,我发现的最简单方法是使用pypi上提供的gpsd-py3软件包。要连接到gpsd,请获取最新的位置信息,然后打印当前位置:

gpsd.get_current()

您可以在需要新位置信息时重复?POLL;调用,并且在幕后gpsd包将执行对{gpsd的gps调用并返回表示响应的对象。

使用内置的telnet模块执行此操作并不是非常简单,但是还有许多其他可用的Python客户端,而且对于任何可以使用的内容都很简单。使用$ telnet localhost 2947 Trying ::1... Connected to localhost. Escape character is '^]'. {"class":"VERSION","release":"3.16","rev":"3.16","proto_major":3,"proto_minor":11} ?WATCH={"enable":true} {"class":"DEVICES","devices":[{"class":"DEVICE","path":"/dev/pts/10","driver":"SiRF","activated":"2018-03-02T21:14:52.687Z","flags":1,"native":1,"bps":4800,"parity":"N","stopbits":1,"cycle":1.00}]} {"class":"WATCH","enable":true,"json":false,"nmea":false,"raw":0,"scaled":false,"timing":false,"split24":false,"pps":false} ?POLL; {"class":"POLL","time":"2018-03-02T21:14:54.873Z","active":1,"tpv":[{"class":"TPV","device":"/dev/pts/10","mode":3,"time":"2005-06-09T14:34:53.280Z","ept":0.005,"lat":46.498332203,"lon":7.567403907,"alt":1343.165,"epx":24.829,"epy":25.326,"epv":78.615,"track":10.3788,"speed":0.091,"climb":-0.085,"eps":50.65,"epc":157.23}],"gst":[{"class":"GST","device":"/dev/pts/10","time":"1970-01-01T00:00:00.000Z","rms":0.000,"major":0.000,"minor":0.000,"orient":0.000,"lat":0.000,"lon":0.000,"alt":0.000}],"sky":[{"class":"SKY","device":"/dev/pts/10","time":"2005-06-09T14:34:53.280Z","xdop":1.66,"ydop":1.69,"vdop":3.42,"tdop":3.05,"hdop":2.40,"gdop":5.15,"pdop":4.16,"satellites":[{"PRN":23,"el":6,"az":84,"ss":0,"used":false},{"PRN":28,"el":7,"az":160,"ss":0,"used":false},{"PRN":8,"el":66,"az":189,"ss":45,"used":true},{"PRN":29,"el":13,"az":273,"ss":0,"used":false},{"PRN":10,"el":51,"az":304,"ss":29,"used":true},{"PRN":4,"el":15,"az":199,"ss":36,"used":true},{"PRN":2,"el":34,"az":241,"ss":41,"used":true},{"PRN":27,"el":71,"az":76,"ss":42,"used":true}]}]} ?POLL; {"class":"POLL","time":"2018-03-02T21:14:58.856Z","active":1,"tpv":[{"class":"TPV","device":"/dev/pts/10","mode":3,"time":"2005-06-09T14:34:53.280Z","ept":0.005,"lat":46.498332203,"lon":7.567403907,"alt":1343.165,"epx":24.829,"epy":25.326,"epv":78.615,"track":10.3788,"speed":0.091,"climb":-0.085,"eps":50.65,"epc":157.23}],"gst":[{"class":"GST","device":"/dev/pts/10","time":"1970-01-01T00:00:00.000Z","rms":0.000,"major":0.000,"minor":0.000,"orient":0.000,"lat":0.000,"lon":0.000,"alt":0.000}],"sky":[{"class":"SKY","device":"/dev/pts/10","time":"2005-06-09T14:34:53.280Z","xdop":1.66,"ydop":1.69,"vdop":3.42,"tdop":3.05,"hdop":2.40,"gdop":5.15,"pdop":4.16,"satellites":[{"PRN":23,"el":6,"az":84,"ss":0,"used":false},{"PRN":28,"el":7,"az":160,"ss":0,"used":false},{"PRN":8,"el":66,"az":189,"ss":45,"used":true},{"PRN":29,"el":13,"az":273,"ss":0,"used":false},{"PRN":10,"el":51,"az":304,"ss":29,"used":true},{"PRN":4,"el":15,"az":199,"ss":36,"used":true},{"PRN":2,"el":34,"az":241,"ss":41,"used":true},{"PRN":27,"el":71,"az":76,"ss":42,"used":true}]}]}

执行套接字通信,包括此示例
{{1}}

答案 2 :(得分:2)

加我的两分钱。

无论出于何种原因,我的覆盆子pi将继续执行一个线程,我必须硬重置pi。

所以我结合了sysnthesizerpatel和我在Dan Mandel的博客here上找到的答案。

我的gps_poller类看起来像这样:

import os 
from gps import *
from time import *
import time 
import threading 

class GpsPoller(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.session = gps(mode=WATCH_ENABLE)
        self.current_value = None 
        self.running = True 

    def get_current_value(self):
        return self.current_value

    def run(self):
        try:
            while self.running:
                self.current_value = self.session.next() 
        except StopIteration:
            pass

使用中的代码如下所示:

from gps_poll import *

if __name__ == '__main__':
    gpsp = GpsPoller()
    try: 
        gpsp.start() 
        while True:
            os.system('clear')
            report = gpsp.get_current_value()
            # print report 
            try: 
                if report.keys()[0] == 'epx':
                    print report['lat']
                    print report['lon']           
                time.sleep(.5)
            except(AttributeError, KeyError):
                pass 
            time.sleep(0.5)

    except(KeyboardInterrupt, SystemExit):
        print "\nKilling Thread.."
        gpsp.running = False 
        gpsp.join()

    print "Done.\nExiting." 

您还可以在此处找到代码:HereHere

答案 3 :(得分:0)

我知道这是一个旧线程,但只是为了让大家理解,您也可以为此使用 pyembedded python 库。

pip install pyembedded

from pyembedded.gps_module.gps import GPS
import time    

gps = GPS(port='COM3', baud_rate=9600)

while True:
    print(gps.get_lat_long())
    time.sleep(1)

https://pypi.org/project/pyembedded/