ros2_control - rolling
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
59#if !defined(_WIN32)
60#include "realtime_tools/mutex.hpp"
61#endif
62
63namespace controller_manager
64{
65class ParamListener;
66struct Params;
67using ControllersListIterator = std::vector<controller_manager::ControllerSpec>::const_iterator;
68
69rclcpp::NodeOptions get_cm_node_options();
70
71class ControllerManager : public rclcpp::Node
72{
73public:
74 static constexpr bool kWaitForAllResources = false;
75 static constexpr auto kInfiniteTimeout = 0;
76
77 ControllerManager(
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());
83
84 ControllerManager(
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());
89
90 ControllerManager(
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());
95
96 virtual ~ControllerManager();
97
99
103
104 void robot_description_callback(const std_msgs::msg::String & msg);
105
106 void init_resource_manager(const std::string & robot_description);
107
108 controller_interface::ControllerInterfaceBaseSharedPtr load_controller(
109 const std::string & controller_name, const std::string & controller_type);
110
112
117 controller_interface::ControllerInterfaceBaseSharedPtr load_controller(
118 const std::string & controller_name);
119
120 controller_interface::return_type unload_controller(const std::string & controller_name);
121
122 controller_interface::return_type cleanup_controller(const std::string & controller_name);
123
124 std::vector<ControllerSpec> get_loaded_controllers() const;
125
126 template <
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)
133 {
134 ControllerSpec controller_spec;
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);
140 }
141
142 controller_interface::ControllerInterfaceBaseSharedPtr add_controller(
143 const ControllerSpec & controller_spec)
144 {
145 return add_controller_impl(controller_spec);
146 }
147
149
154 controller_interface::return_type configure_controller(const std::string & controller_name);
155
157
165 controller_interface::return_type switch_controller(
166 const std::vector<std::string> & activate_controllers,
167 const std::vector<std::string> & deactivate_controllers, int strictness,
168 bool activate_asap = kWaitForAllResources,
169 const rclcpp::Duration & timeout = rclcpp::Duration::from_nanoseconds(kInfiniteTimeout));
170
172
181 controller_interface::return_type switch_controller_cb(
182 const std::vector<std::string> & activate_controllers,
183 const std::vector<std::string> & deactivate_controllers, int strictness, bool activate_asap,
184 const rclcpp::Duration & timeout, std::string & message);
185
187
194 void read(const rclcpp::Time & time, const rclcpp::Duration & period);
195
197
204 controller_interface::return_type update(
205 const rclcpp::Time & time, const rclcpp::Duration & period);
206
208
215 void write(const rclcpp::Time & time, const rclcpp::Duration & period);
216
218
222 // TODO(anyone): Due to issues with the MultiThreadedExecutor, this control loop does not rely on
223 // the executor (see issue #260).
224 // rclcpp::CallbackGroup::SharedPtr deterministic_callback_group_;
225
227
232 {
233 return resource_manager_ && resource_manager_->are_components_initialized();
234 }
235
237
243 unsigned int get_update_rate() const;
244
246
256 rclcpp::Clock::SharedPtr get_trigger_clock() const;
257
258protected:
259 void init_services();
260
261 controller_interface::ControllerInterfaceBaseSharedPtr add_controller_impl(
262 const ControllerSpec & controller);
263
264 void manage_switch();
265
267
275 const std::vector<ControllerSpec> & rt_controller_list,
276 const std::vector<std::string> & controllers_to_deactivate);
277
287 const std::vector<std::string> & chained_mode_switch_list, bool to_chained_mode);
288
290
299 const std::vector<ControllerSpec> & rt_controller_list,
300 const std::vector<std::string> & controllers_to_activate, int strictness);
301
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);
305
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);
309
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);
313
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);
317
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);
321
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);
325
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);
329
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);
333
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);
337
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);
341
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);
345
346 // Per controller update rate support
347 unsigned int update_loop_counter_ = 0;
348 unsigned int update_rate_;
349 std::vector<std::vector<std::string>> chained_controllers_configuration_;
350
351 std::unique_ptr<hardware_interface::ResourceManager> resource_manager_ = nullptr;
352
353private:
354 std::vector<std::string> get_controller_names();
355 std::pair<std::string, std::string> split_command_interface(
356 const std::string & command_interface);
357
359 void init_controller_manager();
360
362
366 void initialize_parameters();
367
369
374 void init_robot_description_callback();
375
377
381 void set_initial_hardware_components_state();
382
388 controller_interface::return_type cleanup_controller(
389 const controller_manager::ControllerSpec & controller);
390
396 void shutdown_controller(const controller_manager::ControllerSpec & controller) const;
397
402 void clear_requests();
403
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);
419
426 void propagate_deactivation_of_chained_mode(const std::vector<ControllerSpec> & controllers);
427
430
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);
460
462
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);
484
487
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);
497
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);
510
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);
533
540 void build_controllers_topology_info(const std::vector<ControllerSpec> & controllers);
541
547 void publish_activity();
548
549 void controller_activity_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
550
551 void hardware_components_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
552
553 void controller_manager_diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
554
562 rclcpp::NodeOptions determine_controller_node_options(const ControllerSpec & controller) const;
563
569 void cleanup_controller_exported_interfaces(const ControllerSpec & controller);
570
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_;
576
577 std::shared_ptr<rclcpp::Executor> executor_;
578
579 std::shared_ptr<pluginlib::ClassLoader<controller_interface::ControllerInterface>> loader_;
580 std::shared_ptr<pluginlib::ClassLoader<controller_interface::ChainableControllerInterface>>
581 chainable_loader_;
582
584
588 rclcpp::CallbackGroup::SharedPtr best_effort_callback_group_;
589
601 class RTControllerListWrapper
602 {
603 // *INDENT-OFF*
604 public:
605 // *INDENT-ON*
606#if !defined(_WIN32)
607 using controllers_lock_type = realtime_tools::prio_inherit_recursive_mutex;
608#else
609 using controllers_lock_type = std::recursive_mutex;
610#endif
612
617 std::vector<ControllerSpec> & update_and_get_used_by_rt_list();
618
627 std::vector<ControllerSpec> & get_unused_list(
628 const std::lock_guard<controllers_lock_type> & guard);
629
631
636 const std::vector<ControllerSpec> & get_updated_list(
637 const std::lock_guard<controllers_lock_type> & guard) const;
638
645 void switch_updated_list(const std::lock_guard<controllers_lock_type> & guard);
646
648
651 void set_on_switch_callback(std::function<void()> callback);
652
653 // Mutex protecting the controllers list
654 // must be acquired before using any list other than the "used by rt"
655 mutable controllers_lock_type controllers_lock_;
656
657 // *INDENT-OFF*
658 private:
659 // *INDENT-ON*
661
664 int get_other_list(int index) const;
665
666 void wait_until_rt_not_using(
667 int index, std::chrono::microseconds sleep_delay = std::chrono::microseconds(200)) const;
668
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;
676 };
677
678 bool use_sim_time_;
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_;
704
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_;
711
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_;
714
715 std::string robot_description_;
716 rclcpp::Subscription<std_msgs::msg::String>::SharedPtr robot_description_subscription_;
717 rclcpp::TimerBase::SharedPtr robot_description_notification_timer_;
718
719 bool activate_all_hw_components_ = false;
720
721 struct ControllerManagerExecutionTime
722 {
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;
732 };
733
734 ControllerManagerExecutionTime execution_time_;
735
736 controller_manager::MovingAverageStatistics periodicity_stats_;
737
738 struct SwitchParams
739 {
740 void reset()
741 {
742 do_switch = false;
743 strictness = 0;
744 activate_asap = false;
745 ready_to_switch = false;
746 }
747
748 std::atomic_bool do_switch{false};
749 std::atomic_bool ready_to_switch{false};
750
751 // Switch options
752 int strictness;
753 std::atomic_bool activate_asap{false};
754 std::chrono::nanoseconds timeout;
755
756 // conditional variable and mutex to wait for the switch to complete
757 std::condition_variable cv;
758 std::mutex mutex;
759
760 bool skip_cycle(const controller_manager::ControllerSpec & spec) const
761 {
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);
767 }
768
769 // The controllers list to activate and deactivate
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;
776 };
777
778 SwitchParams switch_params_;
779
780 struct RTBufferVariables
781 {
782 RTBufferVariables()
783 {
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);
790 }
791
792 const std::string & get_concatenated_string(
793 const std::vector<std::string> & strings, bool clear_string = true)
794 {
795 if (clear_string)
796 {
797 concatenated_string.clear();
798 }
799 for (const auto & str : strings)
800 {
801 concatenated_string.append(str);
802 concatenated_string.append(" ");
803 }
804 return concatenated_string;
805 }
806
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;
813 };
814 RTBufferVariables rt_buffer_;
815};
816
817} // namespace controller_manager
818
819#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:3280
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:1858
unsigned int get_update_rate() const
Update rate of the main control loop in the controller manager.
Definition controller_manager.cpp:3801
controller_interface::return_type update(const rclcpp::Time &time, const rclcpp::Duration &period)
Run update on controllers.
Definition controller_manager.cpp:3376
bool shutdown_controllers()
Shutdown all controllers in the controller manager.
Definition controller_manager.cpp:646
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:1868
void switch_chained_mode(const std::vector< std::string > &chained_mode_switch_list, bool to_chained_mode)
Definition controller_manager.cpp:2596
rclcpp::Clock::SharedPtr get_trigger_clock() const
Get the trigger clock of the controller manager.
Definition controller_manager.cpp:3803
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:2535
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:2642
void write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write values from command interfaces.
Definition controller_manager.cpp:3603
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:1617
bool is_resource_manager_initialized() const
Deterministic (real-time safe) callback group, e.g., update function.
Definition controller_manager.hpp:231
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