Skip to content

Commit 157c0c4

Browse files
authored
bt_navigator_***_rclcpp_node - Namespacing + NodeOptions forwarding (#5310)
* `bt_navigator_***_rclcpp_node` - Namespacing + NodeOptions forwarding Closes #5242 Inspired by: #5202 - `bt_navigator_***_rclcpp_node` now gets the same namespace of `bt_navigator` - `bt_navigator_***_rclcpp_node` now gets the same node options of `bt_navigator` (apart from node name, that is still forced to respect the pattern `bt_navigator_***_rclcpp_node` ) Signed-off-by: Patrick Roncagliolo <ronca.pat@gmail.com> * Deduplicate `replaceOrAddArgument` implementation Moved to `nav2_ros_common/node_utils.hpp`, namespace `nav2` Signed-off-by: Patrick Roncagliolo <ronca.pat@gmail.com> --------- Signed-off-by: Patrick Roncagliolo <ronca.pat@gmail.com>
1 parent 92991ef commit 157c0c4

File tree

4 files changed

+29
-36
lines changed

4 files changed

+29
-36
lines changed

‎nav2_behavior_tree/include/nav2_behavior_tree/bt_action_server_impl.hpp‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,12 @@ bool BtActionServer<ActionT, NodeT>::on_configure()
137137
std::string client_node_name = action_name_;
138138
std::replace(client_node_name.begin(), client_node_name.end(), '/', '_');
139139
// Use suffix '_rclcpp_node' to keep parameter file consistency #1773
140-
auto options = rclcpp::NodeOptions().arguments(
141-
{"--ros-args",
142-
"-r",
143-
std::string("__node:=") +
144-
std::string(node->get_name()) + "_" + client_node_name + "_rclcpp_node",
145-
"-p",
146-
"use_sim_time:=" +
147-
std::string(node->get_parameter("use_sim_time").as_bool() ? "true" : "false"),
148-
"--"});
140+
141+
auto new_arguments = node->get_node_options().arguments();
142+
nav2::replaceOrAddArgument(new_arguments, "-r", "__node", std::string("__node:=") +
143+
std::string(node->get_name()) + "_" + client_node_name + "_rclcpp_node");
144+
auto options = node->get_node_options();
145+
options = options.arguments(new_arguments);
149146

150147
// Support for handling the topic-based goal pose from rviz
151148
client_node_ = std::make_shared<nav2::LifecycleNode>("_", options);

‎nav2_costmap_2d/include/nav2_costmap_2d/costmap_2d_ros.hpp‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,6 @@ class Costmap2DROS : public nav2::LifecycleNode
432432

433433
// free functions
434434

435-
/**
436-
* @brief Given a specified argument name, replaces it with the specified
437-
* new value. If the argument is not in the existing list, a new argument
438-
* is created with the specified option.
439-
*/
440-
void replaceOrAddArgument(
441-
std::vector<std::string> & arguments, const std::string & option,
442-
const std::string & arg_name, const std::string & new_argument);
443-
444435
/**
445436
* @brief Given the node options of a parent node, expands of replaces
446437
* the fields for the node name, namespace and use_sim_time

‎nav2_costmap_2d/src/costmap_2d_ros.cpp‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,17 @@ Costmap2DROS::Costmap2DROS(const rclcpp::NodeOptions & options)
7171
init();
7272
}
7373

74-
void replaceOrAddArgument(
75-
std::vector<std::string> & arguments, const std::string & option,
76-
const std::string & arg_name, const std::string & new_argument)
77-
{
78-
auto argument = std::find_if(arguments.begin(), arguments.end(),
79-
[arg_name](const std::string & value){return value.find(arg_name) != std::string::npos;});
80-
if (argument != arguments.end()) {
81-
*argument = new_argument;
82-
} else {
83-
arguments.push_back("--ros-args");
84-
arguments.push_back(option);
85-
arguments.push_back(new_argument);
86-
}
87-
}
88-
8974
rclcpp::NodeOptions getChildNodeOptions(
9075
const std::string & name,
9176
const std::string & parent_namespace,
9277
const bool & use_sim_time,
9378
const rclcpp::NodeOptions & parent_options)
9479
{
9580
std::vector<std::string> new_arguments = parent_options.arguments();
96-
replaceOrAddArgument(new_arguments, "-r", "__ns",
81+
nav2::replaceOrAddArgument(new_arguments, "-r", "__ns",
9782
"__ns:=" + nav2::add_namespaces(parent_namespace, name));
98-
replaceOrAddArgument(new_arguments, "-r", "__node", name + ":" + "__node:=" + name);
99-
replaceOrAddArgument(new_arguments, "-p", "use_sim_time",
83+
nav2::replaceOrAddArgument(new_arguments, "-r", "__node", name + ":" + "__node:=" + name);
84+
nav2::replaceOrAddArgument(new_arguments, "-p", "use_sim_time",
10085
"use_sim_time:=" + std::string(use_sim_time ? "true" : "false"));
10186
return rclcpp::NodeOptions().arguments(new_arguments);
10287
}

‎nav2_ros_common/include/nav2_ros_common/node_utils.hpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,26 @@ inline void setIntrospectionMode(
378378
#endif
379379
}
380380

381+
/**
382+
* @brief Given a specified argument name, replaces it with the specified
383+
* new value. If the argument is not in the existing list, a new argument
384+
* is created with the specified option.
385+
*/
386+
inline void replaceOrAddArgument(
387+
std::vector<std::string> & arguments, const std::string & option,
388+
const std::string & arg_name, const std::string & new_argument)
389+
{
390+
auto argument = std::find_if(arguments.begin(), arguments.end(),
391+
[arg_name](const std::string & value){return value.find(arg_name) != std::string::npos;});
392+
if (argument != arguments.end()) {
393+
*argument = new_argument;
394+
} else {
395+
arguments.push_back("--ros-args");
396+
arguments.push_back(option);
397+
arguments.push_back(new_argument);
398+
}
399+
}
400+
381401
} // namespace nav2
382402

383403
#endif // NAV2_ROS_COMMON__NODE_UTILS_HPP_

0 commit comments

Comments
 (0)