< >
Home » ROS2与C++入门教程 » ROS2与C++入门教程-编写订阅和发布

ROS2与C++入门教程-编写订阅和发布

ROS2与C++入门教程-编写订阅和发布

说明:

  • 介绍如何编写订阅和发布

编写发布步骤:

  • 新建包cpp_pubsub
cd ~/dev_ws/src
ros2 pkg create --build-type ament_cmake cpp_pubsub
  • 进入src目录,新建文件publisher_member_function.cpp
cd dev_ws/src/cpp_pubsub/src
vim publisher_member_function.cpp
  • 内容如下:
#include <chrono>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

/* This example creates a subclass of Node and uses std::bind() to register a
 * member function as a callback from the timer. */

class MinimalPublisher : public rclcpp::Node
{
public:
  MinimalPublisher()
  : Node("minimal_publisher"), count_(0)
  {
    publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
    timer_ = this->create_wall_timer(
      500ms, std::bind(&MinimalPublisher::timer_callback, this));
  }

private:
  void timer_callback()
  {
    auto message = std_msgs::msg::String();
    message.data = "Hello, world! " + std::to_string(count_++);
    RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
    publisher_->publish(message);
  }
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher<std_msgs::msg::String>::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;
}
  • 编译package.xml
  • <buildtool_depend>ament_cmake</buildtool_depend>后增加
<depend>rclcpp</depend>
<depend>std_msgs</depend>
  • 编译CMakelist.txt
  • find_package(ament_cmake REQUIRED)后增加
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
  • 在增加可执行文件,ros2 run能够调用的名称
add_executable(talker src/publisher_member_function.cpp)
ament_target_dependencies(talker rclcpp std_msgs)
  • 增加可执行文件位置,ros2 run可以找到这个可执行文件
install(TARGETS
  talker
  DESTINATION lib/${PROJECT_NAME})
  • 安装相关依赖
cd ~/dev_ws/
rosdep install -i --from-path src --rosdistro foxy -y
  • 编译包
colcon build --symlink-install --packages-select cpp_pubsub
  • 加载工作空间
. install/setup.bash
  • 执行
ros2 run cpp_pubsub talker 
  • 效果如下:
~/dev_ws$ ros2 run cpp_pubsub talker
[INFO] [1595575890.551113955] [minimal_publisher]: Publishing: 'Hello, world! 0'
[INFO] [1595575891.051032508] [minimal_publisher]: Publishing: 'Hello, world! 1'
[INFO] [1595575891.551034668] [minimal_publisher]: Publishing: 'Hello, world! 2'
[INFO] [1595575892.051249046] [minimal_publisher]: Publishing: 'Hello, world! 3'
[INFO] [1595575892.551164249] [minimal_publisher]: Publishing: 'Hello, world! 4'
[INFO] [1595575893.051148411] [minimal_publisher]: Publishing: 'Hello, world! 5'
[INFO] [1595575893.551117438] [minimal_publisher]: Publishing: 'Hello, world! 6'
^C[INFO] [1595575893.904971833] [rclcpp]: signal_handler(signal_value=2)

编写订阅步骤:

  • 进入cpp_pubsub的src目录下, 新建subscriber_member_function.cpp文件
cd ~/dev_ws/src/cpp_pubsub/src
vim subscriber_member_function.cpp
  • 内容如下:
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;

class MinimalSubscriber : public rclcpp::Node
{
  public:
    MinimalSubscriber()
    : Node("minimal_subscriber")
    {
      subscription_ = this->create_subscription<std_msgs::msg::String>(
      "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
    }

  private:
    void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
    {
      RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
    }
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalSubscriber>());
  rclcpp::shutdown();
  return 0;
}
  • 安装相关依赖
cd ~/dev_ws/
rosdep install -i --from-path src --rosdistro foxy -y
  • 编译包
colcon build --symlink-install --packages-select cpp_pubsub
  • 加载工作空间
. install/setup.bash
  • 执行
ros2 run cpp_pubsub listener
  • 执行效果:
~/dev_ws$ ros2 run cpp_pubsub listener 
[INFO] [1595575948.143876522] [minimal_subscriber]: I heard: 'Hello, world! 51'
[INFO] [1595575948.643331155] [minimal_subscriber]: I heard: 'Hello, world! 52'
[INFO] [1595575949.143391718] [minimal_subscriber]: I heard: 'Hello, world! 53'
[INFO] [1595575949.643239256] [minimal_subscriber]: I heard: 'Hello, world! 54'
[INFO] [1595575950.143320777] [minimal_subscriber]: I heard: 'Hello, world! 55'
[INFO] [1595575950.643215407] [minimal_subscriber]: I heard: 'Hello, world! 56'
^C[INFO] [1595575950.779242884] [rclcpp]: signal_handler(signal_value=2)

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ROS2与C++入门教程