cartographer-project / cartographer

Cartographer is a system that provides real-time simultaneous localization and mapping (SLAM) in 2D and 3D across multiple platforms and sensor configurations.
Apache License 2.0
7.04k stars 2.24k forks source link

Checking if pbstream file was loaded properly #1818

Closed macstepien closed 3 years ago

macstepien commented 3 years ago

Hello! I'm trying to implement the following behavior: if a map file exists, Cartographer should try to load it and start in localization mode. If it fails at some point during loading (for example if the file was empty due to sudden shutdown of the robot) it should remove the map file and start mapping from scratch. From what I saw, by default when there are some problems with the pbstream file Cartographer crashes due to an unsatisfied CHECK condition. I came up with two ideas how to modify it:

Do you know if there is maybe some other, nicer way to implement such behavior? Thanks

MichaelGrupp commented 3 years ago

Doing this check while loading the state would be unfortunately very complex to do, I fear.

partially change Cartographer's code to include exceptions and handle them accordingly

This would change an existing convention in the code base (Google Style Guide : exceptions) and besides that would be a large, non-trivial change.

change default failure function in glog with google::InstallFailureFunction(&YourFailureFunction); to remove broken file. It would trigger such behavior for all FATAL cases though

The code that loads the pbstream file into the pose graph in the map builder is not suited for such a behavior IMO. It has too many intermediate steps that are hard to recover. I also don't think that the goal of such a failure function in glog is to replicate exceptions, and there can be only one as pointed out by you.

macstepien commented 3 years ago

First thanks for your response! I suspected that there may be no clean and easy way to do that, but I wasn't sure if I didn't miss something. 

I found a workaround that seems to work fine in my case - I used a custom glog failure function (sorry, I wasn't exactly precise about what I intended to do with it) to remove the broken state file, but didn't try to recover later. I'm using Cartographer with ROS, so with respawn parameter cartographer_node can come back up after failing to load state. This time, when it starts, the map file isn't present (as it was removed during the failure function execution) and mapping can start from scratch. Additionally with two flags: loading_map and finished_loading_map I was able to check if failure happened during loading and remove map only in this case.

Another idea I had was to simply add an exception to the failure function, but it isn't really that easy (here is another issue about it). It is also possible to redefine CHECK macros (here is what I found), but it seems to be quite a lot of work and potential changes in Cartographer.