plot.cpp
Go to the documentation of this file.
3 
4 namespace toposens_markers
5 {
10 {
11  // Get launch paramaters
12  std::string scans_topic;
13  private_nh.getParam("sensor_mesh",
14  _sensor_mesh); //, "package://toposens_description/meshes/TS3.stl");
15  private_nh.getParam("frame_id", _frame_id); //, "toposens");
16  private_nh.getParam("target_frame", _target_frame); //, "toposens");
17  private_nh.getParam("scans_topic", scans_topic); //, "ts_scans");
18 
19  // Set up RViz
21  _rviz->enableBatchPublishing();
22 
23  // Set up dynamic reconfigure to change marker settings
24  _srv = std::make_shared<Cfg>(private_nh);
25  Cfg::CallbackType f = boost::bind(&Plot::_reconfig, this, _1, _2);
26  _srv->setCallback(f);
27 
28  // Set up transform listener so points from each sensor can be transformed to
29  // global coordinates
31  _tf2_scans_sub.subscribe(nh, scans_topic, 400);
33  _target_frame, 300, nh);
34 
35  // Subscribe to topics
36  _scans_sub = nh.subscribe(scans_topic, 300, &Plot::_plot, this);
38  nh.subscribe<tf2_msgs::TFMessage>("tf_static", 10, &Plot::_staticTFCallback, this);
39 
40  _rviz->enableBatchPublishing();
41 
42  ros::Publisher marker_pub = nh.advertise<visualization_msgs::Marker>("visualization_marker", 1);
43  _rviz->waitForSubscriber(marker_pub, 3);
44 }
45 
48 {
49  delete _tf2_listener;
50  delete _tf2_filter;
51 }
52 
60 void Plot::_reconfig(TsMarkersConfig &cfg, uint32_t level)
61 {
62  if ((int)level > 2)
63  {
64  ROS_INFO("Update skipped: Parameter not recognized");
65  return;
66  }
67 
68  if (cfg.lifetime == 0)
69  {
70  cfg.lifetime = 0.01;
71  }
72 
73  _color_mode = cfg.point_color;
74  _color_range = cfg.color_range;
76  _lifetime = cfg.lifetime;
77  _rviz->setLifetime(_lifetime);
78  _rviz->setGlobalScale(cfg.scale);
79  if (level == -1)
80  ROS_INFO("Marker settings initialized");
81  else
82  ROS_INFO("Marker settings updated");
83 }
84 
91 void Plot::_plot(const toposens_msgs::TsScan::ConstPtr &msg)
92 {
93  // Get the color of the points
94  std_msgs::ColorRGBA color;
95 
96  if (_color_mode == 3)
97  {
98  if (_pose_map.size() > 0)
99  {
100  color = _color_map[msg->header.frame_id];
101  }
102  else
103  {
104  color = _rviz->getColor(rviz_visual_tools::colors::RED);
105  }
106  }
107 
108  for (auto it = msg->points.begin(); it != msg->points.end(); ++it)
109  {
110  // Copy TsPoint into PointStamped
111  toposens_msgs::TsPoint pt = *it;
112  if (_pose_map.size() > 0)
113  {
114  geometry_msgs::PointStamped ps;
115  ps.point.x = pt.location.x;
116  ps.point.y = pt.location.y;
117  ps.point.z = pt.location.z;
118  ps.header.frame_id = msg->header.frame_id;
119  ps.header.stamp = ros::Time(0);
120 
121  // Perform the coordinate transformation
122  try
123  {
125  }
126  catch (tf2::TransformException ex)
127  {
128  ROS_ERROR("TsPoint transformation failed: %s", ex.what());
129  }
130 
131  // Publish the point as a sphere
132  pt.location.x = ps.point.x;
133  pt.location.y = ps.point.y;
134  pt.location.z = ps.point.z;
135  }
136 
137  if (_color_mode == 0)
138  {
139  color = _rviz->getColorScale(pt.location.x / std::max(_color_range, pt.location.x));
140  }
141  else if (_color_mode == 1)
142  {
143  float offset_y = pt.location.y + _half_color_range;
144  if (offset_y < 0)
145  {
146  color = _rviz->getColorScale(0);
147  }
148  else
149  {
150  color = _rviz->getColorScale(offset_y / std::max(_color_range, offset_y));
151  }
152  }
153  else if (_color_mode == 2)
154  {
155  float offset_z = pt.location.z + _half_color_range;
156  if (offset_z < 0)
157  {
158  color = _rviz->getColorScale(0);
159  }
160  else
161  {
162  color = _rviz->getColorScale(offset_z / std::max(_color_range, offset_z));
163  }
164  }
165 
166  Eigen::Vector3d location = _rviz->convertPoint32(pt.location);
167  geometry_msgs::Vector3 scale = _rviz->getScale(_baseScale, pt.intensity);
168  if (scale.x > 0) _rviz->publishSphere(location, color, scale, kMarkersNs);
169  }
170 
171  // Perform batch publish to RViz
172  _rviz->trigger();
173 }
174 
181 void Plot::_staticTFCallback(const tf2_msgs::TFMessage::ConstPtr &tf)
182 {
183  float color_scale = tf->transforms.size() - 1;
184  if (color_scale == 0)
185  {
186  color_scale++;
187  }
188  for (int i = 0; i < tf->transforms.size(); i++)
189  {
190  if (!strncmp(tf->transforms[i].header.frame_id.c_str(), _target_frame.c_str(),
191  _target_frame.length()))
192  {
193  geometry_msgs::Pose pose;
194  pose.position.x = tf->transforms[i].transform.translation.x;
195  pose.position.y = tf->transforms[i].transform.translation.y;
196  pose.position.z = tf->transforms[i].transform.translation.z;
197  pose.orientation.x = tf->transforms[i].transform.rotation.x;
198  pose.orientation.y = tf->transforms[i].transform.rotation.y;
199  pose.orientation.z = tf->transforms[i].transform.rotation.z;
200  pose.orientation.w = tf->transforms[i].transform.rotation.w;
201  _pose_map[tf->transforms[i].child_frame_id] = pose;
202  _mesh_color_map[tf->transforms[i].child_frame_id] = (rviz_visual_tools::colors)((i % 13) + 1);
203  _color_map[tf->transforms[i].child_frame_id] =
204  _rviz->getColor(_mesh_color_map[tf->transforms[i].child_frame_id]);
205  ROS_INFO_STREAM(tf->transforms[i].child_frame_id
206  << ", \tR: " << _color_map[tf->transforms[i].child_frame_id].r
207  << ", G: " << _color_map[tf->transforms[i].child_frame_id].g
208  << ", B: " << _color_map[tf->transforms[i].child_frame_id].b
209  << ", A: " << _color_map[tf->transforms[i].child_frame_id].a
210  << " \t\t Position - X: " << pose.position.x << " \t Y: " << pose.position.y
211  << " \t Z: " << pose.position.z);
212  }
213  }
214 }
215 
217 {
218  std::map<std::string, geometry_msgs::Pose>::iterator pose_it;
222 
223  _rviz->setLifetime(0);
224  for (pose_it = _pose_map.begin(); pose_it != _pose_map.end(); pose_it++)
225  {
226  geometry_msgs::Pose label_pose;
227  label_pose.position.x = pose_it->second.position.x - .1;
228  label_pose.position.y = pose_it->second.position.y;
229  label_pose.position.z = pose_it->second.position.z;
230  label_pose.orientation.x = pose_it->second.orientation.x;
231  label_pose.orientation.y = pose_it->second.orientation.y;
232  label_pose.orientation.z = pose_it->second.orientation.z;
233 
234  _rviz->publishAxis(pose_it->second, axis_scale);
235  _rviz->publishMesh(pose_it->second, _sensor_mesh, _mesh_color_map[pose_it->first], 1.0,
236  kMeshNs);
237  _rviz->publishText(label_pose, pose_it->first, text_color, text_scale, false);
238  }
239  _rviz->trigger();
240  _rviz->setLifetime(_lifetime);
241 }
242 
244 {
245  _rviz->setLifetime(0);
246  geometry_msgs::Pose pose;
247  pose.position.x = 0;
248  pose.position.y = 0;
249  pose.position.z = 0;
250  pose.orientation.x = 0;
251  pose.orientation.y = 0;
252  pose.orientation.z = 0;
253  pose.orientation.w = 0;
254  _rviz->publishMesh(pose, _sensor_mesh, rviz_visual_tools::colors::WHITE, 1.0, kMeshNs);
255  _rviz->trigger();
256  _rviz->setLifetime(_lifetime);
257 }
258 
259 } // namespace toposens_markers
Plot(ros::NodeHandle nh, ros::NodeHandle private_nh)
Definition: plot.cpp:9
void _plot(const toposens_msgs::TsScan::ConstPtr &msg)
Definition: plot.cpp:91
void _reconfig(TsMarkersConfig &cfg, uint32_t level)
Definition: plot.cpp:60
T & transform(const T &in, T &out, const std::string &target_frame, ros::Duration timeout=ros::Duration(0.0)) const
std::string _sensor_mesh
Definition: plot.h:117
std::string _target_frame
Definition: plot.h:109
f
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
tf2_ros::MessageFilter< toposens_msgs::TsScan > * _tf2_filter
Definition: plot.h:114
void _staticTFCallback(const tf2_msgs::TFMessage::ConstPtr &tf)
Definition: plot.cpp:181
#define ROS_ERROR(...)
std::map< std::string, rviz_visual_tools::colors > _mesh_color_map
Definition: plot.h:121
ros::Subscriber _scans_sub
Definition: plot.h:116
float _half_color_range
Definition: plot.h:102
void publishDefaultSensorMesh()
Definition: plot.cpp:243
static const std::string kMarkersTopic
Definition: plot.h:32
tf2_ros::Buffer _tf2_buffer
Definition: plot.h:112
#define ROS_INFO(...)
bool getParam(const std::string &key, std::string &s) const
std::shared_ptr< Cfg > _srv
Definition: plot.h:104
std::map< std::string, std_msgs::ColorRGBA > _color_map
Definition: plot.h:120
Visualizes published TsScans as native Rviz markers. Subscribes to a topic publishing TsScans and con...
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
static const auto _baseScale
Definition: plot.h:52
static const std::string kMarkersNs
Definition: plot.h:34
rviz_visual_tools::RvizVisualToolsPtr _rviz
Definition: plot.h:107
message_filters::Subscriber< toposens_msgs::TsScan > _tf2_scans_sub
Definition: plot.h:111
double _lifetime
Definition: plot.h:98
std::map< std::string, geometry_msgs::Pose > _pose_map
Definition: plot.h:119
#define ROS_INFO_STREAM(args)
void subscribe(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints=ros::TransportHints(), ros::CallbackQueueInterface *callback_queue=0)
void publishSensorMeshes()
Definition: plot.cpp:216
tf2_ros::TransformListener * _tf2_listener
Definition: plot.h:113
std::string _frame_id
Definition: plot.h:97
static const std::string kMeshNs
Definition: plot.h:36
ros::Subscriber _static_tf_sub
Definition: plot.h:118


toposens_markers
Author(s): Adi Singh, Sebastian Dengler, Christopher Lang, Roua Mokchah
autogenerated on Mon Feb 28 2022 23:57:46