basic_event.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2015 Aldebaran
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 #ifndef BASIC_EVENT_RECORDER_HPP
00019 #define BASIC_EVENT_RECORDER_HPP
00020 
00021 /*
00022 * LOCAL includes
00023 */
00024 #include <naoqi_driver/recorder/globalrecorder.hpp>
00025 #include "../helpers/recorder_helpers.hpp"
00026 
00027 /*
00028 * STANDARD includes
00029 */
00030 #include <string>
00031 
00032 namespace naoqi
00033 {
00034 namespace recorder
00035 {
00036 
00037 template<class T>
00038 class BasicEventRecorder
00039 {
00040 
00041 public:
00042   BasicEventRecorder( const std::string& topic ):
00043     topic_( topic ),
00044     buffer_duration_( helpers::recorder::bufferDefaultDuration ),
00045     is_initialized_( false ),
00046     is_subscribed_( false )
00047   {}
00048 
00049   virtual ~BasicEventRecorder() {}
00050 
00051   inline std::string topic() const
00052   {
00053     return topic_;
00054   }
00055 
00056   inline bool isInitialized() const
00057   {
00058     return is_initialized_;
00059   }
00060 
00061   inline void subscribe( bool state)
00062   {
00063     is_subscribed_ = state;
00064   }
00065 
00066   inline bool isSubscribed() const
00067   {
00068     return is_subscribed_;
00069   }
00070 
00071   virtual void write(const T& msg)
00072   {
00073     if (!msg.header.stamp.isZero()) {
00074       gr_->write(topic_, msg, msg.header.stamp);
00075     }
00076     else {
00077       gr_->write(topic_, msg);
00078     }
00079   }
00080 
00081   virtual void writeDump(const ros::Time& time)
00082   {
00083     boost::mutex::scoped_lock lock_write_buffer( mutex_ );
00084     removeOlderThan(time);
00085     typename std::list<T>::iterator it;
00086     for (it = buffer_.begin(); it != buffer_.end(); it++)
00087     {
00088       if (!it->header.stamp.isZero()) {
00089         gr_->write(topic_, *it, it->header.stamp);
00090       }
00091       else {
00092         gr_->write(topic_, *it);
00093       }
00094     }
00095   }
00096 
00097   virtual void bufferize(const T& msg)
00098   {
00099     boost::mutex::scoped_lock lock_bufferize( mutex_ );
00100     typename std::list<T>::iterator it;
00101     removeOld();
00102     buffer_.push_back(msg);
00103 
00104   }
00105 
00106   virtual void reset(boost::shared_ptr<GlobalRecorder> gr, float conv_frequency)
00107   {
00108     gr_ = gr;
00109     is_initialized_ = true;
00110   }
00111 
00112   virtual void setBufferDuration(float duration)
00113   {
00114     boost::mutex::scoped_lock lock_bufferize( mutex_ );
00115     buffer_duration_ = duration;
00116   }
00117 
00118 protected:
00119   bool isTooOld(const T& msg)
00120   {
00121     ros::Duration d( ros::Time::now() - msg.header.stamp );
00122     if (static_cast<float>(d.toSec()) > buffer_duration_)
00123     {
00124       return true;
00125     }
00126     return false;
00127   }
00128 
00129   bool isOlderThan(const T& msg, const ros::Time& time)
00130   {
00131     ros::Duration d( time - msg.header.stamp );
00132     if (static_cast<float>(d.toSec()) > buffer_duration_)
00133     {
00134       return true;
00135     }
00136     return false;
00137   }
00138 
00139   void removeOld()
00140   {
00141     while (buffer_.size() > 0 && isTooOld(buffer_.front()))
00142     {
00143       buffer_.pop_front();
00144     }
00145   }
00146 
00147   void removeOlderThan(const ros::Time& time)
00148   {
00149     while (buffer_.size() > 0 && isOlderThan(buffer_.front(), time))
00150     {
00151       buffer_.pop_front();
00152     }
00153   }
00154 
00155 protected:
00156   std::string topic_;
00157 
00158   std::list<T> buffer_;
00159   float buffer_duration_;
00160   boost::mutex mutex_;
00161 
00162   bool is_initialized_;
00163   bool is_subscribed_;
00164 
00165   boost::shared_ptr<naoqi::recorder::GlobalRecorder> gr_;
00166 
00167 }; // class
00168 
00169 } // publisher
00170 } // naoqi
00171 
00172 #endif


naoqi_driver
Author(s): Karsten Knese
autogenerated on Sun Sep 17 2017 02:57:14