Modulo 5.0.0
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
122 void raise_error() override;
123
124private:
135 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
136 on_configure(const rclcpp_lifecycle::State& previous_state) override;
137
143 bool handle_configure();
144
154 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
155 on_cleanup(const rclcpp_lifecycle::State& previous_state) override;
156
162 bool handle_cleanup();
163
174 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
175 on_activate(const rclcpp_lifecycle::State& previous_state) override;
176
182 bool handle_activate();
183
193 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
194 on_deactivate(const rclcpp_lifecycle::State& previous_state) override;
195
201 bool handle_deactivate();
202
212 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
213 on_shutdown(const rclcpp_lifecycle::State& previous_state) override;
214
220 bool handle_shutdown();
221
232 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
233 on_error(const rclcpp_lifecycle::State& previous_state) override;
234
240 bool handle_error();
241
246 void step() override;
247
252 bool configure_outputs();
253
258 bool activate_outputs();
259
264 bool deactivate_outputs();
265
266 bool has_error_;
267
268 // TODO hide ROS methods
278 using rclcpp_lifecycle::LifecycleNode::get_parameter;
279
280 std::map<
281 std::string,
282 std::function<std::shared_ptr<modulo_core::communication::PublisherInterface>(const std::string& topic_name)>>
283 custom_output_configuration_callables_;
284};
285
286template<typename DataT>
288 const std::string& signal_name, const std::shared_ptr<DataT>& data, const std::string& default_topic,
289 bool fixed_topic, bool publish_on_step) {
290 if (this->get_lifecycle_state().id() != lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED
291 && this->get_lifecycle_state().id() != lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE) {
293 this->get_logger(), *this->get_clock(), 1000,
294 "Adding output in state " << this->get_lifecycle_state().label() << " is not allowed.");
295 return;
296 }
297 try {
300
301 auto parsed_signal_name = this->create_output(
302 PublisherType::LIFECYCLE_PUBLISHER, signal_name, data, default_topic, fixed_topic, publish_on_step);
303
304 auto message_pair = this->outputs_.at(parsed_signal_name)->get_message_pair();
305 if (message_pair->get_type() == modulo_core::communication::MessageType::CUSTOM_MESSAGE) {
307 this->custom_output_configuration_callables_.insert_or_assign(
308 parsed_signal_name, [this, message_pair](const std::string& topic_name) {
309 auto publisher = this->create_publisher<DataT>(topic_name, this->get_qos());
310 return std::make_shared<PublisherHandler<rclcpp_lifecycle::LifecyclePublisher<DataT>, DataT>>(
311 PublisherType::LIFECYCLE_PUBLISHER, publisher)
312 ->create_publisher_interface(message_pair);
313 });
314 }
315 }
317 RCLCPP_ERROR_STREAM(this->get_logger(), "Failed to add output '" << signal_name << "': " << ex.what());
318 }
319}
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.
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.
A wrapper for rclcpp_lifecycle::LifecycleNode to simplify application composition through unified com...
void raise_error() override
Trigger the shutdown and error transitions.
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.