Skip to content

Commit 9d2907e

Browse files
Add option to wait for two subscriptions (#4674)
* Add option to wait for tso subscriptions since, if following the tutorial to the letter, you will currently have both turtlesim and topic echo subscribed. If you do not do this, sometimes the turtle will move, sometimes it wont - depending on which subscriber the command finds first. * wip. Requested to change the flow a bit * As suggested, moved the continuous publishing version to be first, then go into the --once version. * added some changes recommended by clalancette * remove annoying blank line * Update source/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.rst Co-authored-by: Chris Lalancette <clalancette@gmail.com>
1 parent 14bdbb5 commit 9d2907e

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

source/Tutorials/Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics.rst

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,36 +244,43 @@ Now that you have the message structure, you can publish data to a topic directl
244244
245245
The ``'<args>'`` argument is the actual data you'll pass to the topic, in the structure you just discovered in the previous section.
246246

247+
The turtle (and commonly the real robots which it is meant to emulate) require a steady stream of commands to operate continuously.
248+
So, to get the turtle moving, and keep it moving, you can use the following command.
247249
It's important to note that this argument needs to be input in YAML syntax.
248250
Input the full command like so:
249251

250252
.. code-block:: console
251253
252-
ros2 topic pub --once /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
254+
ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
255+
256+
With no command-line options, ``ros2 topic pub`` publishes the command in a steady stream at 1 Hz.
257+
258+
.. image:: images/pub_stream.png
259+
260+
At times you may want to publish data to your topic only once (rather than continuously).
261+
To publish your command just once add the ``--once`` option.
262+
263+
.. code-block:: console
264+
265+
ros2 topic pub --once -w 2 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
253266
254267
``--once`` is an optional argument meaning “publish one message then exit”.
255268

269+
``-w 2`` is an optional argument meaning “wait for two matching subscriptions”.
270+
This is needed because we have both turtlesim and the topic echo subscribed.
271+
256272
You will see the following output in the terminal:
257273

258274
.. code-block:: console
259275
276+
Waiting for at least 2 matching subscription(s)...
260277
publisher: beginning loop
261278
publishing #1: geometry_msgs.msg.Twist(linear=geometry_msgs.msg.Vector3(x=2.0, y=0.0, z=0.0), angular=geometry_msgs.msg.Vector3(x=0.0, y=0.0, z=1.8))
262279
263280
And you will see your turtle move like so:
264281

265282
.. image:: images/pub_once.png
266283

267-
The turtle (and commonly the real robots which it is meant to emulate) require a steady stream of commands to operate continuously.
268-
So, to get the turtle to keep moving, you can run:
269-
270-
.. code-block:: console
271-
272-
ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
273-
274-
The difference here is the removal of the ``--once`` option and the addition of the ``--rate 1`` option, which tells ``ros2 topic pub`` to publish the command in a steady stream at 1 Hz.
275-
276-
.. image:: images/pub_stream.png
277284

278285
You can refresh rqt_graph to see what's happening graphically.
279286
You will see that the ``ros2 topic pub ...`` node (``/_ros2cli_30358``) is publishing over the ``/turtle1/cmd_vel`` topic, which is being received by both the ``ros2 topic echo ...`` node (``/_ros2cli_26646``) and the ``/turtlesim`` node now.

0 commit comments

Comments
 (0)