Skip to content

Commit d9a4cce

Browse files
ci(pre-commit): autofix
1 parent eb474d9 commit d9a4cce

File tree

3 files changed

+126
-90
lines changed

3 files changed

+126
-90
lines changed

Diff for: aip_xx1_description/scripts/compile_xacro.py

+124-88
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from jinja2 import Template
2-
import os
31
import enum
42
import functools
5-
import yaml
3+
import os
64
from typing import Dict
75

6+
from jinja2 import Template
7+
import yaml
8+
89

910
def load_yaml(file_path: str) -> Dict:
1011
with open(file_path, "r") as stream:
@@ -14,6 +15,7 @@ def load_yaml(file_path: str) -> Dict:
1415
print(exc)
1516
return None
1617

18+
1719
class Transformation:
1820
def __init__(self, transformation: Dict, base_frame: str, child_frame: str):
1921
try:
@@ -51,9 +53,7 @@ def serialize(self) -> str:
5153
class Calibration:
5254
def __init__(self, calibration: Dict):
5355
self.base_dict: Dict = calibration
54-
assert (
55-
len(calibration.keys()) == 1
56-
), "Calibration file should have only one base frame"
56+
assert len(calibration.keys()) == 1, "Calibration file should have only one base frame"
5757
assert isinstance(
5858
list(calibration.keys())[0], str
5959
), "Calibration file should have only one base frame with key as a string"
@@ -78,8 +78,9 @@ def __init__(self, calibration: Dict):
7878

7979
class LinkType(enum.Enum):
8080
"""
81-
Enum class for the type of the link
81+
Enum class for the type of the link
8282
"""
83+
8384
CAMERA = "monocular_camera"
8485
IMU = "imu"
8586
LIVOX = "livox_horizon"
@@ -93,44 +94,46 @@ class LinkType(enum.Enum):
9394
RADAR = "radar"
9495
JOINT_UNITS = "units"
9596

96-
def determine_link_type(link_name:str)->LinkType:
97-
if 'cam' in link_name:
97+
98+
def determine_link_type(link_name: str) -> LinkType:
99+
if "cam" in link_name:
98100
return LinkType.CAMERA
99-
100-
if 'imu' in link_name or 'gnss' in link_name:
101+
102+
if "imu" in link_name or "gnss" in link_name:
101103
return LinkType.IMU
102-
103-
if 'livox' in link_name:
104+
105+
if "livox" in link_name:
104106
return LinkType.LIVOX
105-
106-
if 'velodyne' in link_name:
107-
if 'top' in link_name:
107+
108+
if "velodyne" in link_name:
109+
if "top" in link_name:
108110
return LinkType.VLS128
109111
else:
110112
return LinkType.VELODYNE16
111-
112-
if 'radar' in link_name or 'ars' in link_name:
113+
114+
if "radar" in link_name or "ars" in link_name:
113115
return LinkType.RADAR
114-
115-
if 'pandar_40p' in link_name:
116+
117+
if "pandar_40p" in link_name:
116118
return LinkType.PANDAR_40P
117-
118-
if 'pandar_qt' in link_name:
119+
120+
if "pandar_qt" in link_name:
119121
return LinkType.PANDAR_QT
120-
121-
if 'hesai_top' in link_name:
122+
123+
if "hesai_top" in link_name:
122124
return LinkType.PANDAR_OT128
123-
124-
if 'hesai_front' in link_name:
125+
126+
if "hesai_front" in link_name:
125127
return LinkType.PANDAR_XT32
126128

127-
if 'hesai' in link_name:
129+
if "hesai" in link_name:
128130
return LinkType.PANDAR_XT32
129-
131+
130132
else:
131133
print(f"Link type not found for {link_name}, suspected to be a joint unit")
132134
return LinkType.JOINT_UNITS
133135

136+
134137
BASE_STRING = """<xacro:{type}
135138
name=\"{child_frame}\"
136139
parent=\"{base_frame}\"
@@ -154,7 +157,8 @@ def determine_link_type(link_name:str)->LinkType:
154157
/>
155158
</xacro:{type}>"""
156159

