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
33 #include <OgreSceneManager.h>
34 #include <OgreSceneNode.h>
35 
37 #include <tf/message_filter.h>
38 #endif
39 
40 #include "rviz/display_context.h"
41 #include "rviz/frame_manager.h"
43 
44 #include "rviz/display.h"
45 
46 namespace rviz
47 {
48 
53 {
54 Q_OBJECT
55 public:
57  {
58  topic_property_ = new RosTopicProperty( "Topic", "",
59  "", "",
60  this, SLOT( updateTopic() ));
61  unreliable_property_ = new BoolProperty( "Unreliable", false,
62  "Prefer UDP topic transport",
63  this,
64  SLOT( updateTopic() ));
65  }
66 
67 protected Q_SLOTS:
68  virtual void updateTopic() = 0;
69 
70 protected:
73 };
74 
81 template<class MessageType>
83 {
84 // No Q_OBJECT macro here, moc does not support Q_OBJECT in a templated class.
85 public:
89 
91  : tf_filter_( NULL )
92  , messages_received_( 0 )
93  {
94  QString message_type = QString::fromStdString( ros::message_traits::datatype<MessageType>() );
95  topic_property_->setMessageType( message_type );
96  topic_property_->setDescription( message_type + " topic to subscribe to." );
97  }
98 
99  virtual void onInitialize()
100  {
102  fixed_frame_.toStdString(), 10, update_nh_ );
103 
104  tf_filter_->connectInput( sub_ );
105  tf_filter_->registerCallback( boost::bind( &MessageFilterDisplay<MessageType>::incomingMessage, this, _1 ));
107  }
108 
110  {
111  unsubscribe();
112  delete tf_filter_;
113  }
114 
115  virtual void reset()
116  {
117  Display::reset();
118  tf_filter_->clear();
119  messages_received_ = 0;
120  }
121 
122  virtual void setTopic( const QString &topic, const QString &datatype )
123  {
124  topic_property_->setString( topic );
125  }
126 
127 protected:
128  virtual void updateTopic()
129  {
130  unsubscribe();
131  reset();
132  subscribe();
134  }
135 
136  virtual void subscribe()
137  {
138  if( !isEnabled() )
139  {
140  return;
141  }
142 
143  try
144  {
145  ros::TransportHints transport_hint = ros::TransportHints().reliable();
146  // Determine UDP vs TCP transport for user selection.
148  {
149  transport_hint = ros::TransportHints().unreliable();
150  }
151  sub_.subscribe( update_nh_, topic_property_->getTopicStd(), 10, transport_hint);
152  setStatus( StatusProperty::Ok, "Topic", "OK" );
153  }
154  catch( ros::Exception& e )
155  {
156  setStatus( StatusProperty::Error, "Topic", QString( "Error subscribing: " ) + e.what() );
157  }
158  }
159 
160  virtual void unsubscribe()
161  {
162  sub_.unsubscribe();
163  }
164 
165  virtual void onEnable()
166  {
167  subscribe();
168  }
169 
170  virtual void onDisable()
171  {
172  unsubscribe();
173  reset();
174  }
175 
176  virtual void fixedFrameChanged()
177  {
178  tf_filter_->setTargetFrame( fixed_frame_.toStdString() );
179  reset();
180  }
181 
185  void incomingMessage( const typename MessageType::ConstPtr& msg )
186  {
187  if( !msg )
188  {
189  return;
190  }
191 
192  ++messages_received_;
193  setStatus( StatusProperty::Ok, "Topic", QString::number( messages_received_ ) + " messages received" );
194 
195  processMessage( msg );
196  }
197 
201  virtual void processMessage( const typename MessageType::ConstPtr& msg ) = 0;
202 
206 };
207 
208 } // end namespace rviz
209 
210 #endif // MESSAGE_FILTER_DISPLAY_H
#define NULL
Definition: global.h:37
virtual void setTopic(const QString &topic, const QString &datatype)
Set the ROS topic to listen to for this display.
virtual void setString(const QString &str)
void connectInput(F &f)
TransportHints & reliable()
virtual void onInitialize()
Override this function to do subclass-specific initialization.
virtual tf::TransformListener * getTFClient() const =0
Convenience function: returns getFrameManager()->getTFClient().
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz...
Definition: display.h:256
MessageFilterDisplay< MessageType > MFDClass
Convenience typedef so subclasses don&#39;t have to use the long templated class name to refer to their s...
ros::NodeHandle update_nh_
A NodeHandle whose CallbackQueue is run from the main GUI thread (the "update" thread).
Definition: display.h:269
RosTopicProperty * topic_property_
virtual void updateTopic()=0
tf::MessageFilter< MessageType > * tf_filter_
virtual void reset()
Called to tell the display to clear its state.
virtual void setDescription(const QString &description)
Set the description.
Definition: property.cpp:164
bool isEnabled() const
Return true if this Display is enabled, false if not.
Definition: display.cpp:281
void setMessageType(const QString &message_type)
virtual bool getBool() const
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:281
void subscribe()
virtual void onEnable()
Derived classes override this to do the actual work of enabling themselves.
TransportHints & unreliable()
Display subclass using a tf::MessageFilter, templated on the ROS message type.
virtual void reset()
Called to tell the display to clear its state.
Definition: display.cpp:300
void registerFilterForTransformStatusCheck(tf::MessageFilter< M > *filter, Display *display)
Connect a tf::MessageFilter&#39;s callbacks to success and failure handler functions in this FrameManager...
virtual void onDisable()
Derived classes override this to do the actual work of disabling themselves.
virtual FrameManager * getFrameManager() const =0
Return the FrameManager instance.
virtual void fixedFrameChanged()
Called by setFixedFrame(). Override to respond to changes to fixed_frame_.
virtual void queueRender()=0
Queues a render. Multiple calls before a render happens will only cause a single render.
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
void incomingMessage(const typename MessageType::ConstPtr &msg)
Incoming message callback. Checks if the message pointer is valid, increments messages_received_, then calls processMessage().
message_filters::Subscriber< MessageType > sub_
void unsubscribe()
std::string getTopicStd() const
BoolProperty(const QString &name=QString(), bool default_value=false, const QString &description=QString(), Property *parent=0, const char *changed_slot=0, QObject *receiver=0)
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
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 Apr 27 2019 02:33:41