Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions nav2_map_server/include/nav2_map_server/map_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ bool saveMapToFile(
const nav_msgs::msg::OccupancyGrid & map,
const SaveParameters & save_parameters);

/**
* @brief to_string_with_precision
* @param value
* @param precision
* @return
*/
std::string to_string_with_precision(double value, int precision);

/**
* @brief Expand ~/ to home user dir.
* @param yaml_filename Name of input YAML file.
Expand Down
24 changes: 18 additions & 6 deletions nav2_map_server/src/map_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,15 @@ void tryWriteMapToFile(
std::string image_name = mapdatafile.substr(file_name_index + 1);

YAML::Emitter e;
e << YAML::Precision(3);
e << YAML::Precision(7);
e << YAML::BeginMap;
e << YAML::Key << "image" << YAML::Value << image_name;
e << YAML::Key << "mode" << YAML::Value << map_mode_to_string(save_parameters.mode);
e << YAML::Key << "resolution" << YAML::Value << map.info.resolution;
e << YAML::Key << "origin" << YAML::Flow << YAML::BeginSeq << map.info.origin.position.x <<
map.info.origin.position.y << yaw << YAML::EndSeq;
e << YAML::Key << "resolution" << YAML::Value << to_string_with_precision(map.info.resolution,
3);
e << YAML::Key << "origin" << YAML::Flow << YAML::BeginSeq <<
to_string_with_precision(map.info.origin.position.x, 3) <<
to_string_with_precision(map.info.origin.position.y, 3) << yaw << YAML::EndSeq;
e << YAML::Key << "negate" << YAML::Value << 0;

if (save_parameters.mode == MapMode::Trinary) {
Expand All @@ -611,8 +613,10 @@ void tryWriteMapToFile(
e << YAML::Key << "occupied_thresh" << YAML::Value << 0.65;
e << YAML::Key << "free_thresh" << YAML::Value << 0.196;
} else {
e << YAML::Key << "occupied_thresh" << YAML::Value << save_parameters.occupied_thresh;
e << YAML::Key << "free_thresh" << YAML::Value << save_parameters.free_thresh;
e << YAML::Key << "occupied_thresh" << YAML::Value <<
to_string_with_precision(save_parameters.occupied_thresh, 3);
e << YAML::Key << "free_thresh" << YAML::Value <<
to_string_with_precision(save_parameters.free_thresh, 3);
}

if (!e.good()) {
Expand Down Expand Up @@ -648,4 +652,12 @@ bool saveMapToFile(
return true;
}

std::string to_string_with_precision(double value, int precision)
{
std::ostringstream out;
out << std::fixed << std::setprecision(precision) << value;

return out.str();
}

} // namespace nav2_map_server
Loading