157-
def base_string_func(type:str, transform:Transformation)->str:
160+
161+
def base_string_func(type: str, transform: Transformation) -> str:
158162
if type == "monocular_camera_macro":
159163
extra = """fps=\"30\"
160164
width=\"800\"
@@ -167,80 +171,104 @@ def base_string_func(type:str, transform:Transformation)->str:
167171
else:
168172
extra = ""
169173
return BASE_STRING.format(
170-
type=type, base_frame=transform.base_frame, child_frame=transform.child_frame,
171-
x=transform.serialize_single('x'), y=transform.serialize_single('y'), z=transform.serialize_single('z'),
172-
roll=transform.serialize_single('roll'), pitch=transform.serialize_single('pitch'), yaw=transform.serialize_single('yaw'),
173-
extra=extra)
174+
type=type,
175+
base_frame=transform.base_frame,
176+
child_frame=transform.child_frame,
177+
x=transform.serialize_single("x"),
178+
y=transform.serialize_single("y"),
179+
z=transform.serialize_single("z"),
180+
roll=transform.serialize_single("roll"),
181+
pitch=transform.serialize_single("pitch"),
182+
yaw=transform.serialize_single("yaw"),
183+
extra=extra,
184+
)
174185

175-
def VLP16_func(transform:Transformation)->str:
186+
187+
def VLP16_func(transform: Transformation) -> str:
176188
return VLD_STRING.format(
177-
type="VLP-16", base_frame=transform.base_frame, child_frame=transform.child_frame,
178-
x=transform.serialize_single('x'), y=transform.serialize_single('y'), z=transform.serialize_single('z'),
179-
roll=transform.serialize_single('roll'), pitch=transform.serialize_single('pitch'), yaw=transform.serialize_single('yaw')
189+
type="VLP-16",
190+
base_frame=transform.base_frame,
191+
child_frame=transform.child_frame,
192+
x=transform.serialize_single("x"),
193+
y=transform.serialize_single("y"),
194+
z=transform.serialize_single("z"),
195+
roll=transform.serialize_single("roll"),
196+
pitch=transform.serialize_single("pitch"),
197+
yaw=transform.serialize_single("yaw"),
180198
)
181199

182-
def VLS128_func(transform:Transformation)->str:
200+
201+
def VLS128_func(transform: Transformation) -> str:
183202
return VLD_STRING.format(
184-
type="VLS-128", base_frame=transform.base_frame, child_frame=transform.child_frame,
185-
x=transform.serialize_single('x'), y=transform.serialize_single('y'), z=transform.serialize_single('z'),
186-
roll=transform.serialize_single('roll'), pitch=transform.serialize_single('pitch'), yaw=transform.serialize_single('yaw')
203+
type="VLS-128",
204+
base_frame=transform.base_frame,
205+
child_frame=transform.child_frame,
206+
x=transform.serialize_single("x"),
207+
y=transform.serialize_single("y"),
208+
z=transform.serialize_single("z"),
209+
roll=transform.serialize_single("roll"),
210+
pitch=transform.serialize_single("pitch"),
211+
yaw=transform.serialize_single("yaw"),
187212
)
188213

214+
189215
link_dicts = {
190-
LinkType.CAMERA:{
216+
LinkType.CAMERA: {
191217
"including_file": "$(find camera_description)/urdf/monocular_camera.xacro",
192-
"string_api": functools.partial(base_string_func, "monocular_camera_macro")
218+
"string_api": functools.partial(base_string_func, "monocular_camera_macro"),
193219
},
194-
LinkType.IMU:{
220+
LinkType.IMU: {
195221
"including_file": "$(find imu_description)/urdf/imu.xacro",
196-
"string_api": functools.partial(base_string_func, "imu_macro")
222+
"string_api": functools.partial(base_string_func, "imu_macro"),
197223
},
198-
LinkType.VELODYNE16:{
224+
LinkType.VELODYNE16: {
199225
"including_file": "$(find velodyne_description)/urdf/VLP-16.urdf.xacro",
200-
"string_api": VLP16_func
226+
"string_api": VLP16_func,
201227
},
202-
LinkType.VLS128:{
228+
LinkType.VLS128: {
203229
"including_file": "$(find vls_description)/urdf/VLS-128.urdf.xacro",
204-
"string_api": VLS128_func
230+
"string_api": VLS128_func,
205231
},
206-
LinkType.PANDAR_40P:{
232+
LinkType.PANDAR_40P: {
207233
"including_file": "$(find pandar_description)/urdf/pandar_40p.xacro",
208-
"string_api": functools.partial(base_string_func, "Pandar40P")
234+
"string_api": functools.partial(base_string_func, "Pandar40P"),
209235
},
210-
LinkType.PANDAR_OT128:{
236+
LinkType.PANDAR_OT128: {
211237
"including_file": "$(find pandar_description)/urdf/pandar_ot128.xacro",
212-
"string_api": functools.partial(base_string_func, "PandarOT-128")
238+
"string_api": functools.partial(base_string_func, "PandarOT-128"),
213239
},
214-
LinkType.PANDAR_XT32:{
240+
LinkType.PANDAR_XT32: {
215241
"including_file": "$(find pandar_description)/urdf/pandar_xt32.xacro",
216-
"string_api": functools.partial(base_string_func, "PandarXT-32")
242+
"string_api": functools.partial(base_string_func, "PandarXT-32"),
217243
},
218-
LinkType.PANDAR_QT:{
244+
LinkType.PANDAR_QT: {
219245
"including_file": "$(find pandar_description)/urdf/pandar_qt.xacro",
220-
"string_api": functools.partial(base_string_func, "PandarQT")
246+
"string_api": functools.partial(base_string_func, "PandarQT"),
221247
},
222-
LinkType.PANDAR_QT128:{
248+
LinkType.PANDAR_QT128: {
223249
"including_file": "$(find pandar_description)/urdf/pandar_qt128.xacro",
224-
"string_api": functools.partial(base_string_func, "PandarQT-128")
250+
"string_api": functools.partial(base_string_func, "PandarQT-128"),
225251
},
226-
LinkType.LIVOX:{
252+
LinkType.LIVOX: {
227253
"including_file": "$(find livox_description)/urdf/livox_horizon.xacro",
228-
"string_api": functools.partial(base_string_func, "livox_horizon_macro")
254+
"string_api": functools.partial(base_string_func, "livox_horizon_macro"),
229255
},
230-
LinkType.RADAR:{
256+
LinkType.RADAR: {
231257
"including_file": "$(find radar_description)/urdf/radar.xacro",
232-
"string_api": functools.partial(base_string_func, "radar_macro")
258+
"string_api": functools.partial(base_string_func, "radar_macro"),
233259
},
234-
LinkType.JOINT_UNITS:{
260+
LinkType.JOINT_UNITS: {
235261
"including_file": "{filename}.xacro",
236-
}
262+
},
237263
}
238264

239265

240-
def main(template_directory:str, calibration_directory:str, output_directory:str, project_name:str):
266+
def main(
267+
template_directory: str, calibration_directory: str, output_directory: str, project_name: str
268+
):
241269
os.makedirs(output_directory, exist_ok=True)
242270
# Load the template
243-
with open(os.path.join(template_directory, 'sensors.xacro.template'), 'r') as file:
271+
with open(os.path.join(template_directory, "sensors.xacro.template"), "r") as file:
244272
base_template = Template(file.read())
245273

246274
# Render the template
@@ -249,31 +277,34 @@ def main(template_directory:str, calibration_directory:str, output_directory:str
249277
calib = Calibration(calib_yaml)
250278

251279
render_meta_data = dict()
252-
render_meta_data['default_config_path'] = f"$(find {project_name})/config"
253-
render_meta_data["sensor_calibration_yaml_path"] = f"$(find {project_name})/config/sensors_calibration.yaml"
280+
render_meta_data["default_config_path"] = f"$(find {project_name})/config"
281+
render_meta_data[
282+
"sensor_calibration_yaml_path"
283+
] = f"$(find {project_name})/config/sensors_calibration.yaml"
254284
render_meta_data["sensor_units_includes"] = []
255285
render_meta_data["sensor_units"] = []
256286
render_meta_data["isolated_sensors_includes"] = []
257287
render_meta_data["isolated_sensors"] = []
258288

259-
260289
include_text = set()
261290
sensor_items = []
262291
for _, transform in calib.transforms.items():
263-
link_type:LinkType = determine_link_type(transform.child_frame)
292+
link_type: LinkType = determine_link_type(transform.child_frame)
264293
if link_type == LinkType.JOINT_UNITS:
265-
render_meta_data["sensor_units_includes"].append(link_dicts[link_type]['including_file'].format(filename=transform.name))
294+
render_meta_data["sensor_units_includes"].append(
295+
link_dicts[link_type]["including_file"].format(filename=transform.name)
296+
)
266297
render_meta_data["sensor_units"].append(
267298
dict(
268299
base_frame=transform.base_frame,
269300
child_frame=transform.child_frame,
270301
macro_name=f"{transform.name}_macro",
271-
name = transform.name
302+
name=transform.name,
272303
)
273304
)
274305
else:
275-
include_text.add(link_dicts[link_type]['including_file'])
276-
sensor_items.append(link_dicts[link_type]['string_api'](transform))
306+
include_text.add(link_dicts[link_type]["including_file"])
307+
sensor_items.append(link_dicts[link_type]["string_api"](transform))
277308

278309
render_meta_data["isolated_sensors_includes"] = list(include_text)
279310
render_meta_data["isolated_sensors"] = sensor_items
@@ -283,39 +314,43 @@ def main(template_directory:str, calibration_directory:str, output_directory:str
283314

284315
print("=====================================")
285316
# Save the rendered template
286-
with open(os.path.join(output_directory, 'sensors.xacro'), 'w') as file:
317+
with open(os.path.join(output_directory, "sensors.xacro"), "w") as file:
287318
file.write(rendered)
288319

289-
290320
## Write Sensor Units into separate files
291-
with open(os.path.join(template_directory, 'sensor_unit.xacro.template'), 'r') as file:
321+
with open(os.path.join(template_directory, "sensor_unit.xacro.template"), "r") as file:
292322
sensor_units_template = Template(file.read())
293323

294324
for i, sensor_unit in enumerate(render_meta_data["sensor_units"]):
295-
sensor_unit_calib_path = os.path.join(calibration_directory, f"{sensor_unit['name']}_calibration.yaml")
325+
sensor_unit_calib_path = os.path.join(
326+
calibration_directory, f"{sensor_unit['name']}_calibration.yaml"
327+
)
296328
sensor_unit_calib_yaml = load_yaml(sensor_unit_calib_path)
297329
sensor_unit_calib = Calibration(sensor_unit_calib_yaml)
298330
sensor_unit_render_meta_data = dict()
299-
sensor_unit_render_meta_data['unit_macro_name'] = sensor_unit['macro_name']
300-
sensor_unit_render_meta_data['default_config_path'] = render_meta_data['default_config_path']
331+
sensor_unit_render_meta_data["unit_macro_name"] = sensor_unit["macro_name"]
332+
sensor_unit_render_meta_data["default_config_path"] = render_meta_data[
333+
"default_config_path"
334+
]
301335

302-
sensor_unit_render_meta_data['current_base_link'] = sensor_unit_calib.base_frame
336+
sensor_unit_render_meta_data["current_base_link"] = sensor_unit_calib.base_frame
303337
sensor_unit_isolated_sensors = []
304338
for _, transform in sensor_unit_calib.transforms.items():
305-
link_type:LinkType = determine_link_type(transform.child_frame)
306-
include_text.add(link_dicts[link_type]['including_file'])
307-
sensor_unit_isolated_sensors.append(link_dicts[link_type]['string_api'](transform))
339+
link_type: LinkType = determine_link_type(transform.child_frame)
340+
include_text.add(link_dicts[link_type]["including_file"])
341+
sensor_unit_isolated_sensors.append(link_dicts[link_type]["string_api"](transform))
308342
sensor_unit_render_meta_data["isolated_sensors_includes"] = list(include_text)
309343
sensor_unit_render_meta_data["isolated_sensors"] = sensor_unit_isolated_sensors
310344

311345
rendered = sensor_units_template.render(sensor_unit_render_meta_data)
312346
print(rendered)
313-
with open(os.path.join(output_directory, f'{sensor_unit["name"]}.xacro'), 'w') as file:
347+
with open(os.path.join(output_directory, f'{sensor_unit["name"]}.xacro'), "w") as file:
314348
file.write(rendered)
315349
print("=====================================")
316-
350+
317351
return 0
318352

353+
319354
if __name__ == "__main__":
320355
# import argparse
321356
# parser = argparse.ArgumentParser(description="Compile xacro files from calibration files")
@@ -325,5 +360,6 @@ def main(template_directory:str, calibration_directory:str, output_directory:str
325360
# parser.add_argument("--project_name", type=str, help="Name of the project", required=True)
326361
# args = parser.parse_args()
327362
from fire import Fire
363+
328364
Fire(main)
329-
# main(args.template_directory, args.calibration_directory, args.output_directory, args.project_name)
365+
# main(args.template_directory, args.calibration_directory, args.output_directory, args.project_name)

Diff for: aip_xx1_description/templates/sensor_unit.xacro.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
{% endfor %}
2929
</xacro:macro>
3030

31-
</robot>
31+
</robot>

Diff for: aip_xx1_description/urdf/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
*.xacro
1+
*.xacro

0 commit comments

Comments
 (0)