ROS2学习:创建自己的ROS2功能包

一、创建工作空间

创建文件夹

mkdir -p ~/ros2_ws/src
cd ~/ros2_ws
colcon build

二、写简单的发布和订阅

1 创建功能包
创建一个pubsub的包

ros2 pkg create --build-type ament_cmake cpp_pubsub

xml文件里的邮箱

2 写发布节点
创建ros2_talker.cpp文件

touch ros2_talker.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;
  }

注意!!!
记得右击ros2_talker.cpp文件,选择成为可执行文件

3 写订阅节点
创建ros2_listener.cpp文件

touch ros2_listener.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;
}

4 修改 CMakeLists.txt
在 find_package(ament_cmake REQUIRED)下面添加

find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

5 添加可执行文件的名字为ros2_talker和ros2_listener

add_executable(ros2_talker src/ros2_talker.cpp)
ament_target_dependencies(ros2_talker rclcpp std_msgs)
add_executable(ros2_listener src/ros2_listener.cpp)
ament_target_dependencies(ros2_listener rclcpp std_msgs)

6 添加 install(TARGETS…) 把可执行文件安装到install去

install(TARGETS
  ros2_talker
  ros2_listener
  DESTINATION lib/${PROJECT_NAME})

7 编译和运行

cd ~/ros2_ws
colcon build --packages-select cpp_pubsub
source install/setup.bash

运行两个节点

ros2 run cpp_pubsub ros2_talker
ros2 run cpp_pubsub ros2_listener
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页