map_server_node.cpp
Go to the documentation of this file.
00001 /***********************************************************************************
00002  * Revised BSD License *
00003  * Copyright (c) 2014, Markus Bader <markus.bader@tuwien.ac.at> *
00004  * All rights reserved. *
00005  *                                                                                 *
00006  * Redistribution and use in source and binary forms, with or without *
00007  * modification, are permitted provided that the following conditions are met: *
00008  *     * Redistributions of source code must retain the above copyright *
00009  *       notice, this list of conditions and the following disclaimer. *
00010  *     * Redistributions in binary form must reproduce the above copyright *
00011  *       notice, this list of conditions and the following disclaimer in the *
00012  *       documentation and/or other materials provided with the distribution. *
00013  *     * Neither the name of the Vienna University of Technology nor the *
00014  *       names of its contributors may be used to endorse or promote products *
00015  *       derived from this software without specific prior written permission. *
00016  *                                                                                 *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  *AND *
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  **
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
00022  * DISCLAIMED. IN NO EVENT SHALL Markus Bader BE LIABLE FOR ANY *
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  **
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
00027  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  **
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
00031  ***********************************************************************************/
00032 
00033 #include <map_server_node.h>
00034 
00035 #include <mrpt_bridge/map.h>
00036 
00037 #include <mrpt/system/filesystem.h>  // ASSERT_FILE_EXISTS_()
00038 #include <mrpt/utils/CConfigFile.h>
00039 
00040 #include <mrpt/version.h>
00041 #include <mrpt/maps/COccupancyGridMap2D.h>
00042 #include <mrpt/maps/CMultiMetricMap.h>
00043 using mrpt::maps::COccupancyGridMap2D;
00044 using mrpt::maps::CMultiMetricMap;
00045 
00046 MapServer::MapServer(ros::NodeHandle& n)
00047         : n_(n), n_param_("~"), loop_count_(0), frequency_(0), debug_(true)
00048 {
00049 }
00050 
00051 MapServer::~MapServer() {}
00052 void MapServer::init()
00053 {
00054         std::string ini_file;
00055         std::string map_file;
00056         n_param_.param<bool>("debug", debug_, true);
00057         ROS_INFO("debug: %s", (debug_ ? "true" : "false"));
00058         n_param_.param<std::string>("ini_file", ini_file, "map.ini");
00059         ROS_INFO("ini_file: %s", ini_file.c_str());
00060         n_param_.param<std::string>("map_file", map_file, "map.simplemap");
00061         ROS_INFO("map_file: %s", map_file.c_str());
00062         n_param_.param<std::string>(
00063                 "frame_id", resp_ros_.map.header.frame_id, "map");
00064         ROS_INFO("frame_id: %s", resp_ros_.map.header.frame_id.c_str());
00065         n_param_.param<double>("frequency", frequency_, 0.1);
00066         ROS_INFO("frequency: %f", frequency_);
00067 
00068         pub_map_ros_ = n_.advertise<nav_msgs::OccupancyGrid>("map", 1, true);
00069         pub_metadata_ =
00070                 n_.advertise<nav_msgs::MapMetaData>("map_metadata", 1, true);
00071         service_map_ =
00072                 n_.advertiseService("static_map", &MapServer::mapCallback, this);
00073 
00074         if (!mrpt::system::fileExists(ini_file))
00075         {
00076                 ROS_ERROR("ini_file: %s does not exit", ini_file.c_str());
00077         }
00078         if (!mrpt::system::fileExists(map_file))
00079         {
00080                 ROS_ERROR("map_file: %s does not exit", map_file.c_str());
00081         }
00082         ASSERT_FILE_EXISTS_(ini_file);
00083         mrpt::utils::CConfigFile config_file;
00084         config_file.setFileName(ini_file);
00085         metric_map_ = boost::shared_ptr<CMultiMetricMap>(new CMultiMetricMap);
00086         mrpt_bridge::MapHdl::loadMap(
00087                 *metric_map_, config_file, map_file, "metricMap", debug_);
00088         const COccupancyGridMap2D& grid = *metric_map_->m_gridMaps[0];
00089         if (debug_)
00090                 printf(
00091                         "gridMap[0]:  %i x %i @ %4.3fm/p, %4.3f, %4.3f, %4.3f, %4.3f\n",
00092                         grid.getSizeX(), grid.getSizeY(), grid.getResolution(),
00093                         grid.getXMin(), grid.getYMin(), grid.getXMax(), grid.getYMax());
00094 
00095         mrpt_bridge::convert(*metric_map_->m_gridMaps[0], resp_ros_.map);
00096         if (debug_)
00097                 printf(
00098                         "msg:         %i x %i @ %4.3fm/p, %4.3f, %4.3f, %4.3f, %4.3f\n",
00099                         resp_ros_.map.info.width, resp_ros_.map.info.height,
00100                         resp_ros_.map.info.resolution, resp_ros_.map.info.origin.position.x,
00101                         resp_ros_.map.info.origin.position.y,
00102                         resp_ros_.map.info.width * resp_ros_.map.info.resolution +
00103                                 resp_ros_.map.info.origin.position.x,
00104                         resp_ros_.map.info.height * resp_ros_.map.info.resolution +
00105                                 resp_ros_.map.info.origin.position.y);
00106 }
00107 
00108 bool MapServer::mapCallback(
00109         nav_msgs::GetMap::Request& req, nav_msgs::GetMap::Response& res)
00110 {
00111         ROS_INFO("mapCallback: service requested!\n");
00112         res = resp_ros_;
00113         return true;
00114 }
00115 
00116 void MapServer::publishMap()
00117 {
00118         resp_ros_.map.header.stamp = ros::Time::now();
00119         resp_ros_.map.header.seq = loop_count_;
00120         if (pub_map_ros_.getNumSubscribers() > 0)
00121         {
00122                 pub_map_ros_.publish(resp_ros_.map);
00123         }
00124         if (pub_metadata_.getNumSubscribers() > 0)
00125         {
00126                 pub_metadata_.publish(resp_ros_.map.info);
00127         }
00128 }
00129 
00130 void MapServer::loop()
00131 {
00132         if (frequency_ > 0)
00133         {
00134                 ros::Rate rate(frequency_);
00135                 for (loop_count_ = 0; ros::ok(); loop_count_++)
00136                 {
00137                         publishMap();
00138                         ros::spinOnce();
00139                         rate.sleep();
00140                 }
00141         }
00142         else
00143         {
00144                 publishMap();
00145                 ros::spin();
00146         }
00147 }
00148 
00149 int main(int argc, char** argv)
00150 {
00151         // Initialize ROS
00152         ros::init(argc, argv, "mrpt_map_server");
00153         ros::NodeHandle node;
00154         MapServer my_node(node);
00155         my_node.init();
00156         my_node.loop();
00157         return 0;
00158 }


mrpt_map
Author(s):
autogenerated on Mon Sep 18 2017 03:12:11