Modulo 4.2.2
Loading...
Searching...
No Matches
LifecycleComponent.hpp
1#pragma once
2
3#include <lifecycle_msgs/msg/state.hpp>
4#include <rclcpp_lifecycle/lifecycle_node.hpp>
5
6#include "modulo_components/ComponentInterface.hpp"
7
8namespace modulo_components {
9
29class LifecycleComponent : public rclcpp_lifecycle::LifecycleNode, public ComponentInterface {
30public:
31 friend class LifecycleComponentPublicInterface;
32
38 explicit LifecycleComponent(
39 const rclcpp::NodeOptions& node_options, const std::string& fallback_name = "LifecycleComponent");
40
44 virtual ~LifecycleComponent() = default;
45
46protected:
50 [[nodiscard]] std::shared_ptr<state_representation::ParameterInterface> get_parameter(const std::string& name) const;
51
58 virtual bool on_configure_callback();
59
66 virtual bool on_cleanup_callback();
67
74 virtual bool on_activate_callback();
75
82 virtual bool on_deactivate_callback();
83
90 virtual bool on_shutdown_callback();
91
98 virtual bool on_error_callback();
99
109 template<typename DataT>
110 void add_output(
111 const std::string& signal_name, const std::shared_ptr<DataT>& data, const std::string& default_topic = "",
112 bool fixed_topic = false, bool publish_on_step = true);
113
117 rclcpp_lifecycle::State get_lifecycle_state() const;
118
119private:
130 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
131 on_configure(const rclcpp_lifecycle::State& previous_state) override;
132
138 bool handle_configure();
139
149 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
150 on_cleanup(const rclcpp_lifecycle::State& previous_state) override;
151
157 bool handle_cleanup();
158
169 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
170 on_activate(const rclcpp_lifecycle::State& previous_state) override;
171
177 bool handle_activate();
178
188 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
189 on_deactivate(const rclcpp_lifecycle::State& previous_state) override;
190
196 bool handle_deactivate();
197
207 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
208 on_shutdown(const rclcpp_lifecycle::State& previous_state) override;
209
215 bool handle_shutdown();
216
227 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
228 on_error(const rclcpp_lifecycle::State& previous_state) override;
229
235 bool handle_error();
236
241 void step() override;
242
247 bool configure_outputs();
248
253 bool activate_outputs();
254
259 bool deactivate_outputs();
260
264 bool clear_signals();
265
266 // TODO hide ROS methods
275 using rclcpp_lifecycle::LifecycleNode::get_parameter;
276
277 std::map<
278 std::string,
279 std::function<std::shared_ptr<modulo_core::communication::PublisherInterface>(const std::string& topic_name)>>
280 custom_output_configuration_callables_;
281};
282
283template<typename DataT>
285 const std::string& signal_name, const std::shared_ptr<DataT>& data, const std::string& default_topic,
286 bool fixed_topic, bool publish_on_step) {
287 if (this->get_lifecycle_state().id() != lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED
288 && this->get_lifecycle_state().id() != lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE) {
290 this->get_logger(), *this->get_clock(), 1000,
291 "Adding output in state " << this->get_lifecycle_state().label() << " is not allowed.");
292 return;
293 }
294 try {
297
298 auto parsed_signal_name = this->create_output(
299 PublisherType::LIFECYCLE_PUBLISHER, signal_name, data, default_topic, fixed_topic, publish_on_step);
300
301 auto message_pair = this->outputs_.at(parsed_signal_name)->get_message_pair();
302 if (message_pair->get_type() == modulo_core::communication::MessageType::CUSTOM_MESSAGE) {
304 this->custom_output_configuration_callables_.insert_or_assign(
305 parsed_signal_name, [this, message_pair](const std::string& topic_name) {
306 auto publisher = this->create_publisher<DataT>(topic_name, this->get_qos());
307 return std::make_shared<PublisherHandler<rclcpp_lifecycle::LifecyclePublisher<DataT>, DataT>>(
308 PublisherType::LIFECYCLE_PUBLISHER, publisher)
309 ->create_publisher_interface(message_pair);
310 });
311 }
312 }
314 RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to add output '" << signal_name << "': " << ex.what());
315 }
316}
317
318// TODO, if we raise error we need to manually call the lifecycle change state callback,
319// call callback function that this service calls
320}// namespace modulo_components
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.
A wrapper for rclcpp_lifecycle::LifecycleNode to simplify application composition through unified com...
virtual bool on_shutdown_callback()
Steps to execute when shutting down the component.
virtual bool on_deactivate_callback()
Steps to execute when deactivating the component.
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 an output signal of the component.
std::shared_ptr< state_representation::ParameterInterface > get_parameter(const std::string &name) const
Get a parameter by name.
virtual bool on_activate_callback()
Steps to execute when activating the component.
virtual bool on_cleanup_callback()
Steps to execute when cleaning up the component.
virtual ~LifecycleComponent()=default
Virtual default destructor.
virtual bool on_error_callback()
Steps to execute when handling errors.
virtual bool on_configure_callback()
Steps to execute when configuring the component.
rclcpp_lifecycle::State get_lifecycle_state() const
Get the current lifecycle state of the component.
The PublisherHandler handles different types of ROS publishers to activate, deactivate and publish da...
An exception class to notify errors when adding a signal.
Modulo components.
Definition Component.hpp:9
PublisherType
Enum of supported ROS publisher types for the PublisherInterface.