Skip to content

Commit 55263dd

Browse files
Refactor parsing function to common, add for rest of layers
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent 1592489 commit 55263dd

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

nav2_costmap_2d/include/nav2_costmap_2d/costmap_layer.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#ifndef NAV2_COSTMAP_2D__COSTMAP_LAYER_HPP_
3939
#define NAV2_COSTMAP_2D__COSTMAP_LAYER_HPP_
4040

41+
#include <string>
42+
4143
#include <rclcpp/rclcpp.hpp>
4244
#include <nav2_costmap_2d/layer.hpp>
4345
#include <nav2_costmap_2d/layered_costmap.hpp>
@@ -192,6 +194,23 @@ class CostmapLayer : public Layer, public Costmap2D
192194
*/
193195
CombinationMethod combination_method_from_int(const int value);
194196

197+
/**
198+
* Joins the specified topic with the parent namespace of the costmap node.
199+
* If the topic has an absolute path, it is returned instead.
200+
*
201+
* This is necessary for user defined relative topics to work as expected since costmap layers
202+
* add a an additional `costmap_name` namespace to the topic.
203+
* For example:
204+
* * User chosen namespace is `tb4`.
205+
* * User chosen topic is `scan`.
206+
* * Costmap node namespace will be `/tb4/global_costmap`.
207+
* * Without this function, the topic would be `/tb4/global_costmap/scan`.
208+
* * With this function, topic will be remapped to `/tb4/scan`.
209+
*
210+
* @param topic the topic to parse
211+
*/
212+
std::string joinWithParentNamespace(const std::string & topic);
213+
195214
private:
196215
double extra_min_x_, extra_max_x_, extra_min_y_, extra_max_y_;
197216
};

nav2_costmap_2d/plugins/obstacle_layer.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,7 @@ void ObstacleLayer::onInitialize()
189189
node->get_parameter(name_ + "." + source + "." + "raytrace_min_range", raytrace_min_range);
190190
node->get_parameter(name_ + "." + source + "." + "raytrace_max_range", raytrace_max_range);
191191

192-
// If the user passed a relative topic, use it as relative to this node's parent namespace.
193-
// For example:
194-
// * User chosen namespace is `tb4`.
195-
// * User chosen topic is `scan`.
196-
// * Node namespace will be `/tb4/global_costmap`.
197-
// * Topic will be remapped to `/tb4/scan` rather than `/tb4/global_costmap/scan`
198-
if (topic[0] != '/') {
199-
std::string node_namespace = node->get_namespace();
200-
std::string parent_namespace = node_namespace.substr(0, node_namespace.rfind("/"));
201-
topic = parent_namespace + "/" + topic;
202-
}
192+
topic = joinWithParentNamespace(topic);
203193

204194
RCLCPP_DEBUG(
205195
logger_,

nav2_costmap_2d/plugins/range_sensor_layer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ void RangeSensorLayer::onInitialize()
130130

131131
// Traverse the topic names list subscribing to all of them with the same callback method
132132
for (auto & topic_name : topic_names) {
133+
topic_name = joinWithParentNamespace(topic_name);
133134
if (input_sensor_type == InputSensorType::VARIABLE) {
134135
processRangeMessageFunc_ = std::bind(
135136
&RangeSensorLayer::processVariableRangeMsg, this,

nav2_costmap_2d/plugins/static_layer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ StaticLayer::getParameters()
155155
} else {
156156
map_topic_ = global_map_topic;
157157
}
158+
map_topic_ = joinWithParentNamespace(map_topic_);
158159
node->get_parameter(
159160
name_ + "." + "map_subscribe_transient_local",
160161
map_subscribe_transient_local_);

nav2_costmap_2d/src/costmap_layer.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,20 @@ CombinationMethod CostmapLayer::combination_method_from_int(const int value)
264264
return CombinationMethod::Max;
265265
}
266266
}
267+
268+
std::string CostmapLayer::joinWithParentNamespace(const std::string & topic)
269+
{
270+
auto node = node_.lock();
271+
if (!node) {
272+
throw std::runtime_error{"Failed to lock node"};
273+
}
274+
275+
if (topic[0] != '/') {
276+
std::string node_namespace = node->get_namespace();
277+
std::string parent_namespace = node_namespace.substr(0, node_namespace.rfind("/"));
278+
return parent_namespace + "/" + topic;
279+
}
280+
281+
return topic;
282+
}
267283
} // namespace nav2_costmap_2d

0 commit comments

Comments
 (0)