EasyFleet
A simple-by-default framework for multi-robot, multi-capability fleets built on ROS 2
Loading...
Searching...
No Matches
run_capability.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_MISSION_MANAGER__RUN_CAPABILITY_HPP_
17#define EASYFLEET_MISSION_MANAGER__RUN_CAPABILITY_HPP_
18
19#include <functional>
20#include <future>
21#include <memory>
22#include <sstream>
23#include <string>
24
25#include "easyfleet_core/capability_client.hpp"
26
27#include "easyfleet_mission_manager/ansi.hpp"
28#include "easyfleet_mission_manager/output.hpp"
29
30namespace easyfleet_mission_manager
31{
32
33inline std::string outcome_to_string(uint8_t outcome_value)
34{
35 // Mirrors easyfleet_core::CapabilityClient<ActionT>::Outcome's underlying values.
36 switch (outcome_value) {
37 case 0: return "SUCCEEDED";
38 case 1: return "ABORTED";
39 case 2: return "CANCELED";
40 case 3: return "REJECTED";
41 case 4: return "TIMEOUT";
42 case 5: return "SERVER_UNAVAILABLE";
43 default: return "UNKNOWN";
44 }
45}
46
52template<typename ActionT>
53typename easyfleet_core::CapabilityClient<ActionT>::Response run_capability(
55 const std::string & label,
56 const typename ActionT::Goal & goal,
57 std::chrono::seconds timeout,
58 std::function<void(const typename ActionT::Feedback &)> on_feedback)
59{
60 using Client = easyfleet_core::CapabilityClient<ActionT>;
61 using Feedback = typename ActionT::Feedback;
62
63 auto response_promise = std::make_shared<std::promise<typename Client::Response>>();
64 auto response_future = response_promise->get_future();
65
66 {
67 std::ostringstream out;
68 out << ansi::bold << ansi::cyan << "[" << label << "] " << ansi::reset
69 << "sending goal (will run for up to " << timeout.count() << "s)...";
70 safe_print(out.str());
71 }
72
73 client->request(
74 goal,
75 [response_promise](const typename Client::Response & response) {
76 response_promise->set_value(response);
77 },
78 [on_feedback, label](std::shared_ptr<const Feedback> feedback) {
79 if (on_feedback) {
80 on_feedback(*feedback);
81 }
82 });
83
84 typename Client::Response response;
85 if (response_future.wait_for(timeout) == std::future_status::timeout) {
86 std::ostringstream out;
87 out << ansi::yellow << "[" << label << "] " << ansi::reset
88 << timeout.count() << "s elapsed, stopping it...";
89 safe_print(out.str());
90 client->cancel();
91 response = response_future.get();
92 } else {
93 response = response_future.get();
94 }
95
96 std::ostringstream out;
97 out << ansi::bold << "[" << label << "] " << ansi::reset
98 << "finished with outcome " << ansi::magenta
99 << outcome_to_string(static_cast<uint8_t>(response.outcome)) << ansi::reset;
100 safe_print(out.str());
101
102 return response;
103}
104
105} // namespace easyfleet_mission_manager
106
107#endif // EASYFLEET_MISSION_MANAGER__RUN_CAPABILITY_HPP_
void cancel()
Asks the capability to stop whatever it is currently doing (e.g.
Definition capability_client_impl.hpp:88
std::shared_ptr< CapabilityClient< ActionT > > SharedPtr
Shared pointer to a CapabilityClient<ActionT>.
Definition capability_client.hpp:59
void request(const Goal &goal, ResponseCallback on_response, FeedbackCallback on_feedback=nullptr)
Asks the capability to do something.
Definition capability_client_impl.hpp:62