message_filter_display.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 #ifndef MESSAGE_FILTER_DISPLAY_H
00030 #define MESSAGE_FILTER_DISPLAY_H
00031 
00032 #ifndef Q_MOC_RUN
00033 #include <OgreSceneManager.h>
00034 #include <OgreSceneNode.h>
00035 
00036 #include <message_filters/subscriber.h>
00037 #include <tf/message_filter.h>
00038 #endif
00039 
00040 #include "rviz/display_context.h"
00041 #include "rviz/frame_manager.h"
00042 #include "rviz/properties/ros_topic_property.h"
00043 
00044 #include "rviz/display.h"
00045 
00046 namespace rviz
00047 {
00048 
00052 class _RosTopicDisplay: public Display
00053 {
00054 Q_OBJECT
00055 public:
00056   _RosTopicDisplay()
00057     {
00058       topic_property_ = new RosTopicProperty( "Topic", "",
00059                                               "", "",
00060                                               this, SLOT( updateTopic() ));
00061       unreliable_property_ = new BoolProperty( "Unreliable", false,
00062                                                "Prefer UDP topic transport",
00063                                                this,
00064                                                SLOT( updateTopic() ));
00065     }
00066 
00067 protected Q_SLOTS:
00068   virtual void updateTopic() = 0;
00069 
00070 protected:
00071   RosTopicProperty* topic_property_;
00072   BoolProperty* unreliable_property_;
00073 };
00074 
00081 template<class MessageType>
00082 class MessageFilterDisplay: public _RosTopicDisplay
00083 {
00084 // No Q_OBJECT macro here, moc does not support Q_OBJECT in a templated class.
00085 public:
00088   typedef MessageFilterDisplay<MessageType> MFDClass;
00089 
00090   MessageFilterDisplay()
00091     : tf_filter_( NULL )
00092     , messages_received_( 0 )
00093     {
00094       QString message_type = QString::fromStdString( ros::message_traits::datatype<MessageType>() );
00095       topic_property_->setMessageType( message_type );
00096       topic_property_->setDescription( message_type + " topic to subscribe to." );
00097     }
00098 
00099   virtual void onInitialize()
00100     {
00101       tf_filter_ = new tf::MessageFilter<MessageType>( *context_->getTFClient(),
00102                                                        fixed_frame_.toStdString(), 10, update_nh_ );
00103 
00104       tf_filter_->connectInput( sub_ );
00105       tf_filter_->registerCallback( boost::bind( &MessageFilterDisplay<MessageType>::incomingMessage, this, _1 ));
00106       context_->getFrameManager()->registerFilterForTransformStatusCheck( tf_filter_, this );
00107     }
00108 
00109   virtual ~MessageFilterDisplay()
00110     {
00111       unsubscribe();
00112       delete tf_filter_;
00113     }
00114 
00115   virtual void reset()
00116     {
00117       Display::reset();
00118       tf_filter_->clear();
00119       messages_received_ = 0;
00120     }
00121 
00122   virtual void setTopic( const QString &topic, const QString &datatype )
00123     {
00124       topic_property_->setString( topic );
00125     }
00126 
00127 protected:
00128   virtual void updateTopic()
00129     {
00130       unsubscribe();
00131       reset();
00132       subscribe();
00133       context_->queueRender();
00134     }
00135 
00136   virtual void subscribe()
00137     {
00138       if( !isEnabled() )
00139       {
00140         return;
00141       }
00142 
00143       try
00144       {
00145         ros::TransportHints transport_hint = ros::TransportHints().reliable();
00146         // Determine UDP vs TCP transport for user selection.
00147         if (unreliable_property_->getBool())
00148         {
00149           transport_hint = ros::TransportHints().unreliable();
00150         }
00151         sub_.subscribe( update_nh_, topic_property_->getTopicStd(), 10, transport_hint);
00152         setStatus( StatusProperty::Ok, "Topic", "OK" );
00153       }
00154       catch( ros::Exception& e )
00155       {
00156         setStatus( StatusProperty::Error, "Topic", QString( "Error subscribing: " ) + e.what() );
00157       }
00158     }
00159 
00160   virtual void unsubscribe()
00161     {
00162       sub_.unsubscribe();
00163     }
00164 
00165   virtual void onEnable()
00166     {
00167       subscribe();
00168     }
00169 
00170   virtual void onDisable()
00171     {
00172       unsubscribe();
00173       reset();
00174     }
00175 
00176   virtual void fixedFrameChanged()
00177     {
00178       tf_filter_->setTargetFrame( fixed_frame_.toStdString() );
00179       reset();
00180     }
00181 
00185   void incomingMessage( const typename MessageType::ConstPtr& msg )
00186     {
00187       if( !msg )
00188       {
00189         return;
00190       }
00191 
00192       ++messages_received_;
00193       setStatus( StatusProperty::Ok, "Topic", QString::number( messages_received_ ) + " messages received" );
00194 
00195       processMessage( msg );
00196     }
00197 
00201   virtual void processMessage( const typename MessageType::ConstPtr& msg ) = 0;
00202 
00203   message_filters::Subscriber<MessageType> sub_;
00204   tf::MessageFilter<MessageType>* tf_filter_;
00205   uint32_t messages_received_;
00206 };
00207 
00208 } // end namespace rviz
00209 
00210 #endif // MESSAGE_FILTER_DISPLAY_H


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Jun 6 2019 18:02:15