2017-10-05 106 views
0

我正在开发一个程序,在gps上达到速度时产生一个事件。目前我想修改的代码如下:gps事件和if语句

from gps import * 
import time 
import threading 
import math 

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 

if __name__ == '__main__': 
    # create the controller 
    gpsc = GpsController() 
    try: 
     # start controller 
     gpsc.start() 
     while True: 
      #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" 

我想加一个“如果”语句程序先来看一下速度传输和打印或启用的事件时速度达到一定数量。

我不确定何时何地添加“if”代码。

回答

0

在while循环中最有意义。没有规定事件是否应该发生一次或多次达到“一定数量”。

from gps import * 
import time 
import threading 
import math 

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 

if __name__ == '__main__': 
    # create the controller 
    gpsc = GpsController() 
    try: 
     # start controller 
     gpsc.start() 
     while True: 
      if gspc.fix.speed > event_trigger_amt: 
       print "speed (m/s) ", gpsc.fix.speed 
       doEvent() 
      time.sleep(1) 

    #Ctrl C 
    except KeyboardInterrupt: 
     print "User cancelled" 

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

我没有忘记提及它将是一个连续的循环,看着速度和触发事件。我将通过在此期间触发闪烁的事件来测试它。感谢您的帮助! – Byrne