OrocosComponentLibrary  2.8.3
HelloWorld.cpp
Go to the documentation of this file.
1 
8 #include <rtt/os/main.h>
9 
10 #include <rtt/TaskContext.hpp>
11 #include <taskbrowser/TaskBrowser.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;
72  RTT::OutputPort<std::string> outport;
76  RTT::InputPort<std::string> bufferport;
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"
212 ORO_CREATE_COMPONENT( OCL::HelloWorld )
213 
214 #endif
Every component inherits from the &#39;RTT::TaskContext&#39; class.
Definition: HelloWorld.cpp:34
This component allows a text client to browse the peers of a peer RTT::TaskContext and execute comman...
Definition: TaskBrowser.hpp:86
HelloWorld(std::string name)
This example sets the interface up in the Constructor of the component.
Definition: HelloWorld.cpp:118
std::string constant
Constants are aliased, but can only be changed from the component itself.
Definition: HelloWorld.cpp:61
RTT::OutputPort< std::string > outport
We publish our data through this RTT::OutputPort.
Definition: HelloWorld.cpp:72
STL namespace.
bool flag
Attribute that you can toggle to influence what is printed in updateHook()
Definition: HelloWorld.cpp:52
std::string property
Properties take a name, a value and a description and are suitable for XML.
Definition: HelloWorld.cpp:46
This file contains the macros and definitions to create dynamically loadable components.
RTT::InputPort< std::string > bufferport
This RTT::InputPort buffers incoming data.
Definition: HelloWorld.cpp:76
std::string mymethod()
An operation we want to add to our interface.
Definition: HelloWorld.cpp:82
Definition: OCL.hpp:28
The Orocos Component Library.
Definition: Component.hpp:43
bool sayWorld(const std::string &word)
This one is executed in our own thread.
Definition: HelloWorld.cpp:89
std::string attribute
Attributes are aliased to class variables.
Definition: HelloWorld.cpp:56
void loop()
Call this method from ORO_main() to process keyboard input and thus startup the TaskBrowser.
Definition: Category.hpp:10