00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <map_server_node.h>
00034
00035 #include <mrpt_bridge/map.h>
00036
00037 #include <mrpt/system/filesystem.h>
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
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 }