Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
convert_fssim_sdf_to_yaml.py
Go to the documentation of this file.
1import xml.etree.ElementTree as Et
2import sys
3from pathlib import Path
4from ruamel.yaml import YAML
5# from collections import OrderedDict
6from ruamel.yaml.comments import CommentedMap as OrderedDict
7
9 def __init__(self, f) -> None:
10 self.f = f
11
12
13 def write(self, s):
14 self.f.write(self.__clean_yaml(s.decode('utf-8')))
15
16
17 def __clean_yaml(self, yaml_file: str) -> str:
18 yaml_file = yaml_file.replace("'", "")
19 return yaml_file
20
21
22def get_child_with_tag(root: Et.Element, tag: str) -> Et.Element:
23 for child in root:
24 if child.tag == tag:
25 return child
26 return None
27
28
29def get_children_with_tag(root: Et.Element, tag: str) -> list:
30 result = []
31 for child in root:
32 if child.tag == tag:
33 result.append(child)
34
35 return result
36
37
38def convert_to_object_class(uri: str) -> str:
39 if uri == "cone_blue":
40 return "blue"
41 if uri == "cone_yellow":
42 return "yellow"
43 if uri == "time_keeping":
44 return "timekeeping"
45 if uri == "cone_orange":
46 return "small-orange"
47 if uri == "cone_orange_big":
48 return "big-orange"
49 return "unknown"
50
51
52def extract_cones(elements: list) -> dict:
53 left = []
54 right = []
55 time_keeping = []
56 unknown = []
57
58 for element in elements:
59 pose = get_child_with_tag(element, "pose")
60 url = get_child_with_tag(element, "uri")
61 name = get_child_with_tag(element, "name")
62
63 x = float(pose.text.rsplit(" ")[0])
64 y = float(pose.text.rsplit(" ")[1])
65 z = float(pose.text.rsplit(" ")[2])
66
67 object_class = convert_to_object_class(str(url.text.rsplit("/")[-1]))
68
69 if "cone_right" in name.text:
70 right.append((x, y, z, object_class))
71 elif "cone_left" in name.text:
72 left.append((x, y, z, object_class))
73 elif "tk_device" in name.text:
74 time_keeping.append((x, y, z, object_class))
75 else:
76 unknown.append((x, y, z, object_class))
77
78 return {"left": left, "right": right, "time_keeping": time_keeping, "unknown": unknown}
79
80
81def convert_cones_to_yaml(cones: list) -> list:
82 result = []
83 for cone in cones:
84 result.append({"position": f"[{cone[0]}, {cone[1]}, {cone[2]}]", "class": f"{cone[3]}"})
85
86 return result
87
88
89def write_to_yaml(cones: dict, file_path: str) -> None:
90 left = convert_cones_to_yaml(cones["left"])
91 right = convert_cones_to_yaml(cones["right"])
92 time_keeping = convert_cones_to_yaml(cones["time_keeping"])
93 unknown = convert_cones_to_yaml(cones["unknown"])
94 start_position = (0.0, 0.0, 0.0)
95 start_orientation = (0.0, 0.0, 0.0)
96
97 yaml_file = OrderedDict({
98 "track": OrderedDict({
99 "version": 1.0,
100 "lanesFirstWithLastConnected": True,
101 "start": OrderedDict({
102 "position": f'[{start_position[0]}, {start_position[1]}, {start_position[2]}]',
103 "orientation": f'[{start_orientation[0]}, {start_orientation[1]}, {start_orientation[2]}]'}),
104 "earthToTrack": OrderedDict({
105 "position": f'[{0.0}, {0.0}, {0.0}]',
106 "orientation": f'[{0.0}, {0.0}, {0.0}]'}),
107 "left": left,
108 "right": right,
109 "time_keeping": time_keeping,
110 "unknown": unknown
111 })
112 })
113
114 with open(Path(file_path).with_suffix('.yaml'), "w+") as f:
115 f.write("# Map file for PacSim\n")
116 yaml_dumper = My_Yaml_Dump(f)
117 yaml = YAML()
118 yaml.dump(yaml_file, yaml_dumper)
119
120
121def main(file_path: str) -> None:
122 tree = Et.parse(file_path)
123 root = tree.getroot()
124 model = get_child_with_tag(root, "model")
125 objects = get_children_with_tag(model, "include")
126 cones = extract_cones(objects)
127 write_to_yaml(cones, file_path)
128
129
130if __name__ == '__main__':
131 if len(sys.argv) != 2:
132 print("Call with python convert_sdf_to_yaml.py <file_path>")
133 else:
134 main(sys.argv[1])
list get_children_with_tag(Et.Element root, str tag)
Et.Element get_child_with_tag(Et.Element root, str tag)
None write_to_yaml(dict cones, str file_path)
Definition main.py:1