跳至主要内容

Service 與 Action

本頁會先從概念上比較 Service 與 Action,再透過標準 ROS 2 Demo Package,直接從命令列呼叫兩者,讓你實際觀察 Request/Response,以及 Goal/Feedback/Result/Cancel 這兩種不同的通訊模式。

概述​

ROS 2 核心概念 已經從較高層次介紹過 Service(同步 Request/Response)與 Action(非同步、以 Goal 為核心,並支援 Feedback 與取消)。

在本頁中,你會使用 Desktop ROS 2 安裝中常見的 Demo Package,實際呼叫一個 Service 並送出一個 Action Goal,進一步觀察 Action Interaction 中各個部分實際是如何運作的。

為什麼重要​

只看文件時,Service 與 Action 很容易混淆。

直接觀察 CLI 的實際輸出,可以讓差異更具體:Service Call 只回傳一次結果;Action 則可以在執行期間持續輸出 Feedback,最後再回傳 Result。

理解這個差異,可以避免把長時間執行的任務設計成 Service(會讓 Client 等待),或把只需要單次查詢的操作設計成 Action(增加不必要的複雜度)。

核心概念​

Service 快速理解​

  • 一次 Service Interaction 包含一個 Request 與一個 Response。多個 Client 可以呼叫同一個 Service。
  • Service 使用 Request/Response 模式:每一個 Request 都會產生一個 Response。Client 可以同步或非同步呼叫 Service。
  • Service Interface 由 .srv 檔案定義,其中 Request 與 Response 兩個區段使用 --- 分隔。

Action 快速理解​

  • Action Interaction 以 Goal 為核心。多個 Client 可以向同一個 Action Server 傳送 Goal,而 Server 會決定如何處理已接受的 Goal。
  • Action Interface 由 .action 檔案定義,其中包含三個以 --- 分隔的區段:Goal、Result 與 Feedback。
  • Client 可以要求取消正在執行中的 Goal。Server 會先決定是否接受或拒絕 Goal Request;已接受的 Goal 之後可能成功完成(succeed)、中止(abort)或被取消(cancel)。
比較項目ServiceAction
執行時間短可能較長
進度更新無Feedback Message
取消不支援支援
底層組成透過 Middleware 進行 Request/Response底層由 Topic + Service 組成,但對外提供統一 API
常見用途查詢或觸發操作Navigation、Trajectory Execution、長時間運算

:::info 與「Arm Action」不是同一個概念 本頁中的 Action 一律指 ROS 2 的通訊機制(Goal/Feedback/Result/Cancel)。

Manipulation 章節中的 Arm Action 則描述一個 Workflow Stage,通常會在內部使用 ROS 2 Action,例如 control_msgs/action/FollowJointTrajectory,但這兩個名詞不能互相替代。

詳細說明請參考 ROS 2 核心概念 中的提示。 :::

實作步驟:Service​

demo_nodes_cpp(或 demo_nodes_py)Package 在 Desktop ROS 2 安裝中通常會提供一個簡單的加法 Service,使用 example_interfaces/srv/AddTwoInts。

如果尚未安裝,可以先執行:

sudo apt install ros-${ROS_DISTRO}-demo-nodes-cpp

在一個已經 source ROS 2 環境的 Terminal 中啟動 Server:

ros2 run demo_nodes_cpp add_two_ints_server

在第二個 Terminal 中檢查並呼叫它:

# Discover the service and its type
ros2 service list
ros2 service type /add_two_ints
ros2 interface show example_interfaces/srv/AddTwoInts

# Call it
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 2, b: 3}"

預期成果(Service)​

requester: making request: example_interfaces.srv.AddTwoInts_Request(a=2, b=3)

response:
example_interfaces.srv.AddTwoInts_Response(sum=5)

這個 Service Call 只會回傳一次結果。

執行期間不會持續輸出進度資訊,因為這類操作預期會在短時間內完成。

實作步驟:Action​

action_tutorials_cpp(或 action_tutorials_py)Package 提供一個簡單的 Fibonacci Sequence Action Server,使用 action_tutorials_interfaces/action/Fibonacci,常用來示範 Action 通訊模式。

如果尚未安裝,可以先執行:

sudo apt install ros-${ROS_DISTRO}-action-tutorials-cpp

在一個已經 source ROS 2 環境的 Terminal 中啟動 Action Server:

ros2 run action_tutorials_cpp fibonacci_action_server

在第二個 Terminal 中檢查並送出 Goal:

# Discover the action and its type
ros2 action list -t
ros2 interface show action_tutorials_interfaces/action/Fibonacci

# Send a goal and watch feedback and result
ros2 action send_goal --feedback /fibonacci action_tutorials_interfaces/action/Fibonacci "{order: 5}"

預期成果(Action)​

Sending goal:
order: 5

Goal accepted with ID: <uuid>

Feedback:
sequence: [0, 1, 1]

Feedback:
sequence: [0, 1, 1, 2]

Feedback:
sequence: [0, 1, 1, 2, 3]

Result:
sequence: [0, 1, 1, 2, 3, 5]

Goal finished with status: SUCCEEDED

Goal 執行期間會逐步收到 Feedback;最後只會收到一次 Result。

取消也是 ROS 2 Action Protocol 的一部分。Action Client 可以明確要求取消已接受的 Goal,而 Server 會決定是否接受該 Cancellation Request。

常用指令​

# Services
ros2 service list -t
ros2 service type <service_name>
ros2 service call <service_name> <srv_type> "<yaml_args>"
ros2 interface show <package>/srv/<Type>

# Actions
ros2 action list -t
ros2 action info <action_name>
ros2 action send_goal [--feedback] <action_name> <action_type> "<yaml_goal>"
ros2 interface show <package>/action/<Type>

常見問題​

  • ros2 service call 一直等待而沒有回傳 — 一直等待而沒有回傳** — 確認預期的 Server 是否正在執行並正常回應。可以使用 ros2 service list 與 ros2 service type <service_name> 驗證該 Service。如果 Request YAML 無效,CLI 通常會直接回報 Parsing 或 Type Error。
  • ros2 action send_goal 顯示 Goal 被拒絕 — Action Server 選擇不接受該 Goal。Goal Acceptance 規則由 Server Implementation 決定,請檢查 Server Log 取得詳細資訊。
  • 完全沒有顯示 Feedback — 可能是在 ros2 action send_goal 時忘了加上 --feedback,或該 Server 對這個 Goal Type 不會發布 Feedback。
  • Package 'action_tutorials_cpp' not found — 依照上方範例使用 apt 安裝。這是標準 Demo Package,但不一定會包含在 Minimal Installation 中。
  • 呼叫 Service / Action 時發生 Type Mismatch — Package 或 Type Name 與 Server 預期的不完全一致。可參考 Troubleshooting。

重點整理​

  • Service 適合快速的 Request/Response Interaction,不提供進度 Feedback 或取消功能。
  • Action 適合可能長時間執行的 Goal,並支援 Goal Acceptance、Feedback、Result 與 Cancellation。
  • ros2 service call 與 ros2 action send_goal 可以完全透過 CLI 測試這兩種通訊模式,不需要自行撰寫 Client。
  • 「ROS 2 Action」(本頁所介紹的通訊機制)與「Arm Action」(Manipulation Workflow Stage)代表不同概念,雖然後者通常會使用前者來實作。

下一步​

接著前往 Parameter 與 Launch,讓 Node 的執行行為可以透過設定調整;也可以回到 ROS 2 核心概念 複習這些基本術語。