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

Adds the expressions for writing planners for a humanoid robot #10

Merged
merged 106 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 101 commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
b3a2ffe
Added centroidal dynamics from point forces
S-Dafarra Jul 19, 2023
06e5b42
Added com dynamics
S-Dafarra Jul 19, 2023
6ce7964
Added function to compute the CMM from the robot state.
S-Dafarra Jul 20, 2023
4fc6505
Added function to compute CoM position from kinematics
S-Dafarra Jul 24, 2023
4cc9b8e
Added function to compute ppoint position from kinematics
S-Dafarra Jul 24, 2023
5458852
Added quaternion normalization function
S-Dafarra Jul 24, 2023
aa45a8c
Added base class for a generic terrain description and a PlanarTerrain
S-Dafarra Jul 24, 2023
e0d5bee
Small refactor of the terrain descriptor class to allow reusing the f…
S-Dafarra Jul 24, 2023
ecef8ac
Added expressions for friction and normal force positivity
S-Dafarra Jul 24, 2023
783604a
Changed expression for the static friction margin
S-Dafarra Jul 25, 2023
79641aa
Added first version of complementarity functions
S-Dafarra Jul 26, 2023
00619b1
Added function to compute two frame relative position
S-Dafarra Jul 27, 2023
3910ebf
Added missing options in kinematics and complementarity
S-Dafarra Jul 27, 2023
05de558
Added contact points centroid function
S-Dafarra Aug 1, 2023
ff172a5
Added contact_points_yaw_alignment and swing_height_heuristic
S-Dafarra Aug 2, 2023
124908f
Added quaternion_error function
S-Dafarra Aug 2, 2023
799b898
Added ContactPoint variable definition
S-Dafarra Aug 2, 2023
ebc9785
Added initial conditions in contact variables.
S-Dafarra Aug 7, 2023
853e476
Added variables describing a floating base system
S-Dafarra Aug 7, 2023
6cb1bc2
Added f0 initial condition in ContactPoint variable
S-Dafarra Aug 7, 2023
741da06
Allowing to set the individual name of the variables in the centroida…
S-Dafarra Aug 7, 2023
919c724
Fixed missing input in planar complementarity function
S-Dafarra Aug 7, 2023
d41f784
Added dcc complementarity to flat ground planner
S-Dafarra Aug 8, 2023
c1154a4
Added possibility to change the options of a terrain descriptor after…
S-Dafarra Aug 8, 2023
588dda5
Added friction expression to planner and fixed the use of dcc express…
S-Dafarra Aug 8, 2023
c11705d
Added additional constraint for the maximum feet relative height
S-Dafarra Aug 8, 2023
b9364ea
Separated quaternion error function
S-Dafarra Aug 10, 2023
84a377d
Moved feet references to custom class and implemented yaw regularizat…
S-Dafarra Aug 10, 2023
2673c14
Added contact point regularization
S-Dafarra Aug 11, 2023
3b1f3bc
Refactor variables to extract initial states
S-Dafarra Aug 11, 2023
6ffdf9e
Improved definition of composite types
S-Dafarra Aug 16, 2023
217e041
Added method to generate the list of contact point descriptor for a r…
S-Dafarra Aug 16, 2023
cff0948
Fixed initialization of composite variables
S-Dafarra Aug 16, 2023
998f556
Fixed used of casadi transpose and horzcat
S-Dafarra Aug 17, 2023
d451dfa
Multiple bugfixes on expressions
S-Dafarra Aug 17, 2023
97d5de7
Exploiting the OverridableVariable mechanism and added HumanoidState
S-Dafarra Sep 12, 2023
218beb2
Added relaxed complementarity margin
S-Dafarra Oct 4, 2023
acd95cb
Fixed bug in Humanoid state
S-Dafarra Oct 4, 2023
4892881
Moved centroidal and quaternion expressions to the expressions folder
S-Dafarra Oct 4, 2023
a5368e5
Added possibility to set all contact points position from parent fram…
S-Dafarra Oct 4, 2023
c464068
Added static method in FootContactState to set the points transform f…
S-Dafarra Oct 10, 2023
50407af
Fixed setting of point_position_name default value
S-Dafarra Oct 13, 2023
54d6744
Added deps folder and imported surf2stl
S-Dafarra Oct 13, 2023
a734969
Added initial version of HumanStateVisualizer
S-Dafarra Oct 13, 2023
2d16e66
Added visualization of the HumanoidState
S-Dafarra Oct 14, 2023
0ab8bc5
Avoiding to convert a rotation matrix to quaternion
S-Dafarra Oct 15, 2023
9384167
Removed unused import
S-Dafarra Oct 16, 2023
6b08bfa
Avoid using from_matrix when computing frame relative position
S-Dafarra Oct 16, 2023
3bd8d80
Added draft to visualize multiple steps in a row
S-Dafarra Oct 17, 2023
4f4ecf8
Added possibility to set time_multiplier in the states visualization
S-Dafarra Oct 17, 2023
2c9b51c
Changed visualization of arrows
S-Dafarra Oct 17, 2023
1ebf3f9
Added possibility to use the mass regularized version of the centroid…
S-Dafarra Oct 17, 2023
dc01729
Added possibility to save frames and video in the visualizer
S-Dafarra Oct 18, 2023
5b8614d
Added first version of plotter for complementarity
S-Dafarra Oct 30, 2023
13975d5
Added visualization of complementarity.
S-Dafarra Oct 31, 2023
8ec4684
Added visualization of complementarity error
S-Dafarra Oct 31, 2023
fa30b72
Added possibility to force close the complementarity plots
S-Dafarra Oct 31, 2023
fd47e92
Added possibility to plot the contact forces
S-Dafarra Nov 2, 2023
3a8fa35
Create the frames folder only if saving
S-Dafarra Nov 2, 2023
0869c6c
Fixed visualization of forces
S-Dafarra Nov 3, 2023
d2cfcba
Added possibility to specify the terrain name
S-Dafarra Nov 3, 2023
f49e47a
Using the terrain name in the humanoid visualizer and avoid to overwr…
S-Dafarra Nov 3, 2023
4ae3719
Extracted function to create video from frames
S-Dafarra Nov 3, 2023
96c85bb
Added possibility to visualize multiple states at once
S-Dafarra Nov 4, 2023
62e0450
Added method to convert from list to FootContactState
S-Dafarra Nov 23, 2023
d710893
Turned FootContactState into an OptimizationObject
S-Dafarra Nov 24, 2023
a9dae49
Added conversion from FloatingBaseSystem to FloatingBaseSystemState
S-Dafarra Nov 24, 2023
f1943cc
Removed centroidal momentum from the HumanoidState
S-Dafarra Nov 24, 2023
31d2c8f
Added possibility to move from HumanoidState to ExtendedhumanoidState
S-Dafarra Nov 27, 2023
e72646c
Added first implementation of interpolators to define initial guess
S-Dafarra Nov 27, 2023
25fa92d
Added humanoid state interpolator
S-Dafarra Nov 28, 2023
802ad22
Catch ffmpeg exceptions when creating the video
S-Dafarra Nov 28, 2023
669c4e6
Robustification of the visualization in case the inputs are not in th…
S-Dafarra Nov 29, 2023
8c2582b
Improved the definition of the FootContactPhaseDescriptor
S-Dafarra Nov 29, 2023
abaebf6
Interpolators bugfix
S-Dafarra Nov 29, 2023
b276b06
Using __post_init__ in variables differently
S-Dafarra Nov 29, 2023
6fbc310
The foot contact state interpolator does not need to start from the f…
S-Dafarra Nov 29, 2023
f449c34
Allowed interpolating even if the initial time is a swing moment
S-Dafarra Dec 6, 2023
128702c
Fix creation of ground terrain urdf
S-Dafarra Dec 24, 2023
9bfeff8
Avoid to use glob when saving video.
S-Dafarra Dec 29, 2023
55bdcd8
Fixed visualization of the last state.
S-Dafarra Dec 29, 2023
72b88fe
Moved PlanarTerrain to a dedicated file
S-Dafarra Dec 29, 2023
f3beaff
Added first draft of SmoothTerrain
S-Dafarra Jan 2, 2024
0bb4cc1
Moved terrain visualization to separate file
S-Dafarra Jan 2, 2024
30cdbc9
Added first test for SmoothTerrain and debugged
S-Dafarra Jan 2, 2024
00b3744
The height offset in the Smooth Terrain is useless
S-Dafarra Jan 3, 2024
8801f87
Using arrow visualization shipped with iDynTree
S-Dafarra Jan 3, 2024
410d5ea
Allow visualizing terrain normals
S-Dafarra Jan 3, 2024
1671c5b
Allow visualizing terrain frames
S-Dafarra Jan 3, 2024
52f84a8
Added computation of SmoothTerrain normals and frames
S-Dafarra Jan 3, 2024
decc719
Improved the effect of the offset and transformation matrix applied t…
S-Dafarra Jan 3, 2024
d5ad361
Moved computation of normal and terrain orientation directly in terra…
S-Dafarra Jan 4, 2024
b25e8a6
Added possibility to sum smooth terrains
S-Dafarra Jan 4, 2024
e56808f
Removed leftover in humanoid_state_visualizer.py
S-Dafarra Jan 4, 2024
91b6210
Added shortcut functions to create steps, cylinders and planes
S-Dafarra Jan 4, 2024
6b483d4
Added possibility to set the name in the terrain descriptor
S-Dafarra Jan 4, 2024
94a2e83
Fixed bug in humanoid_state_visualizer.py
S-Dafarra Jan 4, 2024
c704337
Added a detail on why the rotations are limited in the SmoothTerrain
S-Dafarra Jan 4, 2024
bea4dfa
Fixed a bug in the footsteps interpolation
S-Dafarra Jan 4, 2024
8b37307
The terrain was not correctly passed in the contact state plotter
S-Dafarra Jan 11, 2024
df7a3fa
Improved documentation of smooth terrain
S-Dafarra Jan 17, 2024
18b27a6
Apply suggestions from code review to centroidal expressions
S-Dafarra Jan 19, 2024
f01632b
Improved OptiCallback.
S-Dafarra Jan 19, 2024
11e715e
Pattern improvements on contact expressions after review
S-Dafarra Jan 19, 2024
12480c5
Improved pattern on interpolators
S-Dafarra Jan 19, 2024
57e4b49
Improved patterns on variables
S-Dafarra Jan 19, 2024
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 src/hippopt/deps/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .surf2stl import surf2stl
7 changes: 7 additions & 0 deletions src/hippopt/deps/surf2stl/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2020 asahidari

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions src/hippopt/deps/surf2stl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .surf2stl import write
254 changes: 254 additions & 0 deletions src/hippopt/deps/surf2stl/surf2stl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
### surf2stl.py --- Write a surface to a STL format file ---

