frame_manager.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, 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 
00030 #ifndef RVIZ_FRAME_MANAGER_H
00031 #define RVIZ_FRAME_MANAGER_H
00032 
00033 #include <map>
00034 
00035 #include <QObject>
00036 
00037 #include <ros/time.h>
00038 
00039 #include <OgreVector3.h>
00040 #include <OgreQuaternion.h>
00041 
00042 #include <boost/thread/mutex.hpp>
00043 
00044 #include <geometry_msgs/Pose.h>
00045 
00046 #ifndef Q_MOC_RUN
00047 #include <tf/message_filter.h>
00048 #endif
00049 
00050 namespace tf
00051 {
00052 class TransformListener;
00053 }
00054 
00055 namespace rviz
00056 {
00057 class Display;
00058 
00063 class FrameManager: public QObject
00064 {
00065 Q_OBJECT
00066 public:
00067 
00068   enum SyncMode {
00069     SyncOff = 0,
00070     SyncExact,
00071     SyncApprox
00072   };
00073 
00074   FrameManager();
00075 
00081   ~FrameManager();
00082 
00087    void setFixedFrame(const std::string& frame);
00088 
00090    void setPause( bool pause );
00091 
00092    bool getPause() { return pause_; }
00093 
00095    void setSyncMode( SyncMode mode );
00096 
00097    SyncMode getSyncMode() { return sync_mode_; }
00098 
00100    void syncTime( ros::Time time );
00101 
00103    ros::Time getTime() { return sync_time_; }
00104 
00110   template<typename Header>
00111   bool getTransform(const Header& header, Ogre::Vector3& position, Ogre::Quaternion& orientation)
00112   {
00113     return getTransform(header.frame_id, header.stamp, position, orientation);
00114   }
00115 
00122   bool getTransform(const std::string& frame, ros::Time time, Ogre::Vector3& position, Ogre::Quaternion& orientation);
00123 
00130   template<typename Header>
00131   bool transform(const Header& header, const geometry_msgs::Pose& pose, Ogre::Vector3& position, Ogre::Quaternion& orientation)
00132   {
00133     return transform(header.frame_id, header.stamp, pose, position, orientation);
00134   }
00135 
00143   bool transform(const std::string& frame, ros::Time time, const geometry_msgs::Pose& pose, Ogre::Vector3& position, Ogre::Quaternion& orientation);
00144 
00146   void update();
00147 
00153   bool frameHasProblems(const std::string& frame, ros::Time time, std::string& error);
00154 
00160   bool transformHasProblems(const std::string& frame, ros::Time time, std::string& error);
00161 
00170   template<class M>
00171   void registerFilterForTransformStatusCheck(tf::MessageFilter<M>* filter, Display* display)
00172   {
00173     filter->registerCallback(boost::bind(&FrameManager::messageCallback<M>, this, _1, display));
00174     filter->registerFailureCallback(boost::bind(&FrameManager::failureCallback<M>, this, _1, _2, display));
00175   }
00176 
00178   const std::string& getFixedFrame() { return fixed_frame_; }
00179 
00181   tf::TransformListener* getTFClient() { return tf_.get(); }
00182 
00184   const boost::shared_ptr<tf::TransformListener>& getTFClientPtr() { return tf_; }
00185 
00195   std::string discoverFailureReason(const std::string& frame_id,
00196                                     const ros::Time& stamp,
00197                                     const std::string& caller_id,
00198                                     tf::FilterFailureReason reason);
00199 
00200 Q_SIGNALS:
00202   void fixedFrameChanged();
00203 
00204 private:
00205 
00206   bool adjustTime( const std::string &frame, ros::Time &time );
00207 
00208   template<class M>
00209   void messageCallback(const ros::MessageEvent<M const>& msg_evt, Display* display)
00210   {
00211     boost::shared_ptr<M const> const &msg = msg_evt.getConstMessage();
00212     std::string authority = msg_evt.getPublisherName();
00213 
00214     messageArrived(msg->header.frame_id, msg->header.stamp, authority, display);
00215   }
00216 
00217   template<class M>
00218   void failureCallback(const ros::MessageEvent<M const>& msg_evt, tf::FilterFailureReason reason, Display* display)
00219   {
00220     boost::shared_ptr<M const> const &msg = msg_evt.getConstMessage();
00221     std::string authority = msg_evt.getPublisherName();
00222 
00223     messageFailed(msg->header.frame_id, msg->header.stamp, authority, reason, display);
00224   }
00225 
00226   void messageArrived(const std::string& frame_id, const ros::Time& stamp, const std::string& caller_id, Display* display);
00227   void messageFailed(const std::string& frame_id, const ros::Time& stamp, const std::string& caller_id, tf::FilterFailureReason reason, Display* display);
00228 
00229   struct CacheKey
00230   {
00231     CacheKey(const std::string& f, ros::Time t)
00232     : frame(f)
00233     , time(t)
00234     {}
00235 
00236     bool operator<(const CacheKey& rhs) const
00237     {
00238       if (frame != rhs.frame)
00239       {
00240         return frame < rhs.frame;
00241       }
00242 
00243       return time < rhs.time;
00244     }
00245 
00246     std::string frame;
00247     ros::Time time;
00248   };
00249 
00250   struct CacheEntry
00251   {
00252     CacheEntry(const Ogre::Vector3& p, const Ogre::Quaternion& o)
00253     : position(p)
00254     , orientation(o)
00255     {}
00256 
00257     Ogre::Vector3 position;
00258     Ogre::Quaternion orientation;
00259   };
00260   typedef std::map<CacheKey, CacheEntry > M_Cache;
00261 
00262   boost::mutex cache_mutex_;
00263   M_Cache cache_;
00264 
00265   boost::shared_ptr<tf::TransformListener> tf_;
00266   std::string fixed_frame_;
00267 
00268   bool pause_;
00269 
00270   SyncMode sync_mode_;
00271 
00272   // the current synchronized time, used to overwrite ros:Time(0)
00273   ros::Time sync_time_;
00274 
00275   // used for approx. syncing
00276   double sync_delta_;
00277   double current_delta_;
00278 };
00279 
00280 }
00281 
00282 #endif // RVIZ_FRAME_MANAGER_H


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:27