Skip to content

Commit

Permalink
Merge pull request #49 from CarlottaSartore/model_parametric
Browse files Browse the repository at this point in the history
Add parametrization of the algorithms w.r.t Hardware Parameters
  • Loading branch information
Giulero authored Dec 6, 2023
2 parents eaaf94b + 7b0b3aa commit 115a87a
Show file tree
Hide file tree
Showing 25 changed files with 3,133 additions and 12 deletions.
3 changes: 2 additions & 1 deletion ci_env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ dependencies:
- pytest-repeat
- icub-models
- idyntree
- gitpython
- jax
- pytorch
- pytorch
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
14 changes: 12 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,16 @@ 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
"""

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]
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_parameters(self, I, mass, c, rpy):
"""
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

0 comments on commit 115a87a

Please sign in to comment.