Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the possibility to load both from string and path #74

Merged
merged 9 commits into from
Jul 2, 2024
4 changes: 2 additions & 2 deletions src/adam/casadi/computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def __init__(
) -> None:
"""
Args:
urdfstring (str): path of the urdf
urdfstring (str): either path or string of the urdf
joints_name_list (list): list of the actuated joints
root_link (str, optional): the first link. Defaults to 'root_link'.
"""
math = SpatialMath()
factory = URDFModelFactory(path=urdfstring, math=math)
factory = URDFModelFactory(urdf_string=urdfstring, math=math)
model = Model.build(factory=factory, joints_name_list=joints_name_list)
self.rbdalgos = RBDAlgorithms(model=model, math=math)
self.NDoF = self.rbdalgos.NDoF
Expand Down
4 changes: 2 additions & 2 deletions src/adam/jax/computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def __init__(
) -> None:
"""
Args:
urdfstring (str): path of the urdf
urdfstring (str): either path or string of the urdf
joints_name_list (list): list of the actuated joints
root_link (str, optional): the first link. Defaults to 'root_link'.
"""
math = SpatialMath()
factory = URDFModelFactory(path=urdfstring, math=math)
factory = URDFModelFactory(urdf_string=urdfstring, math=math)
model = Model.build(factory=factory, joints_name_list=joints_name_list)
self.rbdalgos = RBDAlgorithms(model=model, math=math)
self.NDoF = self.rbdalgos.NDoF
Expand Down
42 changes: 32 additions & 10 deletions src/adam/model/std_factories/std_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pathlib
from typing import List
import xml.etree.ElementTree as ET

import os
import urdf_parser_py.urdf

from adam.core.spatial_math import SpatialMath
Expand Down Expand Up @@ -30,23 +30,45 @@ class URDFModelFactory(ModelFactory):
ModelFactory: the Model factory
"""

def __init__(self, path: str, math: SpatialMath):
def __init__(self, urdf_string: str, math: SpatialMath):
S-Dafarra marked this conversation as resolved.
Show resolved Hide resolved
self.math = math
if type(path) is not pathlib.Path:
path = pathlib.Path(path)
if not path.exists():
raise FileExistsError(path)

isPath = False
isUrdf = False
# Checking if it is a path or an urdf
if type(urdf_string) is not (pathlib.Path):
if os.path.exists(urdf_string):
urdf_string = pathlib.Path(urdf_string)
isPath = True
else:
root = ET.fromstring(urdf_string)
robot_el = None
for elem in root.iter():
if elem.tag == "robot":
xml_string = urdf_string
isUrdf = True
# raise ValueError(f"Invalid urdf string: {urdf_string}. It is neither a path nor a urdf string")
elif urdf_string.exists():
isPath = True

if not (isPath) and not (isUrdf):
raise ValueError(
f"Invalid urdf string: {urdf_string}. It is neither a path nor a urdf string"
)

if isPath:
if not (urdf_string.exists()):
raise FileExistsError(path)
urdf_string = pathlib.Path(urdf_string)
xml_file = open(urdf_string, "r")
xml_string = xml_file.read()
xml_file.close()
# 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_file = open(path, "r")
xml_string = xml_file.read()
xml_file.close()
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
Expand Down
2 changes: 1 addition & 1 deletion src/adam/numpy/computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
root_link (str, optional): the first link. Defaults to 'root_link'.
"""
math = SpatialMath()
factory = URDFModelFactory(path=urdfstring, math=math)
factory = URDFModelFactory(urdf_string=urdfstring, math=math)
model = Model.build(factory=factory, joints_name_list=joints_name_list)
self.rbdalgos = RBDAlgorithms(model=model, math=math)
self.NDoF = model.NDoF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def compute_volume(self, length_multiplier):
volume = math.pi * visual_data_new[1] ** 2 * visual_data_new[0]
elif self.geometry_type == Geometry.SPHERE:
visual_data_new = self.visual_data.radius * length_multiplier
volume = 4 * math.pi * visual_data_new**3 / 3
volume = 4 * math.pi * visual_data_new ** 3 / 3
return volume, visual_data_new

def compute_mass(self):
Expand Down Expand Up @@ -235,7 +235,7 @@ def compute_inertia_parametric(self):
I.izz = I.iyy
return I
elif self.geometry_type == Geometry.SPHERE:
I.ixx = 2 * self.mass * self.visual_data_new**2 / 5
I.ixx = 2 * self.mass * self.visual_data_new ** 2 / 5
I.iyy = I.ixx
I.izz = I.ixx
return I
Expand Down
2 changes: 1 addition & 1 deletion src/adam/pytorch/computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(
root_link (str, optional): the first link. Defaults to 'root_link'.
"""
math = SpatialMath()
factory = URDFModelFactory(path=urdfstring, math=math)
factory = URDFModelFactory(urdf_string=urdfstring, math=math)
model = Model.build(factory=factory, joints_name_list=joints_name_list)
self.rbdalgos = RBDAlgorithms(model=model, math=math)
self.NDoF = self.rbdalgos.NDoF
Expand Down
Loading