### Copyright (c) 2020 asahidari

### This software is released under the MIT License.
### http://opensource.org/licenses/mit-license.php

### Functions in this script (write, tri_write) export
### a stereolithography (STL) file for a surface with geometry
### defined by three matrix arguments, X, Y and Z.

### This idea mainly come from surf2stl.m in MATLAB(or Octave):
### https://jp.mathworks.com/matlabcentral/fileexchange/4512-surf2stl

import datetime
import math
import struct

import numpy as np
from scipy.spatial import Delaunay


def write(filename, x, y, z, mode="binary"):
"""
Write a stl file for a surface with geometry
defined from three matrix arguments, x, y, and z.
Meshes are triangulated sequencially along xyz order.

Parameters
----------
filename : string
output file name

x, y, z : ndarray
Arguments x, y can be 1-dimensional arrays or 2-dimensional grids
(usually generated by np.meshgrid(x,y)), and z must be 2-dimensional,
which must have len(x)=n, len(y)=m where z.shape[m,n].

mode : string
STL file format, 'ascii' or 'binary'(default).

Examples
----------
import numpy as np
import surf2stl

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X ** 2 + Y ** 2))

surf2stl.write('3d-sinusoidal.stl', X, Y, Z)

"""

if type(filename) is not str:
raise Exception("Invalid filename")

