- 在 ROS 消息通讯——服务器/客户端的来龙去脉的基础上,对
service_server.cpp
文件进行修改如下:
#include "ros/ros.h" // ROS Default Header File
#include "ros_tutorials_parameter/SrvTutorial.h"// action Library Header File
#define PLUS 1 // Addition
#define MINUS 2 // Subtraction
#define MULTIPLICATION 3 // Multiplication
#define DIVISION 4 // Division
int g_operator = PLUS;
// The process below is performed if there is a service request
// The service request is declared as 'req', and the service response is declared as 'res'
bool calculation(ros_tutorials_parameter::SrvTutorial::Request &req,
ros_tutorials_parameter::SrvTutorial::Response &res)
{
// The operator will be selected according to the parameter value and calculate 'a' and 'b',
// which were received upon the service request.
// The result is stored as the Response value.
switch(g_operator)
{
case PLUS:
res.result = req.a + req.b; break;
case MINUS:
res.result = req.a - req.b; break;
case MULTIPLICATION:
res.result = req.a * req.b; break;
case DIVISION:
if(req.b == 0)
{
res.result = 0; break;
}
else
{
res.result = req.a / req.b; break;
}
default:
res.result = req.a + req.b; break;
}
// Displays the values of 'a' and 'b' used in the service request, and the 'result' value
// corresponding to the service response.
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("sending back response: [%ld]", (long int)res.result);
return true;
}
int main(int argc, char **argv) // Node Main Function
{
ros::init(argc, argv, "service_server"); // Initializes Node Name
ros::NodeHandle nh; // Node handle declaration
nh.setParam("calculation_method", PLUS); // Reset Parameter Settings
// Declare service server 'service_server' using the 'SrvTutorial' service file
// in the 'ros_tutorials_service' package. The service name is 'ros_tutorial_srv' and
// it is set to execute a 'calculation' function when a service is requested.
ros::ServiceServer ros_tutorials_service_server = nh.advertiseService("ros_tutorial_srv", calculation);
ROS_INFO("ready srv server!");
ros::Rate r(10); // 10 hz
while (ros::ok())
{
nh.getParam("calculation_method", g_operator); // Select the operator according to the value received from the parameter.
ros::spinOnce(); // Callback function process routine
r.sleep(); // Sleep for routine iteration
}
return 0;
}
对service_client.cpp
文件进行修改如下:
#include "ros/ros.h" // ROS Default Header File
#include "ros_tutorials_service/SrvTutorial.h"// SrvTutorial Service File Header (Automatically created after build)
#include <cstdlib> // Library for using the "atoll" function
int main(int argc, char **argv) // Node Main Function
{
ros::init(argc, argv, "service_client"); // Initializes Node Name
if (argc != 3) // Input value error handling
{
ROS_INFO("cmd : rosrun ros_tutorials_service service_client arg0 arg1");
ROS_INFO("arg0: double number, arg1: double number");
return 1;
}
ros::NodeHandle nh; // Node handle declaration for communication with ROS system
// Declares service client 'ros_tutorials_service_client'
// using the 'SrvTutorial' service file in the 'ros_tutorials_service' package.
// The service name is 'ros_tutorial_srv'
ros::ServiceClient ros_tutorials_service_client = nh.serviceClient<ros_tutorials_service::SrvTutorial>("ros_tutorial_srv");
// Declares the 'srv' service that uses the 'SrvTutorial' service file
ros_tutorials_service::SrvTutorial srv;
// Parameters entered when the node is executed as a service request value are stored at 'a' and 'b'
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
// Request the service. If the request is accepted, display the response value
if (ros_tutorials_service_client.call(srv))
{
ROS_INFO("send srv, srv.Request.a and b: %ld, %ld", (long int)srv.request.a, (long int)srv.request.b);
ROS_INFO("receive srv, srv.Response.result: %ld", (long int)srv.response.result);
}
else
{
ROS_ERROR("Failed to call service ros_tutorial_srv");
return 1;
}
return 0;
}
- 运行结果对照:
- 查看参数列表,修改参数试验效果:
- 有参数运行实例:https://github.com/GuoXiaoxiao1/ros_tutorials