创建自定义ROS 2 msg 和 srv 文件 [待校准@7179]
Goal目标: Define定义自定义接口文件 ( .msg
和 .srv
),并将其与Python和c nodes节点一起使用。 [待校准@7180]
教程级别: 初学者 [Alyssa@7088]
Time时间: 20 20分钟 [待校准@7181]
内容
背景
在之前的教程中,您利用消息和服务接口来了解 topics, services, and simple publisher/subscriber (C++/Python) and service/client (C++/Python 节点。在这些情况下,您使用的接口是预定义的。 [待校准@7182]
虽然使用预定义的接口定义是一个很好的做法,但有时您可能还需要定义自己的消息和服务。本教程将向您介绍创建自定义界面定义的最简单方法。 [待校准@7183]
先决条件
你应该有一个 ROS 2 workspace 。 [待校准@7184]
本教程还使用发布者/订阅者 ( C++ and Python) and service/client (C++ and Python ) 教程中创建的包来试用新的自定义消息。 [待校准@7185]
任务
1创建一个新包 [待校准@7186]
在本教程中,您将在自己的包中创建自定义 .msg
和 .srv
文件,然后在单独的包中使用它们。两个包应位于同一工作区中。 [待校准@7187]
由于我们将使用在早期教程中创建的发布/订阅和服务/客户端包,请确保您与这些包 ( dev_ws/src
) 位于同一工作区,然后运行以下命令以创建新包: [待校准@7188]
ros2 pkg create --build-type ament_cmake tutorial_interfaces
纯蟒蛇皮包装的``tutorial_interfaces`` is the name of the new package. Note that it is a CMake package; there currently isn’t a way to generate a .msg
or .srv
file。您可以在CMake包中创建一个自定义接口,然后在Python节点中使用它,这将在最后一节中介绍。 [待校准@7189]
最好将 .msg
和 .srv
文件保存在包中自己的目录中。在 dev_ws/src/tutorial_interfaces
中创建目录: [待校准@7190]
mkdir msg
mkdir srv
2创建自定义 [待校准@7191]
2.1 msg 定义 [待校准@7192]
在您刚刚创建的 tutorial_interfaces/msg
目录中,创建一个调用ed Num.msg
的新文件,其中一行代码声明其数据结构: [待校准@7193]
int64 num
这是您的自定义消息,用于传输调用ed num
的单个64位整数。 [待校准@7194]
2.2 srv 定义 [待校准@7195]
回到您刚刚创建的 tutorial_interfaces/srv
目录,创建一个具有以下请求和响应结构的调用 AddThreeInts.srv
的新文件: [待校准@7196]
int64 a
int64 b
int64 c
---
int64 sum
这是您的自定义服务,它请求三个整数,分别名为 a
、 b
和 c
,并以整数调用ed sum
作为响应。 [待校准@7197]
3 CMakeLists.txt
[待校准@7198]
要将您定义的接口转换为特定于语言的代码 (如C++ 和Python),以便它们可以在这些语言中使用,请在 CMakeLists.txt
中添加以下行: [待校准@7199]
find_package(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Num.msg"
"srv/AddThreeInts.srv"
)
4 package.xml
[待校准@7200]
因为接口依赖于 rosidl_default_generators
来生成特定于语言的代码,所以您需要声明对它的依赖。将以下几行添加到 package.xml
[待校准@7201]
<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
包 [待校准@7202]
现在,自定义接口包的所有部分都已就位,您可以构建包了。在工作区 (''〜/dev_ws '') 的根目录中,运行以下命令: [待校准@7203]
colcon build --packages-select tutorial_interfaces
colcon build --packages-select tutorial_interfaces
colcon build --merge-install --packages-select tutorial_interfaces
现在这些接口将被其他ROS 2包发现。 [待校准@7204]
6确认 msg 和 srv 的创造 [待校准@7205]
在新终端中,从工作区 ( dev_ws
) 中运行以下命令以对其进行源文件: [待校准@7206]
. install/setup.bash
. install/setup.bash
call install/setup.bat
现在,您可以通过使用 ros2 interface show
命令确认您的接口创建有效: [待校准@7207]
ros2 interface show tutorial_interfaces/msg/Num
应返回: [待校准@7208]
int64 num
ros2 interface show tutorial_interfaces/srv/AddThreeInts
应返回: [待校准@7208]
int64 a
int64 b
int64 c
---
int64 sum
7测试新界面 [待校准@7210]
对于此步骤,您可以使用在以前的教程中创建的包。对节点、 CMakeLists
和 package
文件的一些简单修改将允许你使用你的新界面。 [待校准@7211]
7.1用酒吧/潜水艇测试 Num.msg
[待校准@7212]
通过对上一教程 ( C++ or Python ) 中创建的发布者/订阅者包进行一些细微的修改,您可以在动作中看到 Num.msg
。由于您将把标准string msg 改为数字,输出将略有不同。 [待校准@7213]
出版商: [待校准@7214]
#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;
}
import rclpy
from rclpy.node import Node
from tutorial_interfaces.msg import Num # CHANGE
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(Num, 'topic', 10) # CHANGE
timer_period = 0.5
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
def timer_callback(self):
msg = Num() # CHANGE
msg.num = self.i # CHANGE
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%d"' % msg.num) # CHANGE
self.i += 1
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
minimal_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
订户: [待校准@7216]
#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;
}
import rclpy
from rclpy.node import Node
from tutorial_interfaces.msg import Num # CHANGE
class MinimalSubscriber(Node):
def __init__(self):
super().__init__('minimal_subscriber')
self.subscription = self.create_subscription(
Num, # CHANGE
'topic',
self.listener_callback,
10)
self.subscription
def listener_callback(self, msg):
self.get_logger().info('I heard: "%d"' % msg.num) # CHANGE
def main(args=None):
rclpy.init(args=args)
minimal_subscriber = MinimalSubscriber()
rclpy.spin(minimal_subscriber)
minimal_subscriber.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
CMakeLists.txt: [待校准@7217]
添加以下行 (仅conly): [待校准@7218]
#...
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: [待校准@7219]
添加以下行: [待校准@7220]
<depend>tutorial_interfaces</depend>
<exec_depend>tutorial_interfaces</exec_depend>
完成上述编辑并保存所有更改后,构建包: [待校准@7221]
colcon build --packages-select cpp_pubsub
在Windows上: [待校准@7222]
colcon build --merge-install --packages-select cpp_pubsub
colcon build --packages-select py_pubsub
在Windows上: [待校准@7222]
colcon build --merge-install --packages-select py_pubsub
然后打开两个新的终端,每个终端都有源文件 dev_ws
,然后运行: [待校准@7223]
ros2 run cpp_pubsub talker
ros2 run cpp_pubsub listener
ros2 run py_pubsub talker
ros2 run py_pubsub listener
由于 Num.msg
只中继一个整数,谈话者应该只发布整数值,而不是它之前发布的string: [待校准@7224]
[INFO] [minimal_publisher]: Publishing: '0'
[INFO] [minimal_publisher]: Publishing: '1'
[INFO] [minimal_publisher]: Publishing: '2'
7.2与服务/客户一起测试 AddThreeInts.srv
[待校准@7225]
通过对上一教程 ( C++ or Python ) 中创建的服务/客户端包进行一些轻微的修改,您可以在动作中看到 AddThreeInts.srv
。由于您将把原来的两个整数请求 srv 改为三个整数请求srv,输出将略有不同。 [待校准@7226]
服务: [待校准@7227]
#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();
}
from tutorial_interfaces.srv import AddThreeInts # CHANGE
import rclpy
from rclpy.node import Node
class MinimalService(Node):
def __init__(self):
super().__init__('minimal_service')
self.srv = self.create_service(AddThreeInts, 'add_three_ints', self.add_three_ints_callback) # CHANGE
def add_three_ints_callback(self, request, response):
response.sum = request.a + request.b + request.c # CHANGE
self.get_logger().info('Incoming request\na: %d b: %d c: %d' % (request.a, request.b, request.c)) # CHANGE
return response
def main(args=None):
rclpy.init(args=args)
minimal_service = MinimalService()
rclpy.spin(minimal_service)
rclpy.shutdown()
if __name__ == '__main__':
main()
客户: [待校准@7228]
#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;
}
from tutorial_interfaces.srv import AddThreeInts # CHANGE
import sys
import rclpy
from rclpy.node import Node
class MinimalClientAsync(Node):
def __init__(self):
super().__init__('minimal_client_async')
self.cli = self.create_client(AddThreeInts, 'add_three_ints') # CHANGE
while not self.cli.wait_for_service(timeout_sec=1.0):
self.get_logger().info('service not available, waiting again...')
self.req = AddThreeInts.Request() # CHANGE
def send_request(self):
self.req.a = int(sys.argv[1])
self.req.b = int(sys.argv[2])
self.req.c = int(sys.argv[3]) # CHANGE
self.future = self.cli.call_async(self.req)
def main(args=None):
rclpy.init(args=args)
minimal_client = MinimalClientAsync()
minimal_client.send_request()
while rclpy.ok():
rclpy.spin_once(minimal_client)
if minimal_client.future.done():
try:
response = minimal_client.future.result()
except Exception as e:
minimal_client.get_logger().info(
'Service call failed %r' % (e,))
else:
minimal_client.get_logger().info(
'Result of add_three_ints: for %d + %d + %d = %d' % # CHANGE
(minimal_client.req.a, minimal_client.req.b, minimal_client.req.c, response.sum)) # CHANGE
break
minimal_client.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
CMakeLists.txt: [待校准@7217]
添加以下行 (仅conly): [待校准@7218]
#...
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: [待校准@7219]
添加以下行: [待校准@7220]
<depend>tutorial_interfaces</depend>
<exec_depend>tutorial_interfaces</exec_depend>
完成上述编辑并保存所有更改后,构建包: [待校准@7221]
colcon build --packages-select cpp_srvcli
在Windows上: [待校准@7222]
colcon build --merge-install --packages-select cpp_srvcli
colcon build --packages-select py_srvcli
在Windows上: [待校准@7222]
colcon build --merge-install --packages-select py_srvcli
然后打开两个新的终端,每个终端都有源文件 dev_ws
,然后运行: [待校准@7223]
ros2 run cpp_srvcli server
ros2 run cpp_srvcli client 2 3 1
ros2 run py_srvcli service
ros2 run py_srvcli client 2 3 1
总结
在本教程中,您学习了如何在自己的包中创建自定义接口,以及如何在其他包中利用这些接口。 [待校准@7229]
这是一个简单的接口创建和利用方法。你可以了解更多关于接口 here 的信息。 [待校准@7230]
下一步
[需手动修复的语法] next tutorial 涵盖了在ROS 2中使用接口的更多方法。 [待校准@7231]