https://cafe.naver.com/openrt/24629
ROS 2 기초 프로그래밍 예제에서 앞으로 사용할 토픽(Topic), 서비스(Service), 액션(Action) 관련 ROS 2 인터페이스를 만들어 보자.
msg 인터페이스 1개, srv 인터페이스 1개, action 인터페이스 1개를 포함시켜 msg_srv_action_interface_example 이라는 이름의 패키지를 만들 것이다.
$ cd ~/ros2_ws/src
$ ros2 pkg create --build-type ament_cmake msg_srv_action_interface_example
$ cd msg_srv_action_interface_example
$ mkdir msg srv action
2.1 ArithmeticArgument.msg 작성
-저장 위치: msg_srv_action_interface_example/msg
$ cd ~/ros2_ws/src/msg_srv_action_interface_example/msg
$ gedit ArithmeticArgument.msg
-파일 내용
# Messages
builtin_interfaces/Time stamp
float32 argument_a
float32 argument_b
2.2 ArithmeticOperator.srv 생성
-저장 위치: msg_srv_action_interface_example/srv
$ cd ~/ros2_ws/src/msg_srv_action_interface_example/srv
$ gedit ArithmeticOperator.srv
-파일 내용
# Constants
int8 PLUS = 1
int8 MINUS = 2
int8 MULTIPLY = 3
int8 DIVISION = 4
# Request
int8 arithmetic_operator
---
# Response
float32 arithmetic_result
2.3 ArithmeticChecker.action 생성
-저장 위치: msg_srv_action_interface_example/action
$ cd ~/ros2_ws/src/msg_srv_action_interface_example/action
$ gedit ArithmeticChecker.action
-파일 내용
# Goal
float32 goal_sum
---
# Result
string[] all_formula
float32 total_sum
---
# Feedback
string[] formula
원래 파일 내용
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>msg_srv_action_interface_example</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="hcw@todo.todo">hcw</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
수정
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>msg_srv_action_interface_example</name>
<version>0.2.0</version>
<description>
ROS 2 example for message, service and action interface
</description>
<maintainer email="hcw@todo.todo">hcw</maintainer>
<license>Apache 2.0</license>
<author email="hcw@todo.todo">hcw</author>
<author email="routiful@gmail.com">Darby Lim</author>
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>
<exec_depend>builtin_interfaces</exec_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
원래 파일 내용
cmake_minimum_required(VERSION 3.5)
project(msg_srv_action_interface_example)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
수정
# Set minimum required version of cmake, project name and compile options
cmake_minimum_required(VERSION 3.5)
project(msg_srv_action_interface_example)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
endif()
# Find and load build settings from external packages
find_package(ament_cmake REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)
# Declare ROS messages, services and actions
set(msg_files
"msg/ArithmeticArgument.msg"
)
set(srv_files
"srv/ArithmeticOperator.srv"
)
set(action_files
"action/ArithmeticChecker.action"
)
rosidl_generate_interfaces(${PROJECT_NAME}
${msg_files}
${srv_files}
${action_files}
DEPENDENCIES builtin_interfaces
)
# Macro for ament package
ament_export_dependencies(rosidl_default_runtime)
ament_package()
$ cd ~/ros2_ws
$ colcon build --symlink-install --packages-select msg_srv_action_interface_example