Skip to content

Commit

Permalink
Adds a franka kitchen example (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
RussTedrake authored Jan 17, 2025
1 parent d0c90b5 commit 70e0608
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
9 changes: 9 additions & 0 deletions book/robot/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ rt_ipynb_test(
],
)

rt_ipynb_test(
name = "gymnasium_robotics",
srcs = ["gymnasium_robotics.ipynb"],
deps = [
"//manipulation",
"//manipulation:make_drake_compatible_model",
],
)

rt_ipynb_test(
name = "simulation",
srcs = ["simulation.ipynb"],
Expand Down
134 changes: 134 additions & 0 deletions book/robot/gymnasium_robotics.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "05124c35",
"metadata": {
"colab_type": "text",
"id": "EgiF12Hf1Dhs"
},
"source": [
"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!"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "99268d0c",
"metadata": {
"colab": {},
"colab_type": "code",
"id": "eeMrMI0-1Dhu",
"lines_to_end_of_cell_marker": 2
},
"outputs": [],
"source": [
"import numpy as np\n",
"from pydrake.all import PackageMap, Parser, RobotDiagramBuilder, Simulator, StartMeshcat\n",
"\n",
"from manipulation import running_as_notebook\n",
"from manipulation.make_drake_compatible_model import MakeDrakeCompatibleModel\n",
"from manipulation.utils import ApplyDefaultVisualization"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7dec0a5e",
"metadata": {},
"outputs": [],
"source": [
"# Start the visualizer.\n",
"meshcat = StartMeshcat()"
]
},
{
"cell_type": "markdown",
"id": "11c62164",
"metadata": {},
"source": [
"## Gymnasium Robotics\n",
"\n",
"Includes the following environments:\n",
"- Franka kitchen\n",
"- ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfaf4640",
"metadata": {},
"outputs": [],
"source": [
"# TODO(russt): Use ModelVisualizer pending resolution of Drake#22444.\n",
"if running_as_notebook: # I don't want to download the repo in CI.\n",
" meshcat.Delete()\n",
" builder = RobotDiagramBuilder()\n",
" plant = builder.plant()\n",
" parser = Parser(plant)\n",
" package_map = parser.package_map()\n",
" package_map.AddRemote(\n",
" package_name=\"gymnasium_robotics\",\n",
" params=PackageMap.RemoteParams(\n",
" urls=[\n",
" f\"https://github.com/Farama-Foundation/Gymnasium-Robotics/archive/refs/tags/v1.3.1.tar.gz\"\n",
" ],\n",
" sha256=(\"d274b3ee1d34337aa35d4686447fda6a6d20dfcb4d375eab91b4e49b1108afde\"),\n",
" strip_prefix=\"Gymnasium-Robotics-1.3.1/gymnasium_robotics/envs/assets/\",\n",
" ),\n",
" )\n",
" original_file = package_map.ResolveUrl(\n",
" \"package://gymnasium_robotics/kitchen_franka/kitchen_assets/kitchen_env_model.xml\"\n",
" )\n",
" drake_compatible_file = original_file.replace(\".xml\", \".drake.xml\")\n",
" MakeDrakeCompatibleModel(original_file, drake_compatible_file)\n",
" model_instances = parser.AddModels(drake_compatible_file)\n",
" plant.Finalize()\n",
" ApplyDefaultVisualization(builder.builder(), meshcat=meshcat)\n",
" diagram = builder.Build()\n",
" simulator = Simulator(diagram)\n",
"\n",
" # Workaround for drake#22444: set the desired state to zero.\n",
" for model_instance in model_instances:\n",
" desired_state_port = plant.get_desired_state_input_port(model_instance)\n",
" if desired_state_port.size() > 0:\n",
" desired_state_port.FixValue(\n",
" plant.GetMyContextFromRoot(simulator.get_context()),\n",
" np.zeros(desired_state_port.size()),\n",
" )\n",
"\n",
" diagram.ForcedPublish(simulator.get_context())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4565ef03",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
8 changes: 8 additions & 0 deletions manipulation/make_drake_compatible_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ def _convert_mjcf(input_filename: str, output_filename: str, overwrite: bool) ->
texturedir = compiler_element.attrib["texturedir"]
del compiler_element.attrib["texturedir"]

# Truncate all rgba attributes to [0, 1]. See Drake#22445.
elements_with_rgba = root.findall(".//*[@rgba]")
for element in elements_with_rgba:
rgba = element.attrib["rgba"]
rgba = [float(value) for value in rgba.split()]
rgba = [min(max(value, 0), 1) for value in rgba]
element.attrib["rgba"] = " ".join([str(value) for value in rgba])

defaults = {}

# Process all default elements recursively
Expand Down

0 comments on commit 70e0608

Please sign in to comment.