You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.rst
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -135,15 +135,15 @@ Open the file using your preferred text editor.
135
135
2.1 Examine the code
136
136
~~~~~~~~~~~~~~~~~~~~
137
137
138
-
The first lines of code after the comments import ``rclpy`` so its ``Node`` class can be used.
138
+
The first lines of code after the comments import {package(rclpy)} so its `Node<{package_link(rclpy)}api/node.html>`__ class can be used.
139
139
140
140
.. code-block:: python
141
141
142
142
import rclpy
143
143
from rclpy.executors import ExternalShutdownException
144
144
from rclpy.node import Node
145
145
146
-
The next statement imports the built-in string message type that the node uses to structure the data that it passes on the topic.
146
+
The next statement imports the built-in {interface(std_msgs/msg/String)} message type that the node uses to structure the data that it passes on the topic.
147
147
148
148
.. code-block:: python
149
149
@@ -152,19 +152,19 @@ The next statement imports the built-in string message type that the node uses t
152
152
These lines represent the node's dependencies.
153
153
Recall that dependencies have to be added to ``package.xml``, which you'll do in the next section.
154
154
155
-
Next, the ``MinimalPublisher`` class is created, which inherits from (or is a subclass of) ``Node``.
155
+
Next, the ``MinimalPublisher`` class is created, which inherits from (or is a subclass of) `Node<{package_link(rclpy)}api/node.html>`__.
156
156
157
157
.. code-block:: python
158
158
159
159
classMinimalPublisher(Node):
160
160
161
161
Following is the definition of the class's constructor.
162
-
``super().__init__`` calls the ``Node`` class's constructor and gives it your node name, in this case ``minimal_publisher``.
162
+
``super().__init__`` calls the `Node<{package_link(rclpy)}api/node.html>`__ class's constructor and gives it your node name, in this case ``minimal_publisher``.
163
163
164
-
``create_publisher`` declares that the node publishes messages of type ``String`` (imported from the ``std_msgs.msg`` module), over a topic named ``topic``, and that the "queue size" is 10.
165
-
Queue size is a required QoS (quality of service) setting that limits the amount of queued messages if a subscriber is not receiving them fast enough.
164
+
`create_publisher<{package_link(rclpy)}api/node.html#rclpy.node.Node.create_publisher>`__ declares that the node publishes messages of type {interface(std_msgs/msg/String)} (imported from the ``std_msgs.msg`` module), over a topic named ``topic``, and that the "queue size" is 10.
165
+
Queue size is a required :doc:`Quality of Service </Concepts/Intermediate/About-Quality-of-Service-Settings>` (QoS) setting that limits the amount of queued messages if a subscriber is not receiving them fast enough.
166
166
167
-
Next, a timer is created with a callback to execute every 0.5 seconds.
167
+
Next, `create_timer <{package_link(rclpy)}api/node.html#rclpy.node.Node.create_timer>`__ is used to create a callback that executes every 0.5 seconds.
168
168
``self.i`` is a counter used in the callback.
169
169
170
170
.. code-block:: python
@@ -176,7 +176,7 @@ Next, a timer is created with a callback to execute every 0.5 seconds.
``timer_callback`` creates a message with the counter value appended, and publishes it to the console with ``get_logger().info``.
179
+
``timer_callback`` creates a message with the counter value appended, publishes it, and prints it to the console with `get_logger()<{package_link(rclpy)}api/node.html#rclpy.node.Node.get_logger>`__'s `info() <{package_link(rclpy)}rclpy.impl.rcutils_logger.html#rclpy.impl.rcutils_logger.RcutilsLogger.info>`__ function.
180
180
181
181
.. code-block:: python
182
182
@@ -201,7 +201,7 @@ Lastly, the main function is defined.
201
201
pass
202
202
203
203
204
-
First the ``rclpy`` library is initialized, then the node is created, and then it "spins" the node so its callbacks are called.
204
+
First the {package(rclpy)} library is initialized, then the node is created, and then it "spins" the node (using `spin() <{package_link(rclpy)}api/init_shutdown.html#rclpy.spin>`__) so its callbacks are called.
205
205
206
206
2.2 Add dependencies
207
207
~~~~~~~~~~~~~~~~~~~~
@@ -225,7 +225,7 @@ After the lines above, add the following dependencies corresponding to your node
225
225
<exec_depend>rclpy</exec_depend>
226
226
<exec_depend>std_msgs</exec_depend>
227
227
228
-
This declares the package needs ``rclpy`` and ``std_msgs`` when its code is executed.
228
+
This declares the package needs {package(rclpy)} and {package(std_msgs)} when its code is executed.
229
229
230
230
Make sure to save the file.
231
231
@@ -242,7 +242,7 @@ Again, match the ``maintainer``, ``maintainer_email``, ``description`` and ``lic
242
242
description='Examples of minimal publisher/subscriber using rclpy',
243
243
license='Apache-2.0',
244
244
245
-
Add the following line within the ``console_scripts`` brackets of the ``entry_points`` field:
245
+
Add the following line within the ``console_scripts`` brackets of the `entry_points<https://setuptools.pypa.io/en/latest/userguide/entry_point.html>`__ field:
246
246
247
247
.. code-block:: python
248
248
@@ -266,7 +266,7 @@ The contents of the ``setup.cfg`` file should be correctly populated automatical
266
266
[install]
267
267
install_scripts=$base/lib/py_pubsub
268
268
269
-
This is simply telling setuptools to put your executables in ``lib``, because ``ros2 run`` will look for them there.
269
+
This is simply telling `setuptools<https://setuptools.pypa.io/en/latest/userguide>`__ to put your executables in ``lib``, because ``ros2 run`` will look for them there.
270
270
271
271
You could build your package now, source the local setup files, and run it, but let's create the subscriber node first so you can see the full system at work.
272
272
@@ -353,7 +353,7 @@ Open the ``subscriber_member_function.py`` with your text editor.
353
353
main()
354
354
355
355
The subscriber node's code is nearly identical to the publisher's.
356
-
The constructor creates a subscriber with the same arguments as the publisher.
356
+
The constructor creates a subscriber with the same arguments as the publisher using `create_subscription <{package_link(rclpy)}api/node.html#rclpy.node.Node.create_subscription>`__.
357
357
Recall from the :doc:`topics tutorial <../Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics>` that the topic name and message type used by the publisher and subscriber must match to allow them to communicate.
358
358
359
359
.. code-block:: python
@@ -391,7 +391,7 @@ The ``setup.cfg`` file can also remain untouched.
391
391
~~~~~~~~~~~~~~~~~~~~~~
392
392
393
393
Reopen ``setup.py`` and add the entry point for the subscriber node below the publisher's entry point.
394
-
The ``entry_points`` field should now look like this:
394
+
The `entry_points<https://setuptools.pypa.io/en/latest/userguide/entry_point.html>`__ field should now look like this:
395
395
396
396
.. code-block:: python
397
397
@@ -406,8 +406,8 @@ Make sure to save the file, and then your pub/sub system should be ready.
406
406
407
407
4 Build and run
408
408
^^^^^^^^^^^^^^^
409
-
You likely already have the ``rclpy`` and ``std_msgs`` packages installed as part of your ROS 2 system.
410
-
It's good practice to run ``rosdep`` in the root of your workspace (``ros2_ws``) to check for missing dependencies before building:
409
+
You likely already have the {package(rclpy)} and {package(std_msgs)} packages installed as part of your ROS 2 system.
410
+
It's good practice to run `rosdep<https://docs.ros.org/en/independent/api/rosdep/html/>`__ (check the :doc:`rosdep tutorial </Tutorials/Intermediate/Rosdep>`) in the root of your workspace (``ros2_ws``) to check for missing dependencies before building:
0 commit comments