frequencyGoverner.h
Go to the documentation of this file.00001
00015 #ifndef _DLRUTILITIES_FREQUENCYGOVERNER_H_
00016 #define _DLRUTILITIES_FREQUENCYGOVERNER_H_
00017
00018 #include <dlrUtilities/timeUtilities.h>
00019
00020 namespace dlr {
00021
00022 namespace utilities {
00023
00040 class FrequencyGoverner {
00041 public:
00042
00055 explicit
00056 FrequencyGoverner(double frequency, double duration = 0.0)
00057 : m_count(0), m_duration(duration), m_period(1.0 / frequency),
00058 m_startTime(getCurrentTime()) {}
00059
00060
00065 ~FrequencyGoverner() {}
00066
00067
00080 double
00081 getActualFrequency() {
00082 if(m_count == 0) {return 0.0;}
00083 return (getCurrentTime() - m_startTime) / m_count;
00084 }
00085
00086
00094 unsigned int
00095 getCount() {return static_cast<unsigned int>(m_count);}
00096
00097
00106 double
00107 getSlack() {return m_startTime + m_count * m_period - getCurrentTime();}
00108
00109
00117 inline bool
00118 isFinished() {
00119 return ((m_duration != 0.0)
00120 && ((getCurrentTime() - m_startTime) > m_duration));
00121 }
00122
00123
00128 inline void
00129 sleepIfNecessary() {
00130
00131
00132 portableSleep(this->getSlack());
00133 ++m_count;
00134 }
00135
00136 private:
00137
00138 int m_count;
00139 double m_duration;
00140 double m_period;
00141 double m_startTime;
00142
00143 };
00144
00145 }
00146
00147 }
00148
00149
00150
00151
00152 namespace dlr {
00153
00154 using utilities::FrequencyGoverner;
00155
00156 }
00157
00158 #endif // #ifndef _DLRUTILITES_FREQUENCYGOVERNER_H_