gpt4 book ai didi

Python GPS 模块 : Reading latest GPS Data

转载 作者:太空狗 更新时间:2023-10-29 19:33:53 24 4
gpt4 key购买 nike

我一直在尝试使用 python 中的标准 GPS (gps.py) 模块 2.6。这应该充当客户端并从在 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(),它会从队列底部(从 session 开始时)给我缓冲值,而不是最新的 Gps 读数。有没有办法使用这个库获取更新的值?在某种程度上,寻求最新值(value)的流?

有没有人有使用这个库轮询 gps 并获得我正在寻找的值的代码示例?

这是我正在尝试做的:

  1. 开始 session
  2. 等待用户在我的代码中调用 gps_poll() 方法
  3. 在此方法中读取最新的 TPV(时间位置速度)报告并返回经纬度
  4. 返回等待用户调用 gps_poll()

最佳答案

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

如果您不轮询 session 对象,最终您的 UART FIFO 将填满并且您将无法获得任何新值。

考虑为此使用线程,不要等待用户调用 gps_poll(),您应该进行轮询,当用户想要一个新值时,他们使用返回 current_value 的“get_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()

关于Python GPS 模块 : Reading latest GPS Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6146131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com