Orocos Real-Time Toolkit
2.6.0
|
This class allows to define a timer object which can be armed in single shot and periodic mode. More...
#include <rtt/os/Timer.hpp>
Public Types | |
typedef int | TimerId |
A positive numeric ID representing a timer. | |
Public Member Functions | |
Timer (TimerId max_timers, int scheduler=-1, int priority=0) | |
Create a timer object which can hold max_timers timers. | |
virtual void | timeout (TimerId timer_id) |
This function is called each time an armed or periodic timer expires. | |
void | setMaxTimers (TimerId max) |
Change the maximum number of timers in this object. | |
bool | startTimer (TimerId timer_id, Seconds period) |
Start a periodic timer which starts first over period seconds and then every period seconds. | |
bool | arm (TimerId timer_id, Seconds wait_time) |
Arm a timer to fire once over wait_time seconds. | |
TimeService::Seconds | timeRemaining (TimerId timer_id) const |
Returns the remaining time before this timer elapses. | |
bool | isArmed (TimerId timer_id) const |
Check if a given timer id is armed. | |
bool | killTimer (TimerId timer_id) |
Disable an armed timer. | |
virtual os::ThreadInterface * | getThread () const |
Get the thread this object is run in. | |
virtual bool | hasWork () |
This method is for 'intelligent' activity implementations that wish to see if it is required to call step() (again). | |
ActivityInterface * | getActivity () const |
Query for the task this interface is run in. | |
virtual void | setActivity (ActivityInterface *task) |
Set the task this interface is run in. | |
Protected Types | |
typedef TimeService::nsecs | Time |
typedef std::vector< std::pair < Time, Time > > | TimerIds |
Index in vector is the timer id. | |
Protected Member Functions | |
bool | initialize () |
The method that will be called before the first periodical execution of step() ( or non periodical execution of loop() ), when the thread is started. | |
void | finalize () |
The method that will be called after the last periodical execution of step() ( or non periodical execution of loop() ), when the RunnableInterface is stopped. | |
void | step () |
The method that will be periodically executed when this class is run in a periodic thread. | |
void | loop () |
The method that will be executed once when this class is run in a non periodic thread. | |
bool | breakLoop () |
This method is called by the framework to break out of the loop() method. | |
Protected Attributes | |
TimeService * | mTimeserv |
base::ActivityInterface * | mThread |
Semaphore | msem |
Mutex | m |
TimerIds | mtimers |
bool | mdo_quit |
This class allows to define a timer object which can be armed in single shot and periodic mode.
In Order to use this class, derive your class from Timer and implement the timeout() method. The resolution of this class depends completely on the timer resolution of the underlying operating system.
If you do not attach an activity, the Timer will create a thread of its own and start it. That thread will be stopped and cleaned up when the Timer is destroyed.
typedef std::vector<std::pair<Time, Time> > RTT::os::Timer::TimerIds [protected] |
RTT::Timer::Timer | ( | TimerId | max_timers, |
int | scheduler = -1 , |
||
int | priority = 0 |
||
) |
Create a timer object which can hold max_timers timers.
A Timer must be executed in a non periodic thread (or the main thread) or it will refuse to start. If scheduler is set to -1 (default) no thread is created and you need to attach a thread yourself to this Timer.
max_timers | The initial amount of timers this Timer can monitor. Keep as low as possible. See also setMaxTimers(). |
scheduler | The Orocos scheduler type for this timer. ORO_SCHED_OTHER or ORO_SCHED_RT or -1 to attach your own thread. |
priority | The priority within the scheduler of this timer. |
Definition at line 123 of file Timer.cpp.
References RTT::base::ActivityInterface::start().
bool RTT::Timer::arm | ( | TimerId | timer_id, |
Seconds | wait_time | ||
) |
Arm a timer to fire once over wait_time seconds.
timer_id | The number of the timer, starting from zero. |
wait_time | The time in seconds from now, when the timer should expire. This is a floating point number. |
Definition at line 170 of file Timer.cpp.
References RTT::os::TimeService::getNSecs(), and RTT::os::Semaphore::signal().
bool RTT::Timer::breakLoop | ( | ) | [protected, virtual] |
This method is called by the framework to break out of the loop() method.
Reimplement this method to signal loop() to return and return true on success. When this method is not reimplemented by you, it will always return false, denoting that the loop can not be broken. If breakLoop() returns true, the caller will wait until loop() returns.
Reimplemented from RTT::base::RunnableInterface.
ActivityInterface * RTT::base::RunnableInterface::getActivity | ( | ) | const [inline, inherited] |
Query for the task this interface is run in.
Zero denotes that no task is present to run it, and hence no detailed information is available.
Definition at line 167 of file RunnableInterface.hpp.
Referenced by RTT::base::TaskCore::activate(), RTT::base::TaskCore::fatal(), RTT::TaskContext::getActivity(), RTT::base::TaskCore::getCpuAffinity(), RTT::base::TaskCore::getPeriod(), RTT::base::TaskCore::isActive(), RTT::ExecutionEngine::process(), RTT::ExecutionEngine::removeFunction(), RTT::ExecutionEngine::runFunction(), RTT::base::TaskCore::setCpuAffinity(), RTT::base::TaskCore::setPeriod(), RTT::ExecutionEngine::stopTask(), RTT::base::TaskCore::trigger(), RTT::base::TaskCore::update(), RTT::ExecutionEngine::waitForFunctions(), and RTT::ExecutionEngine::waitForMessages().
os::ThreadInterface * RTT::RunnableInterface::getThread | ( | ) | const [virtual, inherited] |
Get the thread this object is run in.
Definition at line 78 of file CoreRunnableInterface.cpp.
References RTT::base::ActivityInterface::thread().
bool RTT::RunnableInterface::hasWork | ( | ) | [virtual, inherited] |
This method is for 'intelligent' activity implementations that wish to see if it is required to call step() (again).
By default, false is returned. You should only return true in case there is a temporary reason to (re-)run step.
Reimplemented in RTT::ExecutionEngine.
Definition at line 65 of file CoreRunnableInterface.cpp.
Referenced by RTT::extras::SequentialActivity::trigger().
bool RTT::Timer::isArmed | ( | TimerId | timer_id | ) | const |
bool RTT::Timer::killTimer | ( | TimerId | timer_id | ) |
void RTT::Timer::loop | ( | ) | [protected, virtual] |
The method that will be executed once when this class is run in a non periodic thread.
The default implementation calls step() once.
Reimplemented from RTT::base::RunnableInterface.
Definition at line 58 of file Timer.cpp.
References RTT::os::TimeService::InfiniteNSecs.
void RTT::RunnableInterface::setActivity | ( | ActivityInterface * | task | ) | [virtual, inherited] |
Set the task this interface is run in.
A Zero means no task is running it.
task | The ActivityInterface running this interface. |
Definition at line 69 of file CoreRunnableInterface.cpp.
References RTT::base::ActivityInterface::disableRun().
Referenced by RTT::base::ActivityInterface::run().
void RTT::Timer::setMaxTimers | ( | TimerId | max | ) |
bool RTT::Timer::startTimer | ( | TimerId | timer_id, |
Seconds | period | ||
) |
Start a periodic timer which starts first over period seconds and then every period seconds.
timer_id | The number of the timer, starting from zero. |
period | The period when the timer should expire. This is a floating point number. |
Definition at line 151 of file Timer.cpp.
References RTT::os::TimeService::getNSecs(), and RTT::os::Semaphore::signal().
void RTT::Timer::timeout | ( | TimerId | timer_id | ) | [virtual] |
double RTT::Timer::timeRemaining | ( | TimerId | timer_id | ) | const |
Returns the remaining time before this timer elapses.
0.0 | if the timer is not armed or has already elapsed. |
Definition at line 201 of file Timer.cpp.
References RTT::os::TimeService::getNSecs().