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 #pragma once
30 
31 #ifndef Q_MOC_RUN
33 #include <tf2_ros/message_filter.h>
34 #endif
35 
36 // The following replaces <rviz/frame_manager.h>
38 #include <rviz/display_context.h>
40 
41 #include <rviz/display.h>
42 
43 namespace grid_map_rviz_plugin {
48  Q_OBJECT
49  public:
51  topic_property_ = new rviz::RosTopicProperty("Topic", "", "", "", this, SLOT(updateTopic()));
52  unreliable_property_ = new rviz::BoolProperty("Unreliable", false, "Prefer UDP topic transport", this, SLOT(updateTopic()));
53  }
54 
55  protected Q_SLOTS:
56  virtual void updateTopic() = 0;
57 
58  protected:
61 };
62 
69 template <class MessageType>
71  // No Q_OBJECT macro here, moc does not support Q_OBJECT in a templated class.
72  public:
76 
77  MessageFilterDisplay() : tf_filter_(nullptr), messages_received_(0) {
78  QString message_type = QString::fromStdString(ros::message_traits::datatype<MessageType>());
79  topic_property_->setMessageType(message_type);
80  topic_property_->setDescription(message_type + " topic to subscribe to.");
81  }
82 
83  void onInitialize() override {
84  tf_filter_ = new tf2_ros::MessageFilter<MessageType>(*context_->getTF2BufferPtr(), fixed_frame_.toStdString(), 1u, update_nh_);
85 
86  tf_filter_->connectInput(sub_);
87  tf_filter_->registerCallback(boost::bind(&MessageFilterDisplay<MessageType>::incomingMessage, this, _1));
89  }
90 
91  ~MessageFilterDisplay() override {
94  delete tf_filter_;
95  }
96 
97  void reset() override {
98  Display::reset();
99  tf_filter_->clear();
100  // Quick fix for #1372. Can be removed if https://github.com/ros/geometry2/pull/402 is released
101  if (tf_filter_) update_nh_.getCallbackQueue()->removeByID((uint64_t)tf_filter_);
102  messages_received_ = 0;
103  }
104 
105  void setTopic(const QString& topic, const QString& /*datatype*/) override { topic_property_->setString(topic); }
106 
107  protected:
108  void updateTopic() override {
109  unsubscribe();
110  reset();
111  subscribe();
113  }
114 
115  virtual void subscribe() {
116  if (!isEnabled()) {
117  return;
118  }
119 
120  try {
121  ros::TransportHints transport_hint = ros::TransportHints().reliable();
122  // Determine UDP vs TCP transport for user selection.
123  if (unreliable_property_->getBool()) {
124  transport_hint = ros::TransportHints().unreliable();
125  }
126  sub_.subscribe(update_nh_, topic_property_->getTopicStd(), 1u, transport_hint);
127  setStatus(rviz::StatusProperty::Ok, "Topic", "OK");
128  } catch (ros::Exception& e) {
129  setStatus(rviz::StatusProperty::Error, "Topic", QString("Error subscribing: ") + e.what());
130  }
131  }
132 
133  virtual void unsubscribe() { sub_.unsubscribe(); }
134 
135  void onEnable() override { subscribe(); }
136 
137  void onDisable() override {
138  unsubscribe();
139  reset();
140  }
141 
142  void fixedFrameChanged() override {
143  tf_filter_->setTargetFrame(fixed_frame_.toStdString());
144  reset();
145  }
146 
150  void incomingMessage(const typename MessageType::ConstPtr& msg) {
151  if (!msg) {
152  return;
153  }
154 
155  ++messages_received_;
156  setStatus(rviz::StatusProperty::Ok, "Topic", QString::number(messages_received_) + " messages received");
157 
158  processMessage(msg);
159  }
160 
164  virtual void processMessage(const typename MessageType::ConstPtr& msg) = 0;
165 
169 };
170 
171 } // end namespace grid_map_rviz_plugin
172 
MessageFilterDisplay< MessageType > MFDClass
Convenience typedef so subclasses don&#39;t have to use the long templated class name to refer to their s...
virtual void setString(const QString &str)
TransportHints & reliable()
tf2_ros::MessageFilter< MessageType > * tf_filter_
message_filters::Subscriber< MessageType > sub_
Display subclass using a tf2_ros::MessageFilter, templated on the ROS message type.
void setTopic(const QString &topic, const QString &) override
DisplayContext * context_
ros::NodeHandle update_nh_
Helper superclass for MessageFilterDisplay, needed because Qt&#39;s moc and c++ templates don&#39;t work nice...
virtual void setDescription(const QString &description)
bool isEnabled() const
virtual void removeByID(uint64_t owner_id)=0
void setMessageType(const QString &message_type)
virtual bool getBool() const
QString fixed_frame_
void subscribe()
TransportHints & unreliable()
virtual void reset()
void registerFilterForTransformStatusCheck(tf::MessageFilter< M > *filter, Display *display)
void incomingMessage(const typename MessageType::ConstPtr &msg)
Incoming message callback. Checks if the message pointer is valid, increments messages_received_, then calls processMessage().
virtual FrameManager * getFrameManager() const =0
virtual void queueRender()=0
CallbackQueueInterface * getCallbackQueue() const
std::string getTopicStd() const
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)


grid_map_rviz_plugin
Author(s): Philipp Krüsi , Péter Fankhauser
autogenerated on Tue Jun 1 2021 02:13:47