ROS 서비스

2024. 4. 17. 20:28ROS

  • ROS에서 서비스(Service)는 요청(request)에 응답하는 서비스 서버 노드(service server)와
  • 요청을 하는 서비스 클라이언트(service client) 노드로 구성됨
  • 토픽과는 다르게 한번 요청에, 한번 회신을 하는 일회성 연결이고, 요청 - 응답이 이루어지면 연결은 끊김
cd ~/catkin_ws/src
catkin_create_pkg ros_service message_generation std_msgs roscpp

package.xml
CMakeLists.txt

서비스 파일 생성

cd /home/ch/catkin_ws/src/ros_service
mkdir srv
cd srv
nano Service1.srv

Nano 에디터로 Service1.srv 파일을 연 후, 아래 데이터를 입력하고 저장

int64 a

int64 b

---

int64 result

//ros_service_server

#include "ros/ros.h"                          // ROS Default Header File
#include "ros_service/Service1.h"// Service1 Service File Header (Automatically created after build)

// The below process is performed when there is a service request
// The service request is declared as 'req', and the service response is declared as 'res'
bool calculation(ros_service::Service1::Request &req,
                 ros_service::Service1::Response &res)
{
  // The service name is 'ros_srv' and it will call 'calculation' function upon the service request.
  res.result = req.a + req.b;

  // Displays 'a' and 'b' values 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

  // Declare service server 'ros_service_server'
  // using the 'Service1' service file in the 'ros_service' package.
  // The service name is 'ros_srv' and it will call 'calculation' function
  // upon the service request.
  ros::ServiceServer ros_service_server = nh.advertiseService("ros_srv", calculation);

  ROS_INFO("ready srv server!");

  ros::spin();    // Wait for the service request

  return 0;
}
//ros_service_client

#include "ros/ros.h"
#include "ros_service/Service1.h"
#include <cstdlib>

int main(int argc, char **argv)
{
    ros::init(argc, argv, "service_client");
    if (argc != 3)
    {
        ROS_INFO("cmd : rosrun ros_service ros_service_client arg0 arg1");
        ROS_INFO("arg0: double number, arg1: double number");
        return 1;
    }
    ros::NodeHandle nh;
    ros::ServiceClient ros_service_client = nh.serviceClient<ros_service::Service1>("ros_srv");
    ros_service::Service1 srv;

    srv.request.a = atoll(argv[1]);
    srv.request.b = atoll(argv[2]);

    if (ros_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_srv");
        return 1;
    }
    return 0;
}

 

cd ~/catkin_ws
catkin_make
roscore
rosrun ros_service ros_service_server
rosrun ros_service ros_service_client 123 456

'ROS' 카테고리의 다른 글

ROS opencv  (0) 2024.04.17
ROS roslaunch  (0) 2024.04.17
ROS 파라미터  (0) 2024.04.17
ROS 토픽  (0) 2024.04.08
ROS 기본(20.04)  (0) 2024.04.02