2020#include < vector>
2121
2222#include " hardware_interface/types/lifecycle_state_names.hpp"
23- #include " lifecycle_msgs/msg/state.hpp"
2423
2524namespace controller_interface
2625{
2726return_type ControllerInterface::init (const std::string & controller_name)
2827{
29- node_ = std::make_shared<rclcpp::Node>(
30- controller_name, rclcpp::NodeOptions ()
31- .allow_undeclared_parameters (true )
32- .automatically_declare_parameters_from_overrides (true ));
28+ node_ = std::make_shared<rclcpp_lifecycle::LifecycleNode>(
29+ controller_name,
30+ rclcpp::NodeOptions ()
31+ .allow_undeclared_parameters (true )
32+ .automatically_declare_parameters_from_overrides (true ),
33+ false ); // disable LifecycleNode service interfaces
3334
34- return_type result = return_type::OK;
35- switch (on_init ())
35+ try
3636 {
37- case LifecycleNodeInterface::CallbackReturn::SUCCESS:
38- lifecycle_state_ = rclcpp_lifecycle::State (
39- lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED,
40- hardware_interface::lifecycle_state_names::UNCONFIGURED);
41- break ;
42- case LifecycleNodeInterface::CallbackReturn::ERROR:
43- case LifecycleNodeInterface::CallbackReturn::FAILURE:
44- result = return_type::ERROR;
45- break ;
37+ auto_declare<int >(" update_rate" , 0 );
4638 }
47- return result;
48- }
49-
50- const rclcpp_lifecycle::State & ControllerInterface::configure ()
51- {
52- if (lifecycle_state_.id () == lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED)
39+ catch (const std::exception & e)
5340 {
54- switch (on_configure (lifecycle_state_))
55- {
56- case LifecycleNodeInterface::CallbackReturn::SUCCESS:
57- lifecycle_state_ = rclcpp_lifecycle::State (
58- lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
59- hardware_interface::lifecycle_state_names::INACTIVE);
60- break ;
61- case LifecycleNodeInterface::CallbackReturn::ERROR:
62- on_error (lifecycle_state_);
63- lifecycle_state_ = rclcpp_lifecycle::State (
64- lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED,
65- hardware_interface::lifecycle_state_names::FINALIZED);
66- break ;
67- case LifecycleNodeInterface::CallbackReturn::FAILURE:
68- break ;
69- }
70-
71- if (node_->has_parameter (" update_rate" ))
72- {
73- update_rate_ = node_->get_parameter (" update_rate" ).as_int ();
74- }
41+ fprintf (stderr, " Exception thrown during init stage with message: %s \n " , e.what ());
42+ return return_type::ERROR;
7543 }
76- return lifecycle_state_;
77- }
7844
79- const rclcpp_lifecycle::State & ControllerInterface::cleanup ()
80- {
81- switch (on_cleanup (lifecycle_state_))
82- {
83- case LifecycleNodeInterface::CallbackReturn::SUCCESS:
84- lifecycle_state_ = rclcpp_lifecycle::State (
85- lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED,
86- hardware_interface::lifecycle_state_names::UNCONFIGURED);
87- break ;
88- case LifecycleNodeInterface::CallbackReturn::ERROR:
89- on_error (lifecycle_state_);
90- lifecycle_state_ = rclcpp_lifecycle::State (
91- lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED,
92- hardware_interface::lifecycle_state_names::FINALIZED);
93- break ;
94- case LifecycleNodeInterface::CallbackReturn::FAILURE:
95- break ;
96- }
97- return lifecycle_state_;
98- }
99- const rclcpp_lifecycle::State & ControllerInterface::deactivate ()
100- {
101- switch (on_deactivate (lifecycle_state_))
45+ switch (on_init ())
10246 {
10347 case LifecycleNodeInterface::CallbackReturn::SUCCESS:
104- lifecycle_state_ = rclcpp_lifecycle::State (
105- lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE,
106- hardware_interface::lifecycle_state_names::INACTIVE);
10748 break ;
10849 case LifecycleNodeInterface::CallbackReturn::ERROR:
109- on_error (lifecycle_state_);
110- lifecycle_state_ = rclcpp_lifecycle::State (
111- lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED,
112- hardware_interface::lifecycle_state_names::FINALIZED);
113- break ;
11450 case LifecycleNodeInterface::CallbackReturn::FAILURE:
115- break ;
51+ return return_type::ERROR ;
11652 }
117- return lifecycle_state_;
118- }
119- const rclcpp_lifecycle::State & ControllerInterface::activate ()
120- {
121- if (lifecycle_state_.id () == lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE)
122- {
123- switch (on_activate (lifecycle_state_))
124- {
125- case LifecycleNodeInterface::CallbackReturn::SUCCESS:
126- lifecycle_state_ = rclcpp_lifecycle::State (
127- lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE,
128- hardware_interface::lifecycle_state_names::ACTIVE);
129- break ;
130- case LifecycleNodeInterface::CallbackReturn::ERROR:
131- on_error (lifecycle_state_);
132- lifecycle_state_ = rclcpp_lifecycle::State (
133- lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED,
134- hardware_interface::lifecycle_state_names::FINALIZED);
135- break ;
136- case LifecycleNodeInterface::CallbackReturn::FAILURE:
137- break ;
138- }
139- }
140- return lifecycle_state_;
53+
54+ node_->register_on_configure (
55+ std::bind (&ControllerInterface::on_configure, this , std::placeholders::_1));
56+
57+ node_->register_on_cleanup (
58+ std::bind (&ControllerInterface::on_cleanup, this , std::placeholders::_1));
59+
60+ node_->register_on_activate (
61+ std::bind (&ControllerInterface::on_activate, this , std::placeholders::_1));
62+
63+ node_->register_on_deactivate (
64+ std::bind (&ControllerInterface::on_deactivate, this , std::placeholders::_1));
65+
66+ node_->register_on_shutdown (
67+ std::bind (&ControllerInterface::on_shutdown, this , std::placeholders::_1));
68+
69+ node_->register_on_error (std::bind (&ControllerInterface::on_error, this , std::placeholders::_1));
70+
71+ return return_type::OK;
14172}
14273
143- const rclcpp_lifecycle::State & ControllerInterface::shutdown ()
74+ const rclcpp_lifecycle::State & ControllerInterface::configure ()
14475{
145- switch (on_shutdown (lifecycle_state_))
146- {
147- case LifecycleNodeInterface::CallbackReturn::SUCCESS:
148- lifecycle_state_ = rclcpp_lifecycle::State (
149- lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED,
150- hardware_interface::lifecycle_state_names::FINALIZED);
151- break ;
152- case LifecycleNodeInterface::CallbackReturn::ERROR:
153- on_error (lifecycle_state_);
154- lifecycle_state_ = rclcpp_lifecycle::State (
155- lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED,
156- hardware_interface::lifecycle_state_names::FINALIZED);
157- break ;
158- case LifecycleNodeInterface::CallbackReturn::FAILURE:
159- break ;
160- }
161- return lifecycle_state_;
162- }
76+ update_rate_ = node_->get_parameter (" update_rate" ).as_int ();
16377
164- const rclcpp_lifecycle::State & ControllerInterface::get_state () const { return lifecycle_state_; }
78+ return node_->configure ();
79+ }
16580
16681void ControllerInterface::assign_interfaces (
16782 std::vector<hardware_interface::LoanedCommandInterface> && command_interfaces,
@@ -177,11 +92,16 @@ void ControllerInterface::release_interfaces()
17792 state_interfaces_.clear ();
17893}
17994
180- std::shared_ptr<rclcpp::Node> ControllerInterface::get_node ()
95+ const rclcpp_lifecycle::State & ControllerInterface::get_state () const
96+ {
97+ return node_->get_current_state ();
98+ }
99+
100+ std::shared_ptr<rclcpp_lifecycle::LifecycleNode> ControllerInterface::get_node ()
181101{
182102 if (!node_.get ())
183103 {
184- throw std::runtime_error (" Node hasn't been initialized yet!" );
104+ throw std::runtime_error (" Lifecycle node hasn't been initialized yet!" );
185105 }
186106 return node_;
187107}
0 commit comments