次のpythonスクリプトがあります:
#! /usr/bin/python
import os
from gps import *
from time import *
import time
import threading
import sys
gpsd = None #seting the global variable
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
while True:
print gpsd.fix.speed
time.sleep(1) ## <<<< THIS LINE HERE
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."
残念ながら、私はPythonがあまり得意ではありません。スクリプトは何らかの形でマルチスレッド化する必要があります(ただし、この質問の範囲ではおそらく問題になりません)。
私を当惑させるのはgpsd.next()
ラインです。正しく理解できれば、新しい GPS データが取得され、読み取る準備ができていることをスクリプトに伝えることになっていました。
ただし、 でwhile True
1 秒の一時停止を伴う無限ループを使用してデータを読み取りますtime.sleep(1)
。
ただし、これにより、同じデータが 2 回エコーされることがあります (センサーは最後の 1 秒間でデータを更新していません)。どういうわけかセンサーデータもスキップすると思います。
スクリプトを変更して、毎秒ではなく、センサーが新しいデータを報告するたびに現在の速度を出力することはできますか? データシートによると、毎秒 (1 Hz センサー) である必要がありますが、明らかに正確に 1 秒ではなく、ミリ秒単位で変化します。