Cpp

El paquete tendrá la siguiente estructura:

 ros2_ws/
  src/
    cpp_float/ #Puedes ser creativo y cambiar este nombre
      src/
        topics/
          listener_float.cpp
          talker_float.cpp
      CMakeLists.txt
      package.xml

El siguiente contenido publicara un mensaje en secuencia

//este ejemplo esta basado en https://github.com/ros2/examples/tree/master/rclcpp_examples/src/topics

#include <iostream>

#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/float32.hpp"

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);

  auto node = rclcpp::node::Node::make_shared("talker_float");

  rmw_qos_profile_t custom_qos_profile = rmw_qos_profile_default;
  custom_qos_profile.depth = 7;

  auto chatter_pub = node->create_publisher<std_msgs::msg::Float32>("chatter", custom_qos_profile);

  rclcpp::WallRate loop_rate(2);
  auto msg = std::make_shared<std_msgs::msg::Float32>();
  auto i = 1;

  while (rclcpp::ok()) {
    msg->data = i++;
    std::cout << "Publishing float: '" << msg->data << "'" << std::endl;
    chatter_pub->publish(msg);
    rclcpp::spin_some(node);
    loop_rate.sleep();
  }

  return 0;
}

El siguiente contenido escuchara mensajes y los imprimirá en pantalla.

//este ejemplo esta basado en https://github.com/ros2/examples/tree/master/rclcpp_examples/src/topics
#include <iostream>
#include <memory>

#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/float32.hpp"

void chatterCallback(const std_msgs::msg::Float32::SharedPtr msg)
{
  std::cout << "I heard: [" << msg->data << "]" << std::endl;
}

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  auto node = rclcpp::Node::make_shared("listener_float");

  auto sub = node->create_subscription<std_msgs::msg::Float32>(
    "chatter", chatterCallback, rmw_qos_profile_default);

  rclcpp::spin(node);

  return 0;
}

Los siguientes archivos son necesarios para contruir el paquete

cmake_minimum_required(VERSION 2.8.3)

project(cpp_float)

if(NOT WIN32)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra")
endif()

find_package(ament_cmake REQUIRED)
find_package(example_interfaces REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rmw REQUIRED)
find_package(rmw_implementation_cmake REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)

function(custom_executable subfolder target)
  add_executable(${target}${target_suffix}
    src/${subfolder}/${target}.cpp)
  ament_target_dependencies(${target}${target_suffix}
    "rclcpp${target_suffix}"
    "std_msgs"
    "example_interfaces")
  install(TARGETS ${target}${target_suffix}
    DESTINATION bin)
endfunction()

macro(targets)
  if(NOT "${target_suffix} " STREQUAL " ")
    get_rclcpp_information("${rmw_implementation}" "rclcpp${target_suffix}")
  endif()
  # Examples of Publish/Subscribe with Topics
  custom_executable(topics talker_float)
  custom_executable(topics listener_float)
endmacro()

call_for_each_rmw_implementation(targets GENERATE_DEFAULT)

ament_package()


<?xml version="1.0"?>
<package format="2">
  <name>cpp_float</name>
  <version>0.0.0</version>
  <description>Package containing examples of how to use the rclcpp API.</description>
  <maintainer email="william@osrfoundation.org">William Woodall</maintainer>
  <license>Apache License 2.0</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <buildtool_depend>rosidl_default_generators</buildtool_depend>

  <build_depend>example_interfaces</build_depend>
  <build_depend>rclcpp</build_depend>
  <build_depend>rmw_implementation</build_depend>
  <build_depend>rmw_implementation_cmake</build_depend>
  <build_depend>sensor_msgs</build_depend>
  <build_depend>std_msgs</build_depend>

  <exec_depend>example_interfaces</exec_depend>
  <exec_depend>rclcpp</exec_depend>
  <exec_depend>rmw_implementation</exec_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <exec_depend>sensor_msgs</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <test_depend>ament_cmake_nose</test_depend>
  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>
  <test_depend>launch</test_depend>
  <test_depend>launch_testing</test_depend>
  <test_depend>rclcpp</test_depend>
  <test_depend>rmw_implementation</test_depend>
  <test_depend>rosidl_default_generators</test_depend>
  <test_depend>rosidl_default_runtime</test_depend>

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

El siguiente comando construirá e instalara tu nuevo paquete

$ cd ~/ros2_ws
$ ament build --symlink-install --only-package cpp_float

Ya solo necesitas ejecutar en dos terminales al mismo tiempo lo siguiente:

$ . ./ros2_ws/install/setup.bash
$ talker_float
$ . ./ros2_ws/install/setup.bash
$ listener_float

¿Necesitas más capacidad de cómputo? Estate atento