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(
88 <<
"] Cannot send goal: action client not initialized (was the "
89 "component created with an action server name?)");
90 return std::shared_future<typename GoalHandle::SharedPtr>();
100 options.goal_response_callback = [
this](
typename GoalHandle::SharedPtr goalHandle)
102 if (goalHandle !=
nullptr)
104 RCLCPP_INFO_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Goal accepted by server");
109 RCLCPP_ERROR_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Goal rejected by server");
115 options.result_callback = [
this, resultCallback](
const WrappedResult & result)
120 getLogger(),
"[" << this->
getName() <<
"] Action result callback, goal id: "
121 << rclcpp_action::to_string(result.goal_id));
123 auto resultCallbackPtr = resultCallback.lock();
124 if (resultCallbackPtr !=
nullptr)
126 RCLCPP_INFO_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Calling user result callback");
127 (*resultCallbackPtr)(result);
139 "[" << this->
getName() <<
"] Sending goal to action server: " << (
long)
client_.get());
141 auto goalFuture =
client_->async_send_goal(goal, options);
153 RCLCPP_INFO_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Cancelling current goal");
155 auto cancelFuture =
client_->async_cancel_all_goals();
161 RCLCPP_WARN_STREAM(
getLogger(),
"[" << this->
getName() <<
"] No active goal to cancel");
174 "[" << this->
getName() <<
"] Waiting for action server: " << *actionServerName);
175 client_->wait_for_action_server();
184 RCLCPP_ERROR_STREAM(
getLogger(),
"[" << this->
getName() <<
"] Action server name not set!");
190 "[" << this->
getName() <<
"] Initializing action client for: " << *actionServerName);
195 "[" << this->
getName() <<
"] DONE Initializing action client for: " << *actionServerName);
202 template <
typename TOrthogonal,
typename TSourceObject>
223 actionFeedbackEvent->feedbackMessage = feedback;
230 template <
typename T>
236 template <
typename T>
242 template <
typename T>
248 template <
typename T>
258 std::shared_ptr<ActionClient>
client_ =
nullptr;
259 std::optional<std::shared_future<typename GoalHandle::SharedPtr>>
lastRequest_;
261 std::shared_future<typename rclcpp_action::Client<ActionType>::CancelResponse::SharedPtr>>
267 typename GoalHandle::SharedPtr ,
268 const std::shared_ptr<const Feedback> feedback_msg)
276 const auto & resultType = result_msg.code;
280 << rclcpp_action::to_string(result_msg.goal_id) <<
"]: " << (
int)resultType);
282 if (resultType == rclcpp_action::ResultCode::SUCCEEDED)
284 RCLCPP_INFO(
getLogger(),
"[%s] Action result: Success", this->
getName().c_str());
288 else if (resultType == rclcpp_action::ResultCode::ABORTED)
290 RCLCPP_INFO(
getLogger(),
"[%s] Action result: Aborted", this->
getName().c_str());
294 else if (resultType == rclcpp_action::ResultCode::CANCELED)
296 RCLCPP_INFO(
getLogger(),
"[%s] Action result: Cancelled", this->
getName().c_str());
303 getLogger(),
"[%s] Action result: Unhandled type: %d", this->
getName().c_str(),
308 template <
typename EvType>
311 auto * ev =
new EvType();
312 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)
virtual ~CpActionClient()=default
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
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