gps事件和if语句

2024-10-04 11:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个程序,以产生一个事件,每当一个速度达到全球定位系统。我当前试图修改的代码如下:

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”语句,以便在速度达到某个数字时首先查看传输和打印或启用事件的速度。你知道吗

我不确定何时何地添加“if”代码。你知道吗


Tags: theimportselftimemodedeffixrunning
1条回答
网友
1楼 · 发布于 2024-10-04 11:22:36

在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()

相关问题 更多 >