ros2_control - kilted
Loading...
Searching...
No Matches
controller_manager.hpp
1// Copyright 2020 Open Source Robotics Foundation, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef CONTROLLER_MANAGER__CONTROLLER_MANAGER_HPP_
16#define CONTROLLER_MANAGER__CONTROLLER_MANAGER_HPP_
17
18#include <atomic>
19#include <chrono>
20#include <condition_variable>
21#include <functional>
22#include <map>
23#include <memory>
24#include <mutex>
25#include <string>
26#include <type_traits>
27#include <unordered_map>
28#include <utility>
29#include <vector>
30
31#include "controller_interface/chainable_controller_interface.hpp"
32#include "controller_interface/controller_interface.hpp"
33#include "controller_interface/controller_interface_base.hpp"
34
35#include "controller_manager/controller_spec.hpp"
36#include "controller_manager_msgs/msg/controller_manager_activity.hpp"
37#include "controller_manager_msgs/srv/cleanup_controller.hpp"
38#include "controller_manager_msgs/srv/configure_controller.hpp"
39#include "controller_manager_msgs/srv/list_controller_types.hpp"
40#include "controller_manager_msgs/srv/list_controllers.hpp"
41#include "controller_manager_msgs/srv/list_hardware_components.hpp"
42#include "controller_manager_msgs/srv/list_hardware_interfaces.hpp"
43#include "controller_manager_msgs/srv/load_controller.hpp"
44#include "controller_manager_msgs/srv/reload_controller_libraries.hpp"
45#include "controller_manager_msgs/srv/set_hardware_component_state.hpp"
46#include "controller_manager_msgs/srv/switch_controller.hpp"
47#include "controller_manager_msgs/srv/unload_controller.hpp"
48
49#include "diagnostic_updater/diagnostic_updater.hpp"
50#include "hardware_interface/helpers.hpp"
51#include "hardware_interface/resource_manager.hpp"
52
53#include "pluginlib/class_loader.hpp"
54
55#include "rclcpp/executor.hpp"
56#include "rclcpp/node.hpp"
57#include "std_msgs/msg/string.hpp"
58
59namespace controller_manager
60{
61class ParamListener;
62struct Params;
63using ControllersListIterator = std::vector<controller_manager::ControllerSpec>::const_iterator;
64
65rclcpp::NodeOptions get_cm_node_options();
66
67class ControllerManager : public rclcpp::Node
68{
69public:
70 static constexpr bool kWaitForAllResources = false;
71 static constexpr auto kInfiniteTimeout = 0;
72
73 ControllerManager(
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());
79
80 ControllerManager(
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());
85
86 ControllerManager(
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());
91
92 virtual ~ControllerManager();
93
95
99
100 void robot_description_callback(const std_msgs::msg::String & msg);
101
102 void init_resource_manager(const std::string & robot_description);
103
104 controller_interface::ControllerInterfaceBaseSharedPtr load_controller(
105 const std::string & controller_name, const std::string & controller_type);
106
108
113 controller_interface::ControllerInterfaceBaseSharedPtr load_controller(
114 const std::string & controller_name);
115
116 controller_interface::return_type unload_controller(const std::string & controller_name);
117
118 controller_interface::return_type cleanup_controller(const std::string & controller_name);
119
120 std::vector<ControllerSpec> get_loaded_controllers() const;
121
122 template <
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)
129 {
130 ControllerSpec controller_spec;
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);
136 }
137
138 controller_interface::ControllerInterfaceBaseSharedPtr add_controller(
139 const ControllerSpec & controller_spec)
140 {
141 return add_controller_impl(controller_spec);
142 }
143
145
150 controller_interface::return_type configure_controller(const std::string & controller_name);
151
153
161 controller_interface::return_type switch_controller(
162 const std::vector<std::string> & activate_controllers,
163 const std::vector<std::string> & deactivate_controllers, int strictness,
164 bool activate_asap = kWaitForAllResources,
165 const rclcpp::Duration & timeout = rclcpp::Duration::from_nanoseconds(kInfiniteTimeout));
166
168
177 controller_interface::return_type switch_controller_cb(
178 const std::vector<std::string> & activate_controllers,
179 const std::vector<std::string> & deactivate_controllers, int strictness, bool activate_asap,
180 const rclcpp::Duration & timeout, std::string & message);
181
183
190 void read(const rclcpp::Time & time, const rclcpp::Duration & period);
191
193
200 controller_interface::return_type update(
201 const rclcpp::Time & time, const rclcpp::Duration & period);
202
204
211 void write(const rclcpp::Time & time, const rclcpp::Duration & period);
212
214
218 // TODO(anyone): Due to issues with the MultiThreadedExecutor, this control loop does not rely on
219 // the executor (see issue #260).
220 // rclcpp::CallbackGroup::SharedPtr deterministic_callback_group_;
221
223
228 {
229 return resource_manager_ && resource_manager_->are_components_initialized();
230 }
231
233
239 unsigned int get_update_rate() const;
240
242
252 rclcpp::Clock::SharedPtr get_trigger_clock() const;
253
254protected:
255 void init_services();
256
257 controller_interface::ControllerInterfaceBaseSharedPtr add_controller_impl(
258 const ControllerSpec & controller);
259
260 void manage_switch();
261
263
271 const std::vector<ControllerSpec> & rt_controller_list,
272 const std::vector<std::string> & controllers_to_deactivate);
273
283 const std::vector<std::string> & chained_mode_switch_list, bool to_chained_mode);
284
286
295 const std::vector<ControllerSpec> & rt_controller_list,
296 const std::vector<std::string> & controllers_to_activate, int strictness);
297
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);
301
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);
305
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);
309
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);
313
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);
317
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);
321
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);
325
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);
329
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);
333
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);
337
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);
341
342 // Per controller update rate support
343 unsigned int update_loop_counter_ = 0;
344 unsigned int update_rate_;
345 std::vector<std::vector<std::string>> chained_controllers_configuration_;
346
347 std::unique_ptr<hardware_interface::ResourceManager> resource_manager_ = nullptr;
348
349private:
350 std::vector<std::string> get_controller_names();
351 std::pair<std::string, std::string> split_command_interface(
352 const std::string & command_interface);
353
355 void init_controller_manager();
356
358
362 void initialize_parameters();
363
365
370 void init_robot_description_callback();
371
373
377 void set_initial_hardware_components_state();
378
384 controller_interface::return_type cleanup_controller(
385 const controller_manager::ControllerSpec & controller);
386
392 void shutdown_controller(const controller_manager::ControllerSpec & controller) const;
393
398 void clear_requests();
399
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);
415
422 void propagate_deactivation_of_chained_mode(const std::vector<ControllerSpec> & controllers);
423
426
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);
456
458
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);
480
483
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);
493
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);
506
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);
529
536 void build_controllers_topology_info(const std::vector<ControllerSpec> & controllers);
537
543 void publish_activity();
544
545 void controller_activity_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
546
547 void hardware_components_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
548
549 void controller_manager_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
550
558 rclcpp::NodeOptions determine_controller_node_options(const ControllerSpec & controller) const;
559
565 void cleanup_controller_exported_interfaces(const ControllerSpec & controller);
566
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_;
572
573 std::shared_ptr<rclcpp::Executor> executor_;
574
575 std::shared_ptr<pluginlib::ClassLoader<controller_interface::ControllerInterface>> loader_;
576 std::shared_ptr<pluginlib::ClassLoader<controller_interface::ChainableControllerInterface>>
577 chainable_loader_;
578
580
584 rclcpp::CallbackGroup::SharedPtr best_effort_callback_group_;
585
597 class RTControllerListWrapper
598 {
599 // *INDENT-OFF*
600 public:
601 // *INDENT-ON*
603
608 std::vector<ControllerSpec> & update_and_get_used_by_rt_list();
609
618 std::vector<ControllerSpec> & get_unused_list(
619 const std::lock_guard<std::recursive_mutex> & guard);
620
622
627 const std::vector<ControllerSpec> & get_updated_list(
628 const std::lock_guard<std::recursive_mutex> & guard) const;
629
636 void switch_updated_list(const std::lock_guard<std::recursive_mutex> & guard);
637
639
642 void set_on_switch_callback(std::function<void()> callback);
643
644 // Mutex protecting the controllers list
645 // must be acquired before using any list other than the "used by rt"
646 mutable std::recursive_mutex controllers_lock_;
647
648 // *INDENT-OFF*
649 private:
650 // *INDENT-ON*
652
655 int get_other_list(int index) const;
656
657 void wait_until_rt_not_using(
658 int index, std::chrono::microseconds sleep_delay = std::chrono::microseconds(200)) const;
659
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;
667 };
668
669 bool use_sim_time_;
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_;
695
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_;
702
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_;
705
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_;
710
711 bool activate_all_hw_components_ = false;
712
713 struct ControllerManagerExecutionTime
714 {
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;
724 };
725
726 ControllerManagerExecutionTime execution_time_;
727
728 controller_manager::MovingAverageStatistics periodicity_stats_;
729
730 struct SwitchParams
731 {
732 void reset()
733 {
734 do_switch = false;
735 strictness = 0;
736 activate_asap = false;
737 ready_to_switch = false;
738 }
739
740 std::atomic_bool do_switch{false};
741 std::atomic_bool ready_to_switch{false};
742
743 // Switch options
744 int strictness;
745 std::atomic_bool activate_asap{false};
746 std::chrono::nanoseconds timeout;
747
748 // conditional variable and mutex to wait for the switch to complete
749 std::condition_variable cv;
750 std::mutex mutex;
751
752 bool skip_cycle(const controller_manager::ControllerSpec & spec) const
753 {
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);
759 }
760
761 // The controllers list to activate and deactivate
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;
768 };
769
770 SwitchParams switch_params_;
771
772 struct RTBufferVariables
773 {
774 RTBufferVariables()
775 {
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);
782 }
783
784 const std::string & get_concatenated_string(
785 const std::vector<std::string> & strings, bool clear_string = true)
786 {
787 if (clear_string)
788 {
789 concatenated_string.clear();
790 }
791 for (const auto & str : strings)
792 {
793 concatenated_string.append(str);
794 concatenated_string.append(" ");
795 }
796 return concatenated_string;
797 }
798
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;
805 };
806 RTBufferVariables rt_buffer_;
807};
808
809} // namespace controller_manager
810
811#endif // CONTROLLER_MANAGER__CONTROLLER_MANAGER_HPP_
void read(const rclcpp::Time &time, const rclcpp::Duration &period)
Read values to state interfaces.
Definition controller_manager.cpp:3322
controller_interface::return_type switch_controller(const std::vector< std::string > &activate_controllers, const std::vector< std::string > &deactivate_controllers, int strictness, bool activate_asap=kWaitForAllResources, const rclcpp::Duration &timeout=rclcpp::Duration::from_nanoseconds(kInfiniteTimeout))
switch_controller Deactivates some controllers and activates others.
Definition controller_manager.cpp:1882
unsigned int get_update_rate() const
Update rate of the main control loop in the controller manager.
Definition controller_manager.cpp:3848
controller_interface::return_type update(const rclcpp::Time &time, const rclcpp::Duration &period)
Run update on controllers.
Definition controller_manager.cpp:3419
bool shutdown_controllers()
Shutdown all controllers in the controller manager.
Definition controller_manager.cpp:652
controller_interface::return_type switch_controller_cb(const std::vector< std::string > &activate_controllers, const std::vector< std::string > &deactivate_controllers, int strictness, bool activate_asap, const rclcpp::Duration &timeout, std::string &message)
switch_controller Deactivates some controllers and activates others.
Definition controller_manager.cpp:1892
void switch_chained_mode(const std::vector< std::string > &chained_mode_switch_list, bool to_chained_mode)
Definition controller_manager.cpp:2625
rclcpp::Clock::SharedPtr get_trigger_clock() const
Get the trigger clock of the controller manager.
Definition controller_manager.cpp:3850
void deactivate_controllers(const std::vector< ControllerSpec > &rt_controller_list, const std::vector< std::string > &controllers_to_deactivate)
Deactivate chosen controllers from real-time controller list.
Definition controller_manager.cpp:2564
void activate_controllers(const std::vector< ControllerSpec > &rt_controller_list, const std::vector< std::string > &controllers_to_activate, int strictness)
Activate chosen controllers from real-time controller list.
Definition controller_manager.cpp:2671
void write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write values from command interfaces.
Definition controller_manager.cpp:3647
controller_interface::return_type configure_controller(const std::string &controller_name)
configure_controller Configure controller by name calling their "configure" method.
Definition controller_manager.cpp:1626
bool is_resource_manager_initialized() const
Deterministic (real-time safe) callback group, e.g., update function.
Definition controller_manager.hpp:227
Controller Specification.
Definition controller_spec.hpp:41
std::string type
Controller type.
Definition controller_info.hpp:33
std::string name
Controller name.
Definition controller_info.hpp:30