扩展ROS2软件包的接口
回顾上一章节,讲解了如何创建用户自定义msg和srv文件对应的软件包。这章节延续上一节的内容,扩展ROS2软件包的接口。将消息msg直接定义节点软件包中,扩展软件包的功能。废话不多说,我们进入正题中去。
一、程序编写
1、创建软件包
ros2 pkg create --build-type ament_cmake more_interfaces
2、创建消息文件夹
mkdir more_interfaces/msg
3、软件包添加新消息
src/more_interfaces/msg/AddressBook.msg
bool FEMALE=true
bool MALE=false
string first_name
string last_name
bool gender
uint8 age
string address
4、修改软件包设置
src/more_interfaces/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>more_interfaces</name>
<version>0.0.0</version>
<description>more interfaces</description>
<maintainer email="motion_gui@126.com">gmotion</maintainer>
<license>Apache License 2.0</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>
5、修改编译选项
src/more_interfaces/CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(more_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)
#添加ros节点包
find_package(rclcpp REQUIRED)
#添加消息生成器软件包
find_package(rosidl_default_generators REQUIRED)
#添加自定义的消息文件
set(msg_files
"msg/AddressBook.msg"
)
#添加消息接口
rosidl_generate_interfaces(${PROJECT_NAME}
${msg_files}
)
#添加运行依赖
ament_export_dependencies(rosidl_default_runtime)
#添加执行路径
add_executable(publish_address_book
src/publish_address_book.cpp
)
ament_target_dependencies(publish_address_book
"rclcpp"
)
#针对接口内部连接
rosidl_target_interfaces(publish_address_book
${PROJECT_NAME} "rosidl_typesupport_cpp")
#软件包生成安装包
install(TARGETS publish_address_book
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()
6、编写发布者程序
/**
* @file publish_address_book.cpp
* @author gmotion (motion_gui@126.com)
* @brief 带接口的发布者程序
* @version 0.1
* @date 2022-06-11
* @copyright gmotion Copyright (c) 2022
*/
#include <iostream> //系统标准接口
#include <chrono> //处理时间类
#include <functional> //函数对象
#include <memory> //内存管理
#include <string> //字符串
#include <rclcpp/rclcpp.hpp>
#include <more_interfaces/msg/address_book.hpp>
using namespace std::chrono_literals;
class AddressBookPublisher : public rclcpp::Node
{
public:
//构造函数
AddressBookPublisher() : Node("address_book_publisher")
{
//初始化发布者
publisher_ = this->create_publisher<more_interfaces::msg::AddressBook>("address_book",10);
//创建回调函数
auto publish_msg=[this]()->void{
auto messgae=more_interfaces::msg::AddressBook();
messgae.first_name="gui";
messgae.last_name="zhiwei";
messgae.age=32;
messgae.gender = messgae.MALE;
messgae.address="unknow";
std::cout << "Publishing Contact\n Name : " << messgae.first_name << messgae.last_name
<<std::endl;
this->publisher_->publish(messgae);
};
//初始化时钟
timer_ = this->create_wall_timer(1s,publish_msg);
}
//析构函数
~AddressBookPublisher()
{
}
private:
//发布者
rclcpp::Publisher<more_interfaces::msg::AddressBook>::SharedPtr publisher_;
//时间节点控制
rclcpp::TimerBase::SharedPtr timer_;
};
int main(int argc,char * argv[])
{
rclcpp::init(argc,argv);
rclcpp::spin(std::make_shared<AddressBookPublisher>());
rclcpp::shutdown();
}
7、编程细节部分
#include <more_interfaces/msg/address_book.hpp>
头文件找不到,因为程序未进行编译操作。
可以提前预先编译
colcon build --packages-up-to more_interfaces
8、补充说明多个消息文件和多个服务文件-编译选项规则
set(msg_files "msg/Message1.msg" "msg/Message2.msg" # etc )
set(srv_files "srv/Service1.srv" "srv/Service2.srv" # etc )
rosidl_generate_interfaces(${PROJECT_NAME} ${msg_files} ${srv_files})
二、程序测试
1、编译软件包
colcon build --packages-up-to more_interfaces
2、测试运行
. install/setup.bash
ros2 run more_interfaces publish_address_book
3、话题订阅测试
. install/setup.bash
ros2 topic list
os2 run more_interfaces publish_address_book
自定义话题准确发出,通过测试,可以正常在系统订阅消息。达到本章节内容指示点,有问题可以发邮件和留言。有不足之处,请多多指教。谢谢大家的阅读,下一章节,我们再会。