testWithStateMachine.cpp
Go to the documentation of this file.
00001 #include <string>
00002 
00003 #include <ocl/HMIConsoleOutput.hpp>
00004 #include <timer/TimerComponent.hpp>
00005 #include <taskbrowser/TaskBrowser.hpp>
00006 
00007 #include <rtt/scripting/Scripting.hpp>
00008 #include <rtt/Activity.hpp>
00009 #include <rtt/scripting/StateMachine.hpp>
00010 #include <iostream>
00011 #include <rtt/os/main.h>
00012 
00013 using namespace std;
00014 using namespace Orocos;
00015 using namespace RTT;
00016 using namespace boost;
00017 
00018 // test TimerComponent when used by state machine (ie via Orocos interface)
00019 class TestStateMachine
00020     : public TaskContext
00021 {
00022     Handle h;
00023         // log a message
00024         RTT::Operation<void(std::string)>                                       log_mtd;
00025     InputPort<os::Timer::TimerId> receiver;
00026 
00027 public:
00028     TestStateMachine(std::string name) :
00029             TaskContext(name, PreOperational),
00030             log_mtd("log", &TestStateMachine::doLog, this)
00031     {
00032         addOperation( log_mtd ).doc("Log a message").arg("message", "Message to log");
00033         addEventPort("TimerIn", receiver);
00034     }
00035 
00036     bool startHook()
00037     {
00038         bool                            rc = false;             // prove otherwise
00039         scripting::StateMachinePtr      p;
00040         shared_ptr<Scripting> scripting = getProvider<Scripting>("scripting");
00041         if (!scripting)
00042             return false;
00043         Logger::In                      in(getName());
00044         std::string         machineName = this->getName();
00045         if ( scripting->hasStateMachine(machineName))
00046         {
00047             if (scripting->activateStateMachine(machineName))
00048             {
00049                 if (scripting->startStateMachine(machineName))
00050                 {
00051                     rc = true;
00052                 }
00053                 else
00054                 {
00055                     log(Error) << "Unable to start state machine: " << machineName << endlog();
00056                 }
00057             }
00058             else
00059             {
00060                 log(Error) << "Unable to activate state machine: " << machineName << endlog();
00061             }
00062         }
00063         else
00064         {
00065             log(Error) << "Unable to find state machine: " << machineName << endlog();
00066         }
00067         return rc;
00068     }
00069 
00070     void doLog(std::string message)
00071     {
00072         Logger::In                      in(getName());
00073         log(Info) << message << endlog();
00074     }
00075 };
00076 
00077 int ORO_main( int argc, char** argv)
00078 {
00079     // Set log level more verbose than default,
00080     // such that we can see output :
00081     if ( Logger::log().getLogLevel() < Logger::Info ) {
00082         Logger::log().setLogLevel( Logger::Info );
00083         log(Info) << argv[0]
00084                       << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
00085     }
00086 
00087     HMIConsoleOutput hmi("hmi");
00088     hmi.setActivity( new Activity(ORO_SCHED_RT, os::HighestPriority, 0.1) );
00089 
00090     TimerComponent tcomp("Timer");
00091     tcomp.setActivity( new Activity(ORO_SCHED_RT, os::HighestPriority ) );
00092 
00093     TestStateMachine peer("testWithStateMachine");  // match filename
00094     peer.setActivity( new Activity(ORO_SCHED_RT, os::HighestPriority, 0.1 ) );
00095 
00096     peer.addPeer(&tcomp);
00097     peer.addPeer(&hmi);
00098 
00099     peer.ports()->getPort("TimerIn")->connectTo( tcomp.ports()->getPort("timeout"));
00100 
00101     std::string name = "testWithStateMachine.osd";
00102     assert (peer.getProvider<Scripting>("scripting"));
00103         if ( !peer.getProvider<Scripting>("scripting")->loadStateMachines(name) )
00104     {
00105         log(Error) << "Unable to load state machine: '" << name << "'" << endlog();
00106         tcomp.getActivity()->stop();
00107         return -1;
00108     }
00109 
00110     TaskBrowser tb( &peer );
00111 
00112     peer.configure();
00113     peer.start();
00114     tcomp.configure();
00115     tcomp.start();
00116     hmi.start();
00117 
00118     tb.loop();
00119 
00120     tcomp.stop();
00121     peer.stop();
00122 
00123     return 0;
00124 }
00125 


ocl
Author(s): OCL Development Team
autogenerated on Mon Oct 6 2014 03:16:37