Skip to content

Commit 4cade8d

Browse files
Pass node options to costmap node (#5202)
* Passing parent node options from controller_server to costmap node Signed-off-by: Marco Bassa <[email protected]> * Passing parent node options to global costmap Signed-off-by: Marco Bassa <[email protected]> --------- Signed-off-by: Marco Bassa <[email protected]>
1 parent 4a959d9 commit 4cade8d

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

nav2_controller/src/controller_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ ControllerServer::ControllerServer(const rclcpp::NodeOptions & options)
6868
// The costmap node is used in the implementation of the controller
6969
costmap_ros_ = std::make_shared<nav2_costmap_2d::Costmap2DROS>(
7070
"local_costmap", std::string{get_namespace()},
71-
get_parameter("use_sim_time").as_bool());
71+
get_parameter("use_sim_time").as_bool(), options);
7272
}
7373

7474
ControllerServer::~ControllerServer()

nav2_costmap_2d/include/nav2_costmap_2d/costmap_2d_ros.hpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class Costmap2DROS : public nav2_util::LifecycleNode
9191
explicit Costmap2DROS(
9292
const std::string & name,
9393
const std::string & parent_namespace = "/",
94-
const bool & use_sim_time = false);
94+
const bool & use_sim_time = false,
95+
const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
9596

9697
/**
9798
* @brief Common initialization for constructors
@@ -430,6 +431,27 @@ class Costmap2DROS : public nav2_util::LifecycleNode
430431
dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters);
431432
};
432433

434+
// free functions
435+
436+
/**
437+
* @brief Given a specified argument name, replaces it with the specified
438+
* new value. If the argument is not in the existing list, a new argument
439+
* is created with the specified option.
440+
*/
441+
void replaceOrAddArgument(
442+
std::vector<std::string> & arguments, const std::string & option,
443+
const std::string & arg_name, const std::string & new_argument);
444+
445+
/**
446+
* @brief Given the node options of a parent node, expands of replaces
447+
* the fields for the node name, namespace and use_sim_time
448+
*/
449+
rclcpp::NodeOptions getChildNodeOptions(
450+
const std::string & name,
451+
const std::string & parent_namespace,
452+
const bool & use_sim_time,
453+
const rclcpp::NodeOptions & parent_options);
454+
433455
} // namespace nav2_costmap_2d
434456

435457
#endif // NAV2_COSTMAP_2D__COSTMAP_2D_ROS_HPP_

nav2_costmap_2d/src/costmap_2d_ros.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,48 @@ 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+
89+
rclcpp::NodeOptions getChildNodeOptions(
90+
const std::string & name,
91+
const std::string & parent_namespace,
92+
const bool & use_sim_time,
93+
const rclcpp::NodeOptions & parent_options)
94+
{
95+
std::vector<std::string> new_arguments = parent_options.arguments();
96+
replaceOrAddArgument(new_arguments, "-r", "__ns",
97+
"__ns:=" + nav2_util::add_namespaces(parent_namespace, name));
98+
replaceOrAddArgument(new_arguments, "-r", "__node", name + ":" + "__node:=" + name);
99+
replaceOrAddArgument(new_arguments, "-p", "use_sim_time",
100+
"use_sim_time:=" + std::string(use_sim_time ? "true" : "false"));
101+
return rclcpp::NodeOptions().arguments(new_arguments);
102+
}
103+
74104
Costmap2DROS::Costmap2DROS(
75105
const std::string & name,
76106
const std::string & parent_namespace,
77-
const bool & use_sim_time)
107+
const bool & use_sim_time,
108+
const rclcpp::NodeOptions & options)
78109
: nav2_util::LifecycleNode(name, "",
79110
// NodeOption arguments take precedence over the ones provided on the command line
80111
// use this to make sure the node is placed on the provided namespace
81112
// TODO(orduno) Pass a sub-node instead of creating a new node for better handling
82113
// of the namespaces
83-
rclcpp::NodeOptions().arguments({
84-
"--ros-args", "-r", std::string("__ns:=") +
85-
nav2_util::add_namespaces(parent_namespace, name),
86-
"--ros-args", "-r", name + ":" + std::string("__node:=") + name,
87-
"--ros-args", "-p", "use_sim_time:=" + std::string(use_sim_time ? "true" : "false"),
88-
})),
114+
getChildNodeOptions(name, parent_namespace, use_sim_time, options)
115+
),
89116
name_(name),
90117
default_plugins_{"static_layer", "obstacle_layer", "inflation_layer"},
91118
default_types_{

nav2_planner/src/planner_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ PlannerServer::PlannerServer(const rclcpp::NodeOptions & options)
6464
// Setup the global costmap
6565
costmap_ros_ = std::make_shared<nav2_costmap_2d::Costmap2DROS>(
6666
"global_costmap", std::string{get_namespace()},
67-
get_parameter("use_sim_time").as_bool());
67+
get_parameter("use_sim_time").as_bool(), options);
6868
}
6969

7070
PlannerServer::~PlannerServer()

0 commit comments

Comments
 (0)