Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
99 changes: 79 additions & 20 deletions examples/jaxsim_walking.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -115,14 +115,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# ==== Set simulation parameters ====\n",
"\n",
"T = 6.0\n",
"js_dt = 0.000_5"
"js_dt = 0.001"
]
},
{
Expand All @@ -148,7 +148,7 @@
"source": [
"# ==== Define JaxSim simulator and set initial position ====\n",
"\n",
"js = JaxsimSimulator(dt=js_dt, contact_model_type=JaxsimContactModelEnum.RIGID)\n",
"js = JaxsimSimulator(dt=js_dt, contact_model_type=JaxsimContactModelEnum.RELAXED_RIGID)\n",
"js.load_model(\n",
" robot_model=robot_model_init,\n",
" s=s_0,\n",
Expand All @@ -165,7 +165,7 @@
"print(f\"Contact model in use: {js._model.contact_model}\")\n",
"print(f\"Link names:\\n{js.link_names}\")\n",
"print(f\"Frame names:\\n{js.frame_names}\")\n",
"print(f\"Mass: {js.total_mass*js._data.standard_gravity()} N\")"
"print(f\"Mass: {-js.total_mass*js._model.gravity} N\")"
]
},
{
Expand Down Expand Up @@ -197,11 +197,30 @@
"# Set desired quantities\n",
"mpc.configure(s_init=s_0, H_b_init=H_b_0)\n",
"tsid.compute_com_position()\n",
"mpc.define_test_com_traj(tsid.COM.toNumPy())\n",
"\n",
"mpc.define_test_com_traj(tsid.COM.toNumPy())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Set initial robot state and plan trajectories\n",
"\n",
"tic = time.perf_counter()\n",
"\n",
"js.step(dry_run=True)\n",
"\n",
"step_compilation_time_s = time.perf_counter() - tic"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Reading the state\n",
"s_js, ds_js, tau_js = js.get_state()\n",
"H_b = js.base_transform\n",
Expand All @@ -215,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -245,6 +264,7 @@
" tau_tsid_log = []\n",
" W_p_CoM_tsid_log = []\n",
" t_log = []\n",
" wall_time_step_log = []\n",
"\n",
" # Define number of steps\n",
" n_step_tsid_js = int(tsid.frequency / js_dt)\n",
Expand Down Expand Up @@ -309,16 +329,20 @@
"\n",
" # Step the simulator\n",
" js.set_input(tau_tsid)\n",
"\n",
" tic = time.perf_counter()\n",
" js.step(n_step=n_step_tsid_js)\n",
" toc = time.perf_counter() - tic\n",
"\n",
" counter = counter + 1\n",
"\n",
" if counter == n_step_mpc_tsid:\n",
" counter = 0\n",
"\n",
" # Stop the simulation if the robot fell down\n",
" if js._data.base_position()[2] < 0.5:\n",
" print(f\"Robot fell down at t={t:.4f}s.\")\n",
" break\n",
" # if js._data.base_position()[2] < 0.5:\n",
" # print(f\"Robot fell down at t={t:.4f}s.\")\n",
" # break\n",
"\n",
" # Log data\n",
" # TODO transform mpc contact forces to wrenches to be compared with jaxsim ones\n",
Expand All @@ -336,6 +360,7 @@
" W_p_lf_sfp_log.append(lf_sfp.transform.translation())\n",
" W_p_rf_sfp_log.append(rf_sfp.transform.translation())\n",
" W_p_CoM_tsid_log.append(tsid.COM.toNumPy())\n",
" wall_time_step_log.append(toc * 1e3)\n",
" if contact_model_type != JaxsimContactModelEnum.VISCO_ELASTIC:\n",
" f_lf_js, f_rf_js = js.feet_wrench\n",
" f_lf_js_log.append(f_lf_js)\n",
Expand All @@ -360,6 +385,7 @@
" \"W_p_lf_sfp\": np.array(W_p_lf_sfp_log),\n",
" \"W_p_rf_sfp\": np.array(W_p_rf_sfp_log),\n",
" \"W_p_CoM_tsid\": np.array(W_p_CoM_tsid_log),\n",
" \"wall_time_step\": np.array(wall_time_step_log),\n",
" }\n",
" if contact_model_type != JaxsimContactModelEnum.VISCO_ELASTIC:\n",
" logs[\"f_lf_js\"] = np.array(f_lf_js_log)\n",
Expand All @@ -385,19 +411,16 @@
"avg_iter_time_ms = (wall_time / (T / js_dt)) * 1000\n",
"\n",
"print(\n",
" f\"\\nRunning simulation took {wall_time:.2f}s for {T:.3f}s simulated time. \\nIteration avg time of {avg_iter_time_ms:.1f} ms.\"\n",
")\n",
"print(f\"RTF: {T / wall_time * 100:.2f}%\")"
" f\"\\nSimulation done.\\nRunning simulation took {wall_time:.2f}s for {T:.3f}s simulated time.\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# ==== Plot the results ====\n",
"\n",
"# Extract logged variables\n",
"t = logs[\"t\"]\n",
"s_js = logs[\"s_js\"]\n",
Expand All @@ -412,9 +435,45 @@
"W_p_lf_sfp = logs[\"W_p_lf_sfp\"]\n",
"W_p_rf_sfp = logs[\"W_p_rf_sfp\"]\n",
"W_p_CoM_tsid = logs[\"W_p_CoM_tsid\"]\n",
"wall_time_step = logs[\"wall_time_step\"]\n",
"if js.contact_model_type != JaxsimContactModelEnum.VISCO_ELASTIC:\n",
" f_lf_js = logs[\"f_lf_js\"]\n",
" f_rf_js = logs[\"f_rf_js\"]\n",
" f_rf_js = logs[\"f_rf_js\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compute simulator step runtime statistics and RTF\n",
"\n",
"min_step_time = np.min(wall_time_step)\n",
"max_step_time = np.max(wall_time_step)\n",
"avg_step_time = np.mean(wall_time_step)\n",
"std_step_time = np.std(wall_time_step)\n",
"total_step_time = np.sum(wall_time_step)\n",
"rtf = (T * 1e3) / total_step_time * 100\n",
"\n",
"print(\"===========================================\")\n",
"print(f\"Step compilation time: {step_compilation_time_s:.2f} s\")\n",
"print(f\"Min step time: {min_step_time:.2f} ms\")\n",
"print(f\"Max step time: {max_step_time:.2f} ms\")\n",
"print(f\"Average step time: {avg_step_time:.2f} ms\")\n",
"print(f\"Std deviation step time: {std_step_time:.2f} ms\")\n",
"print(f\"RTF: {rtf:.1f}%\")\n",
"print(\"===========================================\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==== Plot the results ====\n",
"\n",
"\n",
"n_sim_steps = s_js.shape[0]\n",
"s_0 = np.full_like(a=s_js, fill_value=s_0)\n",
Expand Down Expand Up @@ -577,7 +636,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "comodo",
"display_name": "comododev",
"language": "python",
"name": "python3"
},
Expand All @@ -591,7 +650,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.10.15"
}
},
"nbformat": 4,
Expand Down
Loading