map_writer.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <fstream>
20 
21 #include "glog/logging.h"
22 #include "yaml-cpp/yaml.h"
23 
24 namespace cartographer_ros {
25 
26 namespace {
27 
28 void WriteOccupancyGridToPgm(const ::nav_msgs::OccupancyGrid& grid,
29  const std::string& filename) {
30  LOG(INFO) << "Saving map to '" << filename << "'...";
31  std::ofstream pgm_file(filename, std::ios::out | std::ios::binary);
32  const std::string header = "P5\n# Cartographer map; " +
33  std::to_string(grid.info.resolution) +
34  " m/pixel\n" + std::to_string(grid.info.width) +
35  " " + std::to_string(grid.info.height) + "\n255\n";
36  pgm_file.write(header.data(), header.size());
37  for (size_t y = 0; y < grid.info.height; ++y) {
38  for (size_t x = 0; x < grid.info.width; ++x) {
39  const size_t i = x + (grid.info.height - y - 1) * grid.info.width;
40  if (grid.data[i] >= 0 && grid.data[i] <= 100) {
41  pgm_file.put((100 - grid.data[i]) * 255 / 100);
42  } else {
43  // We choose a value between the free and occupied threshold.
44  constexpr uint8_t kUnknownValue = 128;
45  pgm_file.put(kUnknownValue);
46  }
47  }
48  }
49  pgm_file.close();
50  CHECK(pgm_file) << "Writing '" << filename << "' failed.";
51 }
52 
53 void WriteOccupancyGridInfoToYaml(const ::nav_msgs::OccupancyGrid& grid,
54  const std::string& map_filename,
55  const std::string& yaml_filename) {
56  LOG(INFO) << "Saving map info to '" << yaml_filename << "'...";
57  std::ofstream yaml_file(yaml_filename, std::ios::out | std::ios::binary);
58  {
59  YAML::Emitter out(yaml_file);
60  out << YAML::BeginMap;
61  // TODO(whess): Use basename only?
62  out << YAML::Key << "image" << YAML::Value << map_filename;
63  out << YAML::Key << "resolution" << YAML::Value << grid.info.resolution;
64  // According to map_server documentation "many parts of the system currently
65  // ignore yaw" so it is good we use a zero value.
66  constexpr double kYawButMaybeIgnored = 0.;
67  out << YAML::Key << "origin" << YAML::Value << YAML::Flow << YAML::BeginSeq
68  << grid.info.origin.position.x << grid.info.origin.position.y
69  << kYawButMaybeIgnored << YAML::EndSeq;
70  out << YAML::Key << "occupied_thresh" << YAML::Value << 0.51;
71  out << YAML::Key << "free_thresh" << YAML::Value << 0.49;
72  out << YAML::Key << "negate" << YAML::Value << 0;
73  out << YAML::EndMap;
74  CHECK(out.good()) << out.GetLastError();
75  }
76  yaml_file.close();
77  CHECK(yaml_file) << "Writing '" << yaml_filename << "' failed.";
78 }
79 
80 } // namespace
81 
83  const ::nav_msgs::OccupancyGrid& occupancy_grid, const std::string& stem) {
84  const std::string pgm_filename = stem + ".pgm";
85  WriteOccupancyGridToPgm(occupancy_grid, pgm_filename);
86  WriteOccupancyGridInfoToYaml(occupancy_grid, pgm_filename, stem + ".yaml");
87 }
88 
89 } // namespace cartographer_ros
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
void WriteOccupancyGridToPgmAndYaml(const ::nav_msgs::OccupancyGrid &occupancy_grid, const std::string &stem)
Definition: map_writer.cc:82
const std::string header


cartographer_ros
Author(s):
autogenerated on Wed Jun 5 2019 22:35:56