Modulo 5.0.0
Loading...
Searching...
No Matches
Component.hpp
1#pragma once
2
3#include <thread>
4
5#include <rclcpp/node.hpp>
6
7#include "modulo_components/ComponentInterface.hpp"
8
10
23class Component : public rclcpp::Node, public ComponentInterface {
24public:
25 friend class ComponentPublicInterface;
26
32 explicit Component(const rclcpp::NodeOptions& node_options, const std::string& fallback_name = "Component");
33
37 virtual ~Component();
38
39protected:
43 [[nodiscard]] std::shared_ptr<state_representation::ParameterInterface> get_parameter(const std::string& name) const;
44
48 void execute();
49
54 virtual bool on_execute_callback();
55
65 template<typename DataT>
66 void add_output(
67 const std::string& signal_name, const std::shared_ptr<DataT>& data, const std::string& default_topic = "",
68 bool fixed_topic = false, bool publish_on_step = true);
69
73 void raise_error() override;
74
75private:
79 void step() override;
80
85 void on_execute();
86
87 // TODO hide ROS methods
97 using rclcpp::Node::get_parameter;
98
99 std::thread execute_thread_;
100 bool started_;
101};
102
103template<typename DataT>
105 const std::string& signal_name, const std::shared_ptr<DataT>& data, const std::string& default_topic,
106 bool fixed_topic, bool publish_on_step) {
107 using namespace modulo_core::communication;
108 try {
109 auto parsed_signal_name =
110 this->create_output(PublisherType::PUBLISHER, signal_name, data, default_topic, fixed_topic, publish_on_step);
111 auto topic_name = this->get_parameter_value<std::string>(parsed_signal_name + "_topic");
113 this->get_logger(), "Adding output '" << parsed_signal_name << "' with topic name '" << topic_name << "'.");
114 auto message_pair = this->outputs_.at(parsed_signal_name)->get_message_pair();
115 switch (message_pair->get_type()) {
116 case MessageType::BOOL: {
118 this->outputs_.at(parsed_signal_name) =
119 std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Bool>, std_msgs::msg::Bool>>(
120 PublisherType::PUBLISHER, publisher)
121 ->create_publisher_interface(message_pair);
122 break;
123 }
124 case MessageType::FLOAT64: {
126 this->outputs_.at(parsed_signal_name) =
127 std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Float64>, std_msgs::msg::Float64>>(
128 PublisherType::PUBLISHER, publisher)
129 ->create_publisher_interface(message_pair);
130 break;
131 }
132 case MessageType::FLOAT64_MULTI_ARRAY: {
134 this->outputs_.at(parsed_signal_name) = std::make_shared<PublisherHandler<
135 rclcpp::Publisher<std_msgs::msg::Float64MultiArray>, std_msgs::msg::Float64MultiArray>>(
136 PublisherType::PUBLISHER, publisher)
137 ->create_publisher_interface(message_pair);
138 break;
139 }
140 case MessageType::INT32: {
142 this->outputs_.at(parsed_signal_name) =
143 std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Int32>, std_msgs::msg::Int32>>(
144 PublisherType::PUBLISHER, publisher)
145 ->create_publisher_interface(message_pair);
146 break;
147 }
148 case MessageType::STRING: {
150 this->outputs_.at(parsed_signal_name) =
151 std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::String>, std_msgs::msg::String>>(
152 PublisherType::PUBLISHER, publisher)
153 ->create_publisher_interface(message_pair);
154 break;
155 }
156 case MessageType::ENCODED_STATE: {
158 this->outputs_.at(parsed_signal_name) =
159 std::make_shared<PublisherHandler<rclcpp::Publisher<modulo_core::EncodedState>, modulo_core::EncodedState>>(
160 PublisherType::PUBLISHER, publisher)
161 ->create_publisher_interface(message_pair);
162 break;
163 }
164 case MessageType::CUSTOM_MESSAGE: {
167 this->outputs_.at(parsed_signal_name) =
168 std::make_shared<PublisherHandler<rclcpp::Publisher<DataT>, DataT>>(PublisherType::PUBLISHER, publisher)
169 ->create_publisher_interface(message_pair);
170 }
171 break;
172 }
173 }
174 } catch (const std::exception& ex) {
175 RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to add output '" << signal_name << "': " << ex.what());
176 }
177}
178}// namespace modulo_components
A wrapper for rclcpp::Node to simplify application composition through unified component interfaces.
Definition Component.hpp:23
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 add_output(const std::string &signal_name, const std::shared_ptr< DataT > &data, const std::string &default_topic="", bool fixed_topic=false, bool publish_on_step=true)
Add and configure an output signal of the component.
void raise_error() override
Definition Component.cpp:71
void execute()
Start the execution thread.
Definition Component.cpp:39
Base interface class for modulo components to wrap a ROS Node with custom behaviour.
void publish_outputs()
Helper function to publish all output signals.
std::map< std::string, std::shared_ptr< modulo_core::communication::PublisherInterface > > outputs_
Map of outputs.
std::shared_ptr< state_representation::ParameterInterface > get_parameter(const std::string &name) const
Get a parameter by name.
void finalize_interfaces()
Finalize all interfaces.
T get_parameter_value(const std::string &name) const
Get a parameter value by name.
void publish_predicates()
Helper function to publish all predicates.
std::string create_output(modulo_core::communication::PublisherType publisher_type, const std::string &signal_name, const std::shared_ptr< DataT > &data, const std::string &default_topic, bool fixed_topic, bool publish_on_step)
Helper function to parse the signal name and add an unconfigured PublisherInterface to the map of out...
rclcpp::QoS get_qos() const
Getter of the Quality of Service attribute.
std::map< std::string, std::shared_ptr< modulo_core::communication::SubscriptionInterface > > inputs_
Map of inputs.
void evaluate_periodic_callbacks()
Helper function to evaluate all periodic function callbacks.
std::map< std::string, bool > periodic_outputs_
Map of outputs with periodic publishing flag.
The PublisherHandler handles different types of ROS publishers to activate, deactivate and publish da...
Modulo components.
Definition Component.hpp:9
Modulo Core communication module for handling messages on publication and subscription interfaces.