SMACC2
Loading...
Searching...
No Matches
smacc_action_client_base.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
26
27#include <optional>
28#include <rclcpp_action/rclcpp_action.hpp>
29
30namespace smacc2
31{
32namespace client_bases
33{
34using namespace smacc2::default_events;
35
36template <typename ActionType>
38{
39public:
40 // Inside this macro you can find the typedefs for Goal and other types
41 typedef rclcpp_action::Client<ActionType> ActionClient;
42
43 using Goal = typename ActionClient::Goal;
44 using Feedback = typename ActionClient::Feedback;
45 using GoalHandle = rclcpp_action::ClientGoalHandle<ActionType>;
46 typedef typename GoalHandle::WrappedResult WrappedResult;
47
48 using SendGoalOptions = typename ActionClient::SendGoalOptions;
50 std::function<void(std::shared_future<typename GoalHandle::SharedPtr>)>;
51 using FeedbackCallback = typename GoalHandle::FeedbackCallback;
52 using ResultCallback = typename GoalHandle::ResultCallback;
53 using CancelRequest = typename ActionType::Impl::CancelGoalService::Request;
54 using CancelResponse = typename ActionType::Impl::CancelGoalService::Response;
55 using CancelCallback = std::function<void(typename CancelResponse::SharedPtr)>;
56
58
59 std::string action_endpoint_;
60 SmaccActionClientBase(const std::string & actionServerName)
61 : ISmaccActionClient(), action_endpoint_(actionServerName)
62 {
63 }
64
66
68
69 std::shared_ptr<rclcpp_action::ClientBase> getClientBase() override { return client_; }
70
71 void onInitialize() override
72 {
73 if (name_ == "") name_ = smacc2::demangleSymbol(typeid(*this).name());
74 this->client_ = rclcpp_action::create_client<ActionType>(getNode(), action_endpoint_);
75 // RCLCPP_INFO_STREAM(
76 // this->getLogger(),
77 // "Waiting for action server '" << name_ << "' of type: " << demangledTypeName<ActionType>());
78 //client_->wait_for_action_server();
79 }
80
82 {
83 RCLCPP_INFO_STREAM(
84 this->getLogger(),
85 "Waiting for action server '" << name_ << "' of type: " << demangledTypeName<ActionType>());
86 client_->wait_for_action_server();
87 }
88
89 static std::string getEventLabel()
90 {
92 return type->getNonTemplatedTypeName();
93 }
94
95 std::optional<std::shared_future<typename GoalHandle::SharedPtr>> lastRequest_;
96 std::optional<std::shared_future<typename CancelResponse::SharedPtr>> lastCancelResponse_;
97
100 // Kept on purpose: placeholders for the preempted and rejected outcomes (ROS 1 actions had them; ROS 2 result codes do not).
101 // SmaccActionResultSignal onPreempted_;
102 // SmaccActionResultSignal onRejected_;
104
105 // event creation/posting factory functions
106 std::function<void(WrappedResult)> postSuccessEvent;
107 std::function<void(WrappedResult)> postAbortedEvent;
108 std::function<void(WrappedResult)> postCancelledEvent;
109 // Kept on purpose, as the two signals above.
110 // std::function<void(WrappedResult)> postPreemptedEvent;
111 // std::function<void(WrappedResult)> postRejectedEvent;
112
113 std::function<void(const Feedback &)> postFeedbackEvent;
114
116
117 template <typename EvType>
119 {
120 auto ev = smacc2::makeEvent<EvType>();
121 RCLCPP_INFO(
122 getLogger(), "Action client Posting EVENT %s",
123 demangleSymbol(typeid(ev.get()).name()).c_str());
124 this->postEvent(ev);
125 }
126
127 template <typename TOrthogonal, typename TSourceObject>
129 {
130 // we create here all the event factory functions capturing the TOrthogonal
131 postSuccessEvent = [this](auto msg)
133 postAbortedEvent = [this](auto msg)
135
136 postCancelledEvent = [this](auto msg)
138
139 postFeedbackEvent = [this](auto msg)
140 {
142 actionFeedbackEvent->client = this;
143 actionFeedbackEvent->feedbackMessage = msg;
144 this->postEvent(actionFeedbackEvent);
145 RCLCPP_DEBUG(getLogger(), "[%s] FEEDBACK EVENT", demangleType(typeid(*this)).c_str());
146 };
147
148 feedback_cb = [this](auto client, auto feedback) { this->onFeedback(client, feedback); };
149 }
150
151 template <typename T>
152 smacc2::SmaccSignalConnection onSucceeded(void (T::*callback)(WrappedResult &), T * object)
153 {
154 return this->getStateMachine()->createSignalConnection(onSucceeded_, callback, object);
155 }
156
157 template <typename T>
159 {
160 return this->getStateMachine()->createSignalConnection(onSucceeded_, callback);
161 }
162
163 template <typename T>
164 smacc2::SmaccSignalConnection onAborted(void (T::*callback)(WrappedResult &), T * object)
165 {
166 return this->getStateMachine()->createSignalConnection(onAborted_, callback, object);
167 }
168
169 template <typename T>
171 {
172 return this->getStateMachine()->createSignalConnection(onAborted_, callback);
173 }
174
175 template <typename T>
176 smacc2::SmaccSignalConnection onCancelled(void (T::*callback)(WrappedResult &), T * object)
177 {
178 return this->getStateMachine()->createSignalConnection(onCancelled_, callback, object);
179 }
180
181 template <typename T>
183 {
184 return this->getStateMachine()->createSignalConnection(onCancelled_, callback);
185 }
186
187 bool cancelGoal() override
188 {
189 lastCancelResponse_ = this->client_->async_cancel_all_goals();
190
191 return true;
192 }
193
194 std::shared_future<typename GoalHandle::SharedPtr> sendGoal(
195 Goal & goal, typename SmaccActionResultSignal::WeakPtr resultCallback =
197 {
198 SendGoalOptions options;
199
200 options.feedback_callback = feedback_cb;
201
202 options.result_callback =
203 [this, resultCallback](
204 const typename rclcpp_action::ClientGoalHandle<ActionType>::WrappedResult & result)
205 {
206 RCLCPP_INFO_STREAM(
207 getLogger(), "[" << getName() << "] Action result callback, getting shared future");
208 RCLCPP_INFO_STREAM(
209 getLogger(), "[" << getName() << "] Action client Result goal id: "
210 << rclcpp_action::to_string(result.goal_id));
211
212 auto resultCallbackPtr = resultCallback.lock();
213
214 if (resultCallbackPtr != nullptr)
215 {
216 RCLCPP_INFO_STREAM(
217 getLogger(), "[" << getName() << "] Result CB calling user callback:"
218 << demangleSymbol(typeid(*resultCallbackPtr).name()));
219 (*resultCallbackPtr)(result);
220 }
221 else
222 {
223 RCLCPP_INFO_STREAM(
224 getLogger(), "[" << getName() << "] Result CB calling default callback");
225 this->onResult(result);
226 }
227 };
228
229 RCLCPP_INFO_STREAM(
230 getLogger(), "[" << getName() << "] client ready clients: "
231 << this->client_->get_number_of_ready_clients());
232 RCLCPP_INFO_STREAM(
233 getLogger(),
234 "[" << getName() << "] Waiting it is ready? " << client_->action_server_is_ready());
235
236 RCLCPP_INFO_STREAM(getLogger(), getName() << ": async send goal.");
237 auto lastRequest = this->client_->async_send_goal(goal, options);
238 this->lastRequest_ = lastRequest;
239
240 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] Action request");
241
242 return lastRequest;
243 }
244
245protected:
246 typename ActionClient::SharedPtr client_;
247
249 typename GoalHandle::SharedPtr /*goalhandle*/,
250 const std::shared_ptr<const Feedback> feedback_msg)
251 {
252 postFeedbackEvent(*feedback_msg);
253 }
254
255 void onResult(const WrappedResult & result_msg)
256 {
257 const auto & resultType = result_msg.code;
258
259 RCLCPP_INFO_STREAM(
260 getLogger(), "[" << this->getName() << "] response result ["
261 << rclcpp_action::to_string(result_msg.goal_id) << "]: " << (int)resultType);
262
263 if (resultType == rclcpp_action::ResultCode::SUCCEEDED)
264 {
265 RCLCPP_INFO(getLogger(), "[%s] request result: Success", this->getName().c_str());
266 onSucceeded_(result_msg);
267 postSuccessEvent(result_msg);
268 }
269 else if (resultType == rclcpp_action::ResultCode::ABORTED)
270 {
271 RCLCPP_INFO(getLogger(), "[%s] request result: Aborted", this->getName().c_str());
272 onAborted_(result_msg);
273 postAbortedEvent(result_msg);
274 }
275 else if (resultType == rclcpp_action::ResultCode::CANCELED)
276 {
277 RCLCPP_INFO(getLogger(), "[%s] request result: Cancelled", this->getName().c_str());
278 onCancelled_(result_msg);
279 postCancelledEvent(result_msg);
280 }
281 else
282 {
283 RCLCPP_INFO(
284 getLogger(), "[%s] request result: NOT HANDLED TYPE: %d", this->getName().c_str(),
285 (int)resultType);
286 }
287 }
288};
289
290} // namespace client_bases
291
292} // namespace smacc2
rclcpp::Node::SharedPtr getNode()
Definition client.cpp:60
ISmaccStateMachine * getStateMachine()
rclcpp::Logger getLogger()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
std::weak_ptr< SmaccSignal< void(const WrappedResult &), optional_last_value< typename boost::function_traits< void(const WrappedResult &)>::result_type >, int, std::less< int >, function< void(const WrappedResult &)>, typename extended_signature< function_traits< void(const WrappedResult &)>::arity, void(const WrappedResult &)>::function_type, boost::signals2::mutex > > WeakPtr
std::optional< std::shared_future< typename CancelResponse::SharedPtr > > lastCancelResponse_
typename ActionType::Impl::CancelGoalService::Response CancelResponse
typename ActionType::Impl::CancelGoalService::Request CancelRequest
std::function< void(typename CancelResponse::SharedPtr)> CancelCallback
typename ActionClient::SendGoalOptions SendGoalOptions
smacc2::SmaccSignalConnection onCancelled(std::function< void(WrappedResult &)> callback)
void onFeedback(typename GoalHandle::SharedPtr, const std::shared_ptr< const Feedback > feedback_msg)
smacc2::SmaccSignal< void(const WrappedResult &)> SmaccActionResultSignal
std::function< void(WrappedResult)> postCancelledEvent
smacc2::SmaccSignalConnection onSucceeded(void(T::*callback)(WrappedResult &), T *object)
std::function< void(WrappedResult)> postSuccessEvent
std::shared_ptr< rclcpp_action::ClientBase > getClientBase() override
smacc2::SmaccSignalConnection onAborted(std::function< void(WrappedResult &)> callback)
smacc2::SmaccSignalConnection onCancelled(void(T::*callback)(WrappedResult &), T *object)
rclcpp_action::ClientGoalHandle< ActionType > GoalHandle
rclcpp_action::Client< ActionType > ActionClient
std::function< void(std::shared_future< typename GoalHandle::SharedPtr >)> GoalResponseCallback
std::function< void(WrappedResult)> postAbortedEvent
void onResult(const WrappedResult &result_msg)
std::function< void(const Feedback &)> postFeedbackEvent
SmaccActionClientBase(const std::string &actionServerName)
std::shared_future< typename GoalHandle::SharedPtr > sendGoal(Goal &goal, typename SmaccActionResultSignal::WeakPtr resultCallback=typename SmaccActionResultSignal::WeakPtr())
smacc2::SmaccSignalConnection onAborted(void(T::*callback)(WrappedResult &), T *object)
smacc2::SmaccSignalConnection onSucceeded(std::function< void(WrappedResult &)> callback)
typename GoalHandle::ResultCallback ResultCallback
std::optional< std::shared_future< typename GoalHandle::SharedPtr > > lastRequest_
typename GoalHandle::FeedbackCallback FeedbackCallback
static TypeInfo::Ptr getTypeInfoFromType()
std::string demangleSymbol()
std::string demangledTypeName()
std::string demangleType(const std::type_info *tinfo)
boost::signals2::connection SmaccSignalConnection
boost::intrusive_ptr< EventType > makeEvent(Args &&... args)