I did some tests and looking at the code, and because of this
line:
// From SLAM to localization, change map id
this->incrementMapId();
we don't really need to clear and reload everything. Try commenting the clear/reload
lines like this:
// From SLAM to localization, change map id
this->incrementMapId();
// The easiest way to make sure that the mapping session is saved
// is to save the memory in the database and reload it.
//if((_memoryChanged || _linksChanged) && _dbDriver)
//{
//UWARN("Switching from Mapping to Localization mode, the database will be saved and reloaded.");
// bool memoryChanged = _memoryChanged;
// bool linksChanged = _linksChanged;
// this->clear();
// _memoryChanged = memoryChanged;
// _linksChanged = linksChanged;
// this->loadDataFromDb(false);
// UWARN("Switching from Mapping to Localization mode, the database is reloaded!");
//}
I tested with
robot mapping demo and it seems to work, instantaneous switching from mapping to localization (like the reverse way) while saving the new data (added in mapping mode) to database when closing. Try this on your side and see if it doesn't break something else. I will still keep the old way for now (clearing and reload), but I will try commenting this in other projects with other configurations to see if it seems to be a valid fix or not.
cheers,
Mathieu