Action이란?
장기 실행 작업을 위한 ros2의 통신 방법
topic & service 기반
client-server 모델 사용
실습
1. 설정
터미널 2개를 열고 다음과 같은 명령어를 실행한다.
(참고 : ros2 run <package_name> <executable_name>)
- 터미널 1
ros2 run turtlesim turtlesim_node
- 터미널 2
ros2 run turtlesim turtle_teleop_key
2. Use actions
터미널 2 에 명령어를 입력했을 때 G|B|V~ 라는 출력을 볼 수 있다.
이는 키보드에서 F를 둘러싼 키들인데 각각 거북이의 머리를 어디로 둘 것이냐 하는 키다.
F는 거북이가 회전하는 도중에 누르면 거북이가 회전하다가 멈춘다.
3. ros2 node info
- 터미널 3
ros2 node info /turtlesim
위와 같은 명령어를 치면 /turtlesim의 subscriber, publisher, service, action server, action client list가 출력된다.
/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
Services:
/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
Action Servers:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
Action Clients:
여기서 /turtle1/rotate_absolute이 Action Server인 것을 알 수 있는데 이건 /turtlesim이 /turtle1/rotate_absolute action에 대해 응답하고 피드백한다는 것을 의미한다.
반대로 아래 명령어의 출력을 확인해보면
ros2 node info /teleop_turtle
Action Clients 아래에 /turtle1/rotate_absolute 가 존재하는 것을 알 수 있는데 이건 이 action에 대해 goal을 보낸다는 것을 의미한다.
--- 주관적 해석 ---
/teleop_turtle에서 goal을 설정해주면
/turtlesim에서 응답 및 피드백을 보내준다.
4. ros2 action list
action list를 확인하려면 다음과 같은 명령어를 사용한다.
ros2 action list
현재는 /turtle1/rotate_absolute 이 결과를 반환할 것이다.
추가적으로 다음 명령어를 사용하면 action의 type도 함께 알아낼 수 있다.
ros2 action list -t
현재는 /turtle1/rotate_absolute [turtlesim/action/RotateAbsolute] 을 반환한다.
5. ros2 action info
- 터미널 3
ros2 action info /turtle1/rotate_absolute
출력은 다음과 같다.
Action: /turtle1/rotate_absolute
Action clients: 1
/teleop_turtle
Action servers: 1
/turtlesim
6. ros2 interface show
action goal을 직접 보내거나 실행하기 위해선 구조를 알아야한다. 다음 명령어를 사용한다.
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
section1 : goal request의 구조
section2 : result의 구조
section3 : feedback의 구조
더보기
예를 들어 10을 보냈는데 8만큼 돌았다고 가정해보자.
goal request : 10
result : 8
feedback : 2
7. ros2 action send_goal
action goal을 터미널에서 실행하는 명령어는 다음과 같다.
ros2 action send_goal <action_name> <action_type> <values>
- 터미널 3
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: 87cff62b6d97438b87d5433048a3ba34
Result:
delta: 0.0
Goal finished with status: SUCCEEDED
여기에 피드백을 추가하려면 feedback 옵션을 추가한다.
ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: -1.57}" --feedback
목표가 완료될 때까지 피드백을 계속해서 받는다.