Capabilities2 0.3.0
Loading...
Searching...
No Matches
topic_runner.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
6
8{
9
15template <typename TopicT>
17{
18public:
25
33 virtual void init_subscriber(rclcpp::Node::SharedPtr node, const runner_opts& run_config,
34 const std::string& topic_name)
35 {
36 // initialize the runner base by storing node pointer and run config
37 init_base(node, run_config);
38
39 // create an service client
40 subscription_ = node_->create_subscription<TopicT>(
41 topic_name, 10, [this](const typename TopicT::SharedPtr msg) { this->callback(msg); });
42 }
43
48 virtual void stop(const std::string& bond_id, const std::string& instance_id = "") override
49 {
50 // if the node pointer is empty then throw an error
51 // this means that the runner was not started and is being used out of order
52
53 if (!node_)
54 throw runner_exception("cannot stop runner that was not started");
55
56 // throw an error if the subscription is null
57 // this can happen if the runner is not able to find the topic resource
58
59 if (!subscription_)
60 throw runner_exception("cannot stop runner subscriber that was not started");
61
62 // emit stop event
63 emit_stopped(bond_id, instance_id, param_on_stopped());
64
65 RCLCPP_INFO(node_->get_logger(), "runner cleaned. stopping..");
66 }
67
68protected:
77 virtual void execution(capabilities2_events::EventParameters parameters, const std::string& thread_id) override
78 {
79 // split thread_id to get bond_id and instance_id (format: "bond_id/instance_id")
80 std::string bond_id = ThreadTriggerRunner::bond_from_thread_id(thread_id);
81 std::string instance_id = ThreadTriggerRunner::instance_from_thread_id(thread_id);
82
83 // emit started event
84 emit_started(bond_id, instance_id, param_on_started());
85
86 // block until a message is received in the callback and stored in latest_message_
87 std::unique_lock<std::mutex> lock(block_mutex_);
88 completed_ = false;
89
90 RCLCPP_INFO(node_->get_logger(), "Waiting for Message.");
91
92 // Conditional wait
93 cv_.wait(lock, [this] { return completed_; });
94 RCLCPP_INFO(node_->get_logger(), "Message Received.");
95
97 {
98 // emit success event
99 emit_succeeded(bond_id, instance_id, param_on_success());
100 }
101 else
102 {
103 RCLCPP_ERROR(node_->get_logger(), "Message receiving failed.");
104 // emit failed event
105 emit_failed(bond_id, instance_id, param_on_failure());
106 }
107
108 RCLCPP_INFO(node_->get_logger(), "Thread closing.");
109 }
110
111protected:
120 void callback(const typename TopicT::SharedPtr& msg)
121 {
122 latest_message_ = msg;
123
124 completed_ = true;
125 cv_.notify_all();
126 }
127
131 typename rclcpp::Subscription<TopicT>::SharedPtr subscription_;
132
136 mutable typename TopicT::SharedPtr latest_message_;
137
141 std::condition_variable cv_;
142 std::mutex block_mutex_;
144};
145
146} // namespace capabilities2_runner
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
virtual capabilities2_events::EventParameters param_on_started()
Update on_started event parameters with new data if available.
Definition runner_base.hpp:226
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_started(const std::string &bond_id, const std::string &instance_id, capabilities2_events::EventParameters parameters=capabilities2_events::EventParameters())
emit STARTED event
Definition runner_base.hpp:418
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
Topic runner base class.
Definition topic_runner.hpp:17
TopicT::SharedPtr latest_message_
parameter to store the latest message received in the callback for use in the trigger execution
Definition topic_runner.hpp:136
virtual void stop(const std::string &bond_id, const std::string &instance_id="") override
stop function to cease functionality and shutdown
Definition topic_runner.hpp:48
bool completed_
Definition topic_runner.hpp:143
rclcpp::Subscription< TopicT >::SharedPtr subscription_
parameter to be used in the on_started event emission
Definition topic_runner.hpp:131
virtual void init_subscriber(rclcpp::Node::SharedPtr node, const runner_opts &run_config, const std::string &topic_name)
Initializer function for initializing the topic runner in place of constructor due to plugin semantic...
Definition topic_runner.hpp:33
void callback(const typename TopicT::SharedPtr &msg)
Callback to be executed when receiving a message.
Definition topic_runner.hpp:120
virtual void execution(capabilities2_events::EventParameters parameters, const std::string &thread_id) override
Trigger process to be executed.
Definition topic_runner.hpp:77
std::condition_variable cv_
condition variable and mutex for synchronizing the callback and trigger execution
Definition topic_runner.hpp:141
TopicRunner()
Constructor which needs to be empty due to plugin semantics.
Definition topic_runner.hpp:22
std::mutex block_mutex_
Definition topic_runner.hpp:142
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