如何从 python 中的 gps 检测速度?
How can I detect a speed from a gps in python?
我在 raspberry pi 上使用 gps 芯片,使用 python 进行编码,我想从 gps 获取速度并使用 1 秒循环将其转换为 mph。
如何使用相当精确的数据来实现这一点?我目前正在使用以下代码:
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()
当前代码给了我一个读数,但它似乎不同步大约 15 mph。
您需要从您的芯片中获取 GPS 坐标,将其转换为 cartesian coordinates,并从前一秒中减去当前秒。两点之间的距离就是您在一秒钟内行驶的距离。这在低速时会很嘈杂,因此您可能需要进行某种过滤。
我在 raspberry pi 上使用 gps 芯片,使用 python 进行编码,我想从 gps 获取速度并使用 1 秒循环将其转换为 mph。
如何使用相当精确的数据来实现这一点?我目前正在使用以下代码:
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()
当前代码给了我一个读数,但它似乎不同步大约 15 mph。
您需要从您的芯片中获取 GPS 坐标,将其转换为 cartesian coordinates,并从前一秒中减去当前秒。两点之间的距离就是您在一秒钟内行驶的距离。这在低速时会很嘈杂,因此您可能需要进行某种过滤。