-
Notifications
You must be signed in to change notification settings - Fork 11
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
Added usage section in ReadMe and basic example for simulator #314
base: main
Are you sure you want to change the base?
Added usage section in ReadMe and basic example for simulator #314
Conversation
When loading the icub model, I encountered in the following warning:
I am having issues in backtracking the source, @flferretti do you perhaps know which part of rod raises this warning ? thanks ! |
Hi @CarlottaSartore, thanks for the contribution! The warning is not raised from |
Related: scipy/scipy#19512 and https://mail.python.org/archives/list/scipy-dev@python.org/message/LXT4ZGNP4OVAARSKEOM5AGQCANH6ZV5K/ . I totally agree that this should not be a warning, or there should be a way to provide this conversion without triggering a warning, but unfortunately there was not a follow up in the mailing list. |
Early comment: can we use - import icub_models
- model_path = icub_models.get_model_file("iCubGazeboV2_5")
+ import robot_description.icub_description
+ model_path = robot_description.icub_description.URDF_PATH |
I strongly prefer use any other robot in that case. There is nothing ensuring that the |
I see, thanks for the explanation! I was wondering though if we need the actual IIT model since this is just an example |
The problem is that lab users starts from examples, that they copy and they start using assuming that that is the "best way" to do, especially if the info come from an official repo of the lab. I really do not look forward to having to explain each new student in the lab why they should not be using |
(I also edited my previous comments for more details). |
I opted to use the icub models since it is the model of a humanoid robots, and I wanted to use a robot since this is the main goal of the simulator, and the icub is the model I am the more confident with. |
If it helps reach consensus, feel free to use
The tricky aspect there is that you also need to download the meshes if you want the visualizer to work. |
Thanks @traversaro, I understood the problem. It seems to me that the available options are:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for working on this @CarlottaSartore! I added some comments
"data_batch_t0 = jax.vmap(\n", | ||
" lambda key: data_zero.reset_base_position(base_position=jnp.array([0.0, 0.0, 1.0]))\n", | ||
")(jnp.vstack(subkeys))\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we could think of using js.data.random_model_data
instead, since key
is not really used here, or generating a vector of equal base positions like:
data_batch_v0 = jax.vmap(
data_zero.reset_base_position
)(base_position=jnp.array([0.0, 0.0, 1.0] * batch_size))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with random data is that is is difficult to ensure that all the simulations succed. this is the reason why I decided to set all of them to 1.0 to show how to handle batched initial data while still being sure that all simulations somehow work. Changing the initial base position leads the simulator to encounter "not normalize quaternion" error. I would prefer to leave it like this until we solve such an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I didn't mean to use random data. My comment was about which argument to pass to vmap, the result will be the same
```python | ||
import jax | ||
import jax.numpy as jnp | ||
import jaxsim.api as js | ||
import icub_models | ||
import pathlib | ||
|
||
# Load the iCub model | ||
model_path = icub_models.get_model_file("iCubGazeboV2_5") | ||
joints = ('torso_pitch', 'torso_roll', 'torso_yaw', 'l_shoulder_pitch', | ||
'l_shoulder_roll', 'l_shoulder_yaw', 'l_elbow', 'r_shoulder_pitch', | ||
'r_shoulder_roll', 'r_shoulder_yaw', 'r_elbow', 'l_hip_pitch', | ||
'l_hip_roll', 'l_hip_yaw', 'l_knee', 'l_ankle_pitch', 'l_ankle_roll', | ||
'r_hip_pitch', 'r_hip_roll', 'r_hip_yaw', 'r_knee', 'r_ankle_pitch', | ||
'r_ankle_roll') | ||
ndof = len(joints) | ||
|
||
# Build and reduce the model | ||
model_description = pathlib.Path(model_path) | ||
full_model = js.model.JaxSimModel.build_from_model_description( | ||
model_description=model_description, time_step=0.0001, is_urdf=True | ||
) | ||
model = js.model.reduce(model=full_model, considered_joints=joints) | ||
|
||
# Initialize data and simulation | ||
data = js.data.JaxSimModelData.zero(model=model).reset_base_position( | ||
base_position=jnp.array([0.0, 0.0, 1.0]) | ||
) | ||
T = jnp.arange(start=0, stop=1.0, step=model.time_step) | ||
tau = jnp.zeros(ndof) | ||
|
||
# Simulate | ||
for t in T: | ||
data, _ = js.model.step(model=model, data=data, link_forces=None, joint_force_references=tau) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about compressing the example code in a collapsible section?
I also removed an unused import and reformatted
```python | |
import jax | |
import jax.numpy as jnp | |
import jaxsim.api as js | |
import icub_models | |
import pathlib | |
# Load the iCub model | |
model_path = icub_models.get_model_file("iCubGazeboV2_5") | |
joints = ('torso_pitch', 'torso_roll', 'torso_yaw', 'l_shoulder_pitch', | |
'l_shoulder_roll', 'l_shoulder_yaw', 'l_elbow', 'r_shoulder_pitch', | |
'r_shoulder_roll', 'r_shoulder_yaw', 'r_elbow', 'l_hip_pitch', | |
'l_hip_roll', 'l_hip_yaw', 'l_knee', 'l_ankle_pitch', 'l_ankle_roll', | |
'r_hip_pitch', 'r_hip_roll', 'r_hip_yaw', 'r_knee', 'r_ankle_pitch', | |
'r_ankle_roll') | |
ndof = len(joints) | |
# Build and reduce the model | |
model_description = pathlib.Path(model_path) | |
full_model = js.model.JaxSimModel.build_from_model_description( | |
model_description=model_description, time_step=0.0001, is_urdf=True | |
) | |
model = js.model.reduce(model=full_model, considered_joints=joints) | |
# Initialize data and simulation | |
data = js.data.JaxSimModelData.zero(model=model).reset_base_position( | |
base_position=jnp.array([0.0, 0.0, 1.0]) | |
) | |
T = jnp.arange(start=0, stop=1.0, step=model.time_step) | |
tau = jnp.zeros(ndof) | |
# Simulate | |
for t in T: | |
data, _ = js.model.step(model=model, data=data, link_forces=None, joint_force_references=tau) | |
<details> | |
<summary><b>Using JaxSim as a simulator</b></summary> | |
```python | |
import jax.numpy as jnp | |
import jaxsim.api as js | |
import icub_models | |
import pathlib | |
# Load the iCub model. | |
model_path = icub_models.get_model_file("iCubGazeboV2_5") | |
joints = ('torso_pitch', 'torso_roll', 'torso_yaw', 'l_shoulder_pitch', | |
'l_shoulder_roll', 'l_shoulder_yaw', 'l_elbow', 'r_shoulder_pitch', | |
'r_shoulder_roll', 'r_shoulder_yaw', 'r_elbow', 'l_hip_pitch', | |
'l_hip_roll', 'l_hip_yaw', 'l_knee', 'l_ankle_pitch', 'l_ankle_roll', | |
'r_hip_pitch', 'r_hip_roll', 'r_hip_yaw', 'r_knee', 'r_ankle_pitch', | |
'r_ankle_roll') | |
# Build and reduce the model. | |
model_description = pathlib.Path(model_path) | |
full_model = js.model.JaxSimModel.build_from_model_description( | |
model_description=model_description, time_step=0.001, is_urdf=True | |
) | |
model = js.model.reduce(model=full_model, considered_joints=joints) | |
# Initialize data and simulation. | |
data = js.data.JaxSimModelData.zero(model=model).reset_base_position( | |
base_position=jnp.array([0.0, 0.0, 1.0]) | |
) | |
T = jnp.arange(start=0, stop=1.0, step=model.time_step) | |
tau = jnp.zeros(model.dofs()) | |
# Simulate. | |
for _t in T: | |
data, _ = js.model.step( | |
model=model, data=data, link_forces=None, joint_force_references=tau | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SInce the example are quite short I would prefer to have them directly in the readme, so that it is directly visible that it is possible to use jaxsim within few lines of code, what do you think ?
``` python | ||
import jax.numpy as jnp | ||
import jaxsim.api as js | ||
import icub_models | ||
import pathlib | ||
|
||
# Load the iCub model | ||
model_path = icub_models.get_model_file("iCubGazeboV2_5") | ||
joints = ('torso_pitch', 'torso_roll', 'torso_yaw', 'l_shoulder_pitch', | ||
'l_shoulder_roll', 'l_shoulder_yaw', 'l_elbow', 'r_shoulder_pitch', | ||
'r_shoulder_roll', 'r_shoulder_yaw', 'r_elbow', 'l_hip_pitch', | ||
'l_hip_roll', 'l_hip_yaw', 'l_knee', 'l_ankle_pitch', 'l_ankle_roll', | ||
'r_hip_pitch', 'r_hip_roll', 'r_hip_yaw', 'r_knee', 'r_ankle_pitch', | ||
'r_ankle_roll') | ||
|
||
# Build and reduce the model | ||
model_description = pathlib.Path(model_path) | ||
full_model = js.model.JaxSimModel.build_from_model_description( | ||
model_description=model_description, time_step=0.0001, is_urdf=True | ||
) | ||
model = js.model.reduce(model=full_model, considered_joints=joints) | ||
|
||
# Initialize model data | ||
data = js.data.JaxSimModelData.zero(model=model).reset_base_position( | ||
base_position=jnp.array([0.0, 0.0, 1.0]) | ||
) | ||
|
||
# Frame and dynamics computations | ||
frame_index = js.frame.name_to_idx(model=model, frame_name="l_foot") | ||
W_H_F = js.frame.transform(model=model, data=data, frame_index=frame_index) # Frame transformation | ||
W_J_F = js.frame.jacobian(model=model, data=data, frame_index=frame_index) # Frame Jacobian | ||
|
||
# Dynamics properties | ||
M = js.model.free_floating_mass_matrix(model=model, data=data) # Mass matrix | ||
h = js.model.free_floating_bias_forces(model=model, data=data) # Bias forces | ||
g = js.model.free_floating_gravity_forces(model=model, data=data) # Gravity forces | ||
C = js.model.free_floating_coriolis_matrix(model=model, data=data) # Coriolis matrix | ||
|
||
# Print dynamics results | ||
print(f"M: shape={M.shape}, h: shape={h.shape}, g: shape={g.shape}, C: shape={C.shape}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collapsible section as above. Reformatted and used f-string syntax to print the shapes
``` python | |
import jax.numpy as jnp | |
import jaxsim.api as js | |
import icub_models | |
import pathlib | |
# Load the iCub model | |
model_path = icub_models.get_model_file("iCubGazeboV2_5") | |
joints = ('torso_pitch', 'torso_roll', 'torso_yaw', 'l_shoulder_pitch', | |
'l_shoulder_roll', 'l_shoulder_yaw', 'l_elbow', 'r_shoulder_pitch', | |
'r_shoulder_roll', 'r_shoulder_yaw', 'r_elbow', 'l_hip_pitch', | |
'l_hip_roll', 'l_hip_yaw', 'l_knee', 'l_ankle_pitch', 'l_ankle_roll', | |
'r_hip_pitch', 'r_hip_roll', 'r_hip_yaw', 'r_knee', 'r_ankle_pitch', | |
'r_ankle_roll') | |
# Build and reduce the model | |
model_description = pathlib.Path(model_path) | |
full_model = js.model.JaxSimModel.build_from_model_description( | |
model_description=model_description, time_step=0.0001, is_urdf=True | |
) | |
model = js.model.reduce(model=full_model, considered_joints=joints) | |
# Initialize model data | |
data = js.data.JaxSimModelData.zero(model=model).reset_base_position( | |
base_position=jnp.array([0.0, 0.0, 1.0]) | |
) | |
# Frame and dynamics computations | |
frame_index = js.frame.name_to_idx(model=model, frame_name="l_foot") | |
W_H_F = js.frame.transform(model=model, data=data, frame_index=frame_index) # Frame transformation | |
W_J_F = js.frame.jacobian(model=model, data=data, frame_index=frame_index) # Frame Jacobian | |
# Dynamics properties | |
M = js.model.free_floating_mass_matrix(model=model, data=data) # Mass matrix | |
h = js.model.free_floating_bias_forces(model=model, data=data) # Bias forces | |
g = js.model.free_floating_gravity_forces(model=model, data=data) # Gravity forces | |
C = js.model.free_floating_coriolis_matrix(model=model, data=data) # Coriolis matrix | |
# Print dynamics results | |
print(f"M: shape={M.shape}, h: shape={h.shape}, g: shape={g.shape}, C: shape={C.shape}") | |
<details> | |
<summary><b>Using JaxSim as a multibody dynamics library</b></summary> | |
``` python | |
import jax.numpy as jnp | |
import jaxsim.api as js | |
import icub_models | |
import pathlib | |
# Load the iCub model. | |
model_path = icub_models.get_model_file("iCubGazeboV2_5") | |
joints = ('torso_pitch', 'torso_roll', 'torso_yaw', 'l_shoulder_pitch', | |
'l_shoulder_roll', 'l_shoulder_yaw', 'l_elbow', 'r_shoulder_pitch', | |
'r_shoulder_roll', 'r_shoulder_yaw', 'r_elbow', 'l_hip_pitch', | |
'l_hip_roll', 'l_hip_yaw', 'l_knee', 'l_ankle_pitch', 'l_ankle_roll', | |
'r_hip_pitch', 'r_hip_roll', 'r_hip_yaw', 'r_knee', 'r_ankle_pitch', | |
'r_ankle_roll') | |
# Build and reduce the model. | |
model_description = pathlib.Path(model_path) | |
full_model = js.model.JaxSimModel.build_from_model_description( | |
model_description=model_description, time_step=0.0001, is_urdf=True | |
) | |
model = js.model.reduce(model=full_model, considered_joints=joints) | |
# Initialize model data. | |
data = js.data.JaxSimModelData.zero(model=model).reset_base_position( | |
base_position=jnp.array([0.0, 0.0, 1.0]) | |
) | |
# Frame and dynamics computations. | |
frame_index = js.frame.name_to_idx(model=model, frame_name="l_foot") | |
W_H_F = js.frame.transform(model=model, data=data, frame_index=frame_index) # Frame transformation | |
W_J_F = js.frame.jacobian(model=model, data=data, frame_index=frame_index) # Frame Jacobian | |
# Dynamics properties. | |
M = js.model.free_floating_mass_matrix(model=model, data=data) # Mass matrix | |
h = js.model.free_floating_bias_forces(model=model, data=data) # Bias forces | |
g = js.model.free_floating_gravity_forces(model=model, data=data) # Gravity forces | |
C = js.model.free_floating_coriolis_matrix(model=model, data=data) # Coriolis matrix | |
# Print dynamics results. | |
print(f"{M.shape=} \t {h.shape=} \t {g.shape=} \t {C.shape=}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comments above about the collapse
Stickbot is nice as it is a self-contained URDF. I think there is also some value in showing to users how to load a physical URDF. My assumption is that most users will want to use the simulator with their own model for which they either have the absolute file path or the relocatable |
Co-authored-by: Filippo Luca Ferretti <102977828+flferretti@users.noreply.github.com>
Co-authored-by: Filippo Luca Ferretti <102977828+flferretti@users.noreply.github.com>
Co-authored-by: Filippo Luca Ferretti <102977828+flferretti@users.noreply.github.com>
Co-authored-by: Filippo Luca Ferretti <102977828+flferretti@users.noreply.github.com>
Co-authored-by: Filippo Luca Ferretti <102977828+flferretti@users.noreply.github.com>
Co-authored-by: Filippo Luca Ferretti <102977828+flferretti@users.noreply.github.com>
Concerning the usage of model descriptor, I would stay with the icub models dependency. even though it introduces a new dependencies it shows how:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @CarlottaSartore ! It LGTM! I will update the title of the examples/jaxsim_as_physics_engine_advanced.ipynb
notebook since up to now it's the same of the non advanced one :)
Co-authored-by: Alessandro Croci <57228872+xela-95@users.noreply.github.com>
Thanks @xela-95 you're right! I have also updated the example readme with the new example ! |
This PR:
example_physics_engine
inexample_physics_engine_advanced
📚 Documentation preview 📚: https://jaxsim--314.org.readthedocs.build//314/