71class ControllerManager :
public rclcpp::Node
74 static constexpr bool kWaitForAllResources =
false;
75 static constexpr auto kInfiniteTimeout = 0;
78 std::unique_ptr<hardware_interface::ResourceManager> resource_manager,
79 std::shared_ptr<rclcpp::Executor> executor,
80 const std::string & manager_node_name =
"controller_manager",
81 const std::string & node_namespace =
"",
82 const rclcpp::NodeOptions & options = get_cm_node_options());
85 std::shared_ptr<rclcpp::Executor> executor,
86 const std::string & manager_node_name =
"controller_manager",
87 const std::string & node_namespace =
"",
88 const rclcpp::NodeOptions & options = get_cm_node_options());
91 std::shared_ptr<rclcpp::Executor> executor,
const std::string & urdf,
92 bool activate_all_hw_components,
const std::string & manager_node_name =
"controller_manager",
93 const std::string & node_namespace =
"",
94 const rclcpp::NodeOptions & options = get_cm_node_options());
96 virtual ~ControllerManager();
104 void robot_description_callback(
const std_msgs::msg::String & msg);
106 void init_resource_manager(
const std::string & robot_description);
108 controller_interface::ControllerInterfaceBaseSharedPtr load_controller(
109 const std::string & controller_name,
const std::string & controller_type);
117 controller_interface::ControllerInterfaceBaseSharedPtr load_controller(
118 const std::string & controller_name);
120 controller_interface::return_type unload_controller(
const std::string & controller_name);
122 controller_interface::return_type cleanup_controller(
const std::string & controller_name);
124 std::vector<ControllerSpec> get_loaded_controllers()
const;
127 typename T,
typename std::enable_if<
128 std::is_convertible<T *, controller_interface::ControllerInterfaceBase *>::value,
129 T>::type * =
nullptr>
130 controller_interface::ControllerInterfaceBaseSharedPtr add_controller(
131 std::shared_ptr<T> controller,
const std::string & controller_name,
132 const std::string & controller_type)
135 controller_spec.c = controller;
136 controller_spec.info.
name = controller_name;
137 controller_spec.info.
type = controller_type;
138 controller_spec.last_update_cycle_time = std::make_shared<rclcpp::Time>(0);
139 return add_controller_impl(controller_spec);
142 controller_interface::ControllerInterfaceBaseSharedPtr add_controller(
145 return add_controller_impl(controller_spec);
168 bool activate_asap = kWaitForAllResources,
169 const rclcpp::Duration & timeout = rclcpp::Duration::from_nanoseconds(kInfiniteTimeout));
184 const rclcpp::Duration & timeout, std::string & message);
194 void read(
const rclcpp::Time & time,
const rclcpp::Duration & period);
204 controller_interface::return_type
update(
205 const rclcpp::Time & time,
const rclcpp::Duration & period);
215 void write(
const rclcpp::Time & time,
const rclcpp::Duration & period);
233 return resource_manager_ && resource_manager_->are_components_initialized();
259 void init_services();
261 controller_interface::ControllerInterfaceBaseSharedPtr add_controller_impl(
264 void manage_switch();
275 const std::vector<ControllerSpec> & rt_controller_list,
276 const std::vector<std::string> & controllers_to_deactivate);
287 const std::vector<std::string> & chained_mode_switch_list,
bool to_chained_mode);
299 const std::vector<ControllerSpec> & rt_controller_list,
300 const std::vector<std::string> & controllers_to_activate,
int strictness);
302 void list_controllers_srv_cb(
303 const std::shared_ptr<controller_manager_msgs::srv::ListControllers::Request> request,
304 std::shared_ptr<controller_manager_msgs::srv::ListControllers::Response> response);
306 void list_hardware_interfaces_srv_cb(
307 const std::shared_ptr<controller_manager_msgs::srv::ListHardwareInterfaces::Request> request,
308 std::shared_ptr<controller_manager_msgs::srv::ListHardwareInterfaces::Response> response);
310 void load_controller_service_cb(
311 const std::shared_ptr<controller_manager_msgs::srv::LoadController::Request> request,
312 std::shared_ptr<controller_manager_msgs::srv::LoadController::Response> response);
314 void configure_controller_service_cb(
315 const std::shared_ptr<controller_manager_msgs::srv::ConfigureController::Request> request,
316 std::shared_ptr<controller_manager_msgs::srv::ConfigureController::Response> response);
318 void reload_controller_libraries_service_cb(
319 const std::shared_ptr<controller_manager_msgs::srv::ReloadControllerLibraries::Request> request,
320 std::shared_ptr<controller_manager_msgs::srv::ReloadControllerLibraries::Response> response);
322 void switch_controller_service_cb(
323 const std::shared_ptr<controller_manager_msgs::srv::SwitchController::Request> request,
324 std::shared_ptr<controller_manager_msgs::srv::SwitchController::Response> response);
326 void unload_controller_service_cb(
327 const std::shared_ptr<controller_manager_msgs::srv::UnloadController::Request> request,
328 std::shared_ptr<controller_manager_msgs::srv::UnloadController::Response> response);
330 void cleanup_controller_service_cb(
331 const std::shared_ptr<controller_manager_msgs::srv::CleanupController::Request> request,
332 std::shared_ptr<controller_manager_msgs::srv::CleanupController::Response> response);
334 void list_controller_types_srv_cb(
335 const std::shared_ptr<controller_manager_msgs::srv::ListControllerTypes::Request> request,
336 std::shared_ptr<controller_manager_msgs::srv::ListControllerTypes::Response> response);
338 void list_hardware_components_srv_cb(
339 const std::shared_ptr<controller_manager_msgs::srv::ListHardwareComponents::Request> request,
340 std::shared_ptr<controller_manager_msgs::srv::ListHardwareComponents::Response> response);
342 void set_hardware_component_state_srv_cb(
343 const std::shared_ptr<controller_manager_msgs::srv::SetHardwareComponentState::Request> request,
344 std::shared_ptr<controller_manager_msgs::srv::SetHardwareComponentState::Response> response);
347 unsigned int update_loop_counter_ = 0;
348 unsigned int update_rate_;
349 std::vector<std::vector<std::string>> chained_controllers_configuration_;
351 std::unique_ptr<hardware_interface::ResourceManager> resource_manager_ =
nullptr;
354 std::vector<std::string> get_controller_names();
355 std::pair<std::string, std::string> split_command_interface(
356 const std::string & command_interface);
359 void init_controller_manager();
366 void initialize_parameters();
374 void init_robot_description_callback();
381 void set_initial_hardware_components_state();
388 controller_interface::return_type cleanup_controller(
402 void clear_requests();
414 void perform_hardware_command_mode_change(
415 const std::vector<ControllerSpec> & rt_controller_list,
416 const std::vector<std::string> & activate_controllers_list,
417 const std::vector<std::string> & deactivate_controllers_list,
418 const std::string & rt_cycle_name);
426 void propagate_deactivation_of_chained_mode(
const std::vector<ControllerSpec> & controllers);
456 controller_interface::return_type check_following_controllers_for_activate(
457 const std::vector<ControllerSpec> & controllers,
int strictness,
458 const ControllersListIterator controller_it, std::vector<std::string> & visited_controllers,
459 std::string & message);
481 controller_interface::return_type check_preceding_controllers_for_deactivate(
482 const std::vector<ControllerSpec> & controllers,
int strictness,
483 const ControllersListIterator controller_it, std::string & message);
494 controller_interface::return_type check_fallback_controllers_state_pre_activation(
495 const std::vector<ControllerSpec> & controllers,
const ControllersListIterator controller_it,
496 std::string & message);
507 controller_interface::return_type check_for_interfaces_availability_to_activate(
508 const std::vector<ControllerSpec> & controllers,
const std::vector<std::string> activation_list,
509 const std::vector<std::string> deactivation_list, std::string & message);
530 void update_list_with_controller_chain(
531 const std::string & ctrl_name, std::vector<std::string>::iterator controller_iterator,
532 bool append_to_controller);
540 void build_controllers_topology_info(
const std::vector<ControllerSpec> & controllers);
547 void publish_activity();
549 void controller_activity_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
551 void hardware_components_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
553 void controller_manager_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
562 rclcpp::NodeOptions determine_controller_node_options(
const ControllerSpec & controller)
const;
569 void cleanup_controller_exported_interfaces(
const ControllerSpec & controller);
571 std::shared_ptr<controller_manager::ParamListener> cm_param_listener_;
572 std::shared_ptr<controller_manager::Params> params_;
574 std::atomic_bool overruns_print_warnings_{
true};
575 diagnostic_updater::Updater diagnostics_updater_;
577 std::shared_ptr<rclcpp::Executor> executor_;
579 std::shared_ptr<pluginlib::ClassLoader<controller_interface::ControllerInterface>> loader_;
580 std::shared_ptr<pluginlib::ClassLoader<controller_interface::ChainableControllerInterface>>
588 rclcpp::CallbackGroup::SharedPtr best_effort_callback_group_;
601 class RTControllerListWrapper
607 using controllers_lock_type = realtime_tools::prio_inherit_recursive_mutex;
609 using controllers_lock_type = std::recursive_mutex;
617 std::vector<ControllerSpec> & update_and_get_used_by_rt_list();
627 std::vector<ControllerSpec> & get_unused_list(
628 const std::lock_guard<controllers_lock_type> & guard);
636 const std::vector<ControllerSpec> & get_updated_list(
637 const std::lock_guard<controllers_lock_type> & guard)
const;
645 void switch_updated_list(
const std::lock_guard<controllers_lock_type> & guard);
651 void set_on_switch_callback(std::function<
void()> callback);
655 mutable controllers_lock_type controllers_lock_;
664 int get_other_list(
int index)
const;
666 void wait_until_rt_not_using(
667 int index, std::chrono::microseconds sleep_delay = std::chrono::microseconds(200))
const;
669 std::vector<ControllerSpec> controllers_lists_[2];
671 int updated_controllers_index_ = 0;
673 int used_by_realtime_controllers_index_ = -1;
675 std::function<void()> on_switch_callback_ =
nullptr;
679 rclcpp::Clock::SharedPtr trigger_clock_ =
nullptr;
680 std::unique_ptr<rclcpp::PreShutdownCallbackHandle> preshutdown_cb_handle_{
nullptr};
681 RTControllerListWrapper rt_controllers_wrapper_;
682 std::unordered_map<std::string, ControllerChainSpec> controller_chain_spec_;
683 std::vector<std::string> ordered_controllers_names_;
686 std::mutex services_lock_;
687 rclcpp::Publisher<controller_manager_msgs::msg::ControllerManagerActivity>::SharedPtr
688 controller_manager_activity_publisher_;
689 rclcpp::Service<controller_manager_msgs::srv::ListControllers>::SharedPtr
690 list_controllers_service_;
691 rclcpp::Service<controller_manager_msgs::srv::ListControllerTypes>::SharedPtr
692 list_controller_types_service_;
693 rclcpp::Service<controller_manager_msgs::srv::LoadController>::SharedPtr load_controller_service_;
694 rclcpp::Service<controller_manager_msgs::srv::ConfigureController>::SharedPtr
695 configure_controller_service_;
696 rclcpp::Service<controller_manager_msgs::srv::ReloadControllerLibraries>::SharedPtr
697 reload_controller_libraries_service_;
698 rclcpp::Service<controller_manager_msgs::srv::SwitchController>::SharedPtr
699 switch_controller_service_;
700 rclcpp::Service<controller_manager_msgs::srv::UnloadController>::SharedPtr
701 unload_controller_service_;
702 rclcpp::Service<controller_manager_msgs::srv::CleanupController>::SharedPtr
703 cleanup_controller_service_;
705 rclcpp::Service<controller_manager_msgs::srv::ListHardwareComponents>::SharedPtr
706 list_hardware_components_service_;
707 rclcpp::Service<controller_manager_msgs::srv::ListHardwareInterfaces>::SharedPtr
708 list_hardware_interfaces_service_;
709 rclcpp::Service<controller_manager_msgs::srv::SetHardwareComponentState>::SharedPtr
710 set_hardware_component_state_service_;
712 std::map<std::string, std::vector<std::string>> controller_chained_reference_interfaces_cache_;
713 std::map<std::string, std::vector<std::string>> controller_chained_state_interfaces_cache_;
715 std::string robot_description_;
716 rclcpp::Subscription<std_msgs::msg::String>::SharedPtr robot_description_subscription_;
717 rclcpp::TimerBase::SharedPtr robot_description_notification_timer_;
719 bool activate_all_hw_components_ =
false;
721 struct ControllerManagerExecutionTime
723 double read_time = 0.0;
724 double update_time = 0.0;
725 double write_time = 0.0;
726 double switch_time = 0.0;
727 double total_time = 0.0;
728 double switch_chained_mode_time = 0.0;
729 double switch_perform_mode_time = 0.0;
730 double deactivation_time = 0.0;
731 double activation_time = 0.0;
734 ControllerManagerExecutionTime execution_time_;
736 controller_manager::MovingAverageStatistics periodicity_stats_;
744 activate_asap =
false;
745 ready_to_switch =
false;
748 std::atomic_bool do_switch{
false};
749 std::atomic_bool ready_to_switch{
false};
753 std::atomic_bool activate_asap{
false};
754 std::chrono::nanoseconds timeout;
757 std::condition_variable cv;
760 bool skip_cycle(
const controller_manager::ControllerSpec & spec)
const
762 const std::string controller_name = spec.info.
name;
763 return ros2_control::has_item(activate_request, controller_name) ||
764 ros2_control::has_item(deactivate_request, controller_name) ||
765 ros2_control::has_item(to_chained_mode_request, controller_name) ||
766 ros2_control::has_item(from_chained_mode_request, controller_name);
770 std::vector<std::string> activate_request;
771 std::vector<std::string> deactivate_request;
772 std::vector<std::string> to_chained_mode_request;
773 std::vector<std::string> from_chained_mode_request;
774 std::vector<std::string> activate_command_interface_request;
775 std::vector<std::string> deactivate_command_interface_request;
778 SwitchParams switch_params_;
780 struct RTBufferVariables
784 deactivate_controllers_list.reserve(1000);
785 activate_controllers_using_interfaces_list.reserve(1000);
786 fallback_controllers_list.reserve(1000);
787 interfaces_to_start.reserve(1000);
788 interfaces_to_stop.reserve(1000);
789 concatenated_string.reserve(5000);
792 const std::string & get_concatenated_string(
793 const std::vector<std::string> & strings,
bool clear_string =
true)
797 concatenated_string.clear();
799 for (
const auto & str : strings)
801 concatenated_string.append(str);
802 concatenated_string.append(
" ");
804 return concatenated_string;
807 std::vector<std::string> deactivate_controllers_list;
808 std::vector<std::string> activate_controllers_using_interfaces_list;
809 std::vector<std::string> fallback_controllers_list;
810 std::vector<std::string> interfaces_to_start;
811 std::vector<std::string> interfaces_to_stop;
812 std::string concatenated_string;
814 RTBufferVariables rt_buffer_;