67class ControllerManager :
public rclcpp::Node
70 static constexpr bool kWaitForAllResources =
false;
71 static constexpr auto kInfiniteTimeout = 0;
74 std::unique_ptr<hardware_interface::ResourceManager> resource_manager,
75 std::shared_ptr<rclcpp::Executor> executor,
76 const std::string & manager_node_name =
"controller_manager",
77 const std::string & node_namespace =
"",
78 const rclcpp::NodeOptions & options = get_cm_node_options());
81 std::shared_ptr<rclcpp::Executor> executor,
82 const std::string & manager_node_name =
"controller_manager",
83 const std::string & node_namespace =
"",
84 const rclcpp::NodeOptions & options = get_cm_node_options());
87 std::shared_ptr<rclcpp::Executor> executor,
const std::string & urdf,
88 bool activate_all_hw_components,
const std::string & manager_node_name =
"controller_manager",
89 const std::string & node_namespace =
"",
90 const rclcpp::NodeOptions & options = get_cm_node_options());
92 virtual ~ControllerManager();
100 void robot_description_callback(
const std_msgs::msg::String & msg);
102 void init_resource_manager(
const std::string & robot_description);
104 controller_interface::ControllerInterfaceBaseSharedPtr load_controller(
105 const std::string & controller_name,
const std::string & controller_type);
113 controller_interface::ControllerInterfaceBaseSharedPtr load_controller(
114 const std::string & controller_name);
116 controller_interface::return_type unload_controller(
const std::string & controller_name);
118 controller_interface::return_type cleanup_controller(
const std::string & controller_name);
120 std::vector<ControllerSpec> get_loaded_controllers()
const;
123 typename T,
typename std::enable_if<
124 std::is_convertible<T *, controller_interface::ControllerInterfaceBase *>::value,
125 T>::type * =
nullptr>
126 controller_interface::ControllerInterfaceBaseSharedPtr add_controller(
127 std::shared_ptr<T> controller,
const std::string & controller_name,
128 const std::string & controller_type)
131 controller_spec.c = controller;
132 controller_spec.info.
name = controller_name;
133 controller_spec.info.
type = controller_type;
134 controller_spec.last_update_cycle_time = std::make_shared<rclcpp::Time>(0);
135 return add_controller_impl(controller_spec);
138 controller_interface::ControllerInterfaceBaseSharedPtr add_controller(
141 return add_controller_impl(controller_spec);
164 bool activate_asap = kWaitForAllResources,
165 const rclcpp::Duration & timeout = rclcpp::Duration::from_nanoseconds(kInfiniteTimeout));
180 const rclcpp::Duration & timeout, std::string & message);
190 void read(
const rclcpp::Time & time,
const rclcpp::Duration & period);
200 controller_interface::return_type
update(
201 const rclcpp::Time & time,
const rclcpp::Duration & period);
211 void write(
const rclcpp::Time & time,
const rclcpp::Duration & period);
229 return resource_manager_ && resource_manager_->are_components_initialized();
255 void init_services();
257 controller_interface::ControllerInterfaceBaseSharedPtr add_controller_impl(
260 void manage_switch();
271 const std::vector<ControllerSpec> & rt_controller_list,
272 const std::vector<std::string> & controllers_to_deactivate);
283 const std::vector<std::string> & chained_mode_switch_list,
bool to_chained_mode);
295 const std::vector<ControllerSpec> & rt_controller_list,
296 const std::vector<std::string> & controllers_to_activate,
int strictness);
298 void list_controllers_srv_cb(
299 const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Request> request,
300 std::shared_ptr<controller_manager_msgs::srv::ListControllers::Response> response);
302 void list_hardware_interfaces_srv_cb(
303 const std::shared_ptr<controller_manager_msgs::srv::ListHardwareInterfaces::Request> request,
304 std::shared_ptr<controller_manager_msgs::srv::ListHardwareInterfaces::Response> response);
306 void load_controller_service_cb(
307 const std::shared_ptr<controller_manager_msgs::srv::LoadController::Request> request,
308 std::shared_ptr<controller_manager_msgs::srv::LoadController::Response> response);
310 void configure_controller_service_cb(
311 const std::shared_ptr<controller_manager_msgs::srv::ConfigureController::Request> request,
312 std::shared_ptr<controller_manager_msgs::srv::ConfigureController::Response> response);
314 void reload_controller_libraries_service_cb(
315 const std::shared_ptr<controller_manager_msgs::srv::ReloadControllerLibraries::Request> request,
316 std::shared_ptr<controller_manager_msgs::srv::ReloadControllerLibraries::Response> response);
318 void switch_controller_service_cb(
319 const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Request> request,
320 std::shared_ptr<controller_manager_msgs::srv::SwitchController::Response> response);
322 void unload_controller_service_cb(
323 const std::shared_ptr<controller_manager_msgs::srv::UnloadController::Request> request,
324 std::shared_ptr<controller_manager_msgs::srv::UnloadController::Response> response);
326 void cleanup_controller_service_cb(
327 const std::shared_ptr<controller_manager_msgs::srv::CleanupController::Request> request,
328 std::shared_ptr<controller_manager_msgs::srv::CleanupController::Response> response);
330 void list_controller_types_srv_cb(
331 const std::shared_ptr<controller_manager_msgs::srv::ListControllerTypes::Request> request,
332 std::shared_ptr<controller_manager_msgs::srv::ListControllerTypes::Response> response);
334 void list_hardware_components_srv_cb(
335 const std::shared_ptr<controller_manager_msgs::srv::ListHardwareComponents::Request> request,
336 std::shared_ptr<controller_manager_msgs::srv::ListHardwareComponents::Response> response);
338 void set_hardware_component_state_srv_cb(
339 const std::shared_ptr<controller_manager_msgs::srv::SetHardwareComponentState::Request> request,
340 std::shared_ptr<controller_manager_msgs::srv::SetHardwareComponentState::Response> response);
343 unsigned int update_loop_counter_ = 0;
344 unsigned int update_rate_;
345 std::vector<std::vector<std::string>> chained_controllers_configuration_;
347 std::unique_ptr<hardware_interface::ResourceManager> resource_manager_ =
nullptr;
350 std::vector<std::string> get_controller_names();
351 std::pair<std::string, std::string> split_command_interface(
352 const std::string & command_interface);
355 void init_controller_manager();
362 void initialize_parameters();
370 void init_robot_description_callback();
377 void set_initial_hardware_components_state();
384 controller_interface::return_type cleanup_controller(
398 void clear_requests();
410 void perform_hardware_command_mode_change(
411 const std::vector<ControllerSpec> & rt_controller_list,
412 const std::vector<std::string> & activate_controllers_list,
413 const std::vector<std::string> & deactivate_controllers_list,
414 const std::string & rt_cycle_name);
422 void propagate_deactivation_of_chained_mode(
const std::vector<ControllerSpec> & controllers);
452 controller_interface::return_type check_following_controllers_for_activate(
453 const std::vector<ControllerSpec> & controllers,
int strictness,
454 const ControllersListIterator controller_it, std::vector<std::string> & visited_controllers,
455 std::string & message);
477 controller_interface::return_type check_preceding_controllers_for_deactivate(
478 const std::vector<ControllerSpec> & controllers,
int strictness,
479 const ControllersListIterator controller_it, std::string & message);
490 controller_interface::return_type check_fallback_controllers_state_pre_activation(
491 const std::vector<ControllerSpec> & controllers,
const ControllersListIterator controller_it,
492 std::string & message);
503 controller_interface::return_type check_for_interfaces_availability_to_activate(
504 const std::vector<ControllerSpec> & controllers,
const std::vector<std::string> activation_list,
505 const std::vector<std::string> deactivation_list, std::string & message);
526 void update_list_with_controller_chain(
527 const std::string & ctrl_name, std::vector<std::string>::iterator controller_iterator,
528 bool append_to_controller);
536 void build_controllers_topology_info(
const std::vector<ControllerSpec> & controllers);
543 void publish_activity();
545 void controller_activity_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
547 void hardware_components_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
549 void controller_manager_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
558 rclcpp::NodeOptions determine_controller_node_options(
const ControllerSpec & controller)
const;
565 void cleanup_controller_exported_interfaces(
const ControllerSpec & controller);
567 std::shared_ptr<controller_manager::ParamListener> cm_param_listener_;
568 std::shared_ptr<controller_manager::Params> params_;
570 std::atomic_bool overruns_print_warnings_{
true};
571 diagnostic_updater::Updater diagnostics_updater_;
573 std::shared_ptr<rclcpp::Executor> executor_;
575 std::shared_ptr<pluginlib::ClassLoader<controller_interface::ControllerInterface>> loader_;
576 std::shared_ptr<pluginlib::ClassLoader<controller_interface::ChainableControllerInterface>>
584 rclcpp::CallbackGroup::SharedPtr best_effort_callback_group_;
597 class RTControllerListWrapper
608 std::vector<ControllerSpec> & update_and_get_used_by_rt_list();
618 std::vector<ControllerSpec> & get_unused_list(
619 const std::lock_guard<std::recursive_mutex> & guard);
627 const std::vector<ControllerSpec> & get_updated_list(
628 const std::lock_guard<std::recursive_mutex> & guard)
const;
636 void switch_updated_list(
const std::lock_guard<std::recursive_mutex> & guard);
642 void set_on_switch_callback(std::function<
void()> callback);
646 mutable std::recursive_mutex controllers_lock_;
655 int get_other_list(
int index)
const;
657 void wait_until_rt_not_using(
658 int index, std::chrono::microseconds sleep_delay = std::chrono::microseconds(200))
const;
660 std::vector<ControllerSpec> controllers_lists_[2];
662 int updated_controllers_index_ = 0;
664 int used_by_realtime_controllers_index_ = -1;
666 std::function<void()> on_switch_callback_ =
nullptr;
670 rclcpp::Clock::SharedPtr trigger_clock_ =
nullptr;
671 std::unique_ptr<rclcpp::PreShutdownCallbackHandle> preshutdown_cb_handle_{
nullptr};
672 RTControllerListWrapper rt_controllers_wrapper_;
673 std::unordered_map<std::string, ControllerChainSpec> controller_chain_spec_;
674 std::vector<std::string> ordered_controllers_names_;
677 std::mutex services_lock_;
678 rclcpp::Publisher<controller_manager_msgs::msg::ControllerManagerActivity>::SharedPtr
679 controller_manager_activity_publisher_;
680 rclcpp::Service<controller_manager_msgs::srv::ListControllers>::SharedPtr
681 list_controllers_service_;
682 rclcpp::Service<controller_manager_msgs::srv::ListControllerTypes>::SharedPtr
683 list_controller_types_service_;
684 rclcpp::Service<controller_manager_msgs::srv::LoadController>::SharedPtr load_controller_service_;
685 rclcpp::Service<controller_manager_msgs::srv::ConfigureController>::SharedPtr
686 configure_controller_service_;
687 rclcpp::Service<controller_manager_msgs::srv::ReloadControllerLibraries>::SharedPtr
688 reload_controller_libraries_service_;
689 rclcpp::Service<controller_manager_msgs::srv::SwitchController>::SharedPtr
690 switch_controller_service_;
691 rclcpp::Service<controller_manager_msgs::srv::UnloadController>::SharedPtr
692 unload_controller_service_;
693 rclcpp::Service<controller_manager_msgs::srv::CleanupController>::SharedPtr
694 cleanup_controller_service_;
696 rclcpp::Service<controller_manager_msgs::srv::ListHardwareComponents>::SharedPtr
697 list_hardware_components_service_;
698 rclcpp::Service<controller_manager_msgs::srv::ListHardwareInterfaces>::SharedPtr
699 list_hardware_interfaces_service_;
700 rclcpp::Service<controller_manager_msgs::srv::SetHardwareComponentState>::SharedPtr
701 set_hardware_component_state_service_;
703 std::map<std::string, std::vector<std::string>> controller_chained_reference_interfaces_cache_;
704 std::map<std::string, std::vector<std::string>> controller_chained_state_interfaces_cache_;
706 rclcpp::NodeOptions cm_node_options_;
707 std::string robot_description_;
708 rclcpp::Subscription<std_msgs::msg::String>::SharedPtr robot_description_subscription_;
709 rclcpp::TimerBase::SharedPtr robot_description_notification_timer_;
711 bool activate_all_hw_components_ =
false;
713 struct ControllerManagerExecutionTime
715 double read_time = 0.0;
716 double update_time = 0.0;
717 double write_time = 0.0;
718 double switch_time = 0.0;
719 double total_time = 0.0;
720 double switch_chained_mode_time = 0.0;
721 double switch_perform_mode_time = 0.0;
722 double deactivation_time = 0.0;
723 double activation_time = 0.0;
726 ControllerManagerExecutionTime execution_time_;
728 controller_manager::MovingAverageStatistics periodicity_stats_;
736 activate_asap =
false;
737 ready_to_switch =
false;
740 std::atomic_bool do_switch{
false};
741 std::atomic_bool ready_to_switch{
false};
745 std::atomic_bool activate_asap{
false};
746 std::chrono::nanoseconds timeout;
749 std::condition_variable cv;
752 bool skip_cycle(
const controller_manager::ControllerSpec & spec)
const
754 const std::string controller_name = spec.info.
name;
755 return ros2_control::has_item(activate_request, controller_name) ||
756 ros2_control::has_item(deactivate_request, controller_name) ||
757 ros2_control::has_item(to_chained_mode_request, controller_name) ||
758 ros2_control::has_item(from_chained_mode_request, controller_name);
762 std::vector<std::string> activate_request;
763 std::vector<std::string> deactivate_request;
764 std::vector<std::string> to_chained_mode_request;
765 std::vector<std::string> from_chained_mode_request;
766 std::vector<std::string> activate_command_interface_request;
767 std::vector<std::string> deactivate_command_interface_request;
770 SwitchParams switch_params_;
772 struct RTBufferVariables
776 deactivate_controllers_list.reserve(1000);
777 activate_controllers_using_interfaces_list.reserve(1000);
778 fallback_controllers_list.reserve(1000);
779 interfaces_to_start.reserve(1000);
780 interfaces_to_stop.reserve(1000);
781 concatenated_string.reserve(5000);
784 const std::string & get_concatenated_string(
785 const std::vector<std::string> & strings,
bool clear_string =
true)
789 concatenated_string.clear();
791 for (
const auto & str : strings)
793 concatenated_string.append(str);
794 concatenated_string.append(
" ");
796 return concatenated_string;
799 std::vector<std::string> deactivate_controllers_list;
800 std::vector<std::string> activate_controllers_using_interfaces_list;
801 std::vector<std::string> fallback_controllers_list;
802 std::vector<std::string> interfaces_to_start;
803 std::vector<std::string> interfaces_to_stop;
804 std::string concatenated_string;
806 RTBufferVariables rt_buffer_;