ROS2学习之旅(16)——创建自定义ROS2 msg和srv文件
1.创建功能包
在本文中,将在自己的包中创建自定义的.msg
和.srv
文件,然后在另外的包中使用它们,这两个包应该在同一个工作空间中。
本文将使用在前面文章中创建的pub/sub和service/client包【可参考前几篇RO2学习之旅的文章】,请确保处在dev_ws/src
目录下,然后运行以下命令创建一个新包:
ros2 pkg create --build-type ament_cmake tutorial_interfaces
tutorial_interfaces
是新包的名称。注意,它是一个CMake包,目前还没有办法在纯Python包中生成.msg
或.srv
文件。但是,可以在CMake包中创建自定义接口,然后在Python节点中使用它。
将.msg
和.srv
文件保存在自己的包中是一种很好的做法。在dev_ws/src/tutorial_interfaces
目录下创建:
mkdir msg
mkdir srv
2.创建自定义消息文件
2.1自定义msg文件
在tutorial_interfaces/msg
文件夹下,创建一个名为Num.msg
的新文件,用一行代码声明它的数据结构:
int64 num
这是自定义的消息,它传输一个名称为num
的64位整数。
2.2自定义srv文件
在tutorial_interfaces/srv
文件夹下,创建一个名为AddThreeInts.srv
的新文件,并声明他的请求和响应结构:
int64 a
int64 b
int64 c
---
int64 sum
这是定制的服务,它请求三个名为a、b和c的整数,并以一个名为sum的整数进行响应。
3.CMakeLists.txt
要将自定义的接口(interface)转换成基于语言的代码(如C++和Python),以便它们可以在这些语言中使用,请将以下代码添加到CMakeLists.txt
中:
find_package(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Num.msg"
"srv/AddThreeInts.srv"
)
4.package.xml
因为接口依赖于rosidl_default_generators
来生成基于语言的代码,所以需要声明对它的依赖关系。将以下行添加到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>
5.编译 tutorial_interfaces
包
在工作空间的根目录(~/dev_ws
)中,运行以下命令:
colcon build --packages-select tutorial_interfaces
现在接口将被其他ROS 2包发现。
6.验证msg和srv的创建
打开新终端,在dev_ws
下运行:
. install/setup.bash
可以使用ros2 interface show
命令确认接口创建工作:
ros2 interface show tutorial_interfaces/msg/Num
返回:
int64 num
运行:
ros2 interface show tutorial_interfaces/srv/AddThreeInts
返回:
int64 a
int64 b
int64 c
---
int64 sum
7.测试新接口
使用在前面的文章中创建的包,对节点、CMakelist
和package
文件进行一些简单的修改,就可以使用新的接口。
7.1使用pub/sub测试Num.msg
对前面创建的发布节点进行修改(~/dev_ws/src/cpp_pubsub/src/publisher_member_function.cpp
)。
Publisher:
#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;
}
对前面创建的订阅节点进行修改(~/dev_ws/src/cpp_pubsub/src/subscriber_member_function.cpp.cpp
)。
Subscriber:
#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 src/publisher_member_function.cpp)
ament_target_dependencies(talker rclcpp tutorial_interfaces) # CHANGE
add_executable(listener src/subscriber_member_function.cpp)
ament_target_dependencies(listener rclcpp tutorial_interfaces) # CHANGE
install(TARGETS
talker
listener
DESTINATION lib/${PROJECT_NAME})
ament_package()
在package.xml
中添加:
<depend>tutorial_interfaces</depend>
接着,编译整个包:
colcon build --packages-select cpp_pubsub
打开新终端,source dev_ws
, 并运行:
ros2 run cpp_pubsub talker
ros2 run cpp_pubsub listener
因为Num.msg
只传递一个整数,所以它应该只发布整数值,而不是之前发布的字符串:
[INFO] [minimal_publisher]: Publishing: '0'
[INFO] [minimal_publisher]: Publishing: '1'
[INFO] [minimal_publisher]: Publishing: '2'
7.2使用service/client测试AddThreeInts.srv
对前面创建的发布节点进行修改(~/dev_ws/src/cpp_srvcli/src/add_two_ints_server.cpp
)。
Service:
#include "rclcpp/rclcpp.hpp"
#include "tutorial_interfaces/srv/add_three_ints.hpp" // CHANGE
#include <memory>
void add(const std::shared_ptr<tutorial_interfaces::srv::AddThreeInts::Request> request, // CHANGE
std::shared_ptr<tutorial_interfaces::srv::AddThreeInts::Response> response) // CHANGE
{
response->sum = request->a + request->b + request->c; // CHANGE
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Incoming request\na: %ld" " b: %ld" " c: %ld", // CHANGE
request->a, request->b, request->c); // CHANGE
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "sending back response: [%ld]", (long int)response->sum);
}
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_three_ints_server"); // CHANGE
rclcpp::Service<tutorial_interfaces::srv::AddThreeInts>::SharedPtr service = // CHANGE
node->create_service<tutorial_interfaces::srv::AddThreeInts>("add_three_ints", &add); // CHANGE
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to add three ints."); // CHANGE
rclcpp::spin(node);
rclcpp::shutdown();
}
对前面创建的发布节点进行修改(~/dev_ws/src/cpp_srvcli/src/add_two_ints_client.cpp
)。
Client:
#include "rclcpp/rclcpp.hpp"
#include "tutorial_interfaces/srv/add_three_ints.hpp" // CHANGE
#include <chrono>
#include <cstdlib>
#include <memory>
using namespace std::chrono_literals;
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
if (argc != 4) { // CHANGE
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "usage: add_three_ints_client X Y Z"); // CHANGE
return 1;
}
std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_three_ints_client"); // CHANGE
rclcpp::Client<tutorial_interfaces::srv::AddThreeInts>::SharedPtr client = // CHANGE
node->create_client<tutorial_interfaces::srv::AddThreeInts>("add_three_ints"); // CHANGE
auto request = std::make_shared<tutorial_interfaces::srv::AddThreeInts::Request>(); // CHANGE
request->a = atoll(argv[1]);
request->b = atoll(argv[2]);
request->c = atoll(argv[3]); // CHANGE
while (!client->wait_for_service(1s)) {
if (!rclcpp::ok()) {
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service. Exiting.");
return 0;
}
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "service not available, waiting again...");
}
auto result = client->async_send_request(request);
// Wait for the result.
if (rclcpp::spin_until_future_complete(node, result) ==
rclcpp::executor::FutureReturnCode::SUCCESS)
{
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Sum: %ld", result.get()->sum);
} else {
RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Failed to call service add_three_ints"); // CHANGE
}
rclcpp::shutdown();
return 0;
}
修改CMakeLists.txt
:
#...
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(tutorial_interfaces REQUIRED) # CHANGE
add_executable(server src/add_two_ints_server.cpp)
ament_target_dependencies(server
rclcpp tutorial_interfaces) #CHANGE
add_executable(client src/add_two_ints_client.cpp)
ament_target_dependencies(client
rclcpp tutorial_interfaces) #CHANGE
install(TARGETS
server
client
DESTINATION lib/${PROJECT_NAME})
ament_package()
在package.xml
文件中添加:
<depend>tutorial_interfaces</depend>
编译包:
colcon build --packages-select cpp_srvcli
打开新终端, source dev_ws
, 并运行:
ros2 run cpp_srvcli server
ros2 run cpp_srvcli client 2 3 1
8.总结
在本文中,学习了如何在自己的包中创建自定义接口,以及如何从其他包中利用这些接口。
如果给您带来帮助,希望能给点个关注,以后还会陆续更新有关机器人的内容,点个关注不迷路~欢迎大家一起交流学习。
都看到这了,点个推荐再走吧~
未经允许,禁止转载。