[ROS2 in 5 mins] 002 – How to publish and subscribe to a ROS2 topic using command line
In this video we are going to show how to publish and subscribe to a topic using the command line in ROS2.
COMMANDS TO USE:
* Start the docker machine: docker run -it osrf/ros2:ardent-basic
* ros2 topic list
* ros2 topic pub /chatter std_msgs/String “data: Hello ROS Developers”
* ros2 topic echo /chatter
* ros2 msg list
* ros2 msg show std_msgs/String
- ROS2 Basics Course: Introduce you to the basic concepts that you need to know in order to start working with ROS2.
- ROS Development Studio: Develop your first ROS2 project online (Ready online including Gazebo simulations + ros1_bridge)
The following steps take throught the development as shown in the video.
- Open Robot Development Studio and create a new project (use a suitable name for project).
- Activate the docker image for ROS2 by executing the following command in a SHELL
$ docker run -it osrf/ros2:ardent-basic
- The first command to know is
ros2 topic list. This command is same as the
rostopic listcommand in ros 1.
- The command to publist a topic is
ros2 topic pub. This command takes the topic name and message just like the
rostopic pubcommand in ros 1.
- In ros 2 we can use the command
ros2 msg listto see all the supported message types
- Let us now see the usage with a
Stringtype message. We can see the structure of the
Stringmessage with the following command
$ ros2 msg show std_msgs/String
The output of this command will give us the information about the content of the message type
String. To publish the message we will use the following command
$ ros2 topic pub /chatter std_msgs/String “data: Hello ROS Developers”
- Once we execute the above command, a message will start publishing to the
- Next we can check the published message by using
ros2 topic echocommand. This should show us the message on
$ ros2 topic echo /chatter
The output of the above command should be the message that we encoded in the last command.
This finishes the project as shown in the video.
Did you like this video? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.