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

Add parametrization w.r.t Hardware Parameters #49

Merged
merged 20 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
294d4c4
added model parametric and first version of casadi computation parame…
CarlottaSartore Oct 11, 2023
0686bed
fix import error, fix missing methods
CarlottaSartore Oct 19, 2023
ed89e51
fix non ashable problem
CarlottaSartore Oct 19, 2023
43d2711
fix computations for casadi
CarlottaSartore Oct 20, 2023
c5560ed
working with casadi but for the head frame
CarlottaSartore Oct 20, 2023
e584294
Added computations parametric for numpy, jax, pythorch
CarlottaSartore Oct 20, 2023
cb2eec7
no regression tests working for all backend
CarlottaSartore Nov 6, 2023
6386f7d
fix tests jax with hardware parameters
CarlottaSartore Nov 6, 2023
83fd338
tests for computations with hardware parameters and formatted
CarlottaSartore Nov 6, 2023
4b1263f
remove useless method, add first documentation
CarlottaSartore Nov 6, 2023
8f60f7c
Added documentation for parametric kyndyn classes, refactor of the va…
CarlottaSartore Nov 7, 2023
164de58
rebase on top of main, implement jdotnu for parametric, update tests,…
CarlottaSartore Nov 7, 2023
8bf4fda
update CI
CarlottaSartore Nov 7, 2023
123462d
fix urdf parser py version
CarlottaSartore Nov 7, 2023
0903bd1
remove fix urdf parser py version
CarlottaSartore Nov 7, 2023
5bec824
remove encoding from stickbot for tests with hardware parameters
CarlottaSartore Nov 7, 2023
5bdcf9c
refactor of parmetric implementation
CarlottaSartore Nov 9, 2023
bb5a963
Update src/adam/parametric/jax/computations_parametric.py
CarlottaSartore Dec 5, 2023
ab08cf9
fix typos and copy paste leftovers
CarlottaSartore Dec 5, 2023
7b0b3aa
Merge branch 'main' into model_parametric
Giulero Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci_env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ dependencies:
- pytest-repeat
- icub-models
- idyntree
- gitpython
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test =
idyntree
icub-models
black
gitpython
all =
jax
jaxlib
Expand Down
17 changes: 15 additions & 2 deletions src/adam/casadi/casadi_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class CasadiLike(ArrayLike):
def __matmul__(self, other: Union["CasadiLike", npt.ArrayLike]) -> "CasadiLike":
"""Overrides @ operator"""
if type(other) in [CasadiLike, NumpyLike]:
return CasadiLike(self.array @ other.array)
return CasadiLike(cs.mtimes(self.array, other.array))
else:
return CasadiLike(self.array @ other)
return CasadiLike(cs.mtimes(self.array, other))

def __rmatmul__(self, other: Union["CasadiLike", npt.ArrayLike]) -> "CasadiLike":
"""Overrides @ operator"""
Expand Down Expand Up @@ -196,6 +196,19 @@ def vertcat(*x) -> "CasadiLike":
y = [xi.array if isinstance(xi, CasadiLike) else xi for xi in x]
return CasadiLike(cs.vertcat(*y))

@staticmethod
def horzcat(*x) -> "CasadiLike":
"""
Returns:
CasadiLike: horizontal concatenation of elements
"""
# here the logic is a bit convoluted: x is a tuple containing CasadiLike
# cs.vertcat accepts *args. A list of cs types is created extracting the value
CarlottaSartore marked this conversation as resolved.
Show resolved Hide resolved
# from the CasadiLike stored in the tuple x.
# Then the list is unpacked with the * operator.
y = [xi.array if isinstance(xi, CasadiLike) else xi for xi in x]
return CasadiLike(cs.horzcat(*y))


if __name__ == "__main__":
math = SpatialMath()
Expand Down
50 changes: 48 additions & 2 deletions src/adam/core/spatial_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ def vertcat(x: npt.ArrayLike) -> npt.ArrayLike:
"""
pass

@abc.abstractmethod
def horzcat(x: npt.ArrayLike) -> npt.ArrayLike:
"""
Args:
x (npt.ArrayLike): elements

