Foxy Simple Publisher and Subscriber 20210322
From wikidb
Contents
Context
- Quick overview for the April 20, 2021 Oregon IEEE RAS Burger Build. See the reference below for details.
- Find in eepp_ws/src/cpp_pubsub
References
Publish and Subscribe
- Foxy Tutorials
- Writing a C++ Publish and Subscrib Tutorial
- Automatic Addison Create a Publisher and Subscriber in C++ Rose 2 Foxy itzroy
Prereqs
Steps
Create Workspace and Package - An Overlay
$ mkdir -p explore_images_ws/src $ cd explore_images_ws/src/ $ ros2 pkg create --build-type ament_cmake cpp_pubsub going to create a new package package name: cpp_pubsub destination directory: /home/eepp/explore_images_ws/src package format: 3 version: 0.0.0 description: TODO: Package description maintainer: ['eepp <eepp@todo.todo>'] licenses: ['TODO: License declaration'] build type: ament_cmake dependencies: [] creating folder ./cpp_pubsub creating ./cpp_pubsub/package.xml creating source and include folder creating folder ./cpp_pubsub/src creating folder ./cpp_pubsub/include/cpp_pubsub creating ./cpp_pubsub/CMakeLists.txt
Copy in the Package Files
This is a shortcut - see references for details.
Source Files
$ cd ~/eepp_ws/src/cpp_pubsub/src $ ls publisher_member_function.cpp subscriber_member_function.cpp
Package and Make Files
$ cd ~/eepp_ws/src/cpp_pubsub $ ls CMakeLists.txt include package.xml src
Customizations
<description>Minimal publisher/subscriber from Foxy tutorials</description> <maintainer email="eepp@zdome.net">eepp</maintainer> <license>Apache License 2.0</license>
note
<depend>rclcpp</depend> <depend>std_msgs</depend>
Important cusomizations
add_executable(talker src/publisher_member_function.cpp) ament_target_dependencies(talker rclcpp std_msgs) add_executable(listener src/subscriber_member_function.cpp) ament_target_dependencies(listener rclcpp std_msgs) install(TARGETS talker listener DESTINATION lib/${PROJECT_NAME})
Build
$ cd ~/eepp_ws $ colcon build --packages-select cpp_pubsub Starting >>> cpp_pubsub Finished <<< cpp_pubsub [1.41s] Summary: 1 package finished [1.62s] $ ls install/cpp_pubsub/lib/cpp_pubsub/ listener talker
or
$ colcon build --symlink-install --parallel-workers 1
Put the following empty file into a project direcoty to bypass build.
CATKIN_IGNORE
Execute
TERMINAL 1
$ source ~/eepp_ws/install/setup.bash $ ros2 run cpp_pubsub talker [INFO] [1616611188.838432139] [minimal_publisher]: Publishing: 'Hello, world! 0' [INFO] [1616611189.338379860] [minimal_publisher]: Publishing: 'Hello, world! 1' [INFO] [1616611189.838355264] [minimal_publisher]: Pub ... ...
TERMINAL 2
$ source ~/eepp_ws/install/setup.bash $ ros2 run cpp_pubsub listener [INFO] [1616611370.928743717] [minimal_subscriber]: I heard: 'Hello, world! 52' [INFO] [1616611371.428557842] [minimal_subscriber]: I heard: 'Hello, world! 53' [INFO] [1616611371.928473827] [minimal_subscriber]: I heard: 'Hello, world! 54' [INFO] [1616611372.428607106] [minimal_subscriber]: I heard: 'Hello, world! 55' [INFO] [1616611372.928570767] [minimal_subscriber]: I heard: 'Hello, world! 56' [INFO] [1616611373.428502002] [minimal_subscriber]: I heard: 'Hello, world! 57' [INFO] [1616611373.928500708] [minimal_subscriber]: I heard: 'Hello, world! 58' [INFO] [1616611374.428463893] [minimal_subscriber]: I heard: 'Hello, world! 59' [INFO] [1616611374.928503750] [minimal_subscriber]: I heard: 'Hello, world! 60' ... ...