首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
20 篇文章
1
ROS1&2机器人操作系统免安装在线自主学习平台简介和官方第三方教程列表
2
机器人操作系统二(ROS2)- 启程 Departure
3
机器人操作系统二(ROS2)- 修行 Taoism
4
机器人操作系统二(ROS2)- 仿真 Simulation
5
机器人操作系统二(ROS2)- 实践 Practice
6
ROS2与Arduino发布订阅
7
ROS2网络多机通信DDS和安全加密SROS(多机器人系统)
8
ROS2实时性案例分析之倒立摆系统使用(Inverted pendulum)
9
机器人实践课程镜像分享及使用说明(Arduino+ROS1+ROS2+Gazebo+SLAM+...)
10
使用机器人操作系统ROS 2和仿真软件Gazebo 9搭建机器人教程(一)
11
使用机器人操作系统ROS 2和仿真软件Gazebo 9命令遥控可视化教程(二)
12
使用机器人操作系统ROS 2和仿真软件Gazebo 9环境综合测试教程(三)
13
使用机器人操作系统ROS 2和仿真软件Gazebo 9目标跟踪实践(四)
14
使用机器人操作系统ROS 2和仿真软件Gazebo 9多机器人实践(五)
15
使用机器人操作系统ROS 2和仿真软件Gazebo 9激光传感器算法进阶(六)
16
使用机器人操作系统ROS 2和仿真软件Gazebo 9主题进阶实战(七)- mobot速度发布与里程计订阅
17
使用机器人操作系统ROS 2和仿真软件Gazebo 9服务进阶实战(八)- mobot行驶至目标位置
18
使用机器人操作系统ROS 2和仿真软件Gazebo 9行动进阶实战(九)- mobot区域巡逻
19
ROS 2 Foxy Fitzroy遇见Ubuntu 20.04
20
使用ROS2机器人操作系统进行多机器人编程技术实践(Multi-Robot Programming Via ROS2 )

使用机器人操作系统ROS 2和仿真软件Gazebo 9服务进阶实战(八)- mobot行驶至目标位置

ROS2在基本概念上与ROS1相似,但是实现上差别较大,比如编程规范等。

主题,节点,消息(主题消息)~

那么服务呢,服务器<->客户端,如何实现一个机器人

  1. 获取目标点坐标?
  2. 行驶到目标点?
  3. 显示行驶进度?

这样,三步功能呢,课程复习同样放置在文末。先看效果吧(无解说):


核心要点:

srv:

代码语言:javascript
复制
float32 x
float32 y
---
bool success

x,y为目标坐标,success反馈是否成功接受到坐标。

1. 只接受坐标,机器人mobot不行动!

这里放个水,直接给出参考源码,注意代码并不规范优雅,仅为功能实现。

服务器端:

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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:

代码语言:javascript
复制
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()

其中要点,解析参考官方文档。

下一篇
举报
领券