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:
parent
8c0f29a4ea
commit
364e24e59c
0
python/examples/__init__.py
Normal file
0
python/examples/__init__.py
Normal file
@ -91,7 +91,7 @@ class UavtalkDemo():
|
|||||||
|
|
||||||
def showAttitudeViaObserver(self):
|
def showAttitudeViaObserver(self):
|
||||||
print "Request fast periodic updates for AttitudeState"
|
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.telemetryUpdatePeriod.value = 50
|
||||||
self.objMan.AttitudeState.metadata.updated()
|
self.objMan.AttitudeState.metadata.updated()
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ class UavtalkDemo():
|
|||||||
def showAttitudeViaWait(self):
|
def showAttitudeViaWait(self):
|
||||||
print "Request fast periodic updates for AttitudeState"
|
print "Request fast periodic updates for AttitudeState"
|
||||||
self.objMan.AttitudeState.metadata.telemetryUpdateMode = UAVMetaDataObject.UpdateMode.PERIODIC
|
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()
|
self.objMan.AttitudeState.metadata.updated()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@ -146,10 +146,8 @@ class UavtalkDemo():
|
|||||||
|
|
||||||
def driveServo(self):
|
def driveServo(self):
|
||||||
print "Taking control of self.actuatorCmd"
|
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.ActuatorCommand.metadata.updated()
|
||||||
self.objMan.ManualControlCommand.metadata.access.value = UAVMetaDataObject.Access.READONLY
|
|
||||||
self.objMan.ManualControlCommand.metadata.updated()
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
self.objMan.ActuatorCommand.Channel.value[0] = 1000
|
self.objMan.ActuatorCommand.Channel.value[0] = 1000
|
||||||
|
@ -36,12 +36,13 @@ from librepilot.uavtalk.uavobject import *
|
|||||||
from librepilot.uavtalk.uavtalk import *
|
from librepilot.uavtalk.uavtalk import *
|
||||||
from librepilot.uavtalk.objectManager import *
|
from librepilot.uavtalk.objectManager import *
|
||||||
from librepilot.uavtalk.connectionManager import *
|
from librepilot.uavtalk.connectionManager import *
|
||||||
|
from example import UavtalkDemo
|
||||||
|
|
||||||
|
|
||||||
def _hex02(value):
|
def _hex02(value):
|
||||||
return "%02X" % value
|
return "%02X" % value
|
||||||
|
|
||||||
class UavtalkDemo():
|
class UavtalkLogDemo(UavtalkDemo):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.nbUpdates = 0
|
self.nbUpdates = 0
|
||||||
@ -50,7 +51,7 @@ class UavtalkDemo():
|
|||||||
self.objMan = None
|
self.objMan = None
|
||||||
self.connMan = None
|
self.connMan = None
|
||||||
|
|
||||||
def setup(self, port, filename):
|
def setup(self, filename):
|
||||||
print "Opening File \"%s\"" % filename
|
print "Opening File \"%s\"" % filename
|
||||||
file = open(filename,"rb")
|
file = open(filename,"rb")
|
||||||
if file == None:
|
if file == None:
|
||||||
@ -58,86 +59,13 @@ class UavtalkDemo():
|
|||||||
|
|
||||||
print "Creating UavTalk"
|
print "Creating UavTalk"
|
||||||
self.uavTalk = UavTalk(None, filename)
|
self.uavTalk = UavTalk(None, filename)
|
||||||
|
|
||||||
print "Starting ObjectManager"
|
print "Starting ObjectManager"
|
||||||
self.objMan = ObjManager(self.uavTalk)
|
self.objMan = ObjManager(self.uavTalk)
|
||||||
self.objMan.importDefinitions()
|
self.objMan.importDefinitions()
|
||||||
|
|
||||||
print "Starting UavTalk"
|
print "Starting UavTalk"
|
||||||
self.uavTalk.start()
|
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():
|
def printUsage():
|
||||||
appName = os.path.basename(sys.argv[0])
|
appName = os.path.basename(sys.argv[0])
|
||||||
@ -164,10 +92,12 @@ if __name__ == '__main__':
|
|||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
demo = UavtalkDemo()
|
demo = UavtalkLogDemo()
|
||||||
demo.setup(None, filename)
|
demo.setup(filename)
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
demo.objMan.AttitudeState.waitUpdate()
|
||||||
|
demo._onAttitudeUpdate(demo.objMan.AttitudeState)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
@ -63,24 +63,25 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
serPort = serial.Serial(_port, 57600, timeout=.5)
|
serPort = serial.Serial(_port, 57600, timeout=.5)
|
||||||
uavTalk = UavTalk(serPort, None)
|
uavTalk = UavTalk(serPort, None)
|
||||||
|
|
||||||
|
print "Starting ObjectManager"
|
||||||
objMan = ObjManager(uavTalk)
|
objMan = ObjManager(uavTalk)
|
||||||
objMan.importDefinitions()
|
objMan.importDefinitions()
|
||||||
|
|
||||||
|
print "Starting UavTalk"
|
||||||
uavTalk.start()
|
uavTalk.start()
|
||||||
|
|
||||||
import objectpersistence
|
|
||||||
|
|
||||||
print "Getting Current Settings:"
|
print "Getting Current Settings:"
|
||||||
for _ in range(2): # Try only twice to get the settings
|
for _ in range(2): # Try only twice to get the settings
|
||||||
try:
|
try:
|
||||||
time.sleep(10)
|
time.sleep(1)
|
||||||
objMan.StabilizationSettingsBank1.getUpdate()
|
objMan.StabilizationSettingsBank1.getUpdate()
|
||||||
except TimeoutException:
|
except TimeoutException:
|
||||||
print "Timeout"
|
print "Timeout"
|
||||||
pass
|
pass
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
os._exit(1)
|
exit(1)
|
||||||
else:
|
|
||||||
raise
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
while True:
|
while True:
|
||||||
|
@ -122,12 +122,12 @@ class UavTalkRecThread(threading.Thread):
|
|||||||
self.stop = False
|
self.stop = False
|
||||||
|
|
||||||
if self.uavTalk.logFile is not None:
|
if self.uavTalk.logFile is not None:
|
||||||
file = open(self.uavTalk.logFile)
|
file = open(self.uavTalk.logFile, "rb")
|
||||||
while not self.stop:
|
while not self.stop:
|
||||||
rx = file.read(1)
|
rx = file.read(1)
|
||||||
if len(rx) > 0:
|
if len(rx) > 0:
|
||||||
rx = ord(rx)
|
rx = ord(rx)
|
||||||
self._consumeByte(rx)
|
self._consumeByte(rx)
|
||||||
elif self.uavTalk.serial is not None:
|
elif self.uavTalk.serial is not None:
|
||||||
while not self.stop:
|
while not self.stop:
|
||||||
rx = self.uavTalk.serial.read(1)
|
rx = self.uavTalk.serial.read(1)
|
||||||
@ -233,7 +233,7 @@ class UavTalkRecThread(threading.Thread):
|
|||||||
elif self.rxState == UavTalkRecThread.STATE_CS:
|
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
|
# by now, the CS has been added to the CRC calc, so now the CRC calc should read 0
|
||||||
if self.rxCrc.read() != 0:
|
if self.rxCrc.read() != 0:
|
||||||
logging.error("CRC ERROR")
|
logging.debug("CRC ERROR")
|
||||||
else:
|
else:
|
||||||
self.uavTalk._onRecevedPacket(self.obj, self.rxType, self.rxData)
|
self.uavTalk._onRecevedPacket(self.obj, self.rxType, self.rxData)
|
||||||
self.rxState = UavTalkRecThread.STATE_SYNC
|
self.rxState = UavTalkRecThread.STATE_SYNC
|
||||||
|
Loading…
Reference in New Issue
Block a user