Skip to content

Commit 70e0608

Browse files
authored
1 parent d0c90b5 commit 70e0608

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

book/robot/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ rt_ipynb_test(
2424
],
2525
)
2626

27+
rt_ipynb_test(
28+
name = "gymnasium_robotics",
29+
srcs = ["gymnasium_robotics.ipynb"],
30+
deps = [
31+
"//manipulation",
32+
"//manipulation:make_drake_compatible_model",
33+
],
34+
)
35+
2736
rt_ipynb_test(
2837
name = "simulation",
2938
srcs = ["simulation.ipynb"],

book/robot/gymnasium_robotics.ipynb

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "05124c35",
6+
"metadata": {
7+
"colab_type": "text",
8+
"id": "EgiF12Hf1Dhs"
9+
},
10+
"source": [
11+
"This notebook provides examples to go along with the [textbook](http://manipulation.csail.mit.edu/robot.html). I recommend having both windows open, side-by-side!"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 7,
17+
"id": "99268d0c",
18+
"metadata": {
19+
"colab": {},
20+
"colab_type": "code",
21+
"id": "eeMrMI0-1Dhu",
22+
"lines_to_end_of_cell_marker": 2
23+
},
24+
"outputs": [],
25+
"source": [
26+
"import numpy as np\n",
27+
"from pydrake.all import PackageMap, Parser, RobotDiagramBuilder, Simulator, StartMeshcat\n",
28+
"\n",
29+
"from manipulation import running_as_notebook\n",
30+
"from manipulation.make_drake_compatible_model import MakeDrakeCompatibleModel\n",
31+
"from manipulation.utils import ApplyDefaultVisualization"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"id": "7dec0a5e",
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"# Start the visualizer.\n",
42+
"meshcat = StartMeshcat()"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"id": "11c62164",
48+
"metadata": {},
49+
"source": [
50+
"## Gymnasium Robotics\n",
51+
"\n",
52+
"Includes the following environments:\n",
53+
"- Franka kitchen\n",
54+
"- ..."
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"id": "cfaf4640",
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"# TODO(russt): Use ModelVisualizer pending resolution of Drake#22444.\n",
65+
"if running_as_notebook: # I don't want to download the repo in CI.\n",
66+
" meshcat.Delete()\n",
67+
" builder = RobotDiagramBuilder()\n",
68+
" plant = builder.plant()\n",
69+
" parser = Parser(plant)\n",
70+
" package_map = parser.package_map()\n",
71+
" package_map.AddRemote(\n",
72+
" package_name=\"gymnasium_robotics\",\n",
73+
" params=PackageMap.RemoteParams(\n",
74+
" urls=[\n",
75+
" f\"https://github.com/Farama-Foundation/Gymnasium-Robotics/archive/refs/tags/v1.3.1.tar.gz\"\n",
76+
" ],\n",
77+
" sha256=(\"d274b3ee1d34337aa35d4686447fda6a6d20dfcb4d375eab91b4e49b1108afde\"),\n",
78+
" strip_prefix=\"Gymnasium-Robotics-1.3.1/gymnasium_robotics/envs/assets/\",\n",
79+
" ),\n",
80+
" )\n",
81+
" original_file = package_map.ResolveUrl(\n",
82+
" \"package://gymnasium_robotics/kitchen_franka/kitchen_assets/kitchen_env_model.xml\"\n",
83+
" )\n",
84+
" drake_compatible_file = original_file.replace(\".xml\", \".drake.xml\")\n",
85+
" MakeDrakeCompatibleModel(original_file, drake_compatible_file)\n",
86+
" model_instances = parser.AddModels(drake_compatible_file)\n",
87+
" plant.Finalize()\n",
88+
" ApplyDefaultVisualization(builder.builder(), meshcat=meshcat)\n",
89+
" diagram = builder.Build()\n",
90+
" simulator = Simulator(diagram)\n",
91+
"\n",
92+
" # Workaround for drake#22444: set the desired state to zero.\n",
93+
" for model_instance in model_instances:\n",
94+
" desired_state_port = plant.get_desired_state_input_port(model_instance)\n",
95+
" if desired_state_port.size() > 0:\n",
96+
" desired_state_port.FixValue(\n",
97+
" plant.GetMyContextFromRoot(simulator.get_context()),\n",
98+
" np.zeros(desired_state_port.size()),\n",
99+
" )\n",
100+
"\n",
101+
" diagram.ForcedPublish(simulator.get_context())"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"id": "4565ef03",
108+
"metadata": {},
109+
"outputs": [],
110+
"source": []
111+
}
112+
],
113+
"metadata": {
114+
"kernelspec": {
115+
"display_name": "Python 3 (ipykernel)",
116+
"language": "python",
117+
"name": "python3"
118+
},
119+
"language_info": {
120+
"codemirror_mode": {
121+
"name": "ipython",
122+
"version": 3
123+
},
124+
"file_extension": ".py",
125+
"mimetype": "text/x-python",
126+
"name": "python",
127+
"nbconvert_exporter": "python",
128+
"pygments_lexer": "ipython3",
129+
"version": "3.10.12"
130+
}
131+
},
132+
"nbformat": 4,
133+
"nbformat_minor": 5
134+
}

manipulation/make_drake_compatible_model.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ def _convert_mjcf(input_filename: str, output_filename: str, overwrite: bool) ->
292292
texturedir = compiler_element.attrib["texturedir"]
293293
del compiler_element.attrib["texturedir"]
294294

295+
# Truncate all rgba attributes to [0, 1]. See Drake#22445.
296+
elements_with_rgba = root.findall(".//*[@rgba]")
297+
for element in elements_with_rgba:
298+
rgba = element.attrib["rgba"]
299+
rgba = [float(value) for value in rgba.split()]
300+
rgba = [min(max(value, 0), 1) for value in rgba]
301+
element.attrib["rgba"] = " ".join([str(value) for value in rgba])
302+
295303
defaults = {}
296304

297305
# Process all default elements recursively

0 commit comments

Comments
 (0)