728x90
아래의 블로그를 보고 ROS를 복기하고 있다.
catkin_create_pkg ros_tutorials_service roscpp std_msgs message_generation
package.xml파일 수정
<?xml version="1.0"?>
<package format="2">
<name>ros_tutorials_service</name>
<version>0.0.1</version>
<description>request/response service</description>
<license>Apache License 2.0</license>
<maintainer email="root@to.do">someone</maintainer>
<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_runtime</exec_depend>
<export></export>
</package>
CMakeLists.txt 수정
cmake_minimum_required(VERSION 3.0.2)
project(ros_tutorials_service)
find_package(catkin REQUIRED COMPONENTS
message_generation
std_msgs
roscpp
)
add_service_files(FILES SrvTutorial.srv)
generate_messages(DEPENDENCIES std_msgs)
catkin_package(
LIBRARIES ros_tutorials_service
CATKIN_DEPENDS std_msgs roscpp
)
include_directories(${catkin_INCLUDE_DIRS})
add_executable(srv_server src/server.cpp)
add_dependencies(srv_server ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(srv_server ${catkin_LIBRARIES})
add_executable(srv_client src/client.cpp)
add_dependencies(srv_client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(srv_client ${catkin_LIBRARIES})
서비스 파일 작성
roscd ros_tutorials_service
mkdir srv
gedit srv/SrvTutorial.srv
int64 a
int64 b
---
int64 result
src/server.cpp 작성
#include "ros/ros.h"
#include "ros_tutorials_service/SrvTutorial.h"
// 서비스 요청이 있을 경우, 아래의 처리를 수행
// 요청 = req / 응답 = res
bool add(ros_tutorials_service::SrvTutorial::Request &req, ros_tutorials_service::SrvTutorial::Response &res) {
// add and save result
res.result = req.a + req.b;
// print info
ROS_INFO("request: %ld + %ld", (long int)req.a, (long int)req.b);
ROS_INFO("response: %ld", (long int)res.result);
return true;
}
int main(int argc, char** argv) {
ros::init(argc, argv, "service_server");
ros::NodeHandle nh;
// service name = ros_tutorial_srv
// callback = add
ros::ServiceServer ros_tutorials_service_server = nh.advertiseService("ros_tutorial_srv", add);
ROS_INFO("ready srv server");
// 요청이 들어오면 add 호출
// 아니면 대기합니다.
ros::spin();
return 0;
}
src/client.cpp 코드 작성
#include <cstdlib>
#include "ros/ros.h"
#include "ros_tutorials_service/SrvTutorial.h"
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 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 (client.call(srv)) {
ROS_INFO("send srv: %d + %d", (long int)srv.request.a, (long int)srv.request.b);
ROS_INFO("receive srv: %d", (long int)srv.response.result);
} else {
ROS_ERROR("Failed to call service ros_tutorial_srv");
return 1;
}
return 0;
}
빌드 및 실행
roscore
rosrun ros_tutorials_service srv_server
rosrun ros_tutorials_service srv_client 1 2
작동 이상무.
빌드가 안 되는 상황이 발생했었는데 build와 devel파일을 삭제하고 다시 빌드하니 문제가 해결되었다.
728x90
'ROS(drone포함)' 카테고리의 다른 글
turtlebot3 시뮬레이션 (0) | 2021.10.13 |
---|---|
TurtleBot3 사용하기 (0) | 2021.10.12 |
View Protect사 Z12TL 모델로 알아보는 드론 짐벌 카메라 (0) | 2021.09.08 |
dronekit-python 사용법 (0) | 2021.08.27 |
CMake 불가 디버깅 과정 (0) | 2021.08.25 |