Python GPS 模块:读取最新的 GPS 数据
问题描述
我一直在尝试使用 python 中的标准 GPS (gps.py) 模块 2.6.这应该充当客户端并从在 Ubuntu 中运行的 gpsd 读取 GPS 数据.
I have been trying to work with the standard GPS (gps.py) module in python 2.6. This is supposed to act as a client and read GPS Data from gpsd running in Ubuntu.
根据 GPSD 网页上关于客户端设计的文档(GPSD Client Howto),我应该能够使用以下代码(从示例中稍微修改)来获取最新的 GPS 读数(我主要感兴趣的是 lat long)
According to the documentation from GPSD webpage on client design (GPSD Client Howto), I should be able to use the following code (slightly modified from the example) for getting latest GPS Readings (lat long is what I am mainly interested in)
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(),它会给我来自队列底部的缓冲值(从会话开始时开始),而不是最新的 Gps 读数.有没有办法使用这个库获取更新的值?在某种程度上,寻找 Stream 的最新值?
If I repeatedly use the next() it gives me buffered values from the bottom of the queue (from when the session was started), and not the LATEST Gps reading. Is there a way to get more recent values using this library? In a Way, seek the Stream to the latest values?
有没有人有一个代码示例使用这个库来轮询 gps 并获得我正在寻找的值?
Has anyone got a code example using this library to poll the gps and get the value i am looking for ?
这是我想要做的:
- 开始会话
- 等待用户在我的代码中调用 gps_poll() 方法
- 在这个方法里面读取最新的TPV(Time Position Velocity)报告并返回lat long
- 返回等待用户调用 gps_poll()
解决方案
你需要做的是定期轮询'session.next()' - 这里的问题是你正在处理一个串行接口 - 你得到结果按照他们收到的顺序.由您来维护具有最新检索值的current_value".
What you need to do is regularly poll 'session.next()' - the issue here is that you're dealing with a serial interface - you get results in the order they were received. Its up to you to maintain a 'current_value' that has the latest retrieved value.
如果您不轮询会话对象,最终您的 UART FIFO 将填满,并且您无论如何都不会获得任何新值.
If you don't poll the session object, eventually your UART FIFO will fill up and you won't get any new values anyway.
考虑为此使用线程,不要等待用户调用 gps_poll(),您应该进行轮询,当用户想要一个新值时,他们使用返回 current_value 的 'get_current_value()'.
Consider using a thread for this, don't wait for the user to call gps_poll(), you should be polling and when the user wants a new value they use 'get_current_value()' which returns current_value.
在我的脑海中,它可能就像这样简单:
Off the top of my head it could be something as simple as this:
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()
相关文章