HelloWorld.cpp
Go to the documentation of this file.
1 
8 #include <rtt/os/main.h>
9 
10 #include <rtt/TaskContext.hpp>
12 #include <rtt/Logger.hpp>
13 #include <rtt/Property.hpp>
14 #include <rtt/Attribute.hpp>
15 #include <rtt/OperationCaller.hpp>
16 #include <rtt/Port.hpp>
17 #include <rtt/Activity.hpp>
18 
19 #include <ocl/OCL.hpp>
20 
21 using namespace std;
22 using namespace RTT;
23 using namespace RTT::detail; // workaround in 2.0 transition phase.
24 using namespace Orocos;
25 
26 namespace OCL
27 {
28 
34  class HelloWorld
35  : public RTT::TaskContext
36  {
37  protected:
46  std::string property;
47 
52  bool flag;
56  std::string attribute;
61  std::string constant;
82  std::string mymethod() {
83  return "Hello World";
84  }
85 
89  bool sayWorld( const std::string& word) {
90  cout <<"Saying Hello '"<<word<<"' in own thread." <<endl;
91  if (word == "World")
92  return true;
93  return false;
94  }
95 
96  void updateHook() {
97  if (flag) {
98  cout <<"flag: " << flag <<endl;
99  cout <<"the_property: "<< property <<endl;
100  cout <<"the_attribute: "<< attribute <<endl;
101  cout <<"the_constant: "<< constant <<endl;
102  cout <<"Setting 'flag' back to false."<<endl;
103  flag = false;
104  }
105 
106  outport.write("Hello World!");
107 
108  std::string sample;
109  while(bufferport.read(sample) == NewData) {
110  log(Debug) << "Received " << sample << endlog();
111  }
112  }
113  public:
118  HelloWorld(std::string name)
119  : RTT::TaskContext(name,PreOperational),
120  // Name, description, value
121  property("Hello Property"), flag(false),
122  attribute("Hello Attribute"),
123  constant("Hello Constant"),
124  // Name, initial value
125  outport("the_results",true),
126  // Name, policy
127  bufferport("the_buffer_port",ConnPolicy::buffer(13,ConnPolicy::LOCK_FREE,true) )
128  {
129  // New activity with period 0.1s and priority 0.
130  this->setActivity( new Activity(0, 0.1) );
131 
132  // Set log level more verbose than default,
133  // such that we can see output :
134  //if ( log().getLogLevel() < RTT::Logger::Info ) {
135  // log().setLogLevel( RTT::Logger::Info );
136  // log(Info) << "HelloWorld manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
137  //}
138 
139  // Now add member variables to the interface:
140  this->properties()->addProperty("the_property", property).doc("A friendly property.");
141 
142  this->addAttribute("flag", flag);
143  this->addAttribute("the_attribute", attribute);
144  this->addConstant("the_constant", constant);
145 
146  this->ports()->addPort( outport );
147  this->ports()->addPort( bufferport );
148 
149  this->addOperation( "the_method", &HelloWorld::mymethod, this, ClientThread ).doc("'the_method' Description");
150 
151  this->addOperation( "the_command", &HelloWorld::sayWorld, this, OwnThread).doc("'the_command' Description").arg("the_arg", "Use 'World' as argument to make the command succeed.");
152 
153  // log(Info) << "**** Starting the 'Hello' component is cancelled ****" <<endlog();
154  // Start the component's activity:
155  //this->start();
156  }
157  };
158 }
159 
160 
161 // This define allows to compile the hello world component as a library
162 // liborocos-helloworld.so or as a program (helloworld). Your component
163 // should only be compiled as a library.
164 #ifndef OCL_COMPONENT_ONLY
165 
166 int ORO_main(int argc, char** argv)
167 {
168  RTT::Logger::In in("main()");
169 
170  // Set log level more verbose than default,
171  // such that we can see output :
172  if ( log().getLogLevel() < RTT::Logger::Info ) {
173  log().setLogLevel( RTT::Logger::Info );
174  log(Info) << argv[0] << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
175  }
176 
177  log(Info) << "**** Creating the 'Hello' component ****" <<endlog();
178  // Create the task:
179  HelloWorld hello("Hello");
180 
181  log(Info) << "**** Using the 'Hello' component ****" <<endlog();
182 
183  // Do some 'client' calls :
184  log(Info) << "**** Reading a RTT::Property: ****" <<endlog();
185  RTT::Property<std::string> p = hello.properties()->getProperty("the_property");
186  assert( p.ready() );
187  log(Info) << " "<<p.getName() << " = " << p.value() <<endlog();
188 #if 0
189  log(Info) << "**** Sending a RTT::OperationCaller: ****" <<endlog();
190  RTT::OperationCaller<bool(std::string)> c = hello.getOperation<bool(std::string)>("the_command");
191  assert( c.ready() );
192  log(Info) << " Sending RTT::OperationCaller : " << c.send("World")<<endlog();
193 
194  log(Info) << "**** Calling a RTT::OperationCaller: ****" <<endlog();
195  RTT::OperationCaller<std::string(void)> m = hello.getOperation<std::string(void)>("the_method");
196  assert( m.ready() );
197  log(Info) << " Calling RTT::OperationCaller : " << m() << endlog();
198 #endif
199  log(Info) << "**** Starting the TaskBrowser ****" <<endlog();
200  // Switch to user-interactive mode.
201  TaskBrowser browser( &hello );
202 
203  // Accept user commands from console.
204  browser.loop();
205 
206  return 0;
207 }
208 
209 #else
210 
211 #include "ocl/Component.hpp"
213 
214 #endif
void loop()
Call this method from ORO_main() to process keyboard input and thus startup the TaskBrowser.
int ORO_main(int argc, char **argv)
Definition: HelloWorld.cpp:166
This component allows a text client to browse the peers of a peer RTT::TaskContext and execute comman...
Definition: TaskBrowser.hpp:86
bool ready() const
HelloWorld(std::string name)
Definition: HelloWorld.cpp:118
bool sayWorld(const std::string &word)
Definition: HelloWorld.cpp:89
std::string property
Definition: HelloWorld.cpp:46
RTT::InputPort< std::string > bufferport
Definition: HelloWorld.cpp:76
NewData
OwnThread
FlowStatus read(base::DataSourceBase::shared_ptr source)
reference_t value()
std::string attribute
Definition: HelloWorld.cpp:56
printstream cout
WriteStatus write(const T &sample)
Debug
basic_ostreams & endl(basic_ostreams &s)
Info
base::PropertyBase * getProperty(const std::string &name) const
PropertyBag * properties()
OperationInterfacePart * getOperation(std::string name)
const std::string & getName() const
RTT::OutputPort< std::string > outport
Definition: HelloWorld.cpp:72
std::string mymethod()
Definition: HelloWorld.cpp:82
std::string constant
Definition: HelloWorld.cpp:61
ClientThread
ORO_CREATE_COMPONENT(OCL::logging::Log4cxxAppender)


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