Skip to content

Commit 94e2b7d

Browse files
author
Christoph Hellmann Santos
committed
Add namespaced controllers
1 parent ca33bcc commit 94e2b7d

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

controller_manager/src/controller_manager.cpp

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,64 @@ bool controller_name_compare(const controller_manager::ControllerSpec & a, const
6868
return a.info.name == name;
6969
}
7070

71+
// Pass in full_name and the namespace of the manager node, receive
72+
/**
73+
* \brief Creates controller naming
74+
*
75+
* A command, that based on the full name of the controller (e.g. from load_controller service call)
76+
* calculates the namespace, name and parameter name of the controller.
77+
*
78+
* If the passed in name does not start with a '/' it is assumed to
79+
* be relative to the manager namespace.
80+
*
81+
* If the passed in name starts with a '/' it is assumed to be
82+
* relative to the root namespace. In this case the parameter
83+
* is the full name with the initial '/' removed and all other
84+
* '/' replaced with '.'.
85+
*
86+
* \param[in] passed_in_name
87+
* \param[in] manager_namespace
88+
* \param[out] node_namespace
89+
* \param[out] node_name
90+
* \param[out] node_parameter_name
91+
*/
92+
void calculate_controller_naming(
93+
const std::string passed_in_name, const std::string manager_namespace,
94+
std::string & node_namespace, std::string & node_name, std::string & node_parameter_name)
95+
{
96+
auto split_pos = passed_in_name.find_last_of('/');
97+
if (split_pos == std::string::npos)
98+
{
99+
node_name = passed_in_name;
100+
}
101+
else
102+
{
103+
node_name = passed_in_name.substr(split_pos + 1);
104+
}
105+
auto first_occ = passed_in_name.find_first_of('/');
106+
if (first_occ == std::string::npos)
107+
{
108+
node_namespace = manager_namespace;
109+
node_parameter_name = passed_in_name + ".type";
110+
}
111+
else if (first_occ != 0)
112+
{
113+
node_namespace = manager_namespace + '/' + passed_in_name.substr(0, split_pos);
114+
node_parameter_name = std::regex_replace(passed_in_name, std::regex("/"), ".") + ".type";
115+
}
116+
else
117+
{
118+
node_namespace = passed_in_name.substr(0, split_pos);
119+
node_parameter_name =
120+
std::regex_replace(passed_in_name.substr(1), std::regex("/"), ".") + ".type";
121+
}
122+
123+
RCLCPP_INFO(
124+
rclcpp::get_logger("split_namespace_and_name"),
125+
"node_namespace: %s, node_name: %s, node_param %s", node_namespace.c_str(), node_name.c_str(),
126+
node_parameter_name.c_str());
127+
}
128+
71129
/// Checks if a command interface belongs to a controller based on its prefix.
72130
/**
73131
* A command interface can be provided by a controller in which case is called "reference"
@@ -455,7 +513,10 @@ controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::load_c
455513
controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::load_controller(
456514
const std::string & controller_name)
457515
{
458-
const std::string param_name = controller_name + ".type";
516+
std::string controller_name_temp, controller_namespace, param_name;
517+
calculate_controller_naming(
518+
controller_name, get_namespace(), controller_namespace, controller_name_temp, param_name);
519+
459520
std::string controller_type;
460521

461522
// We cannot declare the parameters for the controllers that will be loaded in the future,
@@ -1138,9 +1199,12 @@ controller_interface::ControllerInterfaceBaseSharedPtr ControllerManager::add_co
11381199
controller.info.name.c_str());
11391200
return nullptr;
11401201
}
1141-
1202+
std::string controller_namespace_, controller_name_, controller_param_name_;
1203+
calculate_controller_naming(
1204+
controller.info.name, get_namespace(), controller_namespace_, controller_name_,
1205+
controller_param_name_);
11421206
if (
1143-
controller.c->init(controller.info.name, get_namespace()) ==
1207+
controller.c->init(controller_name_, controller_namespace_) ==
11441208
controller_interface::return_type::ERROR)
11451209
{
11461210
to.clear();

0 commit comments

Comments
 (0)