您当前的位置: 首页 > 

鱼香ROS

暂无认证

  • 0浏览

    0关注

    498博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

《动手学ROS2》rclcpp客户端,wait_for_service()函数用法(公众号:鱼香ROS)

鱼香ROS 发布时间:2021-08-24 09:13:09 ,浏览量:0

大家好我是《动手学ROS2》教程作者小鱼。

如果你想和小鱼一起学习ROS2,可以关注小鱼的微信公众号《鱼香ROS》,加入鱼群和小鱼一起学习。

今天小鱼来说说ROS2的客户端中wait_for_service()函数函数使用方法,帮助大家理解。

一、函数介绍 1.1 函数功能:

等待服务端准备完成。

1.2 函数输入输出:
#mermaid-svg-7HWIv9qSBMH6swdb .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-7HWIv9qSBMH6swdb .label text{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .node rect,#mermaid-svg-7HWIv9qSBMH6swdb .node circle,#mermaid-svg-7HWIv9qSBMH6swdb .node ellipse,#mermaid-svg-7HWIv9qSBMH6swdb .node polygon,#mermaid-svg-7HWIv9qSBMH6swdb .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-7HWIv9qSBMH6swdb .node .label{text-align:center;fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .node.clickable{cursor:pointer}#mermaid-svg-7HWIv9qSBMH6swdb .arrowheadPath{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-7HWIv9qSBMH6swdb .flowchart-link{stroke:#333;fill:none}#mermaid-svg-7HWIv9qSBMH6swdb .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-7HWIv9qSBMH6swdb .edgeLabel rect{opacity:0.9}#mermaid-svg-7HWIv9qSBMH6swdb .edgeLabel span{color:#333}#mermaid-svg-7HWIv9qSBMH6swdb .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-7HWIv9qSBMH6swdb .cluster text{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-7HWIv9qSBMH6swdb .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-7HWIv9qSBMH6swdb text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-7HWIv9qSBMH6swdb .actor-line{stroke:grey}#mermaid-svg-7HWIv9qSBMH6swdb .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-7HWIv9qSBMH6swdb .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-7HWIv9qSBMH6swdb #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-7HWIv9qSBMH6swdb .sequenceNumber{fill:#fff}#mermaid-svg-7HWIv9qSBMH6swdb #sequencenumber{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb #crosshead path{fill:#333;stroke:#333}#mermaid-svg-7HWIv9qSBMH6swdb .messageText{fill:#333;stroke:#333}#mermaid-svg-7HWIv9qSBMH6swdb .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-7HWIv9qSBMH6swdb .labelText,#mermaid-svg-7HWIv9qSBMH6swdb .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-7HWIv9qSBMH6swdb .loopText,#mermaid-svg-7HWIv9qSBMH6swdb .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-7HWIv9qSBMH6swdb .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-7HWIv9qSBMH6swdb .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-7HWIv9qSBMH6swdb .noteText,#mermaid-svg-7HWIv9qSBMH6swdb .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-7HWIv9qSBMH6swdb .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-7HWIv9qSBMH6swdb .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-7HWIv9qSBMH6swdb .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-7HWIv9qSBMH6swdb .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb .section{stroke:none;opacity:0.2}#mermaid-svg-7HWIv9qSBMH6swdb .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-7HWIv9qSBMH6swdb .section2{fill:#fff400}#mermaid-svg-7HWIv9qSBMH6swdb .section1,#mermaid-svg-7HWIv9qSBMH6swdb .section3{fill:#fff;opacity:0.2}#mermaid-svg-7HWIv9qSBMH6swdb .sectionTitle0{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .sectionTitle1{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .sectionTitle2{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .sectionTitle3{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-7HWIv9qSBMH6swdb .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb .grid path{stroke-width:0}#mermaid-svg-7HWIv9qSBMH6swdb .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-7HWIv9qSBMH6swdb .task{stroke-width:2}#mermaid-svg-7HWIv9qSBMH6swdb .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb .taskText:not([font-size]){font-size:11px}#mermaid-svg-7HWIv9qSBMH6swdb .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-7HWIv9qSBMH6swdb .task.clickable{cursor:pointer}#mermaid-svg-7HWIv9qSBMH6swdb .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-7HWIv9qSBMH6swdb .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-7HWIv9qSBMH6swdb .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-7HWIv9qSBMH6swdb .taskText0,#mermaid-svg-7HWIv9qSBMH6swdb .taskText1,#mermaid-svg-7HWIv9qSBMH6swdb .taskText2,#mermaid-svg-7HWIv9qSBMH6swdb .taskText3{fill:#fff}#mermaid-svg-7HWIv9qSBMH6swdb .task0,#mermaid-svg-7HWIv9qSBMH6swdb .task1,#mermaid-svg-7HWIv9qSBMH6swdb .task2,#mermaid-svg-7HWIv9qSBMH6swdb .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-7HWIv9qSBMH6swdb .taskTextOutside0,#mermaid-svg-7HWIv9qSBMH6swdb .taskTextOutside2{fill:#000}#mermaid-svg-7HWIv9qSBMH6swdb .taskTextOutside1,#mermaid-svg-7HWIv9qSBMH6swdb .taskTextOutside3{fill:#000}#mermaid-svg-7HWIv9qSBMH6swdb .active0,#mermaid-svg-7HWIv9qSBMH6swdb .active1,#mermaid-svg-7HWIv9qSBMH6swdb .active2,#mermaid-svg-7HWIv9qSBMH6swdb .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-7HWIv9qSBMH6swdb .activeText0,#mermaid-svg-7HWIv9qSBMH6swdb .activeText1,#mermaid-svg-7HWIv9qSBMH6swdb .activeText2,#mermaid-svg-7HWIv9qSBMH6swdb .activeText3{fill:#000 !important}#mermaid-svg-7HWIv9qSBMH6swdb .done0,#mermaid-svg-7HWIv9qSBMH6swdb .done1,#mermaid-svg-7HWIv9qSBMH6swdb .done2,#mermaid-svg-7HWIv9qSBMH6swdb .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-7HWIv9qSBMH6swdb .doneText0,#mermaid-svg-7HWIv9qSBMH6swdb .doneText1,#mermaid-svg-7HWIv9qSBMH6swdb .doneText2,#mermaid-svg-7HWIv9qSBMH6swdb .doneText3{fill:#000 !important}#mermaid-svg-7HWIv9qSBMH6swdb .crit0,#mermaid-svg-7HWIv9qSBMH6swdb .crit1,#mermaid-svg-7HWIv9qSBMH6swdb .crit2,#mermaid-svg-7HWIv9qSBMH6swdb .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-7HWIv9qSBMH6swdb .activeCrit0,#mermaid-svg-7HWIv9qSBMH6swdb .activeCrit1,#mermaid-svg-7HWIv9qSBMH6swdb .activeCrit2,#mermaid-svg-7HWIv9qSBMH6swdb .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-7HWIv9qSBMH6swdb .doneCrit0,#mermaid-svg-7HWIv9qSBMH6swdb .doneCrit1,#mermaid-svg-7HWIv9qSBMH6swdb .doneCrit2,#mermaid-svg-7HWIv9qSBMH6swdb .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-7HWIv9qSBMH6swdb .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-7HWIv9qSBMH6swdb .milestoneText{font-style:italic}#mermaid-svg-7HWIv9qSBMH6swdb .doneCritText0,#mermaid-svg-7HWIv9qSBMH6swdb .doneCritText1,#mermaid-svg-7HWIv9qSBMH6swdb .doneCritText2,#mermaid-svg-7HWIv9qSBMH6swdb .doneCritText3{fill:#000 !important}#mermaid-svg-7HWIv9qSBMH6swdb .activeCritText0,#mermaid-svg-7HWIv9qSBMH6swdb .activeCritText1,#mermaid-svg-7HWIv9qSBMH6swdb .activeCritText2,#mermaid-svg-7HWIv9qSBMH6swdb .activeCritText3{fill:#000 !important}#mermaid-svg-7HWIv9qSBMH6swdb .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-7HWIv9qSBMH6swdb g.classGroup text .title{font-weight:bolder}#mermaid-svg-7HWIv9qSBMH6swdb g.clickable{cursor:pointer}#mermaid-svg-7HWIv9qSBMH6swdb g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-7HWIv9qSBMH6swdb g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-7HWIv9qSBMH6swdb .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-7HWIv9qSBMH6swdb .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-7HWIv9qSBMH6swdb .dashed-line{stroke-dasharray:3}#mermaid-svg-7HWIv9qSBMH6swdb #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb .commit-id,#mermaid-svg-7HWIv9qSBMH6swdb .commit-msg,#mermaid-svg-7HWIv9qSBMH6swdb .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-7HWIv9qSBMH6swdb g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-7HWIv9qSBMH6swdb g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-7HWIv9qSBMH6swdb g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-7HWIv9qSBMH6swdb .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-7HWIv9qSBMH6swdb .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-7HWIv9qSBMH6swdb .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-7HWIv9qSBMH6swdb .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-7HWIv9qSBMH6swdb .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-7HWIv9qSBMH6swdb .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-7HWIv9qSBMH6swdb .edgeLabel text{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-7HWIv9qSBMH6swdb .node circle.state-start{fill:black;stroke:black}#mermaid-svg-7HWIv9qSBMH6swdb .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-7HWIv9qSBMH6swdb #statediagram-barbEnd{fill:#9370db}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-state .divider{stroke:#9370db}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-7HWIv9qSBMH6swdb .note-edge{stroke-dasharray:5}#mermaid-svg-7HWIv9qSBMH6swdb .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-7HWIv9qSBMH6swdb .error-icon{fill:#522}#mermaid-svg-7HWIv9qSBMH6swdb .error-text{fill:#522;stroke:#522}#mermaid-svg-7HWIv9qSBMH6swdb .edge-thickness-normal{stroke-width:2px}#mermaid-svg-7HWIv9qSBMH6swdb .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-7HWIv9qSBMH6swdb .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-7HWIv9qSBMH6swdb .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-7HWIv9qSBMH6swdb .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-7HWIv9qSBMH6swdb .marker{fill:#333}#mermaid-svg-7HWIv9qSBMH6swdb .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-7HWIv9qSBMH6swdb { color: rgba(0, 0, 0, 0.75); font: ; }
参数
返回值
最大超时时间,有默认值-1
wait_for_serveice
bool是否在线

输入参数:std::chorm

输出参数:bool类型

  • 如果在没有超时的情况下,服务端准备完毕,返回true
  • 否则一律返回false
1.3函数原型
template
bool rclcpp::ClientBase::wait_for_service	(	std::chrono::duration< RepT, RatioT > 	timeout = std::chrono::duration(-1)	)	

参数只有一个,而且有默认值,这个参数表示等待的超时时间。

二、使用方法 2.1 等待服务上线,不上线每一秒钟打印一个日志信息
client_->wait_for_service(std::chrono::seconds(1))
    void buy_novel(){
        RCLCPP_INFO(this->get_logger(), "买小说去喽");
        village_interfaces::srv::SellNovel_Request request;
        request.money = 10;
        while(!client_->wait_for_service(std::chrono::seconds(1))){
            RCLCPP_INFO(this->get_logger(), "等待服务端上线中");
        }
    };

在这里插入图片描述

参考链接:API文档

写在最后

我是小鱼,机器人领域资深玩家,现深圳某独脚兽机器人算法工程师一枚

初中学习编程,高中开始学习机器人,大学期间打机器人相关比赛实现月入2W+(比赛奖金)

目前在输出机器人学习指南、论文注解、工作经验,欢迎大家关注小鱼,一起交流技术,学习机器人 在这里插入图片描述

关注
打赏
1666092100
查看更多评论
立即登录/注册

微信扫码登录

0.1937s