marker_display.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sstream>
31 
32 #include <tf/transform_listener.h>
33 
36 #include "rviz/display_context.h"
37 #include "rviz/frame_manager.h"
45 #include "rviz/validate_floats.h"
46 
48 
49 namespace rviz
50 {
51 
53 
55  : Display()
56 {
57  marker_topic_property_ = new RosTopicProperty( "Marker Topic", "visualization_marker",
58  QString::fromStdString( ros::message_traits::datatype<visualization_msgs::Marker>() ),
59  "visualization_msgs::Marker topic to subscribe to. <topic>_array will also"
60  " automatically be subscribed with type visualization_msgs::MarkerArray.",
61  this, SLOT( updateTopic() ));
62 
63  queue_size_property_ = new IntProperty( "Queue Size", 100,
64  "Advanced: set the size of the incoming Marker message queue. Increasing this is"
65  " useful if your incoming TF data is delayed significantly from your Marker data, "
66  "but it can greatly increase memory usage if the messages are big.",
67  this, SLOT( updateQueueSize() ));
69 
70  namespaces_category_ = new Property( "Namespaces", QVariant(), "", this );
71 }
72 
74 {
76  fixed_frame_.toStdString(),
78  update_nh_ );
79 
83 
85 }
86 
88 {
89  if ( initialized() )
90  {
91  unsubscribe();
92 
93  clearMarkers();
94 
95  delete tf_filter_;
96  }
97 }
98 
99 void MarkerDisplay::load(const Config& config)
100 {
101  Display::load(config);
102 
103  Config c = config.mapGetChild("Namespaces");
104  for( Config::MapIterator iter = c.mapIterator(); iter.isValid(); iter.advance() )
105  {
106  QString key = iter.currentKey();
107  const Config& child = iter.currentChild();
108  namespace_config_enabled_state_[key] = child.getValue().toBool();
109  }
110 }
111 
113 {
114  markers_.clear();
115  markers_with_expiration_.clear();
116  frame_locked_markers_.clear();
117  tf_filter_->clear();
119  namespaces_.clear();
120 }
121 
123 {
124  subscribe();
125 }
126 
128 {
129  unsubscribe();
130  tf_filter_->clear();
131 
132  clearMarkers();
133 }
134 
136 {
138 }
139 
141 {
142  unsubscribe();
143  subscribe();
144 }
145 
147 {
148  if( !isEnabled() )
149  {
150  return;
151  }
152 
153  std::string marker_topic = marker_topic_property_->getTopicStd();
154  if( !marker_topic.empty() )
155  {
157  sub_.unsubscribe();
158 
159  try
160  {
161  sub_.subscribe( update_nh_, marker_topic, queue_size_property_->getInt() );
163  setStatus( StatusProperty::Ok, "Topic", "OK" );
164  }
165  catch( ros::Exception& e )
166  {
167  setStatus( StatusProperty::Error, "Topic", QString("Error subscribing: ") + e.what() );
168  }
169  }
170 }
171 
173 {
174  sub_.unsubscribe();
176 }
177 
179 {
180  deleteMarkerStatus( id );
181 
182  M_IDToMarker::iterator it = markers_.find( id );
183  if( it != markers_.end() )
184  {
185  markers_with_expiration_.erase(it->second);
186  frame_locked_markers_.erase(it->second);
187  markers_.erase(it);
188  }
189 }
190 
191 void MarkerDisplay::deleteMarkersInNamespace( const std::string& ns )
192 {
193  std::vector<MarkerID> to_delete;
194 
195  // TODO: this is inefficient, should store every in-use id per namespace and lookup by that
196  M_IDToMarker::iterator marker_it = markers_.begin();
197  M_IDToMarker::iterator marker_end = markers_.end();
198  for (; marker_it != marker_end; ++marker_it)
199  {
200  if (marker_it->first.first == ns)
201  {
202  to_delete.push_back(marker_it->first);
203  }
204  }
205 
206  {
207  std::vector<MarkerID>::iterator it = to_delete.begin();
208  std::vector<MarkerID>::iterator end = to_delete.end();
209  for (; it != end; ++it)
210  {
211  deleteMarker(*it);
212  }
213  }
214 }
215 
217 {
218  std::vector<MarkerID> to_delete;
219  M_IDToMarker::iterator marker_it = markers_.begin();
220  for (; marker_it != markers_.end(); ++marker_it)
221  {
222  to_delete.push_back(marker_it->first);
223  }
224 
225  for (std::vector<MarkerID>::iterator it = to_delete.begin(); it != to_delete.end(); ++it)
226  {
227  deleteMarker( *it );
228  }
229 }
230 
231 void MarkerDisplay::setMarkerStatus(MarkerID id, StatusLevel level, const std::string& text)
232 {
233  std::stringstream ss;
234  ss << id.first << "/" << id.second;
235  std::string marker_name = ss.str();
236  setStatusStd(level, marker_name, text);
237 }
238 
240 {
241  std::stringstream ss;
242  ss << id.first << "/" << id.second;
243  std::string marker_name = ss.str();
244  deleteStatusStd(marker_name);
245 }
246 
247 void MarkerDisplay::incomingMarkerArray(const visualization_msgs::MarkerArray::ConstPtr& array)
248 {
249  checkMarkerArrayMsg(*array, this);
250  for (const visualization_msgs::Marker& marker : array->markers)
251  {
252  tf_filter_->add(visualization_msgs::Marker::Ptr(new visualization_msgs::Marker(marker)));
253  }
254 }
255 
256 void MarkerDisplay::incomingMarker( const visualization_msgs::Marker::ConstPtr& marker )
257 {
258  checkMarkerMsg(*marker, this);
259  boost::mutex::scoped_lock lock(queue_mutex_);
260 
261  message_queue_.push_back(marker);
262 }
263 
265 {
266  visualization_msgs::Marker::ConstPtr marker = marker_evt.getConstMessage();
267  if (marker->action == visualization_msgs::Marker::DELETE ||
268  marker->action == 3) // TODO: visualization_msgs::Marker::DELETEALL when message changes in a future version of ROS
269  {
270  return this->processMessage(marker);
271  }
272  std::string authority = marker_evt.getPublisherName();
273  std::string error = context_->getFrameManager()->discoverFailureReason(marker->header.frame_id, marker->header.stamp, authority, reason);
274  setMarkerStatus(MarkerID(marker->ns, marker->id), StatusProperty::Error, error);
275 }
276 
277 bool validateFloats(const visualization_msgs::Marker& msg)
278 {
279  bool valid = true;
280  valid = valid && validateFloats(msg.pose);
281  valid = valid && validateFloats(msg.scale);
282  valid = valid && validateFloats(msg.color);
283  valid = valid && validateFloats(msg.points);
284  return valid;
285 }
286 
287 void MarkerDisplay::processMessage( const visualization_msgs::Marker::ConstPtr& message )
288 {
289  if ( !validateFloats( *message ))
290  {
291  setMarkerStatus( MarkerID( message->ns, message->id ), StatusProperty::Error,
292  "Contains invalid floating point values (nans or infs)" );
293  return;
294  }
295 
296  switch ( message->action )
297  {
298  case visualization_msgs::Marker::ADD:
299  processAdd( message );
300  break;
301 
302  case visualization_msgs::Marker::DELETE:
303  processDelete( message );
304  break;
305 
306  case 3: // TODO: visualization_msgs::Marker::DELETEALL when message changes in a future version of ROS
308  break;
309 
310  default:
311  ROS_ERROR( "Unknown marker action: %d\n", message->action );
312  }
313 }
314 
315 void MarkerDisplay::processAdd( const visualization_msgs::Marker::ConstPtr& message )
316 {
317  QString namespace_name = QString::fromStdString( message->ns );
318  M_Namespace::iterator ns_it = namespaces_.find( namespace_name );
319  if( ns_it == namespaces_.end() )
320  {
321  ns_it = namespaces_.insert( namespace_name, new MarkerNamespace( namespace_name, namespaces_category_, this ));
322 
323  // Adding a new namespace, determine if it's configured to be disabled
324  if( namespace_config_enabled_state_.count(namespace_name) > 0 &&
325  !namespace_config_enabled_state_[namespace_name] )
326  {
327  ns_it.value()->setValue(false); // Disable the namespace
328  }
329  }
330 
331  if( !ns_it.value()->isEnabled() )
332  {
333  return;
334  }
335 
336  bool create = true;
337  MarkerBasePtr marker;
338 
339  M_IDToMarker::iterator it = markers_.find( MarkerID(message->ns, message->id) );
340  if ( it != markers_.end() )
341  {
342  marker = it->second;
343  markers_with_expiration_.erase(marker);
344  if ( message->type == marker->getMessage()->type )
345  {
346  create = false;
347  }
348  else
349  {
350  markers_.erase( it );
351  }
352  }
353 
354  if ( create )
355  {
356  marker.reset(createMarker(message->type, this, context_, scene_node_));
357  if (marker)
358  {
359  markers_.insert(std::make_pair(MarkerID(message->ns, message->id), marker));
360  }
361  }
362 
363  if (marker)
364  {
365  marker->setMessage(message);
366 
367  if (message->lifetime.toSec() > 0.0001f)
368  {
369  markers_with_expiration_.insert(marker);
370  }
371 
372  if (message->frame_locked)
373  {
374  frame_locked_markers_.insert(marker);
375  }
376 
378  }
379 }
380 
381 void MarkerDisplay::processDelete( const visualization_msgs::Marker::ConstPtr& message )
382 {
383  deleteMarker(MarkerID(message->ns, message->id));
384 
386 }
387 
388 void MarkerDisplay::update(float wall_dt, float ros_dt)
389 {
390  V_MarkerMessage local_queue;
391 
392  {
393  boost::mutex::scoped_lock lock(queue_mutex_);
394 
395  local_queue.swap( message_queue_ );
396  }
397 
398  if ( !local_queue.empty() )
399  {
400  V_MarkerMessage::iterator message_it = local_queue.begin();
401  V_MarkerMessage::iterator message_end = local_queue.end();
402  for ( ; message_it != message_end; ++message_it )
403  {
404  visualization_msgs::Marker::ConstPtr& marker = *message_it;
405 
406  processMessage( marker );
407  }
408  }
409 
410  {
411  S_MarkerBase::iterator it = markers_with_expiration_.begin();
412  S_MarkerBase::iterator end = markers_with_expiration_.end();
413  for (; it != end;)
414  {
415  MarkerBasePtr marker = *it;
416  if (marker->expired())
417  {
418  ++it;
419  deleteMarker(marker->getID());
420  }
421  else
422  {
423  ++it;
424  }
425  }
426  }
427 
428  {
429  S_MarkerBase::iterator it = frame_locked_markers_.begin();
430  S_MarkerBase::iterator end = frame_locked_markers_.end();
431  for (; it != end; ++it)
432  {
433  MarkerBasePtr marker = *it;
434  marker->updateFrameLocked();
435  }
436  }
437 }
438 
440 {
441  tf_filter_->setTargetFrame( fixed_frame_.toStdString() );
442 
443  clearMarkers();
444 }
445 
447 {
448  Display::reset();
449  clearMarkers();
450 }
451 
452 void MarkerDisplay::setTopic( const QString &topic, const QString &datatype )
453 {
455 }
456 
458 // MarkerNamespace
459 
460 MarkerNamespace::MarkerNamespace( const QString& name, Property* parent_property, MarkerDisplay* owner )
461  : BoolProperty( name, true,
462  "Enable/disable all markers in this namespace.",
463  parent_property )
464  , owner_( owner )
465 {
466  // Can't do this connect in chained constructor above because at
467  // that point it doesn't really know that "this" is a
468  // MarkerNamespace*, so the signal doesn't get connected.
469  connect( this, SIGNAL( changed() ), this, SLOT( onEnableChanged() ));
470 }
471 
473 {
474  if( !isEnabled() )
475  {
476  owner_->deleteMarkersInNamespace( getName().toStdString() );
477  }
478 
479  // Update the configuration that stores the enabled state of all markers
481 }
482 
483 } // namespace rviz
484 
void setStatusStd(StatusProperty::Level level, const std::string &name, const std::string &text)
Show status level and text, using a std::string. Convenience function which converts std::string to Q...
Definition: display.h:157
void processMessage(const visualization_msgs::Marker::ConstPtr &message)
Processes a marker message.
void deleteStatusStd(const std::string &name)
Delete the status entry with the given std::string name. This is thread-safe.
Definition: display.h:166
virtual void update(float wall_dt, float ros_dt)
Called periodically by the visualization manager.
virtual void setString(const QString &str)
void processAdd(const visualization_msgs::Marker::ConstPtr &message)
Processes an "Add" marker message.
void connectInput(F &f)
std::string discoverFailureReason(const std::string &frame_id, const ros::Time &stamp, const std::string &caller_id, tf::FilterFailureReason reason)
Create a description of a transform problem.
void processDelete(const visualization_msgs::Marker::ConstPtr &message)
Processes a "Delete" marker message.
S_MarkerBase frame_locked_markers_
void setMarkerStatus(MarkerID id, StatusLevel level, const std::string &text)
RosTopicProperty * marker_topic_property_
virtual tf::TransformListener * getTFClient() const =0
Convenience function: returns getFrameManager()->getTFClient().
MarkerNamespace(const QString &name, Property *parent_property, MarkerDisplay *owner)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz...
Definition: display.h:256
void changed()
Emitted by setValue() just after the value has changed.
void add(const MEvent &evt)
std::vector< visualization_msgs::Marker::ConstPtr > V_MarkerMessage
M_Namespace namespaces_
virtual int getInt() const
Return the internal property value as an integer.
Definition: int_property.h:73
MarkerDisplay * owner_
ros::NodeHandle update_nh_
A NodeHandle whose CallbackQueue is run from the main GUI thread (the "update" thread).
Definition: display.h:269
A single element of a property tree, with a name, value, description, and possibly children...
Definition: property.h:100
IntProperty * queue_size_property_
boost::mutex queue_mutex_
virtual void reset()
Called to tell the display to clear its state.
void incomingMarker(const visualization_msgs::Marker::ConstPtr &marker)
ROS callback notifying us of a new marker.
bool checkMarkerMsg(const visualization_msgs::Marker &marker, MarkerDisplay *owner)
void setMin(int min)
bool isValid()
Return true if the iterator currently points to a valid entry, false if not.
Definition: config.cpp:385
virtual void unsubscribe()
Unsubscribes from the "visualization_marker" "visualization_marker_array" topics. ...
Property specialized to provide max/min enforcement for integers.
Definition: int_property.h:38
Ogre::SceneNode * scene_node_
The Ogre::SceneNode to hold all 3D scene elements shown by this Display.
Definition: display.h:264
M_EnabledState namespace_config_enabled_state_
void deleteMarker(MarkerID id)
bool isEnabled() const
Return true if this Display is enabled, false if not.
Definition: display.cpp:281
Configuration data storage class.
Definition: config.h:125
Property * namespaces_category_
void incomingMarkerArray(const visualization_msgs::MarkerArray::ConstPtr &array)
Process a MarkerArray message.
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:281
virtual void setQueueSize(uint32_t new_queue_size)
const std::string & getPublisherName() const
MarkerBase * createMarker(int marker_type, MarkerDisplay *owner, DisplayContext *context, Ogre::SceneNode *parent_node)
void deleteMarkersInNamespace(const std::string &ns)
Delete all the markers within the given namespace.
std::pair< std::string, int32_t > MarkerID
bool checkMarkerArrayMsg(const visualization_msgs::MarkerArray &array, MarkerDisplay *owner)
const boost::shared_ptr< ConstMessage > & getConstMessage() const
M_IDToMarker markers_
Map of marker id to the marker info structure.
virtual void removeChildren(int start_index=0, int count=-1)
Remove and delete some or all child Properties. Does not change the value of this Property...
Definition: property.cpp:100
bool isEnabled() const
bool validateFloats(const sensor_msgs::CameraInfo &msg)
virtual void reset()
Called to tell the display to clear its state.
Definition: display.cpp:300
V_MarkerMessage message_queue_
virtual void load(const Config &config)
Load the settings for this display from the given Config node, which must be a map.
virtual void onDisable()
Derived classes override this to do the actual work of disabling themselves.
QVariant getValue() const
If this config object is valid and is a Value type, this returns its value. Otherwise it returns an i...
Definition: config.cpp:312
void deleteMarkerStatus(MarkerID id)
virtual void fixedFrameChanged()
Called by setFixedFrame(). Override to respond to changes to fixed_frame_.
Iterator class for looping over all entries in a Map type Config Node.
Definition: config.h:282
message_filters::Connection registerFailureCallback(const FailureCallback &callback)
virtual FrameManager * getFrameManager() const =0
Return the FrameManager instance.
ros::Subscriber array_sub_
message_filters::Subscriber< visualization_msgs::Marker > sub_
friend class MarkerNamespace
tf::MessageFilter< visualization_msgs::Marker > * tf_filter_
void failedMarker(const ros::MessageEvent< visualization_msgs::Marker > &marker_evt, tf::FilterFailureReason reason)
S_MarkerBase markers_with_expiration_
virtual void subscribe()
Subscribes to the "visualization_marker" and "visualization_marker_array" topics. ...
virtual void queueRender()=0
Queues a render. Multiple calls before a render happens will only cause a single render.
virtual void load(const Config &config)
Load the settings for this display from the given Config node, which must be a map.
Definition: display.cpp:242
virtual void onEnable()
Derived classes override this to do the actual work of enabling themselves.
MapIterator mapIterator() const
Return a new iterator for looping over key/value pairs.
Definition: config.cpp:344
virtual void setTopic(const QString &topic, const QString &datatype)
Set the ROS topic to listen to for this display.
void deleteAllMarkers()
Delete all known markers to this plugin, regardless of id or namespace.
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)
Property(const QString &name=QString(), const QVariant default_value=QVariant(), const QString &description=QString(), Property *parent=0, const char *changed_slot=0, QObject *receiver=0)
Constructor.
Definition: property.cpp:54
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
void setTargetFrame(const std::string &target_frame)
Config mapGetChild(const QString &key) const
If the referenced Node is a Map and it has a child with the given key, return a reference to the chil...
Definition: config.cpp:201
bool initialized() const
Returns true if the display has been initialized.
Definition: display.h:247
std::string getTopicStd() const
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
Displays "markers" sent in by other ROS nodes on the "visualization_marker" topic.
#define ROS_ERROR(...)
void clearMarkers()
Removes all the markers.
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
Show status level and text. This is thread-safe.
Definition: display.cpp:186
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:159
Connection registerCallback(const C &callback)
virtual void onInitialize()
Override this function to do subclass-specific initialization.


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51