EasyFleet
A simple-by-default framework for multi-robot, multi-capability fleets built on ROS 2
Loading...
Searching...
No Matches
action_client.hpp
1// Copyright 2026 Intelligent Robotics Lab
2//
3// This file is part of the project EasyFleet
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef EASYFLEET_CORE__ACTION_CLIENT_HPP_
17#define EASYFLEET_CORE__ACTION_CLIENT_HPP_
18
19#include <chrono>
20#include <cstdint>
21#include <functional>
22#include <memory>
23#include <mutex>
24#include <string>
25#include <thread>
26#include <unordered_map>
27
28#include "rclcpp/rclcpp.hpp"
29#include "rclcpp_action/rclcpp_action.hpp"
30
31namespace easyfleet_core
32{
33
35
46template<typename ActionT>
47class ActionClient
48{
49public:
51 using SharedPtr = std::shared_ptr<ActionClient<ActionT>>;
53 using Goal = typename ActionT::Goal;
55 using Feedback = typename ActionT::Feedback;
57 using Result = typename ActionT::Result;
59 using ClientGoalHandle = typename rclcpp_action::Client<ActionT>::GoalHandle;
60
62 using FeedbackCallback = std::function<void (std::shared_ptr<const Feedback>)>;
63
66 enum class GoalOutcome : uint8_t
67 {
68 SUCCEEDED,
69 ABORTED,
70 CANCELED,
71 REJECTED,
72 TIMEOUT,
73 SERVER_UNAVAILABLE,
74 };
75
79 {
81 GoalOutcome outcome{GoalOutcome::ABORTED};
83 rclcpp_action::GoalUUID goal_id{};
85 typename Result::SharedPtr result;
86 };
87
89 using ResultCallback = std::function<void (const GoalResult &)>;
92 std::function<void (bool accepted, const rclcpp_action::GoalUUID & goal_id)>;
93
104 static SharedPtr create(
105 rclcpp::Node & parent_node,
106 const std::string & action_name,
107 std::chrono::milliseconds default_server_timeout = std::chrono::seconds(5));
108
109 ActionClient(const ActionClient &) = delete;
110 ActionClient & operator=(const ActionClient &) = delete;
111
112 ~ActionClient();
113
116 const std::string & get_action_name() const noexcept;
117
120 bool is_server_ready() const;
121
125 bool wait_for_server();
129 bool wait_for_server(std::chrono::milliseconds timeout);
130
141 bool send_goal(
142 const Goal & goal,
143 ResultCallback result_callback = nullptr,
144 FeedbackCallback feedback_callback = nullptr,
145 GoalResponseCallback goal_response_callback = nullptr);
146
158 const Goal & goal,
159 FeedbackCallback feedback_callback = nullptr,
160 std::chrono::milliseconds timeout = std::chrono::milliseconds(0));
161
166 bool cancel_goal(const rclcpp_action::GoalUUID & goal_id);
167
169 void cancel_all_goals();
170
174 std::size_t active_goal_count() const;
175
176private:
177 ActionClient(
178 rclcpp::Node & parent_node,
179 const std::string & action_name,
180 std::chrono::milliseconds default_server_timeout);
181
182 static GoalResult to_goal_result(const typename ClientGoalHandle::WrappedResult & wrapped);
183
184 std::string action_name_;
185 std::chrono::milliseconds default_server_timeout_;
186
187 rclcpp::Node::SharedPtr internal_node_;
188 rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
189 std::thread spin_thread_;
190 typename rclcpp_action::Client<ActionT>::SharedPtr client_;
191
192 mutable std::mutex goals_mutex_;
193 std::unordered_map<std::string, typename ClientGoalHandle::SharedPtr> active_goals_;
194};
195
196} // namespace easyfleet_core
197
198#include "easyfleet_core/detail/action_client_impl.hpp"
199
200#endif // EASYFLEET_CORE__ACTION_CLIENT_HPP_
bool wait_for_server()
Block until the server is reachable, using the constructor's default_server_timeout.
Definition action_client_impl.hpp:107
bool is_server_ready() const
Whether the action server is currently reachable.
Definition action_client_impl.hpp:101
std::function< void(const GoalResult &)> ResultCallback
Callback invoked once a goal reaches a terminal state.
Definition action_client.hpp:89
const std::string & get_action_name() const noexcept
The action name this client was created for.
Definition action_client_impl.hpp:95
std::function< void(bool accepted, const rclcpp_action::GoalUUID &goal_id)> GoalResponseCallback
Callback invoked once the server has accepted or rejected a goal.
Definition action_client.hpp:91
std::shared_ptr< ActionClient< ActionT > > SharedPtr
Shared pointer to an ActionClient<ActionT>.
Definition action_client.hpp:51
bool send_goal(const Goal &goal, ResultCallback result_callback=nullptr, FeedbackCallback feedback_callback=nullptr, GoalResponseCallback goal_response_callback=nullptr)
Send a goal without blocking.
Definition action_client_impl.hpp:119
std::size_t active_goal_count() const
Number of goals accepted by the server and not yet in a terminal state.
Definition action_client_impl.hpp:229
GoalResult send_goal_and_wait(const Goal &goal, FeedbackCallback feedback_callback=nullptr, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Send a goal and block the calling thread until it reaches a terminal state (or timeout elapses,...
Definition action_client_impl.hpp:182
typename rclcpp_action::Client< ActionT >::GoalHandle ClientGoalHandle
rclcpp_action goal handle type of the wrapped action.
Definition action_client.hpp:59
bool cancel_goal(const rclcpp_action::GoalUUID &goal_id)
Request cancellation of a specific in-flight goal.
Definition action_client_impl.hpp:207
std::function< void(std::shared_ptr< const Feedback >)> FeedbackCallback
Callback invoked with feedback as it arrives for an in-flight goal.
Definition action_client.hpp:62
typename ActionT::Goal Goal
Goal type of the wrapped action.
Definition action_client.hpp:53
typename ActionT::Result Result
Result type of the wrapped action.
Definition action_client.hpp:57
void cancel_all_goals()
Request cancellation of every goal currently tracked by this client.
Definition action_client_impl.hpp:223
typename ActionT::Feedback Feedback
Feedback type of the wrapped action.
Definition action_client.hpp:55
static SharedPtr create(rclcpp::Node &parent_node, const std::string &action_name, std::chrono::milliseconds default_server_timeout=std::chrono::seconds(5))
Constructs a new client for action_name, spinning its own internal node in the background.
Definition action_client_impl.hpp:36
GoalOutcome
Outcome of a goal, unifying rejection/timeout/unavailability with the normal terminal states of the a...
Definition action_client.hpp:67
Terminal outcome of a goal, as delivered to a ResultCallback or returned by send_goal_and_wait().
Definition action_client.hpp:79
GoalOutcome outcome
How the goal ended.
Definition action_client.hpp:81
rclcpp_action::GoalUUID goal_id
Id of the goal this result belongs to.
Definition action_client.hpp:83
Result::SharedPtr result
Action-specific result, if outcome is GoalOutcome::SUCCEEDED.
Definition action_client.hpp:85