Homepage Demos Overview Downloads Tutorials Reference
Credits

SmoothCompareTrans.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SmoothCompareTrans_h_
00003 #define INCLUDED_SmoothCompareTrans_h_
00004 
00005 #include "Behaviors/Transitions/CompareTrans.h"
00006 
00007 //! A subclass of CompareTrans, which provides monitoring of exponentially weighted averages to a threshold
00008 /*! Has the additional requirement that template types must supply operator*=(float)
00009  *  and operator+=(T) for the weighted average
00010  *
00011  *  The gamma parameter is how much to weight the preceeding value - 1 will cause it to never update, 0 will cause it to only look at most recent.
00012  *  So, the lower the value, the faster it is to switch, but more prone to noise
00013  */
00014 template<class T>
00015 class SmoothCompareTrans : public CompareTrans<T> {
00016 public:
00017   //! constructor, see SmoothCompareTrans class notes for information
00018   SmoothCompareTrans(StateNode* destination, const T* monitor, typename SmoothCompareTrans<T>::Test_t test, const T& value, const EventBase& poll, float gammap=0)
00019     : CompareTrans<T>(destination,&avg,test,value,poll), avg(*monitor), realmon(monitor),
00020     burnin((unsigned int)(1/(1-gammap))), tests(0), g(gammap)
00021   { }
00022 
00023   virtual void DoStart() {
00024     CompareTrans<T>::DoStart();
00025     avg=*realmon;
00026     tests=0;
00027   }
00028 
00029   //! sets number of tests to perform before allowing a transition; default 1/(1-g)
00030   void setBurnIn(unsigned int i) {
00031     burnin=i;
00032   }
00033 
00034   //! returns number of tests to perform before allowing a transition; default 1/(1-g)
00035   unsigned int getBurnIn() {
00036     return burnin;
00037   }
00038 
00039   //!don't care about the event, just a pulse to check the values
00040   virtual void processEvent(const EventBase& e) {
00041     avg*=g;
00042     T x(*realmon);
00043     x*=(1-g);
00044     avg+=x;
00045     tests++;
00046     if(tests>burnin)
00047       CompareTrans<T>::processEvent(e);
00048   }
00049 
00050   virtual std::string getName() const { return "SmoothCompareTrans"; }
00051 
00052 protected:
00053   T avg; //!< the current running average
00054   const T* realmon; //!< pointer to the value being monitored
00055 
00056   unsigned int burnin; //!< number of tests to perform before allowing a transition; default 1/(1-g)
00057   unsigned int tests; //!< counter of tests made since last DoStart()
00058 
00059   //! the gamma value controlling the exponential average, see the class documentation at the top
00060   float g; 
00061 
00062 private:
00063   SmoothCompareTrans(const SmoothCompareTrans& node); //!< don't call this
00064   SmoothCompareTrans operator=(const SmoothCompareTrans& node); //!< don't call this
00065 };
00066 
00067 /*! @file
00068  * @brief Defines SmoothCompareTrans, subclass of CompareTrans, which provides monitoring of exponentially weighted averages to a threshold
00069  * @author ejt (Creator)
00070  *
00071  * $Author: ejt $
00072  * $Name: tekkotsu-2_1 $
00073  * $Revision: 1.8 $
00074  * $State: Exp $
00075  * $Date: 2003/11/11 00:08:18 $
00076  */
00077 
00078 #endif

Tekkotsu v2.1
Generated Tue Mar 16 23:19:15 2004 by Doxygen 1.3.5