Go to the documentation of this file.00001
00011 #include "carl_navigation/map_republisher.h"
00012
00013 MapRepublisher::MapRepublisher()
00014 {
00015 mapSubscriber = n.subscribe("map_server/map", 1, &MapRepublisher::staticMapCallback, this);
00016 obstacleGridSubscriber = n.subscribe("furniture_layer/obstacle_grid", 1,
00017 &MapRepublisher::obstacleCallback, this);
00018
00019 mapPublisher = n.advertise<nav_msgs::OccupancyGrid>("map", 1);
00020 mapMetaDataPublisher = n.advertise<nav_msgs::MapMetaData>("map_metadata", 1);
00021
00022 getMapServer = n.advertiseService("static_map", &MapRepublisher::mapServiceCallback, this);
00023
00024 obstacleCells.clear();
00025 staticMapInitialized = false;
00026 mapSubscribers = 0;
00027 metaDataSubscribers = 0;
00028 }
00029
00030 bool MapRepublisher::mapServiceCallback(nav_msgs::GetMap::Request &req, nav_msgs::GetMap::Response &res)
00031 {
00032 res.map = combinedMap;
00033 return true;
00034 }
00035
00036 void MapRepublisher::staticMapCallback(const nav_msgs::OccupancyGrid& map)
00037 {
00038 staticMap = map;
00039 combinedMap = staticMap;
00040 staticMapInitialized = true;
00041 for (unsigned int i = 0; i < obstacleCells.size(); i++)
00042 {
00043 combinedMap.data[obstacleCells[i].y * staticMap.info.width + obstacleCells[i].x] = -1;
00044 }
00045 for (unsigned int i = 0; i < obstacleEdges.size(); i ++)
00046 {
00047 combinedMap.data[obstacleEdges[i].y * staticMap.info.width + obstacleEdges[i].x] = 100;
00048 }
00049 mapPublisher.publish(combinedMap);
00050 mapMetaDataPublisher.publish(combinedMap.info);
00051 }
00052
00053 void MapRepublisher::obstacleCallback(const carl_navigation::BlockedCells& grid)
00054 {
00055 obstacleCells = grid.blockedCells;
00056 obstacleEdges = grid.edgeCells;
00057 if (staticMapInitialized)
00058 {
00059 combinedMap = staticMap;
00060 for (unsigned int i = 0; i < obstacleCells.size(); i++)
00061 {
00062 combinedMap.data[obstacleCells[i].y * staticMap.info.width + obstacleCells[i].x] = -1;
00063 }
00064 for (unsigned int i = 0; i < obstacleEdges.size(); i ++)
00065 {
00066 combinedMap.data[obstacleEdges[i].y * staticMap.info.width + obstacleEdges[i].x] = 100;
00067 }
00068 mapPublisher.publish(combinedMap);
00069 mapMetaDataPublisher.publish(combinedMap.info);
00070 }
00071 }
00072
00073 void MapRepublisher::publishMap()
00074 {
00075 if (staticMapInitialized)
00076 {
00077 int subscribers = mapPublisher.getNumSubscribers();
00078 if (subscribers != mapSubscribers)
00079 {
00080 if (subscribers > mapSubscribers)
00081 {
00082 ROS_INFO("New subscriber detected for /map, publishing map...");
00083 mapPublisher.publish(combinedMap);
00084 }
00085 mapSubscribers = subscribers;
00086 }
00087
00088 subscribers = mapMetaDataPublisher.getNumSubscribers();
00089 if (subscribers != metaDataSubscribers)
00090 {
00091 if (subscribers > metaDataSubscribers)
00092 {
00093 mapMetaDataPublisher.publish(combinedMap.info);
00094 }
00095 metaDataSubscribers = subscribers;
00096 }
00097 }
00098 }
00099
00100 int main(int argc, char **argv)
00101 {
00102 ros::init(argc, argv, "map_republisher");
00103
00104 MapRepublisher mr;
00105
00106 ros::Rate loop_rate(30);
00107 while (ros::ok())
00108 {
00109 mr.publishMap();
00110 ros::spinOnce();
00111 loop_rate.sleep();
00112 }
00113
00114 return 0;
00115 }