10#include <condition_variable>
12#include <rclcpp/rclcpp.hpp>
13#include <rclcpp_action/rclcpp_action.hpp>
14#include <action_msgs/srv/cancel_goal.hpp>
26template <
typename ActionT>
44 virtual void init_action(rclcpp::Node::SharedPtr node,
const runner_opts& run_config,
const std::string& action_name)
53 RCLCPP_INFO(
node_->get_logger(),
"waiting for action: %s", action_name.c_str());
55 if (!
action_client_->wait_for_action_server(std::chrono::seconds(1000)))
57 RCLCPP_ERROR(
node_->get_logger(),
"failed to connect to action: %s", action_name.c_str());
61 RCLCPP_INFO(
node_->get_logger(),
"connected with action: %s", action_name.c_str());
68 virtual void stop(
const std::string& bond_id,
const std::string& instance_id =
"")
override
88 goal_handle_, [
this, &bond_id, &instance_id](action_msgs::srv::CancelGoal_Response::SharedPtr response) {
89 if (response->return_code != action_msgs::srv::CancelGoal_Response::ERROR_NONE)
92 RCLCPP_ERROR(node_->get_logger(),
"Runner cancellation failed.");
101 auto timeout = std::chrono::steady_clock::now() + std::chrono::seconds(2);
102 while (std::chrono::steady_clock::now() < timeout)
105 if (cancel_future.wait_for(std::chrono::milliseconds(100)) == std::future_status::ready)
109 catch (
const rclcpp_action::exceptions::UnknownGoalHandleError& e)
111 RCLCPP_ERROR(
node_->get_logger(),
"failed to cancel goal: %s", e.what());
116 RCLCPP_INFO(
node_->get_logger(),
"runner cleaned. stopping..");
135 RCLCPP_INFO(
node_->get_logger(),
"goal generated for instance %s", instance_id.c_str());
137 std::mutex block_mutex;
138 std::condition_variable cv;
139 bool completed =
false;
140 std::unique_lock<std::mutex> lock(block_mutex);
144 [
this, &instance_id](
const typename rclcpp_action::ClientGoalHandle<ActionT>::SharedPtr& goal_handle) {
147 RCLCPP_INFO(
node_->get_logger(),
"goal accepted. Waiting for result for instance %s", instance_id.c_str());
151 RCLCPP_ERROR(
node_->get_logger(),
"goal rejected for instance %s", instance_id.c_str());
159 typename rclcpp_action::ClientGoalHandle<ActionT>::SharedPtr goal_handle,
160 const typename ActionT::Feedback::ConstSharedPtr feedback_msg) {
165 RCLCPP_INFO(
node_->get_logger(),
"received feedback: %s for instance %s", feedback.c_str(), instance_id.c_str());
170 [
this, &instance_id, &completed, &cv,
171 &bond_id, &instance_id](
const typename rclcpp_action::ClientGoalHandle<ActionT>::WrappedResult& wrapped_result) {
172 RCLCPP_INFO(
node_->get_logger(),
"received result for instance %s", instance_id.c_str());
173 if (wrapped_result.code == rclcpp_action::ResultCode::SUCCEEDED)
175 RCLCPP_INFO(
node_->get_logger(),
"action succeeded for instance %s", instance_id.c_str());
181 RCLCPP_ERROR(
node_->get_logger(),
"action failed for instance %s", instance_id.c_str());
187 result_ = wrapped_result.result;
193 RCLCPP_INFO(
node_->get_logger(),
"goal sent. Waiting for acceptance for instance %s", instance_id.c_str());
196 cv.wait(lock, [&completed] {
return completed; });
197 RCLCPP_INFO(
node_->get_logger(),
"action complete. Thread closing for instance %s", instance_id.c_str());
224 virtual std::string
generate_feedback(
const typename ActionT::Feedback::ConstSharedPtr msg) = 0;
235 typename rclcpp_action::ClientGoalHandle<ActionT>::SharedPtr
goal_handle_;
250 std::shared_future<typename rclcpp_action::ClientGoalHandle<ActionT>::WrappedResult>
result_future_;
action runner base class
Definition action_runner.hpp:28
virtual void stop(const std::string &bond_id, const std::string &instance_id="") override
stop function to cease functionality and shutdown
Definition action_runner.hpp:68
rclcpp_action::Client< ActionT >::SharedPtr action_client_
Definition action_runner.hpp:228
rclcpp_action::ClientGoalHandle< ActionT >::WrappedResult wrapped_result_
Definition action_runner.hpp:238
ActionRunner()
Constructor which needs to be empty due to plugin semantics.
Definition action_runner.hpp:33
std::shared_future< typename rclcpp_action::ClientGoalHandle< ActionT >::WrappedResult > result_future_
Definition action_runner.hpp:250
virtual ActionT::Goal generate_goal(capabilities2_events::EventParameters ¶meters)=0
Generate a goal from parameters.
ActionT::Goal goal_msg_
Definition action_runner.hpp:244
rclcpp_action::ClientGoalHandle< ActionT >::SharedPtr goal_handle_
Definition action_runner.hpp:235
virtual void execution(capabilities2_events::EventParameters parameters, const std::string &thread_id) override
Trigger process to be executed.
Definition action_runner.hpp:127
rclcpp_action::Client< ActionT >::SendGoalOptions send_goal_options_
Definition action_runner.hpp:232
virtual void init_action(rclcpp::Node::SharedPtr node, const runner_opts &run_config, const std::string &action_name)
Initializer function for initializing the action runner in place of constructor due to plugin semanti...
Definition action_runner.hpp:44
ActionT::Result::SharedPtr result_
Definition action_runner.hpp:241
std::shared_future< typename rclcpp_action::ClientGoalHandle< ActionT >::SharedPtr > goal_handle_future_
Definition action_runner.hpp:247
virtual std::string generate_feedback(const typename ActionT::Feedback::ConstSharedPtr msg)=0
Generate a std::string from feedback message.
virtual capabilities2_events::EventParameters param_on_success()
Update on_success event parameters with new data if available.
Definition runner_base.hpp:271
void emit_succeeded(const std::string &bond_id, const std::string &instance_id, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit SUCCEEDED event
Definition runner_base.hpp:444
void emit_failed(const std::string &bond_id, const std::string &instance_id, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit FAILED event
Definition runner_base.hpp:457
void init_base(rclcpp::Node::SharedPtr node, const runner_opts &run_config)
Initializer function for initializing the base runner in place of constructor due to plugin semantics...
Definition runner_base.hpp:126
virtual capabilities2_events::EventParameters param_on_stopped()
Update on_stopped event parameters with new data if available.
Definition runner_base.hpp:241
virtual capabilities2_events::EventParameters param_on_failure()
Update on_failure event parameters with new data if available.
Definition runner_base.hpp:256
rclcpp::Node::SharedPtr node_
shared pointer to the capabilities node Allows to use ros node related functionalities
Definition runner_base.hpp:469
void emit_stopped(const std::string &bond_id, const std::string &instance_id, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit STOPPED event
Definition runner_base.hpp:431
add threaded trigger execution to the runner
Definition threadtrigger_runner.hpp:21
static const std::string bond_from_thread_id(const std::string &thread_id)
helper function to extract bond_id from thread_id
Definition threadtrigger_runner.hpp:30
static const std::string instance_from_thread_id(const std::string &thread_id)
helper function to extract instance_id from thread_id
Definition threadtrigger_runner.hpp:44
Definition action_runner.hpp:19
capability options for a capability runner given by interface and provider
Definition event_parameters.hpp:167
runner exception
Definition runner_base.hpp:24
runner options
Definition runner_base.hpp:56