@@ -244,17 +244,117 @@ Now that you have the message structure, you can publish data to a topic directl
244
244
245
245
The ``'<args>' `` argument is the actual data you'll pass to the topic, in the structure you just discovered in the previous section.
246
246
247
+ There are four main ways to use the ``pub `` command as shown below.
248
+ However, the autocomplete feature described in ``c. `` and ``d. `` is not supported in Windows.
249
+
250
+ a. **Publishing dictionary strings **:
251
+
252
+ In order to publish data to a topic, you need to pass the data in the form of YAML strings.
253
+
254
+ .. code-block :: bash
255
+
256
+ 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}}"
257
+
258
+ However, you do not need to specify the entire message, if you are just changing the linear or angular velocity, you can just specify the values you want to change.
259
+
260
+ For example, if you want to change the linear velocity to 2.0 and keep the angular velocity at 1.8, you can do the following:
261
+
262
+ .. code-block :: bash
263
+
264
+ ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist " {linear: {x: 2.0}, angular: {z: 1.8}}"
265
+
266
+ b. **Publishing an empty message **:
267
+
268
+ .. code-block :: bash
269
+
270
+ ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist
271
+
272
+ This will publish the default values for the message type at 1 Hz.
273
+ In this case, this equivalent to the following command:
274
+
275
+ .. code-block :: bash
276
+
277
+ ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist " {linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}" --rate 1
278
+
279
+ c. **Using autocomplete **:
280
+
281
+ You can trigger the autocomplete feature of your terminal by the following:
282
+
283
+ .. code-block :: bash
284
+
285
+ $ ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist < TAB>
286
+ --keep-alive
287
+ --max-wait-time-secs
288
+ --node-name
289
+ --once
290
+ --print
291
+ --qos-depth
292
+ --qos-durability
293
+ --qos-history
294
+ --qos-liveliness
295
+ --qos-liveliness-lease-duration-seconds
296
+ --qos-profile
297
+ --qos-reliability
298
+ --rate
299
+ --spin-time
300
+ --stdin
301
+ --times
302
+ --use-sim-time
303
+ --wait-matching-subscriptions
304
+ --yaml-file
305
+ -1
306
+ -n
307
+ -p
308
+ -r
309
+ -s
310
+ -t
311
+ -w
312
+ \' linear:\^ J\ \ x:\ 0.0\^ J\ \ y:\ 0.0\^ J\ \ z:\ 0.0\^ Jangular:\^ J\ \ x:\ 0.0\^ J\ \ y:\ 0.0\^ J\ \ z:\ 0.0\^ J\'
313
+
314
+ All the options will be autocompleted by pressing the :kbd: `tab ` key after the entering the first few characters of the option.
315
+ However, the topic message prototype will only be autocompleted after ``\'<TAB> `` is entered.
316
+
317
+ This is because the terminal does not recognize the single quote as part of the autocomplete string.
318
+ Hence it needs to be escaped by using ``\' `` to be recognized as part of the string.
319
+
320
+ The final autocompleted string will look like this:
321
+
322
+ .. code-block :: bash
323
+
324
+ $ ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist ' linear:
325
+ x: 0.0
326
+ y: 0.0
327
+ z: 0.0
328
+ angular:
329
+ x: 0.0
330
+ y: 0.0
331
+ z: 0.0
332
+ '
333
+
334
+ This string is editable and you can change the values of the message type as required.
335
+
336
+ d. **Using the raw autocompleted string **:
337
+
338
+ As mentioned above, the autocompleted string for ``geometry_msgs/msg/Twist `` looks like this:
339
+
340
+ .. code-block :: bash
341
+
342
+ \' linear:\^ J\ \ x:\ 0.0\^ J\ \ y:\ 0.0\^ J\ \ z:\ 0.0\^ Jangular:\^ J\ \ x:\ 0.0\^ J\ \ y:\ 0.0\^ J\ \ z:\ 0.0\^ J\'
343
+
344
+ This can be directly used in place of the yaml string in the command line.
345
+
346
+ .. code-block :: bash
347
+
348
+ ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist \' linear:\^ J\ \ x:\ 0.0\^ J\ \ y:\ 0.0\^ J\ \ z:\ 0.0\^ Jangular:\^ J\ \ x:\ 0.0\^ J\ \ y:\ 0.0\^ J\ \ z:\ 0.0\^ J\'
349
+
350
+
247
351
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.
249
- It's important to note that this argument needs to be input in YAML syntax.
250
- Input the full command like so:
352
+ So, to get the turtle moving, and keep it moving, you can use the following dictionary string:
251
353
252
354
.. code-block :: console
253
355
254
356
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
357
256
- With no command-line options, ``ros2 topic pub `` publishes the command in a steady stream at 1 Hz.
257
-
258
358
.. image :: images/pub_stream.png
259
359
260
360
At times you may want to publish data to your topic only once (rather than continuously).
0 commit comments