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"
40 
41 #include "rviz/display.h"
42 #include "rviz/rviz_export.h"
43 
44 namespace rviz
45 {
49 class RVIZ_EXPORT _RosTopicDisplay : public Display
50 {
51  Q_OBJECT
52 public:
54  {
55  topic_property_ = new RosTopicProperty("Topic", "", "", "", this, SLOT(updateTopic()));
56  unreliable_property_ =
57  new BoolProperty("Unreliable", false, "Prefer UDP topic transport", this, SLOT(updateTopic()));
58  }
59 
60 protected Q_SLOTS:
61  virtual void updateTopic() = 0;
62 
63 protected:
66 };
67 
74 template <class MessageType>
76 {
77  // No Q_OBJECT macro here, moc does not support Q_OBJECT in a templated class.
78 public:
82 
83  MessageFilterDisplay() : tf_filter_(nullptr), messages_received_(0)
84  {
85  QString message_type = QString::fromStdString(ros::message_traits::datatype<MessageType>());
86  topic_property_->setMessageType(message_type);
87  topic_property_->setDescription(message_type + " topic to subscribe to.");
88  }
89 
90  void onInitialize() override
91  {
92  tf_filter_ = new tf2_ros::MessageFilter<MessageType>(*context_->getTF2BufferPtr(),
93  fixed_frame_.toStdString(), 10, update_nh_);
94 
95  tf_filter_->connectInput(sub_);
96  tf_filter_->registerCallback(
98  context_->getFrameManager()->registerFilterForTransformStatusCheck(tf_filter_, this);
99  }
100 
102  {
105  delete tf_filter_;
106  }
107 
108  void reset() override
109  {
110  Display::reset();
111  tf_filter_->clear();
112  // Quick fix for #1372. Can be removed if https://github.com/ros/geometry2/pull/402 is released
113  if (tf_filter_)
114  update_nh_.getCallbackQueue()->removeByID((uint64_t)tf_filter_);
115  messages_received_ = 0;
116  }
117 
118  void setTopic(const QString& topic, const QString& /*datatype*/) override
119  {
120  topic_property_->setString(topic);
121  }
122 
123 protected:
124  void updateTopic() override
125  {
126  unsubscribe();
127  reset();
128  subscribe();
129  context_->queueRender();
130  }
131 
132  virtual void subscribe()
133  {
134  if (!isEnabled())
135  {
136  return;
137  }
138 
139  try
140  {
141  ros::TransportHints transport_hint = ros::TransportHints().reliable();
142  // Determine UDP vs TCP transport for user selection.
143  if (unreliable_property_->getBool())
144  {
145  transport_hint = ros::TransportHints().unreliable();
146  }
147  sub_.subscribe(update_nh_, topic_property_->getTopicStd(), 10, transport_hint);
148  setStatus(StatusProperty::Ok, "Topic", "OK");
149  }
150  catch (ros::Exception& e)
151  {
152  setStatus(StatusProperty::Error, "Topic", QString("Error subscribing: ") + e.what());
153  }
154  }
155 
156  virtual void unsubscribe()
157  {
158  sub_.unsubscribe();
159  }
160 
161  void onEnable() override
162  {
163  subscribe();
164  }
165 
166  void onDisable() override
167  {
168  unsubscribe();
169  reset();
170  }
171 
172  void fixedFrameChanged() override
173  {
174  tf_filter_->setTargetFrame(fixed_frame_.toStdString());
175  reset();
176  }
177 
181  void incomingMessage(const typename MessageType::ConstPtr& msg)
182  {
183  if (!msg || !isEnabled())
184  {
185  return;
186  }
187 
188  ++messages_received_;
189  setStatus(StatusProperty::Ok, "Topic", QString::number(messages_received_) + " messages received");
190 
191  processMessage(msg);
192  }
193 
197  virtual void processMessage(const typename MessageType::ConstPtr& msg) = 0;
198 
202 };
203 
204 } // end namespace rviz
205 
206 #endif // MESSAGE_FILTER_DISPLAY_H
tf2_ros::MessageFilter< MessageType > * tf_filter_
void reset() override
Called to tell the display to clear its state.
void setTopic(const QString &topic, const QString &) override
Set the ROS topic to listen to for this display.
TransportHints & reliable()
MessageFilterDisplay< MessageType > MFDClass
Convenience typedef so subclasses don&#39;t have to use the long templated class name to refer to their s...
RosTopicProperty * topic_property_
void subscribe()
TransportHints & unreliable()
Display subclass using a tf2_ros::MessageFilter, templated on the ROS message type.
virtual void reset()
Called to tell the display to clear its state.
Definition: display.cpp:287
void onDisable() override
Derived classes override this to do the actual work of disabling themselves.
void onEnable() override
Derived classes override this to do the actual work of enabling themselves.
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
void onInitialize() override
Override this function to do subclass-specific initialization.
void incomingMessage(const typename MessageType::ConstPtr &msg)
Incoming message callback. Checks if the message pointer is valid, increments messages_received_, then calls processMessage().
void fixedFrameChanged() override
Called by setFixedFrame(). Override to respond to changes to fixed_frame_.
message_filters::Subscriber< MessageType > sub_
void unsubscribe()
Helper superclass for MessageFilterDisplay, needed because Qt&#39;s moc and c++ templates don&#39;t work nice...


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:24