From 364e24e59cfccb68fc92a1624d26589696ea5f1b Mon Sep 17 00:00:00 2001 From: padeler Date: Tue, 21 Mar 2017 23:48:22 +0200 Subject: [PATCH] LP-483 Updates example scripts, code cleanup --- python/examples/__init__.py | 0 python/examples/example.py | 8 +-- python/examples/example_readlog.py | 94 ++++------------------------ python/examples/example_tuning.py | 13 ++-- python/librepilot/uavtalk/uavtalk.py | 6 +- 5 files changed, 25 insertions(+), 96 deletions(-) create mode 100644 python/examples/__init__.py diff --git a/python/examples/__init__.py b/python/examples/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/python/examples/example.py b/python/examples/example.py index 3fc708512..a22769f02 100644 --- a/python/examples/example.py +++ b/python/examples/example.py @@ -91,7 +91,7 @@ class UavtalkDemo(): def showAttitudeViaObserver(self): print "Request fast periodic updates for AttitudeState" - self.objMan.AttitudeState.metadata.telemetryUpdateMode.value = UAVMetaDataObject.UpdateMode.PERIODIC + self.objMan.AttitudeState.metadata.telemetryUpdateMode = UAVMetaDataObject.UpdateMode.PERIODIC self.objMan.AttitudeState.metadata.telemetryUpdatePeriod.value = 50 self.objMan.AttitudeState.metadata.updated() @@ -104,7 +104,7 @@ class UavtalkDemo(): def showAttitudeViaWait(self): print "Request fast periodic updates for AttitudeState" self.objMan.AttitudeState.metadata.telemetryUpdateMode = UAVMetaDataObject.UpdateMode.PERIODIC - self.objMan.AttitudeState.metadata.telemetryUpdatePeriod = 50 + self.objMan.AttitudeState.metadata.telemetryUpdatePeriod.value = 50 self.objMan.AttitudeState.metadata.updated() while True: @@ -146,10 +146,8 @@ class UavtalkDemo(): def driveServo(self): print "Taking control of self.actuatorCmd" - self.objMan.ActuatorCommand.metadata.access.value = UAVMetaDataObject.Access.READONLY + self.objMan.ActuatorCommand.metadata.access = UAVMetaDataObject.Access.READONLY self.objMan.ActuatorCommand.metadata.updated() - self.objMan.ManualControlCommand.metadata.access.value = UAVMetaDataObject.Access.READONLY - self.objMan.ManualControlCommand.metadata.updated() while True: self.objMan.ActuatorCommand.Channel.value[0] = 1000 diff --git a/python/examples/example_readlog.py b/python/examples/example_readlog.py index 70b5b9fcf..56bbad901 100644 --- a/python/examples/example_readlog.py +++ b/python/examples/example_readlog.py @@ -36,12 +36,13 @@ from librepilot.uavtalk.uavobject import * from librepilot.uavtalk.uavtalk import * from librepilot.uavtalk.objectManager import * from librepilot.uavtalk.connectionManager import * - - +from example import UavtalkDemo + + def _hex02(value): return "%02X" % value -class UavtalkDemo(): +class UavtalkLogDemo(UavtalkDemo): def __init__(self): self.nbUpdates = 0 @@ -50,7 +51,7 @@ class UavtalkDemo(): self.objMan = None self.connMan = None - def setup(self, port, filename): + def setup(self, filename): print "Opening File \"%s\"" % filename file = open(filename,"rb") if file == None: @@ -58,86 +59,13 @@ class UavtalkDemo(): print "Creating UavTalk" self.uavTalk = UavTalk(None, filename) - + print "Starting ObjectManager" self.objMan = ObjManager(self.uavTalk) self.objMan.importDefinitions() - + print "Starting UavTalk" self.uavTalk.start() - - def stop(self): - if self.uavTalk: - print "Stopping UavTalk" - self.uavTalk.stop() - - def showAttitudeViaObserver(self): - print "Request fast periodic updates for AttitudeActual" - self.objMan.AttitudeActual.metadata.telemetryUpdateMode.value = UAVMetaDataObject.UpdateMode.PERIODIC - self.objMan.AttitudeActual.metadata.telemetryUpdatePeriod.value = 50 - self.objMan.AttitudeActual.metadata.updated() - - print "Install Observer for AttitudeActual updates\n" - self.objMan.regObjectObserver(self.objMan.AttitudeActual, self, "_onAttitudeUpdate") - # Spin until we get interrupted - while True: - time.sleep(1) - - def showAttitudeViaWait(self): - print "Request fast periodic updates for AttitudeActual" - self.objMan.AttitudeActual.metadata.telemetryUpdateMode.value = UAVMetaDataObject.UpdateMode.PERIODIC - self.objMan.AttitudeActual.metadata.telemetryUpdatePeriod.value = 50 - self.objMan.AttitudeActual.metadata.updated() - - while True: - self.objMan.AttitudeActual.waitUpdate() - self._onAttitudeUpdate(self.objMan.AttitudeActual) - - def showAttitudeViaGet(self): - while True: - self.objMan.AttitudeActual.getUpdate() - self._onAttitudeUpdate(self.objMan.AttitudeActual) - - def _onAttitudeUpdate(self, args): - self.nbUpdates += 1 - - now = time.time() - if now-self.lastRateCalc > 1: - self.updateRate = self.nbUpdates/(now-self.lastRateCalc) - self.lastRateCalc = now - self.nbUpdates = 0 - - if self.nbUpdates & 1: - dot = "." - else: - dot= " " - - print " %s Rate: %02.1f Hz " % (dot, self.updateRate), - - roll = self.objMan.AttitudeActual.Roll.value - print "Roll: %-4d " % roll, - i = roll/90 - if i<-1: i=-1 - if i>1: i= 1 - i = int((i+1)*15) - print "-"*i+"*"+"-"*(30-i)+" \r", - - def driveServo(self): - print "Taking control of self.actuatorCmd" - self.objMan.ActuatorCommand.metadata.access.value = UAVMetaDataObject.Access.READONLY - self.objMan.ActuatorCommand.metadata.updated() - self.objMan.ManualControlCommand.metadata.access.value = UAVMetaDataObject.Access.READONLY - self.objMan.ManualControlCommand.metadata.updated() - - while True: - self.objMan.ActuatorCommand.Channel.value[0] = 1000 - self.objMan.ActuatorCommand.updated() - time.sleep(1) - - self.objMan.ActuatorCommand.Channel.value[0] = 2000 - self.objMan.ActuatorCommand.updated() - time.sleep(1) - def printUsage(): appName = os.path.basename(sys.argv[0]) @@ -164,10 +92,12 @@ if __name__ == '__main__': logging.basicConfig(level=logging.INFO) try: - demo = UavtalkDemo() - demo.setup(None, filename) - + demo = UavtalkLogDemo() + demo.setup(filename) + while True: + demo.objMan.AttitudeState.waitUpdate() + demo._onAttitudeUpdate(demo.objMan.AttitudeState) except KeyboardInterrupt: pass diff --git a/python/examples/example_tuning.py b/python/examples/example_tuning.py index ff3d166a4..39f562f43 100644 --- a/python/examples/example_tuning.py +++ b/python/examples/example_tuning.py @@ -63,24 +63,25 @@ if __name__ == '__main__': serPort = serial.Serial(_port, 57600, timeout=.5) uavTalk = UavTalk(serPort, None) + + print "Starting ObjectManager" objMan = ObjManager(uavTalk) objMan.importDefinitions() + + print "Starting UavTalk" uavTalk.start() - - import objectpersistence + print "Getting Current Settings:" for _ in range(2): # Try only twice to get the settings try: - time.sleep(10) + time.sleep(1) objMan.StabilizationSettingsBank1.getUpdate() except TimeoutException: print "Timeout" pass except KeyboardInterrupt: - os._exit(1) - else: - raise + exit(1) while True: while True: diff --git a/python/librepilot/uavtalk/uavtalk.py b/python/librepilot/uavtalk/uavtalk.py index 9bf78a73f..6fe304b50 100644 --- a/python/librepilot/uavtalk/uavtalk.py +++ b/python/librepilot/uavtalk/uavtalk.py @@ -122,12 +122,12 @@ class UavTalkRecThread(threading.Thread): self.stop = False if self.uavTalk.logFile is not None: - file = open(self.uavTalk.logFile) + file = open(self.uavTalk.logFile, "rb") while not self.stop: rx = file.read(1) if len(rx) > 0: rx = ord(rx) - self._consumeByte(rx) + self._consumeByte(rx) elif self.uavTalk.serial is not None: while not self.stop: rx = self.uavTalk.serial.read(1) @@ -233,7 +233,7 @@ class UavTalkRecThread(threading.Thread): elif self.rxState == UavTalkRecThread.STATE_CS: # by now, the CS has been added to the CRC calc, so now the CRC calc should read 0 if self.rxCrc.read() != 0: - logging.error("CRC ERROR") + logging.debug("CRC ERROR") else: self.uavTalk._onRecevedPacket(self.obj, self.rxType, self.rxData) self.rxState = UavTalkRecThread.STATE_SYNC