Skip to content

Commit ce912f6

Browse files
DLuclalancette
andauthored
URDF Sim Tutorial (#3952)
* URDF Sim Tutorial * Transmissions => Controllers Signed-off-by: David V. Lu!! <[email protected]> Co-authored-by: Chris Lalancette <[email protected]>
1 parent ac4d93b commit ce912f6

File tree

3 files changed

+330
-0
lines changed

3 files changed

+330
-0
lines changed

source/Tutorials/Advanced/Simulators/Gazebo/Simulation-Gazebo.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ This set of tutorials will teach you how to configure the Gazebo simulator with
1111
:maxdepth: 1
1212

1313
Gazebo
14+
../../../Intermediate/URDF/Using-a-URDF-in-Gazebo

source/Tutorials/Intermediate/URDF/URDF-Main.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ URDF (Unified Robot Description Format) is a file format for specifying the geom
2121
Adding-Physical-and-Collision-Properties-to-a-URDF-Model
2222
Using-Xacro-to-Clean-Up-a-URDF-File
2323
Using-URDF-with-Robot-State-Publisher
24+
Using-a-URDF-in-Gazebo
2425
Exporting-an-URDF-File
Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
Using a URDF in Gazebo
2+
======================
3+
4+
**Goal:** Simulate your URDF in the Gazebo simulator
5+
6+
**Tutorial level:** Intermediate
7+
8+
**Time:** 30 minutes
9+
10+
.. contents:: Contents
11+
:depth: 2
12+
:local:
13+
14+
15+
Based on `this ROS 1 tutorial <http://wiki.ros.org/urdf/Tutorials/Using%20a%20URDF%20in%20Gazebo>`_.
16+
17+
Let's start by installing the demo package and its dependencies.
18+
19+
.. tabs::
20+
21+
.. group-tab:: Ubuntu Packages
22+
23+
.. code-block:: console
24+
25+
sudo apt install ros-{DISTRO}-urdf-sim-tutorial
26+
27+
.. group-tab:: RHEL Packages
28+
29+
.. code-block:: console
30+
31+
sudo dnf install ros-{DISTRO}-urdf-sim-tutorial
32+
33+
.. group-tab:: From Source
34+
35+
.. code-block:: console
36+
37+
git clone https://github.com/ros/urdf_sim_tutorial.git -b ros2
38+
39+
Nonfunctional Gazebo Interface
40+
------------------------------
41+
We can spawn the model we already created into Gazebo using ``gazebo.launch.py``
42+
43+
.. code-block:: console
44+
45+
ros2 launch urdf_sim_tutorial gazebo.launch.py
46+
47+
This launch file
48+
49+
* Loads the urdf from :doc:`the macro tutorial <Using-Xacro-to-Clean-Up-a-URDF-File>` and publishes it as a topic (``/robot_description``)
50+
* Launches an empty Gazebo world
51+
* Runs the script to read the urdf from the topic and spawn it in Gazebo.
52+
* By default, the Gazebo GUI will also be displayed, and look like this:
53+
54+
.. image:: https://raw.githubusercontent.com/ros/urdf_sim_tutorial/ros2/doc/NonFunctional.png
55+
:alt: Nonfunctional robot in Gazebo
56+
57+
58+
However, it doesn't do anything, and is missing lots of key information that ROS would need to use this robot.
59+
In :doc:`other <Building-a-Visual-Robot-Model-with-URDF-from-Scratch>` :doc:`tutorials <Building-a-Movable-Robot-Model-with-URDF>` we used `joint_state_publisher <https://index.ros.org/p/joint_state_publisher/github-ros-joint_state_publisher/>`_ to specify the pose of each joint.
60+
However, the robot itself should provide that information in the real world or in Gazebo.
61+
Yet without specifying that, Gazebo doesn't know to publish that information.
62+
63+
To get the robot to be interactive (with you and ROS), we need to specify two things: Plugins and Controllers.
64+
65+
Side note: Configuring Meshes
66+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
68+
.. image:: https://raw.githubusercontent.com/ros/urdf_sim_tutorial/ros2/doc/NoMesh.png
69+
:alt: Robot with missing meshes
70+
71+
72+
If you are following along at home with your own robot, or something else is amiss, the meshes may be missing from your model in the Gazebo GUI (i.e. the gripper meshes are not there).
73+
This may also cause Gazebo to take several seconds to start up after the splash screen appears because it is checking the internet for missing models.
74+
75+
This is because your URDF package needs to explicitly tell Gazebo where to load the meshes from.
76+
We do this by modifying the ``package.xml`` of the package where our URDF meshes live to include a new export.
77+
78+
.. code-block:: xml
79+
80+
<export>
81+
<build_type>ament_cmake</build_type>
82+
<gazebo_ros gazebo_model_path="${prefix}/.."/>
83+
</export>
84+
85+
The reasoning behind the exact value of the ``gazebo_model_path`` attribute is `a separate issue <https://github.com/ros-simulation/gazebo_ros_pkgs/issues/1500>`_, but suffice to say, setting it to this value will work assuming:
86+
87+
* Your mesh filenames are specified in the URDF using the ``package://package_name/possible_folder/filename.ext`` syntax.
88+
* The meshes are installed (via CMake) into the proper share folder.
89+
90+
Gazebo Plugin
91+
-------------
92+
To get ROS 2 to interact with Gazebo, we have to dynamically link to the ROS library that will tell Gazebo what to do.
93+
Theoretically, this allows for other Robot Operating Systems to interact with Gazebo in a generic way.
94+
In practice, its just ROS.
95+
96+
Specifically, Gazebo / ROS 2 interaction all happens by linking to a ROS 2 Control library, with new URDF tags.
97+
98+
We specify the following in the URDF, right before the closing ``</robot>`` tag:
99+
100+
.. code-block:: xml
101+
102+
<ros2_control name="GazeboSystem" type="system">
103+
<hardware>
104+
<plugin>gazebo_ros2_control/GazeboSystem</plugin>
105+
</hardware>
106+
<joint name="head_swivel" />
107+
</ros2_control>
108+
109+
<gazebo>
110+
<plugin filename="libgazebo_ros2_control.so" name="gazebo_ros2_control">
111+
<parameters>$(find urdf_sim_tutorial)/config/09a-minimal.yaml</parameters>
112+
</plugin>
113+
</gazebo>
114+
115+
.. note::
116+
117+
* The ``<gazebo>`` and ``<plugin>`` tags work the same way they did in ROS 1.
118+
* We must specify at least one joint for the minimal example to work, but we'll add more later.
119+
120+
The minimal configuration file is:
121+
122+
.. code-block:: yaml
123+
124+
controller_manager:
125+
ros__parameters:
126+
update_rate: 100
127+
128+
129+
You can see this in `09a-minimal.urdf.xacro <https://github.com/ros/urdf_sim_tutorial/blob/ros2/urdf/09a-minimal.urdf.xacro>`_ and by running
130+
131+
.. code-block:: console
132+
133+
ros2 launch urdf_sim_tutorial 09a-minimal.launch.py
134+
135+
This starts up a ``/controller_manager`` node and with the ``load_controller`` service, but doesn't add any immediately useful interaction with the robot.
136+
For that we need to specify more information in the controller yaml.
137+
138+
Spawning Controllers
139+
--------------------
140+
Now that we've linked ROS and Gazebo, we need to specify some bits of ROS code that we want to run within Gazebo, which we generically call controllers.
141+
Now we can look at a larger example based on `this yaml file <https://github.com/ros/urdf_sim_tutorial/blob/ros2/config/joints.yaml>`_ that specifies our first controller.
142+
143+
.. code-block:: yaml
144+
145+
controller_manager:
146+
ros__parameters:
147+
update_rate: 100
148+
use_sim_time: true
149+
150+
joint_state_broadcaster:
151+
type: joint_state_broadcaster/JointStateBroadcaster
152+
153+
This controller is found in the ``joint_state_broadcaster`` package and publishes the state of the robot's joints into ROS directly from Gazebo.
154+
155+
In `09-joints.launch.py <https://github.com/ros/urdf_sim_tutorial/blob/ros2/launch/09-joints.launch.py>`_ we also add a ``ros2_control`` command via ``ExecuteProcess`` to start this specific controller.
156+
157+
You can launch this, but its still not quite there.
158+
159+
.. code-block:: console
160+
161+
ros2 launch urdf_sim_tutorial 09-joints.launch.py
162+
163+
This will run the controller and in fact publish on the ``/joint_states`` topic, but with nothing in them.
164+
165+
.. code-block:: yaml
166+
167+
header:
168+
stamp:
169+
sec: 13
170+
nanosec: 331000000
171+
frame_id: ''
172+
name: []
173+
position: []
174+
velocity: []
175+
effort: []
176+
177+
What else do you want Gazebo!?
178+
Well, it wants to know more information about the joints.
179+
180+
ROS 2 Control Joint Definitions
181+
-------------------------------
182+
For every non-fixed joint, we need to add information about the joint in the ``ros2_control`` tag which tells it what interfaces are supported.
183+
Let's start with the head joint.
184+
Modify the joint tag in your `URDF <https://github.com/ros/urdf_sim_tutorial/blob/ros2/urdf/10-firsttransmission.urdf.xacro#L241>`_ to be the following:
185+
186+
.. code-block:: xml
187+
188+
<joint name="head_swivel">
189+
<command_interface name="position" />
190+
<command_interface name="velocity" />
191+
<state_interface name="position"/>
192+
<state_interface name="velocity"/>
193+
</joint>
194+
195+
* Note that the joint name here matches the joint name from the standard URDF ``<joint>`` tag.
196+
* For the moment, let us focus on the ``state_interface``s, in which we specify that we want to publish both position and velocity of this joint.
197+
198+
You can run this URDF with our previous launch configuration.
199+
200+
.. code-block:: console
201+
202+
ros2 launch urdf_sim_tutorial 09-joints.launch.py urdf_package_path:=urdf/10-firsttransmission.urdf.xacro
203+
204+
Now, the head is displayed properly in RViz because the head joint is listed in the ``joint_states`` messages.
205+
206+
.. code-block:: yaml
207+
208+
header:
209+
stamp:
210+
sec: 4
211+
nanosec: 707000000
212+
frame_id: ''
213+
name:
214+
- head_swivel
215+
position:
216+
- -2.9051283156888985e-08
217+
velocity:
218+
- 7.575990694887896e-06
219+
effort:
220+
- .nan
221+
222+
223+
We could continue adding joint definitions for all the non-fixed joints (and we will) so that all the joints are properly published.
224+
But, there's more to life than just looking at robots.
225+
We want to control them.
226+
So, let's get another controller in here.
227+
228+
Joint Control
229+
-------------
230+
`Here's <https://github.com/ros/urdf_sim_tutorial/blob/ros2/config/head.yaml>`_ the next controller config we're adding.
231+
232+
.. code-block:: yaml
233+
234+
controller_manager:
235+
ros__parameters:
236+
# ... snip ...
237+
238+
head_controller:
239+
type: position_controllers/JointGroupPositionController
240+
241+
head_controller:
242+
ros__parameters:
243+
joints:
244+
- head_swivel
245+
interface_name: position
246+
247+
248+
In English, this is saying to add a new ``JointGroupPositionController`` called ``head_controller``, and then, in a new parameter namespace, specify which joints are included and that we are publishing positions.
249+
We can do this because we specified ``<command_interface name="position" />`` in the joint tag.
250+
251+
Now we can launch this with the added config and another ``ros2 control`` command as before
252+
253+
.. code-block:: console
254+
255+
ros2 launch urdf_sim_tutorial 10-head.launch.py
256+
257+
Now Gazebo is subscribed to a new topic, and you can then control the position of the head by publishing a value in ROS.
258+
259+
.. code-block:: console
260+
261+
ros2 topic pub /head_controller/commands std_msgs/msg/Float64MultiArray "data: [-0.707]"
262+
263+
When this command is published, the position will immediately change to the specified value.
264+
265+
Controlling Multiple Joints and Mimicking
266+
-----------------------------------------
267+
We can change the URDF for the Gripper joints in a similar way, but in this case, we'll associate multiple joints with one controller.
268+
The updated `ROS parameters are here <https://github.com/ros/urdf_sim_tutorial/blob/ros2/config/gripper.yaml>`_.
269+
We also must update `the URDF to include three additional joint interfaces <https://github.com/ros/urdf_sim_tutorial/blob/ros2/urdf/12-gripper.urdf.xacro>`_.
270+
271+
To launch this,
272+
273+
.. code-block:: console
274+
275+
ros2 launch urdf_sim_tutorial 12-gripper.launch.py
276+
277+
We can now move the gripper with an array of three floats.
278+
Open and out:
279+
280+
.. code-block:: console
281+
282+
ros2 topic pub /gripper_controller/commands std_msgs/msg/Float64MultiArray "data: [0.0, 0.5, 0.5]"
283+
284+
Closed and retracted:
285+
286+
.. code-block:: console
287+
288+
ros2 topic pub /gripper_controller/commands std_msgs/msg/Float64MultiArray "data: [-0.4, 0.0, 0.0]"
289+
290+
This gripper is actually set up in a way that we ALWAYS want the left gripper joint to have the same value as the right gripper joint.
291+
We can code this into the URDF and controllers with a few steps.
292+
293+
* Insert ``<mimic joint="left_gripper_joint"/>`` into the URDF definition of the ``right_gripper_joint`` (which is done a bit hackily in `the xacro here <https://github.com/ros/urdf_sim_tutorial/blob/ros2/urdf/12a-mimic-gripper.urdf.xacro>`_
294+
* Insert ``<param name="mimic">left_gripper_joint</param>`` into the ``ros2_control`` joint interface for ``right_gripper_joint``.
295+
* In our new `control parameters <https://github.com/ros/urdf_sim_tutorial/blob/ros2/config/mimic-gripper.urdf>`_, we only list the two joints for the gripper controller, leaving out ``right_gripper_joint``.
296+
297+
We can launch this with
298+
299+
.. code-block:: console
300+
301+
ros2 launch urdf_sim_tutorial 12-gripper.launch.py urdf_package_path:=urdf/12a-mimic-gripper.urdf.xacro
302+
303+
and now we can control it with just two values, e.g.
304+
305+
.. code-block:: console
306+
307+
ros2 topic pub /gripper_controller/commands std_msgs/msg/Float64MultiArray "data: [0.0, 0.5]"
308+
309+
The Wheels on the Droid Go Round and Round
310+
------------------------------------------
311+
To drive the robot around, we first must specify more interfaces in the ``ros2_control`` tag of `the URDF for each of the four wheels <https://github.com/ros/urdf_sim_tutorial/blob/ros2/urdf/13-diffdrive.urdf.xacro>`_, however, now only the velocity command interface is required.
312+
313+
We could specify controllers for each of the individual wheels, but where's the fun in that?
314+
Instead we want to control all the wheels together.
315+
For that, we're going to need `a lot more ROS parameters <https://github.com/ros/urdf_sim_tutorial/blob/ros2/config/diffdrive.yaml>`_ to make use of the ``DiffDriveController`` which subscribes to a standard Twist ``cmd_vel`` message and moves the robot accordingly.
316+
317+
.. code-block:: console
318+
319+
ros2 launch urdf_sim_tutorial 13-diffdrive.launch.py
320+
321+
In addition to loading the above configuration, this also opens the !RobotSteering panel, allowing you to drive the R2D2 robot around, while also observing its actual behavior (in Gazebo) and it's visualized behavior (in RViz):
322+
323+
.. image:: https://raw.githubusercontent.com/ros/urdf_sim_tutorial/ros2/doc/DrivingInterface.png
324+
:alt: Gazebo with Driving Interface
325+
326+
327+
Congrats!
328+
Now you're simulating robots with URDF.

0 commit comments

Comments
 (0)