1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-28 06:24:10 +01:00

LP-483 Updates example scripts, code cleanup

This commit is contained in:
padeler 2017-03-21 23:48:22 +02:00
parent 8c0f29a4ea
commit 364e24e59c
5 changed files with 25 additions and 96 deletions

View File

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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