Modulo 4.2.2
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}
19
20void Component::step() {
21 try {
22 this->evaluate_periodic_callbacks();
23 this->on_step_callback();
24 this->publish_outputs();
25 this->publish_predicates();
26 } catch (const std::exception& ex) {
27 RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to execute step function: " << ex.what());
28 this->raise_error();
29 }
30}
31
33 if (this->started_) {
34 RCLCPP_ERROR(this->get_logger(), "Failed to start execution thread: Thread has already been started.");
35 return;
36 }
37 this->started_ = true;
38 this->execute_thread_ = std::thread([this]() { this->on_execute(); });
39}
40
41void Component::on_execute() {
42 try {
43 if (!this->on_execute_callback()) {
44 this->raise_error();
45 return;
46 }
47 } catch (const std::exception& ex) {
48 RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to run the execute function: " << ex.what());
49 this->raise_error();
50 return;
51 }
52 RCLCPP_DEBUG(this->get_logger(), "Execution finished, setting 'is_finished' predicate to true.");
53 this->set_predicate("is_finished", true);
54}
55
57 return true;
58}
59
60std::shared_ptr<state_representation::ParameterInterface> Component::get_parameter(const std::string& name) const {
62}
63}// namespace modulo_components
std::shared_ptr< state_representation::ParameterInterface > get_parameter(const std::string &name) const
Get a parameter by name.
Definition Component.cpp:60
virtual bool on_execute_callback()
Execute the component logic. To be redefined in derived classes.
Definition Component.cpp:56
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:32
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()
Put the component in error state by setting the 'in_error_state' predicate to true.
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.