00001
00002 #include "TimerComponent.hpp"
00003
00004 #include <rtt/Method.hpp>
00005 #include <rtt/Logger.hpp>
00006
00007 #include "ocl/ComponentLoader.hpp"
00008
00009 ORO_CREATE_COMPONENT_TYPE()
00010 ORO_LIST_COMPONENT_TYPE( OCL::TimerComponent )
00011
00012 namespace OCL
00013 {
00014 using namespace std;
00015 using namespace RTT;
00016
00017 TimerComponent::TimerComponent( std::string name )
00018 : TaskContext( name, PreOperational ), mtimer( 32, mtimeoutEvent ),
00019 mtimeoutEvent("timeout"),
00020 waitForCommand( "waitFor", &TimerComponent::waitFor, &TimerComponent::isTimerExpired, this),
00021 waitCommand( "wait", &TimerComponent::wait, &TimerComponent::isTimerExpired, this)
00022 {
00023
00024
00025
00026
00027 this->methods()->addMethod( method( "arm", &Timer::arm , &mtimer),
00028 "Arm a single shot timer.",
00029 "timerId", "A numeric id of the timer to arm.",
00030 "delay", "The delay in seconds before it fires.");
00031 this->methods()->addMethod( method( "startTimer", &Timer::startTimer , &mtimer),
00032 "Start a periodic timer.",
00033 "timerId", "A numeric id of the timer to start.",
00034 "period", "The period in seconds.");
00035 this->methods()->addMethod( method( "killTimer", &Timer::killTimer , &mtimer),
00036 "Kill (disable) an armed or started timer.",
00037 "timerId", "A numeric id of the timer to kill.");
00038 this->methods()->addMethod( method( "isArmed", &Timer::isArmed , &mtimer),
00039 "Check if a given timer is armed or started.",
00040 "timerId", "A numeric id of the timer to check.");
00041 this->methods()->addMethod( method( "setMaxTimers", &Timer::setMaxTimers , &mtimer),
00042 "Raise or lower the maximum amount of timers.",
00043 "timers", "The largest amount of timers. The highest timerId is max-1.");
00044 this->events()->addEvent( &mtimeoutEvent,
00045 "Timeout is emitted each time a timer expires.",
00046 "timerId", "The numeric id of the timer which expired.");
00047
00048 this->commands()->addCommand( &waitForCommand, "Wait until a timer expires.",
00049 "timerId", "A numeric id of the timer to wait for.");
00050 this->commands()->addCommand( &waitCommand, "Arm and wait until that timer expires.",
00051 "timerId", "A numeric id of the timer to arm and to wait for.",
00052 "delay", "The delay in seconds before the timer expires.");
00053 }
00054
00055 TimerComponent::~TimerComponent() {
00056 this->stop();
00057 }
00058
00059 bool TimerComponent::startHook()
00060 {
00061 return mtimer.getThread() && mtimer.getThread()->start();
00062 }
00063
00064 void TimerComponent::updateHook()
00065 {
00066
00067 }
00068
00069 void TimerComponent::stopHook()
00070 {
00071 mtimer.getThread()->stop();
00072 }
00073
00074 bool TimerComponent::wait(RTT::Timer::TimerId id, double seconds)
00075 {
00076 return mtimer.arm(id, seconds);
00077 }
00078
00079 bool TimerComponent::waitFor(RTT::Timer::TimerId id)
00080 {
00081 return true;
00082 }
00083
00084 bool TimerComponent::isTimerExpired(RTT::Timer::TimerId id) const
00085 {
00086 return !mtimer.isArmed(id);
00087 }
00088 }