From afac51296ab911c71fbfade7edba1dfa5797e5cb Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Fri, 15 Dec 2023 12:10:14 +0100 Subject: [PATCH] Avoid to print a warning for each sensor tag in loaded URDF file --- src/adam/model/std_factories/std_model.py | 25 ++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/adam/model/std_factories/std_model.py b/src/adam/model/std_factories/std_model.py index e7da13da..87f9310a 100644 --- a/src/adam/model/std_factories/std_model.py +++ b/src/adam/model/std_factories/std_model.py @@ -1,11 +1,25 @@ import pathlib from typing import List +import xml.etree.ElementTree as ET import urdf_parser_py.urdf from adam.core.spatial_math import SpatialMath from adam.model import ModelFactory, StdJoint, StdLink +def urdf_remove_sensors_tags(xml_string): + # Parse the XML string + root = ET.fromstring(xml_string) + + # Find and remove all tags named "sensor" that are child of + # root node (i.e. robot) + for sensors_tag in root.findall("sensor"): + root.remove(sensors_tag) + + # Convert the modified XML back to a string + modified_xml_string = ET.tostring(root) + + return modified_xml_string class URDFModelFactory(ModelFactory): """This factory generates robot elements from urdf_parser_py @@ -21,7 +35,16 @@ def __init__(self, path: str, math: SpatialMath): if not path.exists(): raise FileExistsError(path) - self.urdf_desc = urdf_parser_py.urdf.URDF.from_xml_file(path) + # Read URDF, but before passing it to urdf_parser_py get rid of all sensor tags + # sensor tags are valid elements of URDF (see ), + # but they are ignored by urdf_parser_py, that complains every time it sees one. + # As there is nothing to be fixed in the used models, and it is not useful + # to have a useless and noisy warning, let's remove before hands all the sensor elements, + # that anyhow are not parser by urdf_parser_py or adam + # See https://github.com/ami-iit/ADAM/issues/59 + xml_string = open(path, 'r').read() + xml_string_without_sensors_tags = urdf_remove_sensors_tags(xml_string) + self.urdf_desc = urdf_parser_py.urdf.URDF.from_xml_string(xml_string_without_sensors_tags) self.name = self.urdf_desc.name def get_joints(self) -> List[StdJoint]: