액션

평범한컴공생·2022년 8월 4일
0

[ROS2]

목록 보기
9/19
post-thumbnail

배경

액션은 ROS 2에서 통신 방식 중 하나이며 장시간 실행되는 작업에 적합하다. 액션은 목표, 피드백, 결과 세 가지로 구성된다. 액션은 토픽과 서비스를 기반으로 한다. 작동 방식은 서비스와 비슷하지만, 액션은 선점할 수 있어 실행 도중 취소할 수 있다. 또한 서비스는 응답 만을 반환하는 반면에, 액션은 지속적인 피드백이 이루어진다.

액션은 클라이언트-서버 모델을 사용한다. 액션 클라이언트 노드는 액션 서버에게 목표를 전송하고 피드백과 결과를 받는다.

작업

1. 설정

2개의 터미널을 새로 열자.

ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

2. 액션 사용해보기

노드 /teleop_turtle를 실행하면 다음과 같은 메시지가 터미널에 출력될 것이다.

Use arrow keys to move the turtle.
Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. 'F' to cancel a rotation.

첫째 줄은 토픽 /cmd_vel에 관한 내용이었으니 둘째 줄에 주목해 보자. QWERTY 키보드 기준으로 G|B|V|C|D|E|R|T의 키보드 배치를 보면 F키를 중심으로 하나의 박스를 이루고 있다.

F키 주변의 키들은 현재 거북이에 대한 상대 위치 개념이라고 볼 수 있다. 예를 들어 E키를 누르면 F키 기준 왼쪽 상단에 위치하므로, 거북이는 왼쪽 상단 방향으로 회전할 것이다.

/turtlesim을 실행 중인 터미널에 주목해 보자.

[INFO] [1659580458.679824767] [turtlesim]: Rotation goal completed successfully

키를 누를 때마다 /turtlesim노드의 액션 서버에 목표를 보내고 있는 것이다. 이때의 목표는 특정 방향으로 거북이를 회전시키는 것이고, 회전이 성공적으로 끝나면 위와 같은 메시지가 출력되는 것이다.

[INFO] [1659580695.900330968] [turtlesim]: Rotation goal canceled

F키를 눌러 회전 도중에 취소할 수도 있다. 이는 액션이 선점 가능함을 보여 준다.

클라이언트(/teleop) 뿐만 아니라 서버 쪽(/turtlesim)에서도 목표를 선점할 수 있다.

서버 측에서 액션을 선점하면, 목표를 중지하게 된다.

아무 키나 누른 후 회전을 완료 하기 전에 다른 키를 눌러보자. 다음의 메시지가 터미널에 출력될 것이다.

[WARN] [1659580992.444382492] [turtlesim]: Rotation goal received before a previous goal finished. Aborting previous goal

3. ros2 node info

노드 /turtlesim의 액션을 보고 싶다면 다음을 수행한다.

ros2 node info /turtlesim
/turtlesim
  Subscribers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /turtle1/cmd_vel: geometry_msgs/msg/Twist
  Publishers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /rosout: rcl_interfaces/msg/Log
    /turtle1/color_sensor: turtlesim/msg/Color
    /turtle1/pose: turtlesim/msg/Pose
  Service Servers:
    /clear: std_srvs/srv/Empty
    /kill: turtlesim/srv/Kill
    /reset: std_srvs/srv/Empty
    /spawn: turtlesim/srv/Spawn
    /turtle1/set_pen: turtlesim/srv/SetPen
    /turtle1/teleport_absolute: turtlesim/srv/TeleportAbsolute
    /turtle1/teleport_relative: turtlesim/srv/TeleportRelative
    /turtlesim/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /turtlesim/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /turtlesim/get_parameters: rcl_interfaces/srv/GetParameters
    /turtlesim/list_parameters: rcl_interfaces/srv/ListParameters
    /turtlesim/set_parameters: rcl_interfaces/srv/SetParameters
    /turtlesim/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Service Clients:

  Action Servers:
    /turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
  Action Clients:

/turtlesim의 서브스크라이버와 퍼블리셔, 서비스 그리고 액션 서버와 액션 클라이언트를 보여준다.

/turtlesim의 액션 /turtle1/rotate_absolute는 액션 서버임을 주목하자. 이는 /turtlesim이 액션 /turtle1/rotate_absolute에 대해 응답과 피드백을 제공함을 의미한다.

반면에 /teleop_turtle/turtle1/rotate_absolute가 액션 클라이언트에 존재한다.

ros2 node info /teleop_turtle
/teleop_turtle
  Subscribers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
  Publishers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /rosout: rcl_interfaces/msg/Log
    /turtle1/cmd_vel: geometry_msgs/msg/Twist
  Service Servers:
    /teleop_turtle/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /teleop_turtle/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /teleop_turtle/get_parameters: rcl_interfaces/srv/GetParameters
    /teleop_turtle/list_parameters: rcl_interfaces/srv/ListParameters
    /teleop_turtle/set_parameters: rcl_interfaces/srv/SetParameters
    /teleop_turtle/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Service Clients:

  Action Servers:

  Action Clients:
    /turtle1/rotate_absolute: turtlesim/action/RotateAbsolute

4. ros2 action list

ROS Graph에서의 모든 액션을 확인할 때 사용한다.

ros action list
/turtle1/rotate_absolute

4.1 ros2 action list -t

토픽과 서비스처럼 액션 또한 타입을 가지고 있다. 옵션 -t를 추가하여 타입을 알아낼 수 있다.

ros2 action list -t
/turtle1/rotate_absolute [turtlesim/action/RotateAbsolute]

/turtle1/rotate_absolute의 타입은 tutlesim/action/RotateAbsolute임을 알 수 있다.

5. ros2 action info

액션에 대해 더 자세히 알고 싶을 때 사용한다.

ros2 action info <action_name>
ros2 action info /turtle1/rotate_absolute
Action: /turtle1/rotate_absolute
Action clients: 1
    /teleop_turtle
Action servers: 1
    /turtlesim

액션 서버와 액션 클라이언트가 누구인지 알 수 있다.

6. ros2 interface show

액션의 타입을 알았으니 해당 액션의 구조 또한 알아야 액션의 목표를 보내거나 실행할 수 있다.

ros2 action list -t를 통해 /turtle1/rotate_absolute의 타입을 알아낼 수 있다.

ros2 interface show turtlesim/action/RotateAbsolute
# The desired heading in radians
float32 theta
---
# The angular displacement in radians to the starting position
float32 delta
---
# The remaining rotation in radians
float32 remaining

구분선 ---로 구분된 세 가지 영역 중 맨 위 부분은 액션 목표의 구조를 나타낸다. 그다음 차례대로 결과, 피드백이다.

7. ros2 action send_goal

명령어를 통해 액션의 목표를 보낼 수 있다.

ros2 action send_goal <action_name> <action_type> <values>

<values>YAML 형식을 따른다.

ros2 action send_goal turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"
Waiting for an action server to become available...
Sending goal:
     theta: 1.57

Goal accepted with ID: 7199dad372854c5f8d9ea9d8b3100999

Result:
    delta: -1.568000316619873

Goal finished with status: SUCCEEDED

모든 목표는 고유한 ID를 가지고 있다. 위에서는 7199dad372854c5f8d9ea9d8b3100999임을 알 수 있다. 결과의 필드 이름은 delta이며, 시작 위치에 대한 변위를 의미한다.

옵션 --feedback을 추가하면 해당 목표의 피드백까지 볼 수 있다.

ros2 action send_goal --feedback turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"
Waiting for an action server to become available...
Sending goal:
     theta: -1.57

Goal accepted with ID: dcc74fbb12e64a279e63387d2c36cfb6

Feedback:
    remaining: -1.5700000524520874

Feedback:
    remaining: -1.5540000200271606

Feedback:
    remaining: -1.5380001068115234
...중략... 
Feedback:
    remaining: -0.017999768257141113

Result:
    delta: 1.5520002841949463

Goal finished with status: SUCCEEDED

해당 목표를 완수할 때 까지 피드백을 받는다.

참고

ROS 2 Documentation: Foxy

profile
학부 연구생(220627~)

0개의 댓글