OrocosComponentLibrary  2.9.0
TimerComponent.cpp
1 
2 #include "TimerComponent.hpp"
3 #include <rtt/Logger.hpp>
4 #include "ocl/Component.hpp"
5 
6 ORO_CREATE_COMPONENT_TYPE()
7 ORO_LIST_COMPONENT_TYPE( OCL::TimerComponent )
8 
9 namespace OCL
10 {
11  using namespace std;
12  using namespace RTT;
13 
14  TimerComponent::TimerComponent( std::string name /*= "os::Timer" */ )
15  : TaskContext( name, PreOperational ), port_timers(32), mtimeoutEvent("timeout"),
16  mtimer( port_timers, mtimeoutEvent ),
17  waitForCommand( "waitFor", &TimerComponent::waitFor, this), //, &TimerComponent::isTimerExpired, this),
18  waitCommand( "wait", &TimerComponent::wait, this) //&TimerComponent::isTimerExpired, this)
19  {
20 
21  // Add the methods, methods make sure that they are
22  // executed in the context of the (non realtime) caller.
23 
24  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.");
25  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.");
26  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.");
27  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.");
28  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.");
29  this->addOperation( waitForCommand ).doc("Wait until a timer expires.").arg("timerId", "A numeric id of the timer to wait for.");
30  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.");
31  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.");
32  for(unsigned int i=0;i<port_timers.size();i++){
33  ostringstream port_name;
34  port_name<<"timer_"<<i;
35  port_timers[i] = new RTT::OutputPort<RTT::os::Timer::TimerId>(port_name.str());
36  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."));
37  }
38  }
39 
40  TimerComponent::~TimerComponent() {
41  this->stop();
42  for(unsigned int i=0;i<port_timers.size();i++)
43  delete port_timers[i];
44  }
45 
47  {
48  return mtimer.getThread() && mtimer.getThread()->start();
49  }
50 
51  void TimerComponent::updateHook()
52  {
53  // nop, we just process the wait commands.
54  }
55 
56  void TimerComponent::stopHook()
57  {
58  mtimer.getThread()->stop();
59  }
60 
61  bool TimerComponent::wait(RTT::os::Timer::TimerId id, double seconds)
62  {
63  return mtimer.arm(id, seconds) && mtimer.waitFor(id);
64  }
65 
66  bool TimerComponent::waitFor(RTT::os::Timer::TimerId id)
67  {
68  return mtimer.waitFor(id);
69  }
70 
71  bool TimerComponent::isTimerExpired(RTT::os::Timer::TimerId id) const
72  {
73  return !mtimer.isArmed(id);
74  }
75 }
bool startHook()
This hook will check if a Activity has been properly setup.
RTT::Operation< bool(RTT::os::Timer::TimerId)> waitForCommand
Command: wait until a timer expires.
STL namespace.
This file contains the macros and definitions to create dynamically loadable components.
bool wait(RTT::os::Timer::TimerId id, double seconds)
Command Implementation: arm and wait until a timer expires.
A Component interface to the Real-Time types::Toolkit&#39;s timer.
The Orocos Component Library.
Definition: Component.hpp:43
RTT::Operation< bool(RTT::os::Timer::TimerId, double)> waitCommand
Command: arm and wait until a timer expires.
bool isTimerExpired(RTT::os::Timer::TimerId id) const
Command Condition: return true if id expired.
bool waitFor(RTT::os::Timer::TimerId id)
Command Implementation: wait until a timer expires.
Definition: Category.hpp:10