27#include <rclcpp_action/rclcpp_action.hpp>
31namespace client_core_components
35template <
typename ActionType>
41 using Goal =
typename ActionType::Goal;
42 using Feedback =
typename ActionType::Feedback;
43 using Result =
typename ActionType::Result;
44 using GoalHandle = rclcpp_action::ClientGoalHandle<ActionType>;
48 std::function<void(std::shared_future<typename GoalHandle::SharedPtr>)>;
78 std::shared_future<typename GoalHandle::SharedPtr>
sendGoal(
84 std::shared_ptr<ActionClient> client;
92 if (client ==
nullptr)
96 <<
"] Cannot send goal: action client not initialized (was the "
97 "component created with an action server name?)");
98 return std::shared_future<typename GoalHandle::SharedPtr>();
104 options.feedback_callback = feedbackCallback;
108 options.goal_response_callback = [
this](
typename GoalHandle::SharedPtr goalHandle)
110 if (goalHandle !=
nullptr)
112 RCLCPP_INFO_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Goal accepted by server");
117 RCLCPP_ERROR_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Goal rejected by server");
125 options.result_callback = [
this, resultCallback](
const WrappedResult & result)
128 getLogger(),
"[" << this->
getName() <<
"] Action result callback, goal id: "
129 << rclcpp_action::to_string(result.goal_id));
131 auto resultCallbackPtr = resultCallback.lock();
132 if (resultCallbackPtr !=
nullptr)
134 RCLCPP_INFO_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Calling user result callback");
135 (*resultCallbackPtr)(result);
147 <<
static_cast<const void *
>(client.get()));
149 auto goalFuture = client->async_send_goal(goal, options);
162 std::shared_ptr<ActionClient> client;
163 std::shared_future<typename GoalHandle::SharedPtr> request;
173 if (client ==
nullptr)
175 RCLCPP_WARN_STREAM(
getLogger(),
"[" << this->
getName() <<
"] No active goal to cancel");
179 RCLCPP_INFO_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Cancelling current goal");
183 std::shared_future<typename ActionClient::CancelResponse::SharedPtr> cancelFuture;
184 typename GoalHandle::SharedPtr goalHandle;
185 if (request.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
187 goalHandle = request.get();
192 cancelFuture = goalHandle !=
nullptr ? client->async_cancel_goal(goalHandle)
193 : client->async_cancel_goals_before(
getNode()->now());
195 catch (
const rclcpp_action::exceptions::UnknownGoalHandleError &)
197 RCLCPP_INFO_STREAM(
getLogger(),
"[" << this->
getName() <<
"] The goal has already finished");
215 "[" << this->
getName() <<
"] Waiting for action server: " << *actionServerName);
216 client_->wait_for_action_server();
225 RCLCPP_ERROR_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Action server name not set!");
231 "[" << this->
getName() <<
"] Initializing action client for: " << *actionServerName);
236 "[" << this->
getName() <<
"] DONE Initializing action client for: " << *actionServerName);
243 template <
typename TOrthogonal,
typename TSourceObject>
264 actionFeedbackEvent->feedbackMessage = feedback;
271 template <
typename T>
277 template <
typename T>
283 template <
typename T>
289 template <
typename T>
299 std::shared_ptr<ActionClient>
client_ =
nullptr;
300 std::optional<std::shared_future<typename GoalHandle::SharedPtr>>
lastRequest_;
302 std::shared_future<typename rclcpp_action::Client<ActionType>::CancelResponse::SharedPtr>>
308 typename GoalHandle::SharedPtr ,
309 const std::shared_ptr<const Feedback> feedback_msg)
317 const auto & resultType = result_msg.code;
321 << rclcpp_action::to_string(result_msg.goal_id) <<
"]: " << (
int)resultType);
323 if (resultType == rclcpp_action::ResultCode::SUCCEEDED)
325 RCLCPP_INFO(
getLogger(),
"[%s] Action result: Success", this->
getName().c_str());
329 else if (resultType == rclcpp_action::ResultCode::ABORTED)
331 RCLCPP_INFO(
getLogger(),
"[%s] Action result: Aborted", this->
getName().c_str());
335 else if (resultType == rclcpp_action::ResultCode::CANCELED)
337 RCLCPP_INFO(
getLogger(),
"[%s] Action result: Cancelled", this->
getName().c_str());
344 getLogger(),
"[%s] Action result: Unhandled type: %d", this->
getName().c_str(),
349 template <
typename EvType>
353 ev->resultMessage = result;
ISmaccStateMachine * getStateMachine()
virtual std::string getName() const
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
std::function< void(std::shared_future< typename GoalHandle::SharedPtr >)> GoalResponseCallback
typename ActionClient::SendGoalOptions SendGoalOptions
std::function< void(const WrappedResult &)> postCancelledEvent
typename GoalHandle::WrappedResult WrappedResult
std::optional< std::string > actionServerName
void onComponentInitialization()
smacc2::SmaccSignal< void()> onGoalAccepted_
std::optional< std::shared_future< typename GoalHandle::SharedPtr > > lastRequest_
void onResult(const WrappedResult &result_msg)
smacc2::SmaccSignalConnection onSucceeded(void(T::*callback)(const WrappedResult &), T *object)
CpActionClient(const std::string &actionServerName)
std::shared_future< typename GoalHandle::SharedPtr > sendGoal(Goal &goal, typename smacc2::SmaccSignal< void(const WrappedResult &)>::WeakPtr resultCallback=typename smacc2::SmaccSignal< void(const WrappedResult &)>::WeakPtr())
smacc2::SmaccSignalConnection onFeedback(void(T::*callback)(const Feedback &), T *object)
FeedbackCallback feedbackCallback_
smacc2::SmaccSignal< void(const WrappedResult &)> onActionCancelled_
smacc2::SmaccSignal< void(const Feedback &)> onActionFeedback_
typename ActionType::Feedback Feedback
typename ActionType::Result Result
smacc2::SmaccSignal< void()> onGoalRejected_
std::function< void(const WrappedResult &)> postSuccessEvent
void postResultEvent(const WrappedResult &result)
typename GoalHandle::FeedbackCallback FeedbackCallback
smacc2::SmaccSignalConnection onAborted(void(T::*callback)(const WrappedResult &), T *object)
std::optional< std::chrono::milliseconds > serverTimeout
void onInitialize() override
rclcpp_action::Client< ActionType > ActionClient
smacc2::SmaccSignalConnection onCancelled(void(T::*callback)(const WrappedResult &), T *object)
smacc2::SmaccSignal< void(const WrappedResult &)> onActionSucceeded_
std::shared_ptr< ActionClient > client_
void onFeedback(typename GoalHandle::SharedPtr, const std::shared_ptr< const Feedback > feedback_msg)
bool isServerReady() const
std::shared_ptr< ActionClient > getActionClient() const
std::function< void(const WrappedResult &)> postAbortedEvent
smacc2::SmaccSignal< void(const WrappedResult &)> onActionAborted_
std::function< void(const Feedback &)> postFeedbackEvent
typename GoalHandle::ResultCallback ResultCallback
~CpActionClient() override=default
rclcpp_action::ClientGoalHandle< ActionType > GoalHandle
typename ActionType::Goal Goal
std::optional< std::shared_future< typename rclcpp_action::Client< ActionType >::CancelResponse::SharedPtr > > lastCancelResponse_
std::string demangleSymbol()
boost::signals2::connection SmaccSignalConnection
boost::intrusive_ptr< EventType > makeEvent(Args &&... args)