ROS2在基本概念上与ROS1相似,但是实现上差别较大,比如编程规范等。
上一节是主题实战,有兴趣可以编写代码实现机器人速度发布和坐标订阅。
https://blog.75271.com/44743.html
主题,节点,消息(主题消息)~
那么服务呢,服务器<->客户端,如何实现一个机器人
- 获取目标点坐标?
- 行驶到目标点?
- 显示行驶进度?
这样,三步功能呢,课程复习同样放置在文末。先看效果吧(无解说):
核心要点:
srv:
float32 x
float32 y
---
bool success
x,y为目标坐标,success反馈是否成功接受到坐标。
1. 只接受坐标,机器人mobot不行动!
这里放个水,直接给出参考源码,注意代码并不规范优雅,仅为功能实现。
服务器端:
#include "rclcpp/rclcpp.hpp"
#include "mobot/srv/drivegoalsrv.hpp"
#include <memory>
void get(const std::shared_ptr<mobot::srv::Drivegoalsrv::Request> request,
std::shared_ptr<mobot::srv::Drivegoalsrv::Response> response)
{
if(request->x>0.0)
{
response->success=1;
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Get Goal\nx: %lf" " y: %lf",
request->x, request->y);
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Flag: [%d]", response->success);
}
else
{
response->success=0;
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Error, x>0!!!\nx: %lf" " y: %lf",
request->x, request->y);
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Flag: [%d]", response->success);
}
}
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("get_goal_server");
rclcpp::Service<mobot::srv::Drivegoalsrv>::SharedPtr service =
node->create_service<mobot::srv::Drivegoalsrv>("get_goal", &get);
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to get goal.");
rclcpp::spin(node);
rclcpp::shutdown();
}
客户端:思考实现!三个功能客户端都是一样的,就是送个坐标给服务器而已。
需要修改CMakeLists.txt和package.xml。
2. 行驶到目标点
此为实践作业,需独立完成。
3. 显示进度
详细内容参考前文视频。
补充思考题:
如何用行动(action)实现第三个功能呢,显示进度的功能。
复习:服务
官方示例: sum=a+b
客户端发送a b,
服务器计算求和并返回给客户端。
案例非常简单,稍微复杂一下,如上述机器人案例,发送目标坐标,机器人行驶到目标坐标。
客户端:add_two_ints_client.cpp
#include "rclcpp/rclcpp.hpp"
#include "example_interfaces/srv/add_two_ints.hpp"
#include <chrono>
#include <cstdlib>
#include <memory>
using namespace std::chrono_literals;
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
if (argc != 3) {
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "usage: add_two_ints_client X Y");
return 1;
}
std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_two_ints_client");
rclcpp::Client<example_interfaces::srv::AddTwoInts>::SharedPtr client =
node->create_client<example_interfaces::srv::AddTwoInts>("add_two_ints");
auto request = std::make_shared<example_interfaces::srv::AddTwoInts::Request>();
request->a = atoll(argv[1]);
request->b = atoll(argv[2]);
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_two_ints");
}
rclcpp::shutdown();
return 0;
}
服务器端:add_two_ints_server.cpp
#include "rclcpp/rclcpp.hpp"
#include "example_interfaces/srv/add_two_ints.hpp"
#include <memory>
void add(const std::shared_ptr<example_interfaces::srv::AddTwoInts::Request> request,
std::shared_ptr<example_interfaces::srv::AddTwoInts::Response> response)
{
response->sum = request->a + request->b;
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Incoming request\na: %ld" " b: %ld",
request->a, request->b);
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_two_ints_server");
rclcpp::Service<example_interfaces::srv::AddTwoInts>::SharedPtr service =
node->create_service<example_interfaces::srv::AddTwoInts>("add_two_ints", &add);
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to add two ints.");
rclcpp::spin(node);
rclcpp::shutdown();
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(cpp_srvcli)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(example_interfaces REQUIRED)
add_executable(server src/add_two_ints_server.cpp)
ament_target_dependencies(server
rclcpp example_interfaces)
add_executable(client src/add_two_ints_client.cpp)
ament_target_dependencies(client
rclcpp example_interfaces)
install(TARGETS
server
client
DESTINATION lib/${PROJECT_NAME})
ament_package()
其中要点,解析参考官方文档。