message_filter_display.h
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 #ifndef MESSAGE_FILTER_DISPLAY_H
30 #define MESSAGE_FILTER_DISPLAY_H
31 
32 #ifndef Q_MOC_RUN
34 #include <tf2_ros/message_filter.h>
35 #endif
36 
37 #include <rviz/display_context.h>
38 #include <rviz/frame_manager.h>
41 
42 #include <rviz/display.h>
43 #include <rviz/rviz_export.h>
44 
46 
47 namespace rviz
48 {
52 class RVIZ_EXPORT _RosTopicDisplay : public Display
53 {
54  Q_OBJECT
55 public:
57  {
58  topic_property_ = new RosTopicProperty("Topic", "", "", "", this, &_RosTopicDisplay::updateTopic);
59  unreliable_property_ = new BoolProperty("Unreliable", false, "Prefer UDP topic transport", this,
61  queue_size_property_ =
62  new IntProperty("Queue Size", 10,
63  "Size of TF message filter queue.\n"
64  "Increasing this is useful if your TF data is delayed significantly "
65  "w.r.t. your data, but it can greatly increase memory usage as well.",
67  queue_size_property_->setMin(0);
68  qRegisterMetaType<boost::shared_ptr<const void>>(); // required to send via queued connection
69  }
70 
71 protected Q_SLOTS:
72  virtual void updateTopic() = 0;
73  virtual void updateQueueSize() = 0;
74 private Q_SLOTS:
75  virtual void processTypeErasedMessage(boost::shared_ptr<const void> type_erased_msg) = 0;
76 
77 protected:
81 };
82 
89 template <class MessageType>
91 {
92  // No Q_OBJECT macro here, moc does not support Q_OBJECT in a templated class.
93 public:
97 
99  {
100  QString message_type = QString::fromStdString(ros::message_traits::datatype<MessageType>());
101  topic_property_->setMessageType(message_type);
102  topic_property_->setDescription(message_type + " topic to subscribe to.");
103  }
104 
105  void onInitialize() override
106  {
107  tf_filter_ =
109  static_cast<uint32_t>(queue_size_property_->getInt()),
110  update_nh_);
111 
113  tf_filter_->registerCallback(
114  boost::bind(&MessageFilterDisplay<MessageType>::incomingMessage, this, boost::placeholders::_1));
116  }
117 
119  {
122  delete tf_filter_;
123  }
124 
125  void reset() override
126  {
127  Display::reset();
128  tf_filter_->clear();
129  messages_received_ = 0;
130  }
131 
132  void setTopic(const QString& topic, const QString& /*datatype*/) override
133  {
134  topic_property_->setString(topic);
135  }
136 
137 protected:
138  void updateTopic() override
139  {
140  unsubscribe();
141  reset();
142  subscribe();
144  }
145 
146  void updateQueueSize() override
147  {
148  tf_filter_->setQueueSize(static_cast<uint32_t>(queue_size_property_->getInt()));
149  subscribe();
150  }
151 
152  virtual void subscribe()
153  {
154  if (!isEnabled())
155  {
156  return;
157  }
158 
159  try
160  {
161  ros::TransportHints transport_hint = ros::TransportHints().reliable();
162  // Determine UDP vs TCP transport for user selection.
164  {
165  transport_hint = ros::TransportHints().unreliable();
166  }
168  static_cast<uint32_t>(queue_size_property_->getInt()), transport_hint);
169  setStatus(StatusProperty::Ok, "Topic", "OK");
170  }
171  catch (ros::Exception& e)
172  {
173  setStatus(StatusProperty::Error, "Topic", QString("Error subscribing: ") + e.what());
174  }
175  }
176 
177  virtual void unsubscribe()
178  {
179  sub_.unsubscribe();
180  }
181 
182  void onEnable() override
183  {
184  subscribe();
185  }
186 
187  void onDisable() override
188  {
189  unsubscribe();
190  reset();
191  }
192 
193  void fixedFrameChanged() override
194  {
195  tf_filter_->setTargetFrame(fixed_frame_.toStdString());
196  reset();
197  }
198 
202  void incomingMessage(const typename MessageType::ConstPtr& msg)
203  {
204  if (!msg)
205  {
206  return;
207  }
208  // process message synchronously in main GUI thread to avoid race conditions
209  QMetaObject::invokeMethod(this, "processTypeErasedMessage", Qt::QueuedConnection,
211  boost::static_pointer_cast<const void>(msg)));
212  }
213 
215  {
216  if (!isEnabled())
217  return;
218 
219  auto msg = boost::static_pointer_cast<const MessageType>(type_erased_msg);
221  setStatus(StatusProperty::Ok, "Topic", QString::number(messages_received_) + " messages received");
222 
223  processMessage(msg);
224  }
225 
229  virtual void processMessage(const typename MessageType::ConstPtr& msg) = 0;
230 
234 };
235 
236 } // end namespace rviz
237 
238 #endif // MESSAGE_FILTER_DISPLAY_H
ros::TransportHints::unreliable
TransportHints & unreliable()
rviz::BoolProperty::getBool
virtual bool getBool() const
Definition: bool_property.cpp:48
rviz::Display::isEnabled
bool isEnabled() const
Return true if this Display is enabled, false if not.
Definition: display.cpp:271
rviz::MessageFilterDisplay
Display subclass using a tf2_ros::MessageFilter, templated on the ROS message type.
Definition: message_filter_display.h:90
rviz::RosTopicProperty
Definition: ros_topic_property.h:39
rviz::MessageFilterDisplay::reset
void reset() override
Called to tell the display to clear its state.
Definition: message_filter_display.h:125
frame_manager.h
rviz::DisplayContext::queueRender
virtual void queueRender()=0
Queues a render. Multiple calls before a render happens will only cause a single render.
rviz::MessageFilterDisplay::unsubscribe
virtual void unsubscribe()
Definition: message_filter_display.h:177
rviz::MessageFilterDisplay::updateQueueSize
void updateQueueSize() override
Definition: message_filter_display.h:146
tf2_ros::MessageFilter< MessageType >
rviz::MessageFilterDisplay::fixedFrameChanged
void fixedFrameChanged() override
Called by setFixedFrame(). Override to respond to changes to fixed_frame_.
Definition: message_filter_display.h:193
boost::shared_ptr
rviz::_RosTopicDisplay
Helper superclass for MessageFilterDisplay, needed because Qt's moc and c++ templates don't work nice...
Definition: message_filter_display.h:52
rviz::StatusProperty::Error
@ Error
Definition: status_property.h:46
ros_topic_property.h
rviz::MessageFilterDisplay::incomingMessage
void incomingMessage(const typename MessageType::ConstPtr &msg)
Incoming message callback. Checks if the message pointer is valid, increments messages_received_,...
Definition: message_filter_display.h:202
int_property.h
rviz::BoolProperty
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
rviz::EditableEnumProperty::setString
virtual void setString(const QString &str)
Definition: editable_enum_property.cpp:74
ros::TransportHints
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(ros::Time)
ros::Exception
rviz::Display::fixed_frame_
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:312
rviz::MessageFilterDisplay::subscribe
virtual void subscribe()
Definition: message_filter_display.h:152
message_filters::Subscriber::unsubscribe
void unsubscribe()
rviz::MessageFilterDisplay::onEnable
void onEnable() override
Derived classes override this to do the actual work of enabling themselves.
Definition: message_filter_display.h:182
rviz::_RosTopicDisplay::queue_size_property_
IntProperty * queue_size_property_
Definition: message_filter_display.h:80
message_filters::Subscriber< MessageType >
rviz::Display
Definition: display.h:63
rviz::MessageFilterDisplay::MessageFilterDisplay
MessageFilterDisplay()
Definition: message_filter_display.h:98
ros::TransportHints::reliable
TransportHints & reliable()
rviz::MessageFilterDisplay::setTopic
void setTopic(const QString &topic, const QString &) override
Set the ROS topic to listen to for this display.
Definition: message_filter_display.h:132
rviz::Display::setStatus
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
Show status level and text. This is thread-safe.
Definition: display.cpp:176
tf2_ros::MessageFilter::clear
void clear()
message_filter.h
rviz::_RosTopicDisplay::updateQueueSize
virtual void updateQueueSize()=0
rviz
Definition: add_display_dialog.cpp:54
subscriber.h
rviz::_RosTopicDisplay::unreliable_property_
BoolProperty * unreliable_property_
Definition: message_filter_display.h:79
rviz::StatusProperty::Ok
@ Ok
Definition: status_property.h:44
rviz::MessageFilterDisplay::processTypeErasedMessage
void processTypeErasedMessage(boost::shared_ptr< const void > type_erased_msg) override
Definition: message_filter_display.h:214
rviz::MessageFilterDisplay::MFDClass
MessageFilterDisplay< MessageType > MFDClass
Convenience typedef so subclasses don't have to use the long templated class name to refer to their s...
Definition: message_filter_display.h:96
rviz::_RosTopicDisplay::_RosTopicDisplay
_RosTopicDisplay()
Definition: message_filter_display.h:56
rviz::MessageFilterDisplay::~MessageFilterDisplay
~MessageFilterDisplay() override
Definition: message_filter_display.h:118
tf2_ros::MessageFilter::setTargetFrame
void setTargetFrame(const std::string &target_frame)
rviz::_RosTopicDisplay::topic_property_
RosTopicProperty * topic_property_
Definition: message_filter_display.h:78
rviz::DisplayContext::getFrameManager
virtual FrameManager * getFrameManager() const =0
Return the FrameManager instance.
rviz::MessageFilterDisplay::messages_received_
uint32_t messages_received_
Definition: message_filter_display.h:233
tf2_ros::MessageFilter::setQueueSize
virtual void setQueueSize(uint32_t new_queue_size)
rviz::RosTopicProperty::getTopicStd
std::string getTopicStd() const
Definition: ros_topic_property.h:85
tf2_ros::MessageFilter::connectInput
void connectInput(F &f)
message_filters::Subscriber::subscribe
void subscribe()
rviz::MessageFilterDisplay::onInitialize
void onInitialize() override
Override this function to do subclass-specific initialization.
Definition: message_filter_display.h:105
rviz::MessageFilterDisplay::onDisable
void onDisable() override
Derived classes override this to do the actual work of disabling themselves.
Definition: message_filter_display.h:187
rviz::Property::setDescription
virtual void setDescription(const QString &description)
Set the description.
Definition: property.cpp:169
rviz::_RosTopicDisplay::updateTopic
virtual void updateTopic()=0
display.h
rviz::Display::context_
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz....
Definition: display.h:287
rviz::RosTopicProperty::setMessageType
void setMessageType(const QString &message_type)
Definition: ros_topic_property.cpp:49
rviz::FrameManager::registerFilterForTransformStatusCheck
void registerFilterForTransformStatusCheck(tf2_ros::MessageFilter< M > *filter, Display *display)
Definition: frame_manager.h:194
rviz::MessageFilterDisplay::tf_filter_
tf2_ros::MessageFilter< MessageType > * tf_filter_
Definition: message_filter_display.h:232
rviz::Display::reset
virtual void reset()
Called to tell the display to clear its state.
Definition: display.cpp:290
rviz::IntProperty::getInt
virtual int getInt() const
Return the internal property value as an integer.
Definition: int_property.h:96
display_context.h
rviz::MessageFilterDisplay::processMessage
virtual void processMessage(const typename MessageType::ConstPtr &msg)=0
Implement this to process the contents of a message.
rviz::DisplayContext::getTF2BufferPtr
std::shared_ptr< tf2_ros::Buffer > getTF2BufferPtr() const
Convenience function: returns getFrameManager()->getTF2BufferPtr().
Definition: display_context.h:98
rviz::Display::update_nh_
ros::NodeHandle update_nh_
A NodeHandle whose CallbackQueue is run from the main GUI thread (the "update" thread).
Definition: display.h:300
rviz::MessageFilterDisplay::updateTopic
void updateTopic() override
Definition: message_filter_display.h:138
rviz::IntProperty
Property specialized to provide max/min enforcement for integers.
Definition: int_property.h:37
rviz::MessageFilterDisplay::sub_
message_filters::Subscriber< MessageType > sub_
Definition: message_filter_display.h:231


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri May 10 2024 02:32:22