Home » ROS2与C++入门教程 » ROS2与C++入门教程-创建消息(msg)文件

ROS2与C++入门教程-创建消息(msg)文件

ROS2与C++入门教程-创建消息(msg)文件

说明:

  • 介绍如何创建消息msg文件

步骤:

  • 使用已经建好的工作空间dev_ws
  • 新建包
cd ~/dev-ws/src
ros2 pkg create --build-type ament_cmake tutorial_interfaces
  • 新建目录mkdir msg
cd ~/dev-ws/src/tutorial_interfaces
mkdir msg 
  • 增加msg定义文件

    cd ~/dev-ws/src/tutorial_interfaces/msg
    vim Num.msg

  • 内容如下:

int64 num
  • 定义一个64位的整数
  • 编辑CMakeLists.txt,增加如下行
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/Num.msg"
 )
  • 编辑package.xml,增加如下行
<build_depend>rosidl_default_generators</build_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
  • 编译包
cd ~/dev_ws/
colcon build --symlink-install --packages-select tutorial_interfaces
  • 接着重新加载工作空间
. install/setup.bash
  • 查询消息接口
ros2 interface show tutorial_interfaces/msg/Num
  • 效果如下:
int64 num

测试:

  • 使用之前的cpp_pubsub包的发布
  • 复制publisher_member_function.cpp到publisher_member_function_num.cpp
cd ~/dev_ws/src/cpp_pubsub/src
cp publisher_member_function.cpp publisher_member_function_num.cpp
  • 修改为:
#include <chrono>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "tutorial_interfaces/msg/num.hpp"     // CHANGE

using namespace std::chrono_literals;

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

private:
  void timer_callback()
  {
    auto message = tutorial_interfaces::msg::Num();                               // CHANGE
    message.num = this->count_++;                                        // CHANGE
    RCLCPP_INFO(this->get_logger(), "Publishing: '%d'", message.num);    // CHANGE
    publisher_->publish(message);
  }
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher<tutorial_interfaces::msg::Num>::SharedPtr publisher_;         // CHANGE
  size_t count_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<MinimalPublisher>());
  rclcpp::shutdown();
  return 0;
}
  • 使用之前的cpp_pubsub包的订阅
  • 复制subscriber_member_function.cpp到subscriber_member_function_num.cpp
  • 修改如下:
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "tutorial_interfaces/msg/num.hpp"     // CHANGE
using std::placeholders::_1;

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

private:
  void topic_callback(const tutorial_interfaces::msg::Num::SharedPtr msg) const       // CHANGE
  {
    RCLCPP_INFO(this->get_logger(), "I heard: '%d'", msg->num);              // CHANGE
  }
  rclcpp::Subscription<tutorial_interfaces::msg::Num>::SharedPtr subscription_;       // CHANGE
};

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

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(tutorial_interfaces REQUIRED)                         # CHANGE

add_executable(talker_num src/publisher_member_function_num.cpp)         # CHANGE
ament_target_dependencies(talker_num rclcpp tutorial_interfaces)         # CHANGE

add_executable(listener_num src/subscriber_member_function_num.cpp)    # CHANGE
ament_target_dependencies(listener_num rclcpp tutorial_interfaces)     # CHANGE

install(TARGETS
  talker
  listener
  talker_num                                 # CHANGE
  listener_num                               # CHANGE
  DESTINATION lib/${PROJECT_NAME})

ament_package()
  • 修改package.xml,增加:
<depend>tutorial_interfaces</depend>
  • 重新编译包
cd ~/dev_ws/
colcon build --packages-select cpp_pubsub
  • 运行测试发布:
ros2 run cpp_pubsub talker_num
[INFO] [minimal_publisher]: Publishing: '0'
[INFO] [minimal_publisher]: Publishing: '1'
[INFO] [minimal_publisher]: Publishing: '2'
  • 运行测试订阅
ros2 run cpp_pubsub listener_num

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

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


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