目录
1.ros2接口描述
2.ros2接口定义
3.ROS2接口常用的命令行指令
4.测试自定义接口
ros2节点之间通讯一般使用std_msgs/下的标准数据类型,当我们要定义自己的消息类型的时候,需要定义通讯接口(数据类型)文件。自定义的通讯数据类型可以用已有的通讯数据类型进行组装,也可以使用ros2规定的原始数据类型组装。已有的通讯接口定义可以使用ros2的命令查看:
ros2 interface list
ros2规定的原始的数据类型只有九类。其中每一个都可以在后面加上[]
将其变成数组形式:
bool
byte
char
float32, float64
int8, uint8
int16, uint16
int32, uint32
int64, uint64
string
在ROS2中根据不同通讯类型定义不同接口文件,该文件后缀分别为msg
、srv
、action.
例如:
话题接口
文件名: *.msg
int64 num
服务接口
文件名:*.srv
int64 a #这三个是输入参数
int64 b
int64 c
---
int64 sum #这个是返回结果
动作接口
文件名:*.action
int32 order
---
int32[] sequence
---
int32[] partial_sequence
至于参数这种方式不存在接口定义。
以话题通讯方式举例定义接口:
ros2 pkg create test --dependencies rclcpp --build-type ament_cmake
mkdir msg
touch name.msg
如图所示:
1.添加消息内容(注意文件名:Name.msg首字母必须大写)
2.编辑CmakeLists.txt文件(注意添加位置)
# 这两句添加依赖
find_package(sensor_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
# 声明msg文件所属的工程名字, 文件位置, 依赖DEPENDENCIES
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Name.msg"
DEPENDENCIES sensor_msgs
)
3.修改package.xml
sensor_msgs
rosidl_default_generators
rosidl_default_runtime
rosidl_interface_packages
4.执行编译生成c++头文件
colcon build
会在build/interface路径下生成一系列目录文件
编写订阅发布程序
intertest.h文件
#ifndef INTERTEST_H
#define INTERTEST_H
#include "rclcpp/rclcpp.hpp"
#include "interface/msg/name.hpp"
#include "rclcpp/publisher.hpp"
#include "rclcpp/subscription.hpp"
#include "std_msgs/msg/string.h"
class intertest:public rclcpp::Node
{
public:
intertest(std::string text);
//private:
void interfaceCallback(interface::msg::Name msg);
void timerCallback();
private:
// interface::msg::Name
rclcpp::Publisher::SharedPtr puber_;
rclcpp::Subscription::SharedPtr Suber_;
rclcpp::TimerBase::SharedPtr timerfun_;
// rclcpp::TimerBase::SharedPtr timer_;
};
#endif // INTERTEST_H
intertest.cpp文件
#include "intertest.h"
using ::std::placeholders::_1;
intertest::intertest(std::string text):
rclcpp::Node(text)
{
RCLCPP_INFO(this->get_logger(),"%s",text.c_str());
puber_ = this->create_publisher("interface_test",10);
Suber_ = this->create_subscription("interface_test",10,
std::bind(&intertest::interfaceCallback,this,_1));
timerfun_ = this->create_wall_timer(std::chrono::milliseconds(100),std::bind(&intertest::timerCallback,this));
}
void intertest::interfaceCallback(interface::msg::Name msg)
{
RCLCPP_INFO(this->get_logger(),"%s,%d",msg.text.c_str(),msg.count);
}
void intertest::timerCallback()
{
static interface::msg::Name msg;
msg.count++;
msg.text = "interfacetest";
puber_->publish(msg);
}
main.cpp文件
#include "intertest.h"
int main(int argc,char** argv)
{
rclcpp::init(argc,argv);
auto p = std::make_shared("test");
rclcpp::spin(p);
rclcpp::shutdown();
return 0;
}
修改CMakeLists.txt
find_package(interface REQUIRED)
add_executable(inter src/main.cpp src/intertest.cpp)
ament_target_dependencies(inter "rclcpp" "std_msgs" "interface")
install(TARGETS
inter
DESTINATION lib/${PROJECT_NAME}
)
make_minimum_required(VERSION 3.8)
project(interface)
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)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
# 这三句添加依赖
find_package(sensor_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(interface REQUIRED)
# 声明msg文件所属的工程名字, 文件位置, 依赖DEPENDENCIES
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Name.msg"
DEPENDENCIES sensor_msgs
)
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()
# target_compile_features(interface PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
add_executable(inter src/main.cpp src/intertest.cpp)
ament_target_dependencies(inter "rclcpp" "std_msgs" "interface")
install(TARGETS
inter
DESTINATION lib/${PROJECT_NAME}
)
package.xml保持原样不用修改
interface
0.0.0
TODO: Package description
klppc
TODO: License declaration
ament_cmake
rclcpp
sensor_msgs
std_msgs
rosidl_default_generators
rosidl_default_runtime
rosidl_interface_packages
ament_lint_auto
ament_lint_common
ament_cmake
编译程序:
source ./install/setup.bash
colcon build
ros2 run interface inter
运行结果:
rqt查看:
注意:使用命令打印topic消息时需要先source一下环境。
source ./cache/interface/install/setup.bash
ros2 topic echo /interface_test
参考资料:
ROS2——什么是接口_ros2 std_msgs_范子琦的博客-CSDN博客