Orocos Real-Time Toolkit
2.9.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>
Classes | |
struct | TimerInfo |
Public Types | |
typedef int | TimerId |
A positive numeric ID representing a timer. More... | |
enum | WorkReason { TimeOut = 0, Trigger, IOReady } |
Public Member Functions | |
Timer (TimerId max_timers, int scheduler=-1, int priority=0, const std::string &name=std::string("Timer")) | |
Create a named timer object which can hold max_timers timers. More... | |
~Timer () | |
virtual void | timeout (TimerId timer_id) |
This function is called each time an armed or periodic timer expires. More... | |
void | setMaxTimers (TimerId max) |
Change the maximum number of timers in this object. More... | |
bool | startTimer (TimerId timer_id, Seconds period) |
Start a periodic timer which starts first over period seconds and then every period seconds. More... | |
bool | arm (TimerId timer_id, Seconds wait_time) |
Arm a timer to fire once over wait_time seconds. More... | |
TimeService::Seconds | timeRemaining (TimerId timer_id) const |
Returns the remaining time before this timer elapses. More... | |
bool | isArmed (TimerId timer_id) const |
Check if a given timer id is armed. More... | |
bool | killTimer (TimerId timer_id) |
Disable an armed timer. More... | |
bool | waitFor (RTT::os::Timer::TimerId id) |
Wait for a timer to expire. More... | |
bool | waitForUntil (RTT::os::Timer::TimerId id, nsecs abs_time) |
Wait for a timer to expire with timeout. More... | |
virtual void | work (WorkReason reason) |
Identical to step() but gives a reason why the function was called. More... | |
virtual os::ThreadInterface * | getThread () const |
Get the thread this object is run in. More... | |
virtual bool | hasWork () |
This method is for 'intelligent' activity implementations that wish to see if it is required to call step() (again). More... | |
ActivityInterface * | getActivity () const |
Query for the task this interface is run in. More... | |
virtual void | setActivity (ActivityInterface *task) |
Set the task this interface is run in. More... | |
Protected Types | |
typedef TimeService::nsecs | Time |
typedef std::vector< TimerInfo > | TimerIds |
Index in vector is the timer id. More... | |
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. More... | |
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. More... | |
void | step () |
The method that will be (periodically) executed when this object is run in an Activity. More... | |
void | loop () |
The method that will be executed once when this class is run in a non periodic Activity. More... | |
bool | breakLoop () |
This method is called by the framework to break out of the loop() method. More... | |
Protected Attributes | |
base::ActivityInterface * | mThread |
Condition | mcond |
Mutex | mmutex |
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.
|
protected |
typedef int RTT::os::Timer::TimerId |
|
protected |
|
inherited |
Enumerator | |
---|---|
TimeOut | |
Trigger | |
IOReady |
Definition at line 76 of file RunnableInterface.hpp.
RTT::Timer::Timer | ( | TimerId | max_timers, |
int | scheduler = -1 , |
||
int | priority = 0 , |
||
const std::string & | name = std::string("Timer") |
||
) |
Create a named 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. |
name | The name of the timer (ie the underlying Activity/Thread) |
Definition at line 137 of file Timer.cpp.
References mThread, mtimers, and RTT::base::ActivityInterface::start().
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 183 of file Timer.cpp.
References RTT::os::Condition::broadcast(), RTT::Error, mcond, mmutex, mtimers, rtos_get_time_ns(), and RTT::Seconds_to_nsecs().
|
protectedvirtual |
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.
|
protectedvirtual |
The method that will be called after the last periodical execution of step() ( or non periodical execution of loop() ), when the RunnableInterface is stopped.
Implements RTT::base::RunnableInterface.
|
inlineinherited |
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 175 of file RunnableInterface.hpp.
Referenced by RTT::base::TaskCore::activate(), RTT::scripting::StateMachine::createEventTransition(), 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(), and RTT::base::TaskCore::update().
|
virtualinherited |
Get the thread this object is run in.
Reimplemented in RTT::ExecutionEngine.
Definition at line 82 of file CoreRunnableInterface.cpp.
References RTT::base::ActivityInterface::thread().
Referenced by RTT::ExecutionEngine::getThread().
|
virtualinherited |
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 67 of file CoreRunnableInterface.cpp.
Referenced by RTT::extras::SequentialActivity::trigger().
|
protectedvirtual |
The method that will be called before the first periodical execution of step() ( or non periodical execution of loop() ), when the thread is started.
Implements RTT::base::RunnableInterface.
bool RTT::Timer::isArmed | ( | TimerId | timer_id | ) | const |
Check if a given timer id is armed.
timer_id | The number of the timer, starting from zero. |
Definition at line 203 of file Timer.cpp.
References RTT::Error, mmutex, and mtimers.
bool RTT::Timer::killTimer | ( | TimerId | timer_id | ) |
Disable an armed timer.
timer_id | The number of the timer, starting from zero. |
Definition at line 230 of file Timer.cpp.
References RTT::Error, mmutex, and mtimers.
|
protectedvirtual |
The method that will be executed once when this class is run in a non periodic Activity.
The default implementation calls step() once.
Reimplemented from RTT::base::RunnableInterface.
Definition at line 61 of file Timer.cpp.
References rtos_get_time_ns().
|
virtualinherited |
Set the task this interface is run in.
A Zero means no task is running it.
task | The ActivityInterface running this interface. |
Reimplemented in RTT::ExecutionEngine.
Definition at line 73 of file CoreRunnableInterface.cpp.
References RTT::base::ActivityInterface::disableRun().
Referenced by RTT::base::ActivityInterface::ActivityInterface(), RTT::base::ActivityInterface::run(), RTT::ExecutionEngine::setActivity(), and RTT::base::ActivityInterface::~ActivityInterface().
void RTT::Timer::setMaxTimers | ( | TimerId | max | ) |
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 164 of file Timer.cpp.
References RTT::os::Condition::broadcast(), RTT::Error, mcond, mmutex, mtimers, rtos_get_time_ns(), and RTT::Seconds_to_nsecs().
|
protectedvirtual |
The method that will be (periodically) executed when this object is run in an Activity.
Implements RTT::base::RunnableInterface.
|
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 214 of file Timer.cpp.
References RTT::Error, mmutex, mtimers, RTT::nsecs_to_Seconds(), and rtos_get_time_ns().
bool RTT::Timer::waitFor | ( | RTT::os::Timer::TimerId | id | ) |
Wait for a timer to expire.
timer_id | The number of the timer, starting from zero. |
Definition at line 244 of file Timer.cpp.
References RTT::Error, mmutex, and mtimers.
bool RTT::Timer::waitForUntil | ( | RTT::os::Timer::TimerId | id, |
nsecs | abs_time | ||
) |
Wait for a timer to expire with timeout.
timer_id | The number of the timer, starting from zero. |
abs_time | The absolute time to wait until before the condition happens. Use rtos_get_time_ns() to get the current time and Seconds_to_nsecs to add a certain amount to the result. |
Definition at line 257 of file Timer.cpp.
References RTT::Error, mmutex, and mtimers.
|
virtualinherited |
Identical to step() but gives a reason why the function was called.
Both step() and work() will be called an equal amount of times, so you need to use only one, but work gives you the reason why.
Reimplemented in RTT::ExecutionEngine.
Definition at line 71 of file CoreRunnableInterface.cpp.
Referenced by RTT::extras::SlaveActivity::execute(), RTT::Activity::loop(), RTT::extras::SequentialActivity::trigger(), RTT::extras::FileDescriptorSimulationActivity::work(), RTT::extras::PeriodicActivity::work(), RTT::Activity::work(), and RTT::extras::FileDescriptorActivity::work().
|
protected |
Definition at line 78 of file Timer.hpp.
Referenced by arm(), and startTimer().
|
mutableprotected |
Definition at line 79 of file Timer.hpp.
Referenced by arm(), isArmed(), killTimer(), setMaxTimers(), startTimer(), timeRemaining(), waitFor(), and waitForUntil().
|
protected |
|
protected |
Definition at line 98 of file Timer.hpp.
Referenced by arm(), isArmed(), killTimer(), setMaxTimers(), startTimer(), Timer(), timeRemaining(), waitFor(), and waitForUntil().