px4-firmware/Tools/px4airframes/xmlout.py

123 lines
6.0 KiB
Python
Raw Normal View History

2015-07-29 14:13:28 -03:00
import xml.etree.ElementTree as ET
import codecs
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
class XMLOutput():
def __init__(self, groups, board):
xml_parameters = ET.Element("airframes")
xml_version = ET.SubElement(xml_parameters, "version")
xml_version.text = "1"
xml_version = ET.SubElement(xml_parameters, "airframe_version_major")
xml_version.text = "1"
xml_version = ET.SubElement(xml_parameters, "airframe_version_minor")
2015-07-29 14:13:28 -03:00
xml_version.text = "1"
last_param_name = ""
board_specific_param_set = False
for group in groups:
xml_group = ET.SubElement(xml_parameters, "airframe_group")
xml_group.attrib["name"] = group.GetName()
if (group.GetName() == "Standard Plane"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "Plane"
elif (group.GetName() == "Flying Wing"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "FlyingWing"
elif (group.GetName() == "Quadrotor x"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "QuadRotorX"
elif (group.GetName() == "Quadrotor +"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "QuadRotorPlus"
elif (group.GetName() == "Hexarotor x"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "HexaRotorX"
elif (group.GetName() == "Hexarotor +"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "HexaRotorPlus"
elif (group.GetName() == "Octorotor +"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "OctoRotorPlus"
elif (group.GetName() == "Octorotor x"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "OctoRotorX"
elif (group.GetName() == "Octorotor Coaxial"):
xml_group.attrib["image"] = "OctoRotorXCoaxial"
elif (group.GetName() == "Octo Coax Wide"):
xml_group.attrib["image"] = "OctoRotorXCoaxial"
elif (group.GetName() == "Quadrotor Wide"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "QuadRotorWide"
elif (group.GetName() == "Quadrotor H"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "QuadRotorH"
elif (group.GetName() == "Simulation"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "AirframeSimulation"
elif (group.GetName() == "Plane A-Tail"):
xml_group.attrib["image"] = "PlaneATail"
elif (group.GetName() == "VTOL Duo Tailsitter"):
xml_group.attrib["image"] = "VTOLDuoRotorTailSitter"
elif (group.GetName() == "Standard VTOL"):
xml_group.attrib["image"] = "VTOLPlane"
elif (group.GetName() == "VTOL Quad Tailsitter"):
xml_group.attrib["image"] = "VTOLQuadRotorTailSitter"
2016-01-24 08:22:45 -04:00
elif (group.GetName() == "VTOL Tiltrotor"):
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "VTOLTiltRotor"
elif (group.GetName() == "Coaxial Helicopter"):
xml_group.attrib["image"] = "HelicopterCoaxial"
2016-08-04 12:26:17 -03:00
elif (group.GetName() == "Helicopter"):
xml_group.attrib["image"] = "Helicopter"
2016-01-13 19:43:23 -04:00
elif (group.GetName() == "Hexarotor Coaxial"):
xml_group.attrib["image"] = "Y6A"
elif (group.GetName() == "Y6B"):
xml_group.attrib["image"] = "Y6B"
elif (group.GetName() == "Tricopter Y-"):
xml_group.attrib["image"] = "YMinus"
elif (group.GetName() == "Tricopter Y+"):
xml_group.attrib["image"] = "YPlus"
elif (group.GetName() == "Rover"):
xml_group.attrib["image"] = "Rover"
elif (group.GetName() == "Boat"):
xml_group.attrib["image"] = "Boat"
else:
2016-01-13 19:43:23 -04:00
xml_group.attrib["image"] = "AirframeUnknown"
2015-07-29 14:13:28 -03:00
for param in group.GetParams():
2016-12-20 13:13:07 -04:00
# check if there is an exclude tag for this airframe
excluded = False
for code in param.GetArchCodes():
if "CONFIG_ARCH_BOARD_{0}".format(code) == board and param.GetArchValue(code) == "exclude":
excluded = True
if not excluded and ((last_param_name == param.GetName() and not board_specific_param_set) or last_param_name != param.GetName()):
#print("generating: {0} {1}".format(param.GetName(), excluded))
2015-07-29 14:13:28 -03:00
xml_param = ET.SubElement(xml_group, "airframe")
xml_param.attrib["name"] = param.GetName()
xml_param.attrib["id"] = param.GetId()
xml_param.attrib["maintainer"] = param.GetMaintainer()
last_param_name = param.GetName()
for code in param.GetFieldCodes():
value = param.GetFieldValue(code)
2016-12-20 13:13:07 -04:00
xml_field = ET.SubElement(xml_param, code)
xml_field.text = value
2015-07-29 14:13:28 -03:00
for code in param.GetOutputCodes():
value = param.GetOutputValue(code)
2015-11-05 16:30:10 -04:00
valstrs = value.split(";")
2015-07-29 14:13:28 -03:00
xml_field = ET.SubElement(xml_param, "output")
xml_field.attrib["name"] = code
2015-11-05 16:30:10 -04:00
for attrib in valstrs[1:]:
attribstrs = attrib.split(":")
xml_field.attrib[attribstrs[0].strip()] = attribstrs[1].strip()
xml_field.text = valstrs[0]
2015-07-29 14:13:28 -03:00
if last_param_name != param.GetName():
board_specific_param_set = False
indent(xml_parameters)
self.xml_document = ET.ElementTree(xml_parameters)
def Save(self, filename):
self.xml_document.write(filename, encoding="UTF-8")