Returns:
npt.ArrayLike: horizontal concatenation of elements x
"""
pass

@abc.abstractmethod
def mtimes(x: npt.ArrayLike, y: npt.ArrayLike) -> npt.ArrayLike:
pass

@abc.abstractmethod
def sin(x: npt.ArrayLike) -> npt.ArrayLike:
"""
Expand Down Expand Up @@ -227,7 +242,9 @@ def H_revolute_joint(
T = self.factory.eye(4)
R = self.R_from_RPY(rpy) @ self.R_from_axis_angle(axis, q)
T[:3, :3] = R
T[:3, 3] = xyz
T[0, 3] = xyz[0]
T[1, 3] = xyz[1]
T[2, 3] = xyz[2]
Comment on lines +245 to +247
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remember me why the assignment T[:3, 3] = xyz does not work?
It's more of a log for the feature :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was that xyz contains both symbolic and not symbolic elements if the robot model is parametric, therefore it was complaining due to different types in xyz !

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it!

return T

def H_prismatic_joint(
Expand Down Expand Up @@ -264,7 +281,9 @@ def H_from_Pos_RPY(self, xyz: npt.ArrayLike, rpy: npt.ArrayLike) -> npt.ArrayLik
"""
T = self.factory.eye(4)
T[:3, :3] = self.R_from_RPY(rpy)
T[:3, 3] = xyz
T[0, 3] = xyz[0]
T[1, 3] = xyz[1]
T[2, 3] = xyz[2]
return T

def R_from_RPY(self, rpy: npt.ArrayLike) -> npt.ArrayLike:
Expand Down Expand Up @@ -381,6 +400,33 @@ def spatial_inertia(
IO[:3, :3] = self.factory.eye(3) * mass
return IO

def spatial_inertial_with_parameter(self, I, mass, c, rpy):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameter or parameters?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ab08cf9

"""
Args:
I (npt.ArrayLike): inertia values parametric
mass (npt.ArrayLike): mass value parametric
c (npt.ArrayLike): origin of the link parametric
rpy (npt.ArrayLike): orientation of the link from urdf

Returns:
npt.ArrayLike: the 6x6 inertia matrix parametric expressed at the origin of the link (with rotation)
"""
IO = self.factory.zeros(6, 6)
Sc = self.skew(c)
R = self.factory.zeros(3, 3)
R_temp = self.R_from_RPY(rpy)
inertia_matrix = self.vertcat(
self.horzcat(I.ixx, I.ixy, I.ixz),
self.horzcat(I.iyx, I.iyy, I.iyz),
self.horzcat(I.ixz, I.iyz, I.izz),
)

IO[3:, 3:] = R_temp @ inertia_matrix @ R_temp.T + mass * Sc @ Sc.T
IO[3:, :3] = mass * Sc
IO[:3, 3:] = mass * Sc.T
IO[:3, :3] = self.factory.eye(3) * mass
return IO

def spatial_skew(self, v: npt.ArrayLike) -> npt.ArrayLike:
"""
Args:
Expand Down
16 changes: 14 additions & 2 deletions src/adam/jax/jax_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,19 @@ def vertcat(*x) -> "JaxLike":
JaxLike: Vertical concatenation of elements
"""
if isinstance(x[0], JaxLike):
v = jnp.vstack([x[i].array for i in range(len(x))]).reshape(-1, 1)
v = jnp.vstack([x[i].array for i in range(len(x))])
else:
v = jnp.vstack([x[i] for i in range(len(x))]).reshape(-1, 1)
v = jnp.vstack([x[i] for i in range(len(x))])
return JaxLike(v)

@staticmethod
def horzcat(*x) -> "JaxLike":
"""
Returns:
JaxLike: Horizontal concatenation of elements
"""
if isinstance(x[0], JaxLike):
v = jnp.hstack([x[i].array for i in range(len(x))])
else:
v = jnp.hstack([x[i] for i in range(len(x))])
return JaxLike(v)
16 changes: 14 additions & 2 deletions src/adam/numpy/numpy_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,21 @@ def vertcat(*x: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike":
NumpyLike: vertical concatenation of x
"""
if isinstance(x[0], NumpyLike):
v = np.vstack([x[i].array for i in range(len(x))]).reshape(-1, 1)
v = np.vstack([x[i].array for i in range(len(x))])
else:
v = np.vstack([x[i] for i in range(len(x))]).reshape(-1, 1)
v = np.vstack([x[i] for i in range(len(x))])
return NumpyLike(v)

@staticmethod
def horzcat(*x: Union["NumpyLike", npt.ArrayLike]) -> "NumpyLike":
"""
Returns:
NumpyLike: horrizontal concatenation of x
"""
if isinstance(x[0], NumpyLike):
v = np.hstack([x[i].array for i in range(len(x))])
else:
v = np.hstack([x[i] for i in range(len(x))])
return NumpyLike(v)

@staticmethod
Expand Down
3 changes: 3 additions & 0 deletions src/adam/parametric/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright (C) 2021 Istituto Italiano di Tecnologia (IIT). All rights reserved.
# This software may be modified and distributed under the terms of the
# GNU Lesser General Public License v2.1 or any later version.
4 changes: 4 additions & 0 deletions src/adam/parametric/casadi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (C) 2021 Istituto Italiano di Tecnologia (IIT). All rights reserved.
# This software may be modified and distributed under the terms of the
# GNU Lesser General Public License v2.1 or any later version.
from .computations_parametric import KinDynComputationsParametric
Loading
Loading