if mode != "ascii":
mode = "binary"

if z.ndim != 2:
raise Exception("Variable z must be a 2-dimensional array")

### x, y can not be used as dx, dy in Python
### if arguments type of x(or y) is 'int',
### type error will raise in next 'if' block
# if type(x) == int and type(y) == int:
# x = np.arange(0, z.shape[1], x)
# x = np.arange(0, z.shape[0], y)

if (
len(x.shape) == 1
and x.shape[0] == z.shape[1]
and len(y.shape) == 1
and y.shape[0] == z.shape[0]
):
x, y = np.meshgrid(x, y)

if (
len(x.shape) != len(z.shape)
or len(y.shape) != len(z.shape)
or x.shape[1] != z.shape[1]
or y.shape[0] != z.shape[0]
):
raise Exception("Unable to resolve x and y variables")

nfacets = 0
title_str = "Created by surf2stl.py %s" % datetime.datetime.now().strftime(
"%d-%b-%Y %H:%M:%S"
)

f = open(filename, "wb" if mode != "ascii" else "w")

if mode == "ascii":
f.write("solid %s\n" % title_str)
else:
title_str_ljust = title_str.ljust(80)
# f.write(title_str_ljust.encode('utf-8')) # same as 'ascii' for alphabet characters
f.write(title_str_ljust.encode("ascii"))
f.write(struct.pack("i", 0))

for i in range(z.shape[0] - 1):
for j in range(z.shape[1] - 1):
p1 = np.array([x[i, j], y[i, j], z[i, j]])
p2 = np.array([x[i, j + 1], y[i, j + 1], z[i, j + 1]])
p3 = np.array([x[i + 1, j + 1], y[i + 1, j + 1], z[i + 1, j + 1]])
val = local_write_facet(f, p1, p2, p3, mode)
nfacets += val

p1 = np.array([x[i + 1, j + 1], y[i + 1, j + 1], z[i + 1, j + 1]])
p2 = np.array([x[i + 1, j], y[i + 1, j], z[i + 1, j]])
p3 = np.array([x[i, j], y[i, j], z[i, j]])
val = local_write_facet(f, p1, p2, p3, mode)
nfacets += val

if mode == "ascii":
f.write("endsolid %s\n" % title_str)
else:
f.seek(80, 0)
f.write(struct.pack("i", nfacets))

f.close()

print("Wrote %d facets" % nfacets)
return


def tri_write(filename, x, y, z, tri, mode="binary"):
"""
Write a stl file for a surface with geometry
defined from three matrix arguments, x, y, and z
with Delaunay Triangle parameter(tri).

Meshes are triangulated with the 'tri' parameter
usually made with parameters which determine
the sequence of vertices (like [u, v]).

Parameters
----------
filename : string
output file name

x, y, z : ndarray
Each of these arguments must be 1-dimensional.

tri : scipy.spatial.Delaunay
Delaunay Triangulation object.
When xyz coordinates are determined from other parameters(like (u, v)),
this triangle faces are basically calculated with the parameters.

mode : string
STL file format, 'ascii' or 'binary'(default).

Examples
----------
import numpy as np
from scipy.spatial import Delaunay
import surf2stl

u = np.linspace(0, 2.0 * np.pi, endpoint=True, num=50)
v = np.linspace(-0.5, 0.5, endpoint=True, num=10)
u, v = np.meshgrid(u, v)
u, v = u.flatten(), v.flatten()

x = (1 + 0.5 * v * np.cos(u / 2.0)) * np.cos(u)
y = (1 + 0.5 * v * np.cos(u / 2.0)) * np.sin(u)
z = 0.5 * v * np.sin(u / 2.0)

delaunay_tri = Delaunay(np.array([u, v]).T)
surf2stl.tri_write('mobius.stl', x, y, z, delaunay_tri)

"""

if type(filename) is not str:
raise Exception("Invalid filename")

if mode != "ascii":
mode = "binary"

if len(x.shape) != 1 or len(y.shape) != 1 or len(z.shape) != 1:
raise Exception("Each variable x,y,z must be a 1-dimensional array")

if x.shape[0] != z.shape[0] or y.shape[0] != z.shape[0]:
raise Exception("Number of x,y,z elements must be equal")

nfacets = 0
title_str = "Created by surf2stl.py %s" % datetime.datetime.now().strftime(
"%d-%b-%Y %H:%M:%S"
)

f = open(filename, "wb" if mode != "ascii" else "w")

if mode == "ascii":
f.write("solid %s\n" % title_str)
else:
title_str_ljust = title_str.ljust(80)
# f.write(title_str_ljust.encode('utf-8')) # same as 'ascii' for alphabet characters
f.write(title_str_ljust.encode("ascii"))
f.write(struct.pack("i", 0))

indices = tri.simplices
verts = tri.points[indices]
for i in range(0, indices.shape[0], 1):
p = indices[i]
p1 = np.array([x[p[0]], y[p[0]], z[p[0]]])
p2 = np.array([x[p[1]], y[p[1]], z[p[1]]])
p3 = np.array([x[p[2]], y[p[2]], z[p[2]]])
val = local_write_facet(f, p1, p2, p3, mode)
nfacets += val

if mode == "ascii":
f.write("endsolid %s\n" % title_str)
else:
f.seek(80, 0)
f.write(struct.pack("i", nfacets))

f.close()

print("Wrote %d facets" % nfacets)
return


# Local subfunctions


def local_write_facet(f, p1, p2, p3, mode):
if np.isnan(p1).any() or np.isnan(p2).any() or np.isnan(p3).any():
return 0

n = local_find_normal(p1, p2, p3)
if mode == "ascii":
f.write("facet normal %.7f %.7f %.7f\n" % (n[0], n[1], n[2]))
f.write("outer loop\n")
f.write("vertex %.7f %.7f %.7f\n" % (p1[0], p1[1], p1[2]))
f.write("vertex %.7f %.7f %.7f\n" % (p2[0], p2[1], p2[2]))
f.write("vertex %.7f %.7f %.7f\n" % (p3[0], p3[1], p3[2]))
f.write("endloop\n")
f.write("endfacet\n")
else:
f.write(struct.pack("%sf" % len(n), *n))
f.write(struct.pack("%sf" % len(p1), *p1))
f.write(struct.pack("%sf" % len(p2), *p2))
f.write(struct.pack("%sf" % len(p3), *p3))
f.write(struct.pack("h", 0))
return 1


def local_find_normal(p1, p2, p3):
v1 = p2 - p1
v2 = p3 - p1
v3 = np.cross(v1, v2)
n = v3 / math.sqrt(np.sum(v3 * v3))
return n
77 changes: 77 additions & 0 deletions src/hippopt/robot_planning/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from . import expressions, utilities, variables
from .expressions.centroidal import (
centroidal_dynamics_with_point_forces,
com_dynamics_from_momentum,
)
from .expressions.complementarity import (
dcc_complementarity_margin,
dcc_planar_complementarity,
relaxed_complementarity_margin,
)
from .expressions.contacts import (
contact_points_centroid,
contact_points_yaw_alignment_error,
friction_cone_square_margin,
normal_force_component,
swing_height_heuristic,
)
from .expressions.kinematics import (
center_of_mass_position_from_kinematics,
centroidal_momentum_from_kinematics,
frames_relative_position,
point_position_from_kinematics,
rotation_error_from_kinematics,
)
from .expressions.quaternion import (
quaternion_xyzw_error,
quaternion_xyzw_normalization,
quaternion_xyzw_velocity_to_right_trivialized_angular_velocity,
)
from .utilities.foot_contact_state_plotter import (
ContactPointStatePlotter,
ContactPointStatePlotterSettings,
FootContactStatePlotter,
FootContactStatePlotterSettings,
)
from .utilities.humanoid_state_visualizer import (
HumanoidStateVisualizer,
HumanoidStateVisualizerSettings,
)
from .utilities.interpolators import (
feet_contact_points_interpolator,
floating_base_system_state_interpolator,
foot_contact_state_interpolator,
free_floating_object_state_interpolator,
humanoid_state_interpolator,
kinematic_tree_state_interpolator,
linear_interpolator,
quaternion_slerp,
transform_interpolator,
)
from .utilities.planar_terrain import PlanarTerrain
from .utilities.smooth_terrain import SmoothTerrain
from .utilities.terrain_descriptor import TerrainDescriptor
from .utilities.terrain_sum import TerrainSum
from .utilities.terrain_visualizer import TerrainVisualizer, TerrainVisualizerSettings
from .variables.contacts import (
ContactPointDescriptor,
ContactPointState,
ContactPointStateDerivative,
FeetContactPhasesDescriptor,
FeetContactPointDescriptors,
FeetContactPoints,
FootContactPhaseDescriptor,
FootContactState,
)
from .variables.floating_base import (
FloatingBaseSystem,
FloatingBaseSystemState,
FloatingBaseSystemStateDerivative,
FreeFloatingObject,
FreeFloatingObjectState,
FreeFloatingObjectStateDerivative,
KinematicTree,
KinematicTreeState,
KinematicTreeStateDerivative,
)
from .variables.humanoid import HumanoidState
Empty file.
1 change: 1 addition & 0 deletions src/hippopt/robot_planning/expressions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import complementarity, contacts, kinematics
Loading
Loading