Skip to content

Commit

Permalink
Revamp a couple of tf2 tutorials to flow better. (ros2#2781)
Browse files Browse the repository at this point in the history
* Revamp a couple of tf2 tutorials to flow better.

That is, make sure we put in all the steps that need to be
done, and more logically organize the sections.  This should
make them easier to follow.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
  • Loading branch information
clalancette authored Jul 6, 2022
1 parent dfd8830 commit 739d840
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
40 changes: 32 additions & 8 deletions source/Tutorials/Intermediate/Tf2/Adding-A-Frame-Py.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ Now open the file called ``fixed_frame_tf2_broadcaster.py``.
rclpy.shutdown()
Don't forget to add the node to the ``setup.py``.
The code is very similar to the tf2 broadcaster tutorial example and the only difference is that the transform here does not change over time.

1.1 Examine the code
Expand All @@ -145,11 +144,23 @@ The ``carrot1`` frame is 2 meters offset in y axis in terms of the ``turtle1`` f
t.transform.translation.y = 2.0
t.transform.translation.z = 0.0
1.2 Write the launch file
1.2 Add an entry point
~~~~~~~~~~~~~~~~~~~~~~

To allow the ``ros2 run`` command to run your node, you must add the entry point
to ``setup.py`` (located in the ``src/learning_tf2_py`` directory).

Finally, add the following line between the ``'console_scripts':`` brackets:

.. code-block:: python
'fixed_frame_tf2_broadcaster = learning_tf2_py.fixed_frame_tf2_broadcaster:main',
1.3 Write the launch file
~~~~~~~~~~~~~~~~~~~~~~~~~

Now let's create a launch file for this example.
With your text editor, create a new file called ``turtle_tf2_fixed_frame_demo.launch.py``, and add the following lines:
With your text editor, create a new file called ``launch/turtle_tf2_fixed_frame_demo.launch.py``, and add the following lines:

.. code-block:: python
Expand Down Expand Up @@ -193,7 +204,8 @@ The last part of the code will add our fixed ``carrot1`` frame to the turtlesim
name='fixed_broadcaster',
),
1.3 Build and run
1.4 Build and run
~~~~~~~~~~~~~~~~~

Rebuild the package and start the turtle broadcaster demo:
Expand All @@ -206,7 +218,7 @@ You should notice that the new ``carrot1`` frame appeared in the transformation

.. image:: images/turtlesim_frames_carrot.png

1.4 Checking the results
1.5 Checking the results
~~~~~~~~~~~~~~~~~~~~~~~~

If you drive the first turtle around, you should notice that the behavior didn't change from the previous tutorial, even though we added a new frame.
Expand Down Expand Up @@ -334,10 +346,22 @@ Instead of a fixed definition of our x and y offsets, we are using the ``sin()``
t.transform.translation.x = 10 * math.sin(x)
t.transform.translation.y = 10 * math.cos(x)
2.2 Write the launch file
2.2 Add an entry point
~~~~~~~~~~~~~~~~~~~~~~

To allow the ``ros2 run`` command to run your node, you must add the entry point
to ``setup.py`` (located in the ``src/learning_tf2_py`` directory).

Finally, add the following line between the ``'console_scripts':`` brackets:

.. code-block:: python
'dynamic_frame_tf2_broadcaster = learning_tf2_py.dynamic_frame_tf2_broadcaster:main',
2.3 Write the launch file
~~~~~~~~~~~~~~~~~~~~~~~~~

To test this code, create a new launch file ``turtle_tf2_dynamic_frame_demo.launch.py`` and paste the following code:
To test this code, create a new launch file ``launch/turtle_tf2_dynamic_frame_demo.launch.py`` and paste the following code:

.. code-block:: python
Expand Down Expand Up @@ -369,7 +393,7 @@ To test this code, create a new launch file ``turtle_tf2_dynamic_frame_demo.laun
),
])
2.3 Build and run
2.4 Build and run
~~~~~~~~~~~~~~~~~

Rebuild the package, and start the ``turtle_tf2_dynamic_frame_demo.launch.py`` launch file, and now you’ll see that the second turtle is following the carrot's position that is constantly changing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ In the second terminal window type the following command:
ros2 run turtlesim turtle_teleop_key
3 Checking the results
4 Checking the results
^^^^^^^^^^^^^^^^^^^^^^

To see if things work, simply drive around the first turtle using the arrow keys (make sure your terminal window is active, not your simulator window), and you'll see the second turtle following the first one!
Expand Down
47 changes: 42 additions & 5 deletions source/Tutorials/Intermediate/Tf2/Writing-A-Tf2-Listener-Py.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,20 @@ All this is wrapped in a try-except block to handle possible exceptions.
from_frame_rel,
now)
2 Build and run
^^^^^^^^^^^^^^^
1.2 Add an entry point
~~~~~~~~~~~~~~~~~~~~~~

To allow the ``ros2 run`` command to run your node, you must add the entry point
to ``setup.py`` (located in the ``src/learning_tf2_py`` directory).

Finally, add the following line between the ``'console_scripts':`` brackets:

.. code-block:: python
'turtle_tf2_listener = learning_tf2_py.turtle_tf2_listener:main',
2 Update the launch file
^^^^^^^^^^^^^^^^^^^^^^^^

With your text editor, open the launch file called ``turtle_tf2_demo.launch.py``, and add the following lines after your first ``turtle1`` broadcaster node.
Additionally, include the imports of ``DeclareLaunchArgument`` and ``LaunchConfiguration`` in the beginning of the file:
Expand Down Expand Up @@ -256,6 +268,34 @@ Additionally, include the imports of ``DeclareLaunchArgument`` and ``LaunchConfi
])
This will declare a ``target_frame`` launch argument, start a broadcaster for second turtle that we will spawn and listener that will subscribe to those transformations.


3 Build and run
^^^^^^^^^^^^^^^

Run ``rosdep`` in the root of your workspace to check for missing dependencies.

.. tabs::

.. group-tab:: Linux

.. code-block:: console
rosdep install -i --from-path src --rosdistro {DISTRO} -y
.. group-tab:: macOS

rosdep only runs on Linux, so you will need to install ``geometry_msgs``, ``tf_transformations`` and ``turtlesim`` dependencies yourself

.. group-tab:: Windows

rosdep only runs on Linux, so you will need to install ``geometry_msgs``, ``tf_transformations`` and ``turtlesim`` dependencies yourself

Build your updated package, and source the setup files.

4 Checking the results
^^^^^^^^^^^^^^^^^^^^^^

Now you're ready to start your full turtle demo:

.. code-block:: console
Expand All @@ -269,9 +309,6 @@ In the second terminal window type the following command:
ros2 run turtlesim turtle_teleop_key
3 Checking the results
^^^^^^^^^^^^^^^^^^^^^^

To see if things work, simply drive around the first turtle using the arrow keys (make sure your terminal window is active, not your simulator window), and you'll see the second turtle following the first one!

Summary
Expand Down

0 comments on commit 739d840

Please sign in to comment.