Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created October 24, 2019 02:05
Show Gist options
  • Save surinoel/039fd9cb6f68db8dc440ee7a985442f7 to your computer and use it in GitHub Desktop.
Save surinoel/039fd9cb6f68db8dc440ee7a985442f7 to your computer and use it in GitHub Desktop.
#include "ros/ros.h"
#include "ros_tutorials_service/SrvTutorial.h"
#include <cstdlib>
int main(int argc, char **argv)
{
ros::init(argc, argv, "service_client");
if (argc != 3)
{
ROS_INFO("cmd : rosrun ros_tutorials_service service_client arg0 arg1");
ROS_INFO("arg0: double number, arg1: double number");
return 1;
}
ros::NodeHandle nh;
ros::ServiceClient ros_tutorials_service_client = nh.serviceClient<ros_tutorials_service::SrvTutorial>("ros_tutorial_srv");
ros_tutorials_service::SrvTutorial srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
int count = 0;
while (ros_tutorials_service_client.call(srv) && ++count < 100)
{
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);
srv.request.a = srv.request.b;
srv.request.b = srv.response.result;
}
ROS_INFO("end!");
return 0;
}
#include "ros/ros.h"
#include "ros_tutorials_service/SrvTutorial.h"
bool calculation(ros_tutorials_service::SrvTutorial::Request &req,
ros_tutorials_service::SrvTutorial::Response &res)
{
res.result = req.a + req.b;
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) {
ros::init(argc, argv, "service_server");
ros::NodeHandle nh;
ros::ServiceServer ros_tutorials_service_server = nh.advertiseService("ros_tutorial_srv", calculation);
ROS_INFO("ready srv server!");
ros::spin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment