ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Ok, just for ROS2 beginners like myself, here is what I ended up doing to get this minimal publisher/subscriber example with a custom message running.

First, it gets much simpler when using only one single package that contains both the publisher and the subscriber. Based on the example packages and the very helpful remarks of @Dirk Thomas, I came up with the following files which I put in a src/minimal_chatter folder:

  • lambda_pub.cpp
  • lambda_sub.cpp
  • msg/LNumM.msg

and of course

  • CMakeLists.txt
  • package.xml

Here is the content of the files:

lambda_pub.cpp

#include <chrono>
#include "rclcpp/rclcpp.hpp"
#include "minimal_chatter/msg/l_num_m.hpp"

using namespace std::chrono_literals;

class MinimalPublisher : public rclcpp::Node
{
public:
  MinimalPublisher()
  : Node("minimal_publisher"), count_(0)
  {
    publisher_ = this->create_publisher<minimal_chatter::msg::LNumM>("chatter");
    auto timer_callback =
      [this]() -> void {
        auto message = minimal_chatter::msg::LNumM();
        message.num = this->count_++;
        RCLCPP_INFO(this->get_logger(), "Publishing: '%d'", message.num);
        this->publisher_->publish(message);
      };
    timer_ = this->create_wall_timer(500ms, timer_callback);
  }

private:
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher<minimal_chatter::msg::LNumM>::SharedPtr publisher_;
  size_t count_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalPublisher>());
  rclcpp::shutdown();
  return 0;
}

lambda_sub.cpp

#include <iostream>
#include "rclcpp/rclcpp.hpp"
#include "minimal_chatter/msg/l_num_m.hpp"

class MinimalSubscriber : public rclcpp::Node
{
public:
  MinimalSubscriber()
  : Node("minimal_subscriber")
  {
    subscription_ = this->create_subscription<minimal_chatter::msg::LNumM>(
      "chatter",
      [this](minimal_chatter::msg::LNumM::UniquePtr msg) {
      RCLCPP_INFO(this->get_logger(), "I heard: '%d'", msg->num);
    });
  }

private:
  rclcpp::Subscription<minimal_chatter::msg::LNumM>::SharedPtr subscription_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalSubscriber>());
  rclcpp::shutdown();
  return 0;
}

msg/LNumM.msg

int64 num

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(minimal_chatter)

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)
find_package(rosidl_default_generators REQUIRED)

set(msg_files 
   "msg/LNumM.msg"
)

rosidl_generate_interfaces(${PROJECT_NAME}
   ${msg_files}
   DEPENDENCIES std_msgs
)

add_executable(subscriber_lambda lambda_sub.cpp)
ament_target_dependencies(subscriber_lambda rclcpp std_msgs)
rosidl_target_interfaces(subscriber_lambda ${PROJECT_NAME} "rosidl_typesupport_cpp")

add_executable(publisher_lambda lambda_pub.cpp)
ament_target_dependencies(publisher_lambda rclcpp std_msgs)
rosidl_target_interfaces(publisher_lambda ${PROJECT_NAME} "rosidl_typesupport_cpp")

ament_package()

install(TARGETS 
   subscriber_lambda
   publisher_lambda
   DESTINATION lib/${PROJECT_NAME}
)

package.xml

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>minimal_chatter</name>
  <version>0.6.1</version>
  <description>Examples of a minimal publisher/subscriber pair</description>
  <maintainer email="jacob@openrobotics.org">Jacob Perron</maintainer>
  <license>Apache License 2.0</license>
  <author>Mikael Arguedas</author>
  <author>Morgan Quigley</author>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <buildtool_depend>rosidl_default_generators</buildtool_depend>

  <build_depend>rclcpp</build_depend>
  <build_depend>std_msgs</build_depend>

  <member_of_group>rosidl_interface_packages</member_of_group>

  <exec_depend>rosidl_default_runtime</exec_depend>
  <exec_depend>rclcpp</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>