occupancy_grid.cpp
Go to the documentation of this file.
1 
4 /*****************************************************************************
5 ** Includes
6 *****************************************************************************/
7 
10 #include <nav_msgs/OccupancyGrid.h>
11 #include "../../include/cost_map_visualisations/occupancy_grid.hpp"
12 
13 /*****************************************************************************
14 ** Namespaces
15 *****************************************************************************/
16 
17 namespace cost_map {
18 
19 /*****************************************************************************
20 ** Implementation
21 *****************************************************************************/
22 
24 {
25  ros::NodeHandle nodehandle("~");
26 
27  subscriber_ = nodehandle.subscribe("cost_map", 10, &OccupancyGrid::_costMapCallback, this);
28 }
29 
30 void OccupancyGrid::_costMapCallback(const cost_map_msgs::CostMap::ConstPtr& msg) {
31  for (const auto& layer : msg->layers) {
32  if ( publishers_.count(layer) == 0 ) {
33  ros::NodeHandle nodehandle("~");
34  auto result = publishers_.insert(std::pair<std::string, ros::Publisher>(layer, ros::Publisher(nodehandle.advertise<nav_msgs::OccupancyGrid>(layer, 1, true))));
35  }
36  // TODO check if layers disappeared and remove the publishers
37  ros::Publisher& publisher = publishers_[layer];
38  if (publisher.getNumSubscribers() >= 0) {
40  cost_map::fromMessage(*msg, cost_map);
41  nav_msgs::OccupancyGrid occupancy_grid_msg;
42  cost_map::toOccupancyGrid(cost_map, layer, occupancy_grid_msg);
43  publisher.publish(occupancy_grid_msg);
44  }
45  }
46 }
47 
48 /*****************************************************************************
49  ** Trailers
50  *****************************************************************************/
51 
52 } // namespace cost_map
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void _costMapCallback(const cost_map_msgs::CostMap::ConstPtr &msg)
std::map< std::string, ros::Publisher > publishers_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void toOccupancyGrid(const cost_map::CostMap &cost_map, const std::string &layer, nav_msgs::OccupancyGrid &msg)
ros::Subscriber subscriber_
uint32_t getNumSubscribers() const
bool fromMessage(const cost_map_msgs::CostMap &message, cost_map::CostMap &cost_map)


cost_map_visualisations
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:03:52