dlr::utilities::FrequencyGoverner Class Reference

The FrequencyGoverner class allows you to conveniently throttle a loop so that it runs at a specific speed. More...

#include <frequencyGoverner.h>

List of all members.

Public Member Functions

 FrequencyGoverner (double frequency, double duration=0.0)
 The constructor initializes the FrequencyGoverner and starts the internal timer.
 ~FrequencyGoverner ()
 The destructor destroys the FrequencyGoverner instance and deletes any file created by the constructor, thereby releasing the lock.
double getActualFrequency ()
 This method returns the average frequency at which method sleepIfNecessary() has been called.
unsigned int getCount ()
 This method returns the number of times that method sleepIfNecessary() has been called.
double getSlack ()
 This method returns how long, in seconds, method sleepIfNecessary() would have slept if it had been called instead of getSlack().
bool isFinished ()
 This method reports whether or not the specified period (from constructor argument "duration") has elapsed.
void sleepIfNecessary ()
 This method sleeps just long enough to prevent a loop from running faster than the specifed frequency.


Detailed Description

The FrequencyGoverner class allows you to conveniently throttle a loop so that it runs at a specific speed.

Here's an example of how it might be used:

   double frequency = 30.0; // Hz.
   double duration = 10.0;  // Seconds.
   FrequencyGoverner governer(frequency, duration);
   while(!governer.isFinished()) {
     // Do something.
     // [..]
     governer.sleepIfNecessary();
   }

Definition at line 40 of file frequencyGoverner.h.


Constructor & Destructor Documentation

dlr::utilities::FrequencyGoverner::FrequencyGoverner ( double  frequency,
double  duration = 0.0 
) [inline, explicit]

The constructor initializes the FrequencyGoverner and starts the internal timer.

Parameters:
frequency This argument specifies how fast you'd like the loop to run. Setting this argument to zero indicates no throttling of loop speed.
duration This argument specifies how long you'd like the loop to run. Setting this argument to zero indicates no timeout.

Definition at line 56 of file frequencyGoverner.h.

dlr::utilities::FrequencyGoverner::~FrequencyGoverner (  )  [inline]

The destructor destroys the FrequencyGoverner instance and deletes any file created by the constructor, thereby releasing the lock.

Definition at line 65 of file frequencyGoverner.h.


Member Function Documentation

double dlr::utilities::FrequencyGoverner::getActualFrequency (  )  [inline]

This method returns the average frequency at which method sleepIfNecessary() has been called.

This average includes any time spent between the constructor and the first call to sleepIfNecessary(). As long as the other code in the frequency-governed loop isn't too heavy, the return value should be approximately equal to the frequency specified in the constructor call.

Returns:
The return value is an estimage of the actual frequency at which the sleepIfNecessary is being called.

Definition at line 81 of file frequencyGoverner.h.

unsigned int dlr::utilities::FrequencyGoverner::getCount (  )  [inline]

This method returns the number of times that method sleepIfNecessary() has been called.

Returns:
The return value indicates the totall number of calls to sleepIfNecessary().

Definition at line 95 of file frequencyGoverner.h.

double dlr::utilities::FrequencyGoverner::getSlack (  )  [inline]

This method returns how long, in seconds, method sleepIfNecessary() would have slept if it had been called instead of getSlack().

Returns:
The return value is positive if we are ahead of schedule, negative otherwise.

Definition at line 107 of file frequencyGoverner.h.

Referenced by sleepIfNecessary().

bool dlr::utilities::FrequencyGoverner::isFinished (  )  [inline]

This method reports whether or not the specified period (from constructor argument "duration") has elapsed.

Returns:
The return value is true if the FrequencyGoverner was instantiated more than "duration" seconds ago, false otherwise.

Definition at line 118 of file frequencyGoverner.h.

void dlr::utilities::FrequencyGoverner::sleepIfNecessary (  )  [inline]

This method sleeps just long enough to prevent a loop from running faster than the specifed frequency.

Definition at line 129 of file frequencyGoverner.h.

References getSlack().


The documentation for this class was generated from the following file:

Generated on Wed Nov 25 11:07:10 2009 for dlrUtilities Utility Library by  doxygen 1.5.8