Skip to content

Commit a05e0d0

Browse files
committed
fix: log invalid layered map regions
Signed-off-by: SamuelFoo <fooenzesamuel@gmail.com>
1 parent a43dc3a commit a05e0d0

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

  • map_server/rmf_layered_map_server/src

map_server/rmf_layered_map_server/src/lib.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ impl LayeredMapServer {
266266
}
267267

268268
fn handle_region_update(&mut self, msg: MapRegionUpdate) {
269+
log_region_update_errors(&self.node, &msg);
269270
let now_nsec = self.now_nsec();
270271
if self.map.ingest_region_update(msg, now_nsec) {
271272
rclrs::log!(
@@ -382,6 +383,56 @@ fn region_update_qos(topic: &str) -> PrimitiveOptions<'_> {
382383
topic.keep_last(MAP_QOS_DEPTH).reliable()
383384
}
384385

386+
fn log_region_update_errors(node: &Node, update: &MapRegionUpdate) {
387+
for patch in &update.patches {
388+
if !matches!(
389+
patch.update_type,
390+
MapRegionPatch::UPDATE_OBSTACLE | MapRegionPatch::UPDATE_CLEAR
391+
) {
392+
rclrs::log_error!(
393+
node.logger(),
394+
"Ignoring map region patch with unsupported update_type {}",
395+
patch.update_type
396+
);
397+
continue;
398+
}
399+
400+
for region in &patch.regions {
401+
if let Some(error) = region_validation_error(region) {
402+
rclrs::log_error!(node.logger(), "Ignoring map region: {}", error);
403+
}
404+
}
405+
}
406+
}
407+
408+
fn region_validation_error(region: &Region) -> Option<&'static str> {
409+
if region.points.len() < 2 {
410+
return Some("region must contain at least one x/y point pair");
411+
}
412+
413+
if region.points.len() % 2 != 0 {
414+
return Some("region points must contain complete x/y pairs");
415+
}
416+
417+
if !is_supported_region_hint(region.hint) {
418+
return Some("unsupported region hint");
419+
}
420+
421+
None
422+
}
423+
424+
fn is_supported_region_hint(hint: u8) -> bool {
425+
matches!(
426+
hint,
427+
Region::HINT_UNSPECIFIED
428+
| Region::HINT_POINT
429+
| Region::HINT_AXIS_ALIGNED_RECTANGLE
430+
| Region::HINT_RECTANGLE
431+
| Region::HINT_CONVEX_POLYGON
432+
| Region::HINT_POLYGON
433+
)
434+
}
435+
385436
fn normalize_grid_data(grid: &mut OccupancyGrid) {
386437
let Some(expected_len) = grid_len(grid) else {
387438
grid.data.clear();
@@ -420,6 +471,10 @@ fn rasterized_indices(grid: &OccupancyGrid, region: &Region) -> Vec<usize> {
420471
return Vec::new();
421472
}
422473

474+
if !is_supported_region_hint(region.hint) {
475+
return Vec::new();
476+
}
477+
423478
if region.hint == Region::HINT_POINT || region.points.len() == 2 {
424479
return point_index(
425480
region.points[0],

0 commit comments

Comments
 (0)