Modulo 5.0.0
Loading...
Searching...
No Matches
Component.cpp
1#include "modulo_components/Component.hpp"
2
3using namespace modulo_core::communication;
4using namespace rclcpp;
5
6namespace modulo_components {
7
8Component::Component(const NodeOptions& node_options, const std::string& fallback_name)
9 : Node(modulo_utils::parsing::parse_node_name(node_options, fallback_name), node_options),
10 ComponentInterface(std::make_shared<node_interfaces::NodeInterfaces<ALL_RCLCPP_NODE_INTERFACES>>(
11 Node::get_node_base_interface(), Node::get_node_clock_interface(), Node::get_node_graph_interface(),
12 Node::get_node_logging_interface(), Node::get_node_parameters_interface(),
13 Node::get_node_services_interface(), Node::get_node_time_source_interface(),
14 Node::get_node_timers_interface(), Node::get_node_topics_interface(),
15 Node::get_node_type_descriptions_interface(), Node::get_node_waitables_interface())),
16 started_(false) {
17 this->add_predicate("is_finished", false);
18 this->add_predicate("in_error_state", false);
19}
20
22 if (this->execute_thread_.joinable()) {
23 this->execute_thread_.join();
24 }
25}
26
27void Component::step() {
28 try {
29 this->evaluate_periodic_callbacks();
30 this->on_step_callback();
31 this->publish_outputs();
32 this->publish_predicates();
33 } catch (const std::exception& ex) {
34 RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to execute step function: " << ex.what());
35 this->raise_error();
36 }
37}
38
40 if (this->started_) {
41 RCLCPP_ERROR(this->get_logger(), "Failed to start execution thread: Thread has already been started.");
42 return;
43 }
44 this->started_ = true;
45 this->execute_thread_ = std::thread([this]() { this->on_execute(); });
46}
47
48void Component::on_execute() {
49 try {
50 if (!this->on_execute_callback()) {
51 this->raise_error();
52 return;
53 }
54 } catch (const std::exception& ex) {
55 RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to run the execute function: " << ex.what());
56 this->raise_error();
57 return;
58 }
59 RCLCPP_DEBUG(this->get_logger(), "Execution finished, setting 'is_finished' predicate to true.");
60 this->set_predicate("is_finished", true);
61}
62
64 return true;
65}
66
67std::shared_ptr<state_representation::ParameterInterface> Component::get_parameter(const std::string& name) const {
69}
70
73 this->set_predicate("in_error_state", true);
74 this->finalize_interfaces();
75}
76}// namespace modulo_components
std::shared_ptr< state_representation::ParameterInterface > get_parameter(const std::string &name) const
Get a parameter by name.
Definition Component.cpp:67
virtual ~Component()
Destructor that joins the thread if necessary.
Definition Component.cpp:21
virtual bool on_execute_callback()
Execute the component logic. To be redefined in derived classes.
Definition Component.cpp:63
void raise_error() override
Definition Component.cpp:71
Component(const rclcpp::NodeOptions &node_options, const std::string &fallback_name="Component")
Constructor from node options.
Definition Component.cpp:8
void execute()
Start the execution thread.
Definition Component.cpp:39
Base interface class for modulo components to wrap a ROS Node with custom behaviour.
std::shared_ptr< state_representation::ParameterInterface > get_parameter(const std::string &name) const
Get a parameter by name.
T get_parameter_value(const std::string &name) const
Get a parameter value by name.
virtual void on_step_callback()
Steps to execute periodically. To be redefined by derived Component classes.
void set_predicate(const std::string &predicate_name, bool predicate_value)
Set the value of the predicate given as parameter, if the predicate is not found does not do anything...
virtual void raise_error()
Notify an error in the component.
void add_predicate(const std::string &predicate_name, bool predicate_value)
Add a predicate to the map of predicates.
Modulo components.
Definition Component.hpp:9
Modulo Core communication module for handling messages on publication and subscription interfaces.