@@ -71,21 +71,48 @@ Costmap2DROS::Costmap2DROS(const rclcpp::NodeOptions & options)
71
71
init ();
72
72
}
73
73
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
+
74
104
Costmap2DROS::Costmap2DROS (
75
105
const std::string & name,
76
106
const std::string & parent_namespace,
77
- const bool & use_sim_time)
107
+ const bool & use_sim_time,
108
+ const rclcpp::NodeOptions & options)
78
109
: nav2_util::LifecycleNode(name, " " ,
79
110
// NodeOption arguments take precedence over the ones provided on the command line
80
111
// use this to make sure the node is placed on the provided namespace
81
112
// TODO(orduno) Pass a sub-node instead of creating a new node for better handling
82
113
// 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
+ ),
89
116
name_(name),
90
117
default_plugins_{" static_layer" , " obstacle_layer" , " inflation_layer" },
91
118
default_types_{
0 commit comments