hector_elevation_visualization.cpp
Go to the documentation of this file.
2 
3 
4 // compute linear index for given map coords
5 #define MAP_IDX(sx, i, j) ((sx) * (j) + (i))
6 
7 
9 {
10  nHandle.param("max_height_levels",max_height_levels,10); //[cell]
11  nHandle.param("min_height",min_height,-1.0); //[m]
12  nHandle.param("max_height",max_height,1.5); //[m]
13  nHandle.param("color_factor",color_factor,0.8); //[]
14  double r, g, b, a;
15  nHandle.param("color/r", r, 0.0);
16  nHandle.param("color/g", g, 0.0);
17  nHandle.param("color/b", b, 1.0);
18  nHandle.param("color/a", a, 1.0);
19  marker_color.r = r;
20  marker_color.g = g;
21  marker_color.b = b;
22  marker_color.a = a;
23  nHandle.param("use_color_map", use_color_map, true); //[]
24 
25  nHandle.param("elevation_map_frame_id", elevation_map_frame_id,std::string("/elevation_map_local"));
26  nHandle.param("paramSysMsgTopic", sys_msg_topic, std::string("/syscommand"));
27 
28  map_marker_array_publisher = nHandle.advertise<visualization_msgs::MarkerArray>("elevation_map_marker_array", 1,true);
29 
32 
33  dyn_rec_server_.setCallback(boost::bind(&ElevationVisualization::dynRecParamCallback, this, _1, _2));
34 
35  ros::spin();
36 }
37 
39 {
40 
41 }
42 
43 void ElevationVisualization::dynRecParamCallback(hector_elevation_visualization::ElevationVisualizationConfig &config, uint32_t level)
44 {
45  max_height_levels = config.max_height_levels;
46  min_height = config.min_height;
47  max_height = config.max_height;
48  color_factor = config.color_factor;
49  use_color_map = config.use_color_map;
50 
51  if(std::strcmp(elevation_map_frame_id.c_str(),(config.elevation_map_frame_id).c_str()))
52  {
53  elevation_map_frame_id = config.elevation_map_frame_id;
54 
56  }
57 }
58 
59 
60 void ElevationVisualization::visualize_map(const hector_elevation_msgs::ElevationGrid& elevation_map, tf::StampedTransform local_map_transform)
61 {
62  int current_height_level;
63 
64  for (unsigned i = 0; i < map_marker_array_msg.markers.size(); ++i)
65  {
66  map_marker_array_msg.markers[i].points.clear();
67  }
68  map_marker_array_msg.markers.clear();
69 
70 
71  // each array stores all cubes of one height level:
72  map_marker_array_msg.markers.resize(max_height_levels+1);
73 
74  for (int index_y = 0; index_y < (int)elevation_map.info.height; ++index_y)
75  {
76  for (int index_x = 0; index_x < (int)elevation_map.info.width; ++index_x)
77  {
78  // visualize only known cells
79  if(elevation_map.data[MAP_IDX(elevation_map.info.width, index_x, index_y)] != (int16_t)-elevation_map.info.zero_elevation)
80  {
81  geometry_msgs::Point cube_center;
82  Eigen::Vector2f index_map(index_x, index_y);
83  Eigen::Vector2f index_world = world_map_transform.getC1Coords(index_map);
84 
85  cube_center.x = index_world(0);//+elevation_map.info.resolution_xy/2.0;
86  cube_center.y = index_world(1);//+elevation_map.info.resolution_xy/2.0;
87  cube_center.z = (elevation_map.data[MAP_IDX(elevation_map.info.width, index_x, index_y)]-elevation_map.info.zero_elevation)*elevation_map.info.resolution_z;
88  current_height_level = max_height_levels/2+(int)round(std::min(std::max((double)cube_center.z+local_map_transform.getOrigin().z(), -(double)max_height), (double)max_height)*(double)max_height_levels/((double)max_height*2.0f));
89  map_marker_array_msg.markers[current_height_level].points.push_back(cube_center);
90 
91  if(use_color_map)
92  {
93  double h = (1.0 - std::min(std::max((cube_center.z-min_height)/ (max_height - min_height), 0.0), 1.0)) *color_factor;
94  map_marker_array_msg.markers[current_height_level].colors.push_back(heightMapColor(h));
95  }
96  }
97  }
98  }
99 
100  for (unsigned i = 0; i < map_marker_array_msg.markers.size(); ++i)
101  {
102  std::stringstream ss;
103  ss <<"Level "<<i;
104  map_marker_array_msg.markers[i].ns = ss.str();
105  map_marker_array_msg.markers[i].id = i;
106  map_marker_array_msg.markers[i].header.frame_id = "/map";
107  map_marker_array_msg.markers[i].header.stamp = elevation_map.header.stamp;
108  map_marker_array_msg.markers[i].lifetime = ros::Duration();
109  map_marker_array_msg.markers[i].type = visualization_msgs::Marker::CUBE_LIST;
110  map_marker_array_msg.markers[i].scale.x = elevation_map.info.resolution_xy;
111  map_marker_array_msg.markers[i].scale.y = elevation_map.info.resolution_xy;
112  map_marker_array_msg.markers[i].scale.z = elevation_map.info.resolution_z;
113  map_marker_array_msg.markers[i].color = marker_color;
114 
115  if (map_marker_array_msg.markers[i].points.size() > 0)
116  map_marker_array_msg.markers[i].action = visualization_msgs::Marker::ADD;
117  else
118  map_marker_array_msg.markers[i].action = visualization_msgs::Marker::DELETE;
119  }
120 
121 }
122 
123 
124 void ElevationVisualization::map_callback(const hector_elevation_msgs::ElevationGrid& elevation_map)
125 {
126  // Visualization of Elevation Map
128  {
129  tf::TransformListener listener;
130  tf::StampedTransform local_map_transform;
131 
132  // get local map transform
133  try
134  {
135  listener.waitForTransform("/base_footprint", "/map",ros::Time(0),ros::Duration(5.0));
136  listener.lookupTransform("/base_footprint", "/map", ros::Time(0), local_map_transform);
137  }
138  catch (tf::TransformException ex)
139  {
140  ROS_ERROR("%s",ex.what());
141  return;
142  }
143 
144  // init transform
145  nav_msgs::MapMetaData map_meta;
146  map_meta.resolution = elevation_map.info.resolution_xy;
147  map_meta.height = elevation_map.info.height;
148  map_meta.width = elevation_map.info.width;
149  map_meta.origin = elevation_map.info.origin;
150 
151  world_map_transform.setTransforms(map_meta);
152 
153  // visualize map
154  visualize_map(elevation_map, local_map_transform);
155 
156 
157  // publish map
159  }
160 }
161 
162 void ElevationVisualization::sys_message_callback(const std_msgs::String& string)
163 {
164  ROS_DEBUG("sysMsgCallback, msg contents: %s", string.data.c_str());
165 
166  if (string.data == "reset")
167  {
168  ROS_INFO("reset");
169 
170  for (unsigned i = 0; i < map_marker_array_msg.markers.size(); ++i)
171  {
172  map_marker_array_msg.markers[i].points.clear();
173  }
174  map_marker_array_msg.markers.clear();
175  }
176 }
177 
178 std_msgs::ColorRGBA ElevationVisualization::heightMapColor(double h)
179 {
180 
181  std_msgs::ColorRGBA color;
182  color.a = 1.0;
183  // blend over HSV-values (more colors)
184 
185  double s = 1.0;
186  double v = 1.0;
187 
188  h -= floor(h);
189  h *= 6;
190  int i;
191  double m, n, f;
192 
193  i = floor(h);
194  f = h - i;
195  if (!(i & 1))
196  f = 1 - f; // if i is even
197  m = v * (1 - s);
198  n = v * (1 - s * f);
199 
200  switch (i) {
201  case 6:
202  case 0:
203  color.r = v; color.g = n; color.b = m;
204  break;
205  case 1:
206  color.r = n; color.g = v; color.b = m;
207  break;
208  case 2:
209  color.r = m; color.g = v; color.b = n;
210  break;
211  case 3:
212  color.r = m; color.g = n; color.b = v;
213  break;
214  case 4:
215  color.r = n; color.g = m; color.b = v;
216  break;
217  case 5:
218  color.r = v; color.g = m; color.b = n;
219  break;
220  default:
221  color.r = 1; color.g = 0.5; color.b = 0.5;
222  break;
223  }
224 
225  return color;
226 }
227 
228 
229 
~ElevationVisualization()
Default deconstructor.
void publish(const boost::shared_ptr< M > &message) const
f
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void map_callback(const hector_elevation_msgs::ElevationGrid &elevation_map)
map_callback get called if a new elevation map is avaible
XmlRpcServer s
dynamic_reconfigure::Server< hector_elevation_visualization::ElevationVisualizationConfig > dyn_rec_server_
ROSCPP_DECL void spin(Spinner &spinner)
bool waitForTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, const ros::Duration &timeout, const ros::Duration &polling_sleep_duration=ros::Duration(0.01), std::string *error_msg=NULL) const
TFSIMD_FORCE_INLINE const tfScalar & z() const
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
#define MAP_IDX(sx, i, j)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
uint32_t getNumSubscribers() const
visualization_msgs::MarkerArray map_marker_array_msg
void dynRecParamCallback(hector_elevation_visualization::ElevationVisualizationConfig &config, uint32_t level)
dynRecParamCallback This function get called if new parameters has been set with the dynamic reconfig...
void visualize_map(const hector_elevation_msgs::ElevationGrid &elevation_map, tf::StampedTransform local_map_transform)
visualize_map calculates visualization markers to vizualize the elevation map in rviz ...
HectorMapTools::CoordinateTransformer< float > world_map_transform
#define ROS_ERROR(...)
ElevationVisualization()
Default constructor.
void sys_message_callback(const std_msgs::String &string)
sysMessageCallback This function listen to system messages
#define ROS_DEBUG(...)
static std_msgs::ColorRGBA heightMapColor(double h)
heightMapColor calculates the marker color as a function of height


hector_elevation_visualization
Author(s):
autogenerated on Mon Jun 10 2019 13:34:38