Ros2 Programming(interface)

jngeun·2021년 5월 5일
0

Ros

목록 보기
4/4

참고 링크 : oroca 027강

인터페이스 신규 작성

  • 인터페이스 : 노드 간에 데이터를 주고 받을 때에 토픽, 서비스, 액션이 사용되는데 이 때 사용되는 데이터의 형태
  • 구성된 패키지를 별도로 만들어 사용하는 것이 의존성면에서 관리하기 편하다.
    • A라는 패키지에서 a라는 인터페이스를 사용
    • B,C 라는 패키지에서 a라는 인터페이스를 사용
    • a인터페이스가 A 패키지에 들어 있다면 B,C는 A패키지를 통째로 의존해야 된다.

인터페이스 패키지 만들기

$ cd ~/robot_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

# Messages
builtin_interfaces/Time stamp
float32 argument_a
float32 argument_b

2.2 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

# Goal
float32 goal_sum
---
# Result
string[] all_formula
float32 total_sum
---
# Feedback
string[] formula

패키지 설정 파일(package.xml)

<?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="mine1372@korea.ac.kr">Pyo</maintainer>
  <license>Apache 2.0</license>

  <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>

인터페이스 전용 패키지를 만들 때에 필수적인 의존성 패키지

  • rosidl_default_generators
  • builtin_interfaces
  • rosidl_default_runtime

빌드 설정 파일 ( CMakeLists.txt)

################################################################################
# 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()

빌드

$ cw
$ cbp msg_srv_action_interface_example
  • ~/.bashrc에 alias 설정
    • 'cw' : cd ~/robot_ws
    • 'cbp' : colcon build --symlink-install --packages-select

0개의 댓글