Skip to content

Commit 5111c27

Browse files
doisygGuillaume Doisy
authored andcommitted
[DEX] Enforce 3 digits precision (#5398)
Signed-off-by: Guillaume Doisy <[email protected]> Co-authored-by: Guillaume Doisy <[email protected]>
1 parent 0b024e8 commit 5111c27

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

nav2_map_server/include/nav2_map_server/map_io.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ bool saveMapToFile(
9898
const nav_msgs::msg::OccupancyGrid & map,
9999
const SaveParameters & save_parameters);
100100

101+
/**
102+
* @brief to_string_with_precision
103+
* @param value
104+
* @param precision
105+
* @return
106+
*/
107+
std::string to_string_with_precision(double value, int precision);
108+
101109
/**
102110
* @brief Expand ~/ to home user dir.
103111
* @param yaml_filename Name of input YAML file.

nav2_map_server/src/map_io.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,13 +594,15 @@ void tryWriteMapToFile(
594594
std::string image_name = mapdatafile.substr(file_name_index + 1);
595595

596596
YAML::Emitter e;
597-
e << YAML::Precision(3);
597+
e << YAML::Precision(7);
598598
e << YAML::BeginMap;
599599
e << YAML::Key << "image" << YAML::Value << image_name;
600600
e << YAML::Key << "mode" << YAML::Value << map_mode_to_string(save_parameters.mode);
601-
e << YAML::Key << "resolution" << YAML::Value << map.info.resolution;
602-
e << YAML::Key << "origin" << YAML::Flow << YAML::BeginSeq << map.info.origin.position.x <<
603-
map.info.origin.position.y << yaw << YAML::EndSeq;
601+
e << YAML::Key << "resolution" << YAML::Value << to_string_with_precision(map.info.resolution,
602+
3);
603+
e << YAML::Key << "origin" << YAML::Flow << YAML::BeginSeq <<
604+
to_string_with_precision(map.info.origin.position.x, 3) <<
605+
to_string_with_precision(map.info.origin.position.y, 3) << yaw << YAML::EndSeq;
604606
e << YAML::Key << "negate" << YAML::Value << 0;
605607

606608
if (save_parameters.mode == MapMode::Trinary) {
@@ -611,8 +613,10 @@ void tryWriteMapToFile(
611613
e << YAML::Key << "occupied_thresh" << YAML::Value << 0.65;
612614
e << YAML::Key << "free_thresh" << YAML::Value << 0.196;
613615
} else {
614-
e << YAML::Key << "occupied_thresh" << YAML::Value << save_parameters.occupied_thresh;
615-
e << YAML::Key << "free_thresh" << YAML::Value << save_parameters.free_thresh;
616+
e << YAML::Key << "occupied_thresh" << YAML::Value <<
617+
to_string_with_precision(save_parameters.occupied_thresh, 3);
618+
e << YAML::Key << "free_thresh" << YAML::Value <<
619+
to_string_with_precision(save_parameters.free_thresh, 3);
616620
}
617621

618622
if (!e.good()) {
@@ -648,4 +652,12 @@ bool saveMapToFile(
648652
return true;
649653
}
650654

655+
std::string to_string_with_precision(double value, int precision)
656+
{
657+
std::ostringstream out;
658+
out << std::fixed << std::setprecision(precision) << value;
659+
660+
return out.str();
661+
}
662+
651663
} // namespace nav2_map_server

0 commit comments

Comments
 (0)