97 pluginlib::ClassLoader<ControllerHandleAllocator> loader_;
98 typedef std::map<std::string, controller_manager_msgs::msg::ControllerState> ControllersMap;
101 ControllersMap managed_controllers_;
103 ControllersMap active_controllers_;
105 typedef std::map<std::string, ControllerHandleAllocatorPtr> AllocatorsMap;
106 AllocatorsMap allocators_;
108 typedef std::map<std::string, moveit_controller_manager::MoveItControllerHandlePtr> HandleMap;
111 rclcpp::Time controllers_stamp_{ 0, 0, RCL_ROS_TIME };
116 std::mutex controllers_mutex_;
118 rclcpp::Node::SharedPtr node_;
119 rclcpp::Client<controller_manager_msgs::srv::ListControllers>::SharedPtr list_controllers_service_;
120 rclcpp::Client<controller_manager_msgs::srv::SwitchController>::SharedPtr switch_controller_service_;
123 std::unordered_map<std::string , std::vector<std::string> > dependency_map_;
130 static bool isActive(
const controller_manager_msgs::msg::ControllerState& s)
132 return s.state == std::string(
"active");
141 void discover(
bool force =
false)
144 if (!force && ((node_->now() - controllers_stamp_) < CONTROLLER_INFORMATION_VALIDITY_AGE))
149 controllers_stamp_ = node_->now();
151 auto request = std::make_shared<controller_manager_msgs::srv::ListControllers::Request>();
152 auto result_future = list_controllers_service_->async_send_request(request);
153 if (result_future.wait_for(std::chrono::duration<double>(SERVICE_CALL_TIMEOUT)) == std::future_status::timeout)
155 RCLCPP_WARN_STREAM(getLogger(),
"Failed to read controllers from "
156 << list_controllers_service_->get_service_name() <<
" within "
157 << SERVICE_CALL_TIMEOUT <<
" seconds");
161 managed_controllers_.clear();
162 active_controllers_.clear();
164 auto result = result_future.get();
170 for (
const controller_manager_msgs::msg::ControllerState& controller : result->controller)
173 if (isActive(controller))
176 auto& claimed_interfaces = active_controllers_.insert(std::make_pair(controller.name, controller))
177 .first->second.claimed_interfaces;
180 std::transform(claimed_interfaces.cbegin(), claimed_interfaces.cend(), claimed_interfaces.begin(),
181 [](
const std::string& claimed_interface) {
182 return parseJointNameFromResource(claimed_interface);
187 if (loader_.isClassAvailable(controller.type))
189 std::string absname = getAbsName(controller.name);
190 auto controller_it = managed_controllers_.insert(std::make_pair(absname, controller)).first;
192 auto& required_interfaces = controller_it->second.required_command_interfaces;
195 std::transform(required_interfaces.cbegin(), required_interfaces.cend(), required_interfaces.begin(),
196 [](
const std::string& required_interface) {
197 return parseJointNameFromResource(required_interface);
199 allocate(absname, controller_it->second);
210 void allocate(
const std::string& name,
const controller_manager_msgs::msg::ControllerState& controller)
212 if (handles_.find(name) == handles_.end())
214 const std::string& type = controller.type;
215 AllocatorsMap::iterator alloc_it = allocators_.find(type);
216 if (alloc_it == allocators_.end())
218 alloc_it = allocators_.insert(std::make_pair(type, loader_.createUniqueInstance(type))).first;
221 std::vector<std::string> resources;
223 for (
const auto& resource : controller.claimed_interfaces)
228 moveit_controller_manager::MoveItControllerHandlePtr handle =
229 alloc_it->second->alloc(node_, name, resources);
231 handles_.insert(std::make_pair(name, handle));
240 std::string getAbsName(
const std::string& name)
250 : loader_(
"moveit_ros_control_interface",
"moveit_ros_control_interface::ControllerHandleAllocator")
259 : ns_(ns), loader_(
"moveit_ros_control_interface",
"moveit_ros_control_interface::ControllerHandleAllocator")
261 RCLCPP_INFO_STREAM(getLogger(),
"Started moveit_ros_control_interface::Ros2ControlManager for namespace " << ns_);
264 void initialize(
const rclcpp::Node::SharedPtr& node)
override
269 if (!node_->has_parameter(
"ros_control_namespace"))
271 ns_ = node_->declare_parameter<std::string>(
"ros_control_namespace",
"/");
275 node_->get_parameter<std::string>(
"ros_control_namespace", ns_);
279 list_controllers_service_ = node_->create_client<controller_manager_msgs::srv::ListControllers>(
280 getAbsName(
"controller_manager/list_controllers"));
281 switch_controller_service_ = node_->create_client<controller_manager_msgs::srv::SwitchController>(
282 getAbsName(
"controller_manager/switch_controller"));
284 std::scoped_lock<std::mutex> lock(controllers_mutex_);
292 moveit_controller_manager::MoveItControllerHandlePtr
getControllerHandle(
const std::string& name)
override
294 std::scoped_lock<std::mutex> lock(controllers_mutex_);
295 HandleMap::iterator it = handles_.find(name);
296 if (it != handles_.end())
300 return moveit_controller_manager::MoveItControllerHandlePtr();
309 std::scoped_lock<std::mutex> lock(controllers_mutex_);
312 for (std::pair<const std::string, controller_manager_msgs::msg::ControllerState>& managed_controller :
313 managed_controllers_)
315 names.push_back(managed_controller.first);
325 std::scoped_lock<std::mutex> lock(controllers_mutex_);
328 for (std::pair<const std::string, controller_manager_msgs::msg::ControllerState>& managed_controller :
329 managed_controllers_)
331 if (isActive(managed_controller.second))
332 names.push_back(managed_controller.first);
343 std::scoped_lock<std::mutex> lock(controllers_mutex_);
344 ControllersMap::iterator it = managed_controllers_.find(name);
345 if (it != managed_controllers_.end())
347 for (
const auto& required_resource : it->second.required_command_interfaces)
349 joints.push_back(required_resource);
361 std::scoped_lock<std::mutex> lock(controllers_mutex_);
365 ControllersMap::iterator it = managed_controllers_.find(name);
366 if (it != managed_controllers_.end())
368 c.
active_ = isActive(it->second);
381 const std::vector<std::string>& deactivate_base)
override
384 std::vector<std::string> activate = activate_base;
385 std::vector<std::string> deactivate = deactivate_base;
386 for (
auto controllers : { &activate, &deactivate })
388 auto queue = *controllers;
389 while (!queue.empty())
391 auto controller = queue.back();
392 controller.erase(0, 1);
394 for (
const auto& dependency : dependency_map_[controller])
396 queue.push_back(dependency);
397 controllers->push_back(
"/" + dependency);
402 std::reverse(activate.begin(), activate.end());
404 std::scoped_lock<std::mutex> lock(controllers_mutex_);
425 typedef boost::bimap<boost::bimaps::unordered_multiset_of<std::string>, std::string> resources_bimap;
427 resources_bimap claimed_resources;
430 for (std::pair<const std::string, controller_manager_msgs::msg::ControllerState>& active_controller :
433 for (
const auto& resource : active_controller.second.claimed_interfaces)
435 claimed_resources.insert(resources_bimap::value_type(active_controller.second.name, resource));
439 auto request = std::make_shared<controller_manager_msgs::srv::SwitchController::Request>();
440 for (
const std::string& it : deactivate)
442 ControllersMap::iterator c = managed_controllers_.find(it);
443 if (c != managed_controllers_.end())
445 request->deactivate_controllers.push_back(c->second.name);
446 claimed_resources.left.erase(c->second.name);
452 for (
const std::string& it : activate)
454 ControllersMap::iterator c = managed_controllers_.find(it);
455 if (c != managed_controllers_.end())
457 request->activate_controllers.push_back(c->second.name);
458 for (
const auto& required_resource : c->second.required_command_interfaces)
460 resources_bimap::right_const_iterator res = claimed_resources.right.find(required_resource);
461 if (res != claimed_resources.right.end())
463 if (std::find(request->deactivate_controllers.begin(), request->deactivate_controllers.end(),
464 res->second) == request->deactivate_controllers.end())
466 request->deactivate_controllers.push_back(res->second);
468 claimed_resources.left.erase(res->second);
476 request->strictness = controller_manager_msgs::srv::SwitchController::Request::STRICT;
478 if (!request->activate_controllers.empty() || !request->deactivate_controllers.empty())
480 auto result_future = switch_controller_service_->async_send_request(request);
481 if (result_future.wait_for(std::chrono::duration<double>(SERVICE_CALL_TIMEOUT)) == std::future_status::timeout)
483 RCLCPP_ERROR_STREAM(getLogger(),
"Couldn't switch controllers at "
484 << switch_controller_service_->get_service_name() <<
" within "
485 << SERVICE_CALL_TIMEOUT <<
" seconds");
489 return result_future.get()->ok;
500 std::unordered_map<std::string, size_t> controller_name_map;
501 for (
size_t i = 0; i < result->controller.size(); ++i)
503 controller_name_map[result->controller[i].name] = i;
505 for (
auto& controller : result->controller)
507 if (controller.chain_connections.size() > 1)
509 RCLCPP_ERROR_STREAM(getLogger(),
510 "Controller with name %s chains to more than one controller. Chaining to more than "
511 "one controller is not supported.");
514 dependency_map_[controller.name].clear();
515 for (
const auto& chained_controller : controller.chain_connections)
517 auto ind = controller_name_map[chained_controller.name];
518 dependency_map_[controller.name].push_back(chained_controller.name);
519 controller.required_command_interfaces = result->controller[ind].required_command_interfaces;
520 controller.claimed_interfaces = result->controller[ind].claimed_interfaces;
521 result->controller[ind].claimed_interfaces.clear();
522 result->controller[ind].required_command_interfaces.clear();
535 typedef std::map<std::string, moveit_ros_control_interface::Ros2ControlManagerPtr> ControllerManagersMap;
536 ControllerManagersMap controller_managers_;
537 rclcpp::Time controller_managers_stamp_{ 0, 0, RCL_ROS_TIME };
538 std::mutex controller_managers_mutex_;
540 rclcpp::Node::SharedPtr node_;
542 void initialize(
const rclcpp::Node::SharedPtr& node)
override
553 if ((node_->now() - controller_managers_stamp_) < CONTROLLER_INFORMATION_VALIDITY_AGE)
556 controller_managers_stamp_ = node_->now();
558 const std::map<std::string, std::vector<std::string>> services = node_->get_service_names_and_types();
560 for (
const auto& service : services)
562 const auto& service_name = service.first;
563 std::size_t found = service_name.find(
"controller_manager/list_controllers");
564 if (found != std::string::npos)
566 std::string ns = service_name.substr(0, found);
567 if (controller_managers_.find(ns) == controller_managers_.end())
569 RCLCPP_INFO_STREAM(getLogger(),
"Adding controller_manager interface for node at namespace " << ns);
570 auto controller_manager = std::make_shared<moveit_ros_control_interface::Ros2ControlManager>(ns);
571 controller_manager->initialize(node_);
572 controller_managers_.insert(std::make_pair(ns, controller_manager));
583 static std::string getNamespace(
const std::string& name)
585 size_t pos = name.find(
'/', 1);
586 if (pos == std::string::npos)
588 return name.substr(0, pos + 1);
597 moveit_controller_manager::MoveItControllerHandlePtr
getControllerHandle(
const std::string& name)
override
599 std::unique_lock<std::mutex> lock(controller_managers_mutex_);
601 std::string ns = getNamespace(name);
602 ControllerManagersMap::iterator it = controller_managers_.find(ns);
603 if (it != controller_managers_.end())
605 return it->second->getControllerHandle(name);
607 return moveit_controller_manager::MoveItControllerHandlePtr();
616 std::unique_lock<std::mutex> lock(controller_managers_mutex_);
619 for (std::pair<const std::string, moveit_ros_control_interface::Ros2ControlManagerPtr>& controller_manager :
620 controller_managers_)
622 controller_manager.second->getControllersList(names);
632 std::unique_lock<std::mutex> lock(controller_managers_mutex_);
635 for (std::pair<const std::string, moveit_ros_control_interface::Ros2ControlManagerPtr>& controller_manager :
636 controller_managers_)
638 controller_manager.second->getActiveControllers(names);
649 std::unique_lock<std::mutex> lock(controller_managers_mutex_);
651 std::string ns = getNamespace(name);
652 ControllerManagersMap::iterator it = controller_managers_.find(ns);
653 if (it != controller_managers_.end())
655 it->second->getControllerJoints(name, joints);
666 std::unique_lock<std::mutex> lock(controller_managers_mutex_);
668 std::string ns = getNamespace(name);
669 ControllerManagersMap::iterator it = controller_managers_.find(ns);
670 if (it != controller_managers_.end())
672 return it->second->getControllerState(name);
683 bool switchControllers(
const std::vector<std::string>& activate,
const std::vector<std::string>& deactivate)
override
685 std::unique_lock<std::mutex> lock(controller_managers_mutex_);
687 for (std::pair<const std::string, moveit_ros_control_interface::Ros2ControlManagerPtr>& controller_manager :
688 controller_managers_)
690 if (!controller_manager.second->switchControllers(activate, deactivate))