00001 #include <boost/bind.hpp>
00002 #include <timer/TimerComponent.hpp>
00003 #include <taskbrowser/TaskBrowser.hpp>
00004
00005 #include <rtt/Activity.hpp>
00006 #include <rtt/InputPort.hpp>
00007 #include <iostream>
00008 #include <rtt/os/main.h>
00009
00010 using namespace std;
00011 using namespace Orocos;
00012 using namespace RTT;
00013
00014 class TestTaskContext
00015 : public RTT::TaskContext
00016 {
00017 InputPort<os::Timer::TimerId> receiver;
00018 public:
00019 TestTaskContext(std::string name)
00020 : RTT::TaskContext(name, PreOperational),
00021 receiver("TimerIn")
00022 {
00023 ports()->addEventPort( receiver );
00024 }
00025
00026 bool configureHook()
00027 {
00028 if ( receiver.connected() )
00029 log(Info) << this->getName() <<" starts listening for timeout events." << endlog();
00030 return receiver.connected();
00031 }
00032
00033 void updateHook()
00034 {
00035 os::Timer::TimerId id;
00036 if (receiver.read(id) == NewData)
00037 log(Info) << this->getName() <<" detects timeout for timer " << id << endlog();
00038 }
00039 };
00040
00041 int ORO_main( int argc, char** argv)
00042 {
00043
00044
00045 if ( RTT::Logger::log().getLogLevel() < RTT::Logger::Info ) {
00046 RTT::Logger::log().setLogLevel( RTT::Logger::Info );
00047 log(Info) << argv[0]
00048 << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
00049 }
00050
00051
00052 TimerComponent tcomp("Timer");
00053 tcomp.setActivity( new RTT::Activity(ORO_SCHED_RT, os::HighestPriority, 0.0) );
00054
00055 TestTaskContext gtc("Peer");
00056 gtc.setActivity( new RTT::Activity(ORO_SCHED_RT, os::HighestPriority, 0.1) );
00057
00058 gtc.ports()->getPort("TimerIn")->connectTo( tcomp.ports()->getPort("timeout"));
00059
00060 TaskBrowser tb( >c );
00061
00062 gtc.configure();
00063 gtc.start();
00064 gtc.addPeer( &tcomp );
00065 tcomp.configure();
00066 tcomp.start();
00067
00068 cout <<endl<< " This demo allows testing the TimerComponent." << endl;
00069 cout << " Use 'Timer.arm(0, 1.5)' to arm timer '0' to end over 1.5 seconds. " <<endl;
00070 cout << " 32 timers are initially available (0..31)." <<endl;
00071 cout << " Other methods (type 'this') are available as well."<<endl;
00072
00073 tb.loop();
00074
00075 tcomp.stop();
00076 gtc.stop();
00077
00078 return 0;
00079 }
00080