1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0"?>
<package format="2">
  <name>ros_tutorials_service</name>
  <version>0.0.0</version>
  <description>The ros_tutorials_service package</description>
  <maintainer email="hyeok@todo.todo">hyeok</maintainer>
  <license>TODO</license>
  <author email="jane.doe@example.com">Jane Doe</author>
 
  <buildtool_depend>catkin</buildtool_depend>
 
  <build_depend>message_generation</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>std_msgs</build_depend>
 
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
 
  <exec_depend>roscpp</exec_depend>
  <exec_depend>std_msgs</exec_depend>
  <exec_depend>message_generation</exec_depend>
  <exec_depend>message_runtime</exec_depend>
 
  <export>
  </export>
</package>
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
cmake_minimum_required(VERSION 2.8.3)
project(ros_tutorials_service)
 
add_compile_options(-std=c++11)
 
find_package(catkin REQUIRED COMPONENTS
  message_generation
  roscpp
  std_msgs
)
 
## Generate services in the 'srv' folder
 add_service_files(
   FILES
   SrvTutorial.srv
 )
 
## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  std_msgs
)
 
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES ros_tutorials_service
  CATKIN_DEPENDS message_generation roscpp std_msgs
  DEPENDS system_lib
)
 
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)
 
 add_executable(service_server src/service_server.cpp)
 add_dependencies(service_server ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
 target_link_libraries(service_server
   ${catkin_LIBRARIES}
 )
 
 add_executable(service_client src/service_client.cpp)
 add_dependencies(service_client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
 target_link_libraries(service_client
   ${catkin_LIBRARIES}
 )
 
 
cs



1
2
3
4
5
int64 a
int64 b
---
int64 result
 
cs




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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=%1d, y=%1d", (long int)req.a, (long int)req.b);
    ROS_INFO("sending back response: %1d", (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;
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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]);
 
    if (ros_tutorials_service_client.call(srv)){
        
        ROS_INFO("send srv, srv.Request.a and b: %1d, %1d",(long int)srv.request.a, (long int) srv.request.b);
 
        ROS_INFO("receive srv, srv.Response.result:%1d", (long int)srv.response.result);
 
    }else{
        ROS_ERROR("Failed to call service ros_tutorial_srv:");
        return 1;
 
    }
    
    return 0;
 
}
 
cs




//service server with param

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "ros/ros.h"
#include "ros_tutorials_service/SrvTutorial.h"
 
#define PLUS  1
#define MINUS 2
#define MUL   3
#define DIV   4
 
int g_operator = PLUS;
 
bool calculation(ros_tutorials_service::SrvTutorial::Request &req, ros_tutorials_service::SrvTutorial::Response &res){
    
    switch(g_operator){
    
    case PLUS:
        res.result = req.a + req.b;
        break;
    case MINUS:
        res.result = req.a - req.b;
        break;
    case MUL:
        res.result = req.a * req.b;
        break;
    case DIV:
        res.result = req.a / req.b;
        break;
    }
 
    ROS_INFO("request: x=%1d, y=%1d", (long int)req.a, (long int)req.b);
    ROS_INFO("sending back response: %1d", (long int)res.result);
 
    return true;
}
 
int main(int argc, char** argv){
    
    ros::init(argc, argv, "service_server");
    ros::NodeHandle nh;
 
    nh.setParam("calculation_method", PLUS); // 매개변수를 만들고 PLUS로 초기화
 
    ros::ServiceServer ros_tutorials_service_server = nh.advertiseService("ros_tutorial_srv", calculation);
 
    ROS_INFO("ready srv server!");
 
    ros::Rate r(10);
 
    while(1){
    
    nh.getParam("calculation_method", g_operator);
    ros::spinOnce();
    r.sleep();
 
    }
 
    //ros::spin();
    return 0;
}
 
cs


'ROS' 카테고리의 다른 글

ROS 컴퓨터간 통신 테스트 주의사항  (0) 2019.05.14
Topic Pub & Sub Xml Cmake Cpp 예제  (0) 2019.05.12
exec_depend와 run_depend의 차이  (0) 2019.05.11
rqtbag 레퍼런스  (0) 2019.05.11
ros 기본 명령어  (0) 2019.05.11

+ Recent posts