OrocosComponentLibrary
2.7.0
|
00001 00002 #include "TimerComponent.hpp" 00003 #include <rtt/Logger.hpp> 00004 #include "ocl/Component.hpp" 00005 00006 ORO_CREATE_COMPONENT_TYPE() 00007 ORO_LIST_COMPONENT_TYPE( OCL::TimerComponent ) 00008 00009 namespace OCL 00010 { 00011 using namespace std; 00012 using namespace RTT; 00013 00014 TimerComponent::TimerComponent( std::string name /*= "os::Timer" */ ) 00015 : TaskContext( name, PreOperational ), port_timers(32), mtimeoutEvent("timeout"), 00016 mtimer( port_timers, mtimeoutEvent ), 00017 waitForCommand( "waitFor", &TimerComponent::waitFor, this), //, &TimerComponent::isTimerExpired, this), 00018 waitCommand( "wait", &TimerComponent::wait, this) //&TimerComponent::isTimerExpired, this) 00019 { 00020 00021 // Add the methods, methods make sure that they are 00022 // executed in the context of the (non realtime) caller. 00023 00024 this->addOperation("arm", &os::Timer::arm , &mtimer, RTT::ClientThread).doc("Arm a single shot timer.").arg("timerId", "A numeric id of the timer to arm.").arg("delay", "The delay in seconds before it fires."); 00025 this->addOperation("startTimer", &os::Timer::startTimer , &mtimer, RTT::ClientThread).doc("Start a periodic timer.").arg("timerId", "A numeric id of the timer to start.").arg("period", "The period in seconds."); 00026 this->addOperation("killTimer", &os::Timer::killTimer , &mtimer, RTT::ClientThread).doc("Kill (disable) an armed or started timer.").arg("timerId", "A numeric id of the timer to kill."); 00027 this->addOperation("isArmed", &os::Timer::isArmed , &mtimer, RTT::ClientThread).doc("Check if a given timer is armed or started.").arg("timerId", "A numeric id of the timer to check."); 00028 this->addOperation("setMaxTimers", &os::Timer::setMaxTimers , &mtimer, RTT::ClientThread).doc("Raise or lower the maximum amount of timers.").arg("timers", "The largest amount of timers. The highest timerId is max-1."); 00029 this->addOperation( waitForCommand ).doc("Wait until a timer expires.").arg("timerId", "A numeric id of the timer to wait for."); 00030 this->addOperation( waitCommand ).doc("Arm and wait until that timer expires.").arg("timerId", "A numeric id of the timer to arm and to wait for.").arg("delay", "The delay in seconds before the timer expires."); 00031 this->addPort(mtimeoutEvent).doc("This port is written each time ANY timer expires. The timer id is the value sent in this port. This port is for backwards compatibility only. It is advised to use the timer_* ports."); 00032 for(unsigned int i=0;i<port_timers.size();i++){ 00033 ostringstream port_name; 00034 port_name<<"timer_"<<i; 00035 port_timers[i] = new RTT::OutputPort<RTT::os::Timer::TimerId>(port_name.str()); 00036 this->addPort(*(port_timers[i])).doc(string("This port is written each time ")+port_name.str()+string(" expires. The timer id is the value sent in this port.")); 00037 } 00038 } 00039 00040 TimerComponent::~TimerComponent() { 00041 this->stop(); 00042 for(unsigned int i=0;i<port_timers.size();i++) 00043 delete port_timers[i]; 00044 } 00045 00046 bool TimerComponent::startHook() 00047 { 00048 return mtimer.getThread() && mtimer.getThread()->start(); 00049 } 00050 00051 void TimerComponent::updateHook() 00052 { 00053 // nop, we just process the wait commands. 00054 } 00055 00056 void TimerComponent::stopHook() 00057 { 00058 mtimer.getThread()->stop(); 00059 } 00060 00061 bool TimerComponent::wait(RTT::os::Timer::TimerId id, double seconds) 00062 { 00063 return mtimer.arm(id, seconds); 00064 } 00065 00066 bool TimerComponent::waitFor(RTT::os::Timer::TimerId id) 00067 { 00068 return true; 00069 } 00070 00071 bool TimerComponent::isTimerExpired(RTT::os::Timer::TimerId id) const 00072 { 00073 return !mtimer.isArmed(id); 00074 } 00075 }