ROS2 学习--创建自己的消息,并被其它节点使用

简介:在ros编程中常用到系统消息,如std_msgs等,但有时候也需要使用特定的消息类型。本文详细阐述创建自己的消息数据类型,然后在另一个功能包中使用的过程。
1. 创建消息数据类型功能包

cd ~/dev_ws/src
~/dev_ws/src$ ros2 pkg create --build-type ament_cmake tutorial_interfaces

2. 新建msg消息目录,并创建消息类型
在tutorial_interfaces功能包中新建msg文件夹,然后,创建一个长整形消息数据类型的文件Num.msg。

//Num.msg
int64 num

3. 编辑package.xml

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>tutorial_interfaces</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="wanghq2020@yeah.net">wanghq2023</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <build_depend>rosidl_default_generators</build_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

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

其中,增加三个语句:rosidl 相关编译和运行时消息生成的。

  <build_depend>rosidl_default_generators</build_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

4. 编辑CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(tutorial_interfaces)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

find_package(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME} "msg/Num.msg" )

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

其中,增加两个语句:

find_package(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME} "msg/Num.msg" )

有了上述配置,编译时就能自动生成相应消息类型的头文件,在其他功能包中便可以引用了。
5. 编译消息包

cd ~/dev_ws/
colcon build --symlink-install --packages-select tutorial_interfaces

6. 环境设置

source ./install/setup.bash

7. 查询消息接口

ros2 interface show tutorial_interfaces/msg/Num
int64 num

显示结果表明,消息编译正常,可以使用。
8. 新建消息发布签收功能包和节点,并使用自己的消息类型
8.1 新建 cpp_pubsub 功能包

cd ~/dev-ws/src
ros2 pkg create --build-type ament_cmake cpp_pubsub

8.2 在包中新建发布消息节点和签收消息节点
注意自定义消息类型。

//publisher_member_function_num.cpp
#include <chrono>
#include <memory>

#include "rclcpp/rclcpp.hpp"
//#include "std_msgs/msg/string.hpp"
#include "tutorial_interfaces/msg/num.hpp" 
/* 包含自定义消息头文件,该头文件由消息包编译器产生。在code调试时,需要正确设置包含路径,这里是:"${workspaceFolder}/build/tutorial_interfaces/rosidl_generator_cpp" */

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<tutorial_interfaces::msg::Num>("topic", 10);
    timer_ = this->create_wall_timer(
      500ms, std::bind(&MinimalPublisher::timer_callback, this));
  }
// 注意:发布器的消息类型是tutorial_interfaces::msg::Num
private:
  void timer_callback()
  {
    auto message = tutorial_interfaces::msg::Num();
    message.num =this->count_++;
    RCLCPP_INFO(this->get_logger(), "Publishing: '%ld'", message.num);
    publisher_->publish(message);
  }
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher<tutorial_interfaces::msg::Num>::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;
}
//subscriber_member_function_num.cpp
#include <memory>

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

using std::placeholders::_1;

class MinimalSubscriber : public rclcpp::Node
{
  public:
    MinimalSubscriber()
    : Node("minimal_subscriber")
    {
      subscription_ = this->create_subscription<tutorial_interfaces::msg::Num>(
      "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
    }
//注意:签收器的消息类型是tutorial_interfaces::msg::Num
  private:
    void topic_callback(const tutorial_interfaces::msg::Num::SharedPtr msg) const
    {
      RCLCPP_INFO(this->get_logger(), "I heard: '%ld'", msg->num);
    }
    rclcpp::Subscription<tutorial_interfaces::msg::Num>::SharedPtr subscription_;
};

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

8.3 修改package.xml

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>cpp_pubsub</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="wanghq2020@yeah.net">wanghq2023</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <depend>tutorial_interfaces</depend>
  
  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

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

其中,增加了消息接口依赖语句:

<depend>tutorial_interfaces</depend>

8.4 修改CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(cpp_pubsub)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

find_package(rclcpp REQUIRED)
# find_package(std_msgs REQUIRED)

find_package(tutorial_interfaces REQUIRED) 

# add_executable(talker src/publisher_member_function.cpp)
# ament_target_dependencies(talker rclcpp std_msgs)

# add_executable(listener src/subscriber_member_function.cpp)
# ament_target_dependencies(listener rclcpp std_msgs)

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

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

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

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

其中,增加语句:

find_package(tutorial_interfaces REQUIRED)

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

add_executable(listener_num src/subscriber_member_function_num.cpp)
ament_target_dependencies(listener_num rclcpp tutorial_interfaces)
install(TARGETS
  #talker
  #listener
  talker_num                               
  listener_num
  DESTINATION lib/${PROJECT_NAME})

8.5 重新编译包

cd ~/dev_ws/
colcon build --packages-select cpp_pubsub

8.6 环境设置

cd ~/dev_ws/
source ./install/setup.bash

8.7 运行节点

ros2 run cpp_pubsub talker_num
ros2 run cpp_pubsub listener_num
[INFO] [1682590576.168105610] [minimal_publisher]: Publishing: '0'
[INFO] [1682590576.667926369] [minimal_publisher]: Publishing: '1'
[INFO] [1682590577.168088412] [minimal_publisher]: Publishing: '2'
[INFO] [1682590577.668228015] [minimal_publisher]: Publishing: '3'

参考文献:
【1】ROS2与C++入门教程-创建消息(msg)文件

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值