Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中如何从gps检测速度?_Python_Raspberry Pi_Gps - Fatal编程技术网

在python中如何从gps检测速度?

在python中如何从gps检测速度?,python,raspberry-pi,gps,Python,Raspberry Pi,Gps,我正在使用一个gps芯片,我的raspberry pi使用python进行编码,我想从gps获取速度,并使用1秒的循环将其转换为英里/小时 如何用相当精确的数据实现这一点?我目前正在使用以下代码: From gps import * import time import threading import math import RPi.GPIO as GPIO ## Import GPIO library import time ## Import 'time' library. Allows

我正在使用一个gps芯片,我的raspberry pi使用python进行编码,我想从gps获取速度,并使用1秒的循环将其转换为英里/小时

如何用相当精确的数据实现这一点?我目前正在使用以下代码:

From gps import *
import time
import threading
import math
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import 'time' library. Allows us to use 'sleep'

GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(40,GPIO.OUT) ## Setup GPIO Pin 40 to OUT

class GpsController(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.running = False

    def run(self):
        self.running = True
        while self.running:
            # grab EACH set of gpsd info to clear the buffer
            self.gpsd.next()

    def stopController(self):
        self.running = False

    @property
    def fix(self):
        return self.gpsd.fix

    @property
    def utc(self):
        return self.gpsd.utc

    @property
    def satellites(self):
        return self.gpsd.satellites

mph = 15

if __name__ == '__main__':
    # create the controller
    gpsc = GpsController() 
    try:
        # start controller
        gpsc.start()
        while True:
            if gpsc.fix.speed < mph :
                print "speed is under 15 mph",gpsc.fix.speed
                print mph
                GPIO.output(40,GPIO.HIGH)
                time.sleep(1)
                GPIO.output(40,GPIO.LOW)
                time.sleep(1)
                #GPIO.output(40,True)
                #time.sleep(.5)
                #GPIO.output(40,False)
                #time.sleep(.10)

            elif gpsc.fix.speed > mph :
                print "speed (m/s) ",gpsc.fix.speed
                #    GPIO.cleanup()

            else:
                print "fine"
                #GPIO.cleanup()

            #print "latitude ", gpsc.fix.laif 
            #print "longitude ", gpsc.fix.longitude
            #print "time utc ", gpsc.utc, " + ", gpsc.fix.time
            #print "altitude (m)", gpsc.fix.altitude
            #print "eps ", gpsc.fix.eps
            #print "epx ", gpsc.fix.epx
            #print "epv ", gpsc.fix.epv
            #print "ept ", gpsc.gpsd.fix.ept
            #print "speed (m/s) ", gpsc.fix.speed
            #print "climb ", gpsc.fix.climb
            #print "track ", gpsc.fix.track
            #print "mode ", gpsc.fix.mode
            #print "sats ", gpsc.satellites
            time.sleep(1)

#Error
    #except:
    #   print "Unexpected error:", sys.exc_info()[0]
    #  raise

    #Ctrl C
    except KeyboardInterrupt:
        print "User cancelled"

    finally:
        print "Stopping gps controller"
        gpsc.stopController()
        #wait for the thread to finish
        gpsc.join()

    print "Done"

GPIO.cleanup()
来自gps导入的
*
导入时间
导入线程
输入数学
将RPi.GPIO作为GPIO导入##导入GPIO库
导入时间##导入“时间”库。允许我们使用“睡眠”
GPIO.setmode(GPIO.BOARD)##使用板引脚编号
GPIO.setup(40,GPIO.OUT)##将GPIO引脚40设置为OUT
类GpsController(threading.Thread):
定义初始化(自):
threading.Thread.\uuuuu init\uuuuuu(自)
self.gpsd=gps(mode=WATCH#u ENABLE)#启动信息流
self.running=False
def运行(自):
self.running=True
自运行时:
#抓取每组gpsd信息以清除缓冲区
self.gpsd.next()
def停止控制器(自身):
self.running=False
@财产
def修复(自我):
返回self.gpsd.fix
@财产
def utc(自我):
返回self.gpsd.utc
@财产
def卫星(自身):
返回self.gpsd.satellites
英里小时=15
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#创建控制器
gpsc=GpsController()
尝试:
#启动控制器
gpsc.start()
尽管如此:
如果gpsc.fix.speed英里/小时:
打印“速度(m/s)”,gpsc.fix.speed
#GPIO.cleanup()
其他:
打印“罚款”
#GPIO.cleanup()
#打印“纬度”,gpsc.fix.laif
#打印“经度”,gpsc.fix.longitude
#打印“utc时间”,gpsc.utc,“+”,gpsc.fix.time
#打印“高度(m)”,gpsc.fix.altime
#打印“eps”,gpsc.fix.eps
#打印“epx”,gpsc.fix.epx
#打印“epv”,gpsc.fix.epv
#打印“ept”,gpsc.gpsd.fix.ept
#打印“速度(m/s)”,gpsc.fix.speed
#打印“爬升”,gpsc.fix.gramp
#打印“track”,gpsc.fix.track
#打印“模式”,gpsc.fix.mode
#打印“卫星”,gpsc.satellites
时间。睡眠(1)
#错误
#除:
#打印“意外错误:”,sys.exc_info()[0]
#提高
#Ctrl C
除键盘中断外:
打印“用户已取消”
最后:
打印“停止gps控制器”
gpsc.stopController()
#等待线程完成
gpsc.join()
打印“完成”
GPIO.cleanup()

当前代码给了我一个读数,但它似乎约为15英里/小时不同步。

您需要从芯片中获取GPS坐标,将其转换为,并从前一秒中减去当前秒。两个点之间的距离就是你在一秒钟内行驶的距离。低速时会产生噪音,因此您可能需要进行某种过滤。

请提供一些起始位置(例如您尝试过的代码、芯片的输出),您是在寻找m/s还是mph?似乎任何地方都没有转换,但您的数据以m/s为单位,您正在将其与mph进行比较。我想转换为mph您从fix获得的数字。速度将以m/s为单位,您必须乘以2.23694才能转换为mph。