map_loader.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  STDR Simulator - Simple Two DImensional Robot Simulator
3  Copyright (C) 2013 STDR Simulator
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 3 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 
16  Authors :
17  * Manos Tsardoulias, etsardou@gmail.com
18  * Aris Thallas, aris.thallas@gmail.com
19  * Chris Zalidis, zalidis@gmail.com
20 ******************************************************************************/
21 
22 #include "stdr_server/map_loader.h"
23 
24 namespace stdr_server {
25 
26  namespace map_loader {
27 
33  nav_msgs::OccupancyGrid loadMap(const std::string& fname) {
34 
35  nav_msgs::GetMap::Response map_resp_;
36 
37  std::string mapfname = "";
38  std::ifstream fin(fname.c_str());
39  double origin[3];
40  double res;
41  int negate;
42  double occ_th, free_th;
43  std::string frame_id = "map";
44 
45  #ifdef HAVE_NEW_YAMLCPP
46  // The document loading process changed in yaml-cpp 0.5.
47  YAML::Node doc = YAML::Load(fin);
48  #else
49  YAML::Parser parser(fin);
50  YAML::Node doc;
51  parser.GetNextDocument(doc);
52  #endif
53  try {
54  doc["resolution"] >> res;
55  } catch (YAML::InvalidScalar) {
56  ROS_ERROR(
57  "The map does not contain a resolution tag or it is invalid.");
58  exit(-1);
59  }
60  try {
61  doc["negate"] >> negate;
62  } catch (YAML::InvalidScalar) {
63  ROS_ERROR("The map does not contain a negate tag or it is invalid.");
64  exit(-1);
65  }
66  try {
67  doc["occupied_thresh"] >> occ_th;
68  } catch (YAML::InvalidScalar) {
69  ROS_ERROR(
70  "The map does not contain an occupied_thresh tag or it is invalid.");
71  exit(-1);
72  }
73  try {
74  doc["free_thresh"] >> free_th;
75  } catch (YAML::InvalidScalar) {
76  ROS_ERROR(
77  "The map does not contain a free_thresh tag or it is invalid.");
78  exit(-1);
79  }
80  try {
81  doc["origin"][0] >> origin[0];
82  doc["origin"][1] >> origin[1];
83  doc["origin"][2] >> origin[2];
84  } catch (YAML::InvalidScalar) {
85  ROS_ERROR("The map does not contain an origin tag or it is invalid.");
86  exit(-1);
87  }
88  try {
89  doc["image"] >> mapfname;
90  // TODO: make this path-handling more robust
91  if(mapfname.size() == 0)
92  {
93  ROS_ERROR("The image tag cannot be an empty string.");
94  exit(-1);
95  }
96  if(mapfname[0] != '/')
97  {
98  // dirname can modify what you pass it
99  char* fname_copy = strdup(fname.c_str());
100  mapfname = std::string(dirname(fname_copy)) + '/' + mapfname;
101  free(fname_copy);
102  }
103  } catch (YAML::InvalidScalar) {
104  ROS_ERROR("The map does not contain an image tag or it is invalid.");
105  exit(-1);
106  }
107 
108  ROS_INFO("Loading map from image \"%s\"", mapfname.c_str());
109  map_server::loadMapFromFile(&map_resp_,mapfname.c_str(),
110  res,negate,occ_th,free_th, origin);
111 
112  map_resp_.map.info.map_load_time = ros::Time::now();
113  map_resp_.map.header.frame_id = frame_id;
114  map_resp_.map.header.stamp = ros::Time::now();
115  ROS_INFO("Read a %d X %d map @ %.3lf m/cell",
116  map_resp_.map.info.width,
117  map_resp_.map.info.height,
118  map_resp_.map.info.resolution);
119 
120  return map_resp_.map;
121  }
122 
123  } // end of namespace map_loader
124 
125 } // end of namespace stdr_server
The main namespace for STDR Server.
nav_msgs::OccupancyGrid loadMap(const std::string &fname)
Loads a map from an image file.
Definition: map_loader.cpp:33
#define ROS_INFO(...)
void loadMapFromFile(nav_msgs::GetMap::Response *resp, const char *fname, double res, bool negate, double occ_th, double free_th, double *origin, MapMode mode=TRINARY)
parser
static Time now()
#define ROS_ERROR(...)
The namespace for STDR map loader.


stdr_server
Author(s): Chris Zalidis
autogenerated on Mon Jun 10 2019 15:15:07