zoukankan      html  css  js  c++  java
  • 参数的用法——利用参数创建节点

    ROS 消息通讯——服务器/客户端的来龙去脉的基础上,对service_server.cpp文件进行修改如下:

     1 #include <ros/ros.h>
     2 #include <ros_tutorials_service/SrvTutorial.h>//服务头文件
     3 
     4 #define PLUS 1
     5 #define MINUS 2
     6 #define MULTIPLICATION 3
     7 #define DIVISION 4
     8 
     9 int g_operator=PLUS;
    10 //如果收到客户端的请求,将执行以下处理,服务请求为req,服务响应为res
    11 bool calculation(ros_tutorials_service::SrvTutorial::Request &req,
    12                 ros_tutorials_service::SrvTutorial::Response &res)
    13 {
    14 //收到请求时,将两数之和保存到服务响应值中
    15 switch(g_operator)
    16 {
    17 case PLUS:
    18 res.result = req.a+req.b;break;
    19 case MINUS:
    20 res.result = req.a-req.b;break;
    21 case MULTIPLICATION:
    22 res.result = req.a*req.b;break;
    23 case DIVISION:
    24 if(req.b==0)
    25 {
    26 res.result =0;break;
    27 }
    28 else
    29 {
    30 res.result = req.a/req.b;break;
    31 }
    32 default:
    33 res.result = req.a+req.b;break;
    34 //显示a,b的值及结果
    35 ROS_INFO("request:x=%ld,y=%ld",(long int)req.a,(long int)req.b);
    36 ROS_INFO("sending back response:%ld",(long int)res.result);
    37 return true;
    38 }
    39 
    40 int main(int argc,char **argv)
    41 {
    42 ros::init(argc,argv,"service_server");//声明服务器节点名称
    43 ros::NodeHandle nh;
    44 nh.setParam("calculation_method",PLUS);//初始化设置参数
    45 //声明服务器,创建一个使用ros_tutorials_service功能包SrvTutorial服务文件的服务器
    46 //ros_tutorial_service_server,服务名称是ros_tutorial_srv,有服务请求,执行calculation函数
    47 ros::ServiceServer ros_tutorials_service_server = nh.advertiseService("ros_tutorial_srv", calculation);
    48 
    49 ROS_INFO("ready srv server!");
    50 ros::Rater(10);
    51 while(1)
    52 {
    53 nh.getParam("calculation_method",g_operator);//获取参数
    54 ros::spinOnce();
    55 r.sleep();
    56 }
    57 
    58 return 0;
    59 }
    • 运行结果对照:查看参数列表,修改参数试验效果

    •  有参数运行实例:git clone https://github.com/ROBOTIS-GIT/ros_tutoruals.git

  • 相关阅读:
    CodeForces 7B
    CodeForces 4D
    离散化
    线段树入门
    洛谷 P3951 小凯的疑惑(赛瓦维斯特定理)
    Codeforces 1295D Same GCDs (欧拉函数)
    Codeforces 1295C Obtain The String (二分)
    Codeforces 1295B Infinite Prefixes
    Codeforces 1295A Display The Number(思维)
    Codeforces 1294F Three Paths on a Tree(树的直径,思维)
  • 原文地址:https://www.cnblogs.com/fuzhuoxin/p/12584992.html
Copyright © 2011-2022 走看看