我正在使用raspberry pi开发一个应用程序,将其用作位置跟踪器。我通过USB接口使用neo-6m GPS来获取覆盆子pi中的位置数据。为此我设置GPSD指向USB串行设备。 (见instructions)
以下python脚本轮询GPSD守护程序并通过Unix域套接字将位置数据发送到父进程:
#!/usr/bin/python
import os
from gps import *
from time import *
import time
import threading
import socket
import math
t1, t2 = socket.socketpair()
gpsd = None #seting the global variable
host = "localhost"
port = 8888
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE,host=host,port=port) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
print("%%%%%%%GPS RUN")
global gpsd
while self.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
time.sleep(3)
def poll_gps(socket):
print('GPS POLL')
gpsp = GpsPoller() # create the thread
gpsp.start()
try:
while True:
#It may take a second or two to get good data
#print gpsd.fix.latitude,', ',gpsd.fix.longitude,' Time: ',gpsd.utc
#print 'latitude ' , gpsd.fix.latitude
#print 'longitude ' , gpsd.fix.longitude
if gpsd.fix.latitude is not None and gpsd.fix.longitude is not None and not math.isnan(gpsd.fix.longitude ) and not math.isnan(gpsd.fix.latitude ) and gpsd.fix.latitude != 0.0 and gpsd.fix.longitude != 0.0 :
gps_str='{0:.8f},{1:.8f}'.format(gpsd.fix.latitude, gpsd.fix.longitude)
dict_str="{'type' : 'gps', 'value' : '"+gps_str+"'}"
dict_str_new="{'type' : 'gps', 'value' : '"+str(gpsd.fix.latitude)+","+str(gpsd.fix.longitude)+"'}"
print("GPS123_OLD" +dict_str)
print("GPS123_NEW" +dict_str_new)
if socket == None:
print("GOT GPS VALUE")
sys.exit(0)
socket.sendall(dict_str+'\n')
else:
print('%%%%%%%%%%%%%%%%%%GPS reading returnd None!')
time.sleep(3) #set to whatever
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
print "\nKilling Thread..."
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
print "Done.\nExiting."
if __name__ == '__main__':
poll_gps(None)
当我运行此代码并将覆盆子pi设置移动到1公里外时,我可以在控制台中看到新的不同的lat-long值。但是当我绘制这些值时,我发现所有这些位置都在起点。即所有值都围绕相同的起始点聚集。我无法看到1公里外的一条通道。
为了检查问题是否与我的代码有关,我在raspberry pi中安装了“navit”软件并将其指向GPSD守护程序。当我使用navit绘制我的路径时,它在地图中正确显示了我的进度。所以我得出结论,问题出在我的代码上。
如果我的代码存在任何问题,有人可以看看并告诉我
答案 0 :(得分:0)
我想出了这个问题。在“GpsPoller”类的“run”方法中,我正在调用sleep调用。似乎这种延迟使得python客户端在检索由GPSD守护进程排队的位置数据时落后于GPSD恶魔。我刚刚取消了睡眠,我开始及时找到正确的位置。
def run(self):
print("%%%%%%%GPS RUN")
global gpsd
while self.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
#time.sleep(3) REMOVE/COMMENT THIS LINE TO GET CORRECT GPS VALUES