31 #include <nav_msgs/OccupancyGrid.h> 32 #include <map_organizer_msgs/OccupancyGridArray.h> 43 #include <yaml-cpp/yaml.h> 45 #ifdef HAVE_NEW_YAMLCPP 68 pub_map_array_ = nh_.
advertise<map_organizer_msgs::OccupancyGridArray>(
"maps", 1,
true);
70 map_organizer_msgs::OccupancyGridArray
maps;
72 std::string files_str;
75 double origin[3], height;
77 double occ_th, free_th;
80 pnh_.
param(
"map_files", files_str, std::string(
""));
81 pnh_.
param(
"frame_id", frame_id, std::string(
"map"));
85 std::stringstream ss(files_str);
86 while (std::getline(ss, file,
','))
88 std::ifstream fin(file);
91 ROS_ERROR(
"Map_server could not open %s.", file.c_str());
95 #ifdef HAVE_NEW_YAMLCPP 97 YAML::Node doc = YAML::Load(fin);
101 parser.GetNextDocument(doc);
105 doc[
"resolution"] >> res;
107 catch (YAML::InvalidScalar)
109 ROS_ERROR(
"The map does not contain a resolution tag or it is invalid.");
115 doc[
"negate"] >> negate;
117 catch (YAML::InvalidScalar)
119 ROS_ERROR(
"The map does not contain a negate tag or it is invalid.");
125 doc[
"occupied_thresh"] >> occ_th;
127 catch (YAML::InvalidScalar)
129 ROS_ERROR(
"The map does not contain an occupied_thresh tag or it is invalid.");
135 doc[
"free_thresh"] >> free_th;
137 catch (YAML::InvalidScalar)
139 ROS_ERROR(
"The map does not contain a free_thresh tag or it is invalid.");
145 std::string modeS =
"";
146 doc[
"mode"] >> modeS;
148 if (modeS ==
"trinary")
150 else if (modeS ==
"scale")
152 else if (modeS ==
"raw")
156 ROS_ERROR(
"Invalid mode tag \"%s\".", modeS.c_str());
160 catch (YAML::Exception)
162 ROS_DEBUG(
"The map does not contain a mode tag or it is invalid... assuming trinary");
167 doc[
"origin"][0] >> origin[0];
168 doc[
"origin"][1] >> origin[1];
169 doc[
"origin"][2] >> origin[2];
171 catch (YAML::InvalidScalar)
173 ROS_ERROR(
"The map does not contain an origin tag or it is invalid.");
179 doc[
"height"] >> height;
181 catch (YAML::Exception)
187 doc[
"image"] >> mapfname;
189 if (mapfname.size() == 0)
191 ROS_ERROR(
"The image tag cannot be an empty string.");
195 if (mapfname[0] !=
'/')
198 char* fname_copy = strdup(file.c_str());
199 mapfname = std::string(dirname(fname_copy)) +
'/' + mapfname;
203 catch (YAML::InvalidScalar)
205 ROS_ERROR(
"The map does not contain an image tag or it is invalid.");
210 ROS_INFO(
"Loading map from image \"%s\"", mapfname.c_str());
212 nav_msgs::GetMap::Response map_resp;
214 mapfname.c_str(), res, negate, occ_th, free_th, origin, mode);
215 map_resp.map.info.origin.position.z = height;
217 map_resp.map.header.frame_id = frame_id;
219 ROS_INFO(
"Read a %d X %d map @ %.3lf m/cell",
220 map_resp.map.info.width,
221 map_resp.map.info.height,
222 map_resp.map.info.resolution);
223 maps.maps.push_back(map_resp.map);
224 pub_map_.push_back(nh_.
advertise<nav_msgs::OccupancyGrid>(
225 "map" + std::to_string(i), 1,
true));
226 pub_map_.back().publish(map_resp.map);
233 int main(
int argc,
char** argv)
int main(int argc, char **argv)
map_organizer_msgs::OccupancyGridArray maps
void publish(const boost::shared_ptr< M > &message) const
std::istream & operator>>(std::istream &is, Vector &v)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL void spin(Spinner &spinner)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
std::vector< ros::Publisher > pub_map_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
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)
ros::Publisher pub_map_array_
ROSCPP_DECL void shutdown()