testWithStateMachine.cpp
Go to the documentation of this file.
1 #include <string>
2 
6 
8 #include <rtt/Activity.hpp>
10 #include <iostream>
11 #include <rtt/os/main.h>
12 
13 using namespace std;
14 using namespace Orocos;
15 using namespace RTT;
16 using namespace boost;
17 
18 // test TimerComponent when used by state machine (ie via Orocos interface)
20  : public TaskContext
21 {
23  // log a message
26 
27 public:
28  TestStateMachine(std::string name) :
29  TaskContext(name, PreOperational),
30  log_mtd("log", &TestStateMachine::doLog, this)
31  {
32  addOperation( log_mtd ).doc("Log a message").arg("message", "Message to log");
33  addEventPort("TimerIn", receiver);
34  }
35 
36  bool startHook()
37  {
38  bool rc = false; // prove otherwise
40  shared_ptr<Scripting> scripting = getProvider<Scripting>("scripting");
41  if (!scripting)
42  return false;
43  Logger::In in(getName());
44  std::string machineName = this->getName();
45  if ( scripting->hasStateMachine(machineName))
46  {
47  if (scripting->activateStateMachine(machineName))
48  {
49  if (scripting->startStateMachine(machineName))
50  {
51  rc = true;
52  }
53  else
54  {
55  log(Error) << "Unable to start state machine: " << machineName << endlog();
56  }
57  }
58  else
59  {
60  log(Error) << "Unable to activate state machine: " << machineName << endlog();
61  }
62  }
63  else
64  {
65  log(Error) << "Unable to find state machine: " << machineName << endlog();
66  }
67  return rc;
68  }
69 
70  void doLog(std::string message)
71  {
72  Logger::In in(getName());
73  log(Info) << message << endlog();
74  }
75 };
76 
77 int ORO_main( int argc, char** argv)
78 {
79  // Set log level more verbose than default,
80  // such that we can see output :
81  if ( Logger::log().getLogLevel() < Logger::Info ) {
82  Logger::log().setLogLevel( Logger::Info );
83  log(Info) << argv[0]
84  << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
85  }
86 
87  HMIConsoleOutput hmi("hmi");
88  hmi.setActivity( new Activity(ORO_SCHED_RT, os::HighestPriority, 0.1) );
89 
90  TimerComponent tcomp("Timer");
91  tcomp.setActivity( new Activity(ORO_SCHED_RT, os::HighestPriority ) );
92 
93  TestStateMachine peer("testWithStateMachine"); // match filename
94  peer.setActivity( new Activity(ORO_SCHED_RT, os::HighestPriority, 0.1 ) );
95 
96  peer.addPeer(&tcomp);
97  peer.addPeer(&hmi);
98 
99  peer.ports()->getPort("TimerIn")->connectTo( tcomp.ports()->getPort("timeout"));
100 
101  std::string name = "testWithStateMachine.osd";
102  assert (peer.getProvider<Scripting>("scripting"));
103  if ( !peer.getProvider<Scripting>("scripting")->loadStateMachines(name) )
104  {
105  log(Error) << "Unable to load state machine: '" << name << "'" << endlog();
106  tcomp.getActivity()->stop();
107  return -1;
108  }
109 
110  TaskBrowser tb( &peer );
111 
112  peer.configure();
113  peer.start();
114  tcomp.configure();
115  tcomp.start();
116  hmi.start();
117 
118  tb.loop();
119 
120  tcomp.stop();
121  peer.stop();
122 
123  return 0;
124 }
125 
void loop()
Call this method from ORO_main() to process keyboard input and thus startup the TaskBrowser.
Operation< Signature > & doc(const std::string &description)
This component allows a text client to browse the peers of a peer RTT::TaskContext and execute comman...
Definition: TaskBrowser.hpp:86
virtual bool stop()
int ORO_main(int argc, char **argv)
RTT::Operation< void(std::string)> log_mtd
void doLog(std::string message)
virtual bool configure()
bool setActivity(base::ActivityInterface *new_act)
DataFlowInterface * ports()
base::PortInterface * getPort(const std::string &name) const
OperationCaller< bool(const std::string &)> loadStateMachines
A Component interface to the Real-Time types::Toolkit&#39;s timer. It must be configured with a Activity ...
InputPort< os::Timer::TimerId > receiver
boost::shared_ptr< StateMachine > StateMachinePtr
TestStateMachine(std::string name)
Error
Info
This component can be used to display messages on the standard output.
virtual bool connectTo(PortInterface *other, ConnPolicy const &policy)=0
#define ORO_SCHED_RT
virtual bool start()
virtual bool addPeer(TaskContext *peer, std::string alias="")
base::ActivityInterface * getActivity()
boost::shared_ptr< ServiceType > getProvider(const std::string &name)


ocl
Author(s): OCL Development Team
autogenerated on Wed Jun 26 2019 19:26:27