3.15. How to write a Publisher and Subscriber#

This concept focuses around a Publisher and Subscriber model, which is essentially just 2 nodes communicating with each other. For more info about nodes, check out the section of the guide about nodes.

The code and instructions used in this tutorial is adapted from the ROS2 documentation, and the code can be found at this link.

Before using this tutorial, please look into the tutorials for how to create a workspace and package found in our guides.

3.16. Create the Package#

In a new terminal, source your ROS2 installation (source install/local_setup.bash or call install/local_setup.bat), and then navigate to your ros2_ws directory.

After entering your workspace, navigate to (or create) your src directory within your workspace. Within this workspace, create a package using the following command:

ros2 pkg create --build-type ament_cmake --license Apache-2.0 cpp_pubsub

The terminal should verify the creation of the package with the name cpp_pubsub.

Next, enter the package you just created using cd ros2_ws/src/cpp_pubsub/src

3.17. Writing the publisher/talker node#

Recall from the installation tutorial we tested if ROS2 was installed through talker and listener nodes? This is the same concept.

On Linux and macOS, use the following command to download the publisher code:

wget -O publisher_member_function.cpp https://raw.githubusercontent.com/ros2/examples/iron/rclcpp/topics/minimal_publisher/member_function.cpp

And on Windows, if you are using command line prompt:

curl -sk https://raw.githubusercontent.com/ros2/examples/iron/rclcpp/topics/minimal_publisher/member_function.cpp -o publisher_member_function.cpp

Or if you are using powershell:

curl https://raw.githubusercontent.com/ros2/examples/iron/rclcpp/topics/minimal_publisher/member_function.cpp -o publisher_member_function.cpp

There should now be a new file called publisher_member_function.cpp, and within the above directory (ros2_ws/src/cpp_pubsub) there should be two new files CMakeLists.txt and package.xml.

If you open package.xml using a text editor, you can see there is a section with the labels <description>, <maintainer>, and <license>, which you should fill in with your own items by placing your information between the tags.

<description> Place your description here </description>
<maintainer email="your@email.com"> Put your name here </maintainer>
<license> Put your license here </license>

And for this tutorial, there should be a line with the ament_cmake dependency. Below that line, put the following dependencies:

<depend>rclcpp</depend>
<depend>std_msgs</depend>

As this package needs both to run. Save the file and exit.

Next, open CMakeLists.txt and locate the line that says find_package(ament_cmakeREQUIRED). Below this line, add the following code:

find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(talker src/publisher_member_function.cpp)
ament_target_dependencies(talker rclcpp std_msgs)
install(TARGETS
  talker
  DESTINATION lib/${PROJECT_NAME})

So your total code should now look as follows:

cmake_minimum_required(VERSION 3.5)
project(cpp_pubsub)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

add_executable(talker src/publisher_member_function.cpp)
ament_target_dependencies(talker rclcpp std_msgs)

install(TARGETS
  talker
  DESTINATION lib/${PROJECT_NAME})

ament_package()

At this point, you should be able to build and run the package, but it is recommended to make the subscriber node next.

3.18. Review Quiz#