Modulo 4.2.2
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() = default;
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
70private:
74 void step() override;
75
80 void on_execute();
81
82 // TODO hide ROS methods
91 using rclcpp::Node::get_parameter;
92
93 std::thread execute_thread_;
94 bool started_;
95};
96
97template<typename DataT>
99 const std::string& signal_name, const std::shared_ptr<DataT>& data, const std::string& default_topic,
100 bool fixed_topic, bool publish_on_step) {
101 using namespace modulo_core::communication;
102 try {
103 auto parsed_signal_name =
104 this->create_output(PublisherType::PUBLISHER, signal_name, data, default_topic, fixed_topic, publish_on_step);
105 auto topic_name = this->get_parameter_value<std::string>(parsed_signal_name + "_topic");
107 this->get_logger(), "Adding output '" << parsed_signal_name << "' with topic name '" << topic_name << "'.");
108 auto message_pair = this->outputs_.at(parsed_signal_name)->get_message_pair();
109 switch (message_pair->get_type()) {
110 case MessageType::BOOL: {
112 this->outputs_.at(parsed_signal_name) =
113 std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Bool>, std_msgs::msg::Bool>>(
114 PublisherType::PUBLISHER, publisher)
115 ->create_publisher_interface(message_pair);
116 break;
117 }
118 case MessageType::FLOAT64: {
120 this->outputs_.at(parsed_signal_name) =
121 std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Float64>, std_msgs::msg::Float64>>(
122 PublisherType::PUBLISHER, publisher)
123 ->create_publisher_interface(message_pair);
124 break;
125 }
126 case MessageType::FLOAT64_MULTI_ARRAY: {
128 this->outputs_.at(parsed_signal_name) = std::make_shared<PublisherHandler<
129 rclcpp::Publisher<std_msgs::msg::Float64MultiArray>, std_msgs::msg::Float64MultiArray>>(
130 PublisherType::PUBLISHER, publisher)
131 ->create_publisher_interface(message_pair);
132 break;
133 }
134 case MessageType::INT32: {
136 this->outputs_.at(parsed_signal_name) =
137 std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::Int32>, std_msgs::msg::Int32>>(
138 PublisherType::PUBLISHER, publisher)
139 ->create_publisher_interface(message_pair);
140 break;
141 }
142 case MessageType::STRING: {
144 this->outputs_.at(parsed_signal_name) =
145 std::make_shared<PublisherHandler<rclcpp::Publisher<std_msgs::msg::String>, std_msgs::msg::String>>(
146 PublisherType::PUBLISHER, publisher)
147 ->create_publisher_interface(message_pair);
148 break;
149 }
150 case MessageType::ENCODED_STATE: {
152 this->outputs_.at(parsed_signal_name) =
153 std::make_shared<PublisherHandler<rclcpp::Publisher<modulo_core::EncodedState>, modulo_core::EncodedState>>(
154 PublisherType::PUBLISHER, publisher)
155 ->create_publisher_interface(message_pair);
156 break;
157 }
158 case MessageType::CUSTOM_MESSAGE: {
161 this->outputs_.at(parsed_signal_name) =
162 std::make_shared<PublisherHandler<rclcpp::Publisher<DataT>, DataT>>(PublisherType::PUBLISHER, publisher)
163 ->create_publisher_interface(message_pair);
164 }
165 break;
166 }
167 }
168 } catch (const std::exception& ex) {
169 RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to add output '" << signal_name << "': " << ex.what());
170 }
171}
172}// 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:60
virtual ~Component()=default
Virtual default destructor.
virtual bool on_execute_callback()
Execute the component logic. To be redefined in derived classes.
Definition Component.cpp:56
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.
Definition Component.hpp:98
void execute()
Start the execution thread.
Definition Component.cpp:32
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.
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.