00001
00008 #include <rtt/os/main.h>
00009
00010 #include <rtt/TaskContext.hpp>
00011 #include <taskbrowser/TaskBrowser.hpp>
00012 #include <rtt/Logger.hpp>
00013 #include <rtt/Property.hpp>
00014 #include <rtt/Attribute.hpp>
00015 #include <rtt/Method.hpp>
00016 #include <rtt/Command.hpp>
00017 #include <rtt/Event.hpp>
00018 #include <rtt/Ports.hpp>
00019 #include <rtt/PeriodicActivity.hpp>
00020
00021 #include <ocl/OCL.hpp>
00022
00023 using namespace std;
00024 using namespace RTT;
00025 using namespace Orocos;
00026
00027 namespace OCL
00028 {
00029
00035 class HelloWorld
00036 : public TaskContext
00037 {
00038 protected:
00047 Property<std::string> property;
00051 Attribute<std::string> attribute;
00055 Constant<std::string> constant;
00066 DataPort<std::string> dataport;
00071 BufferPort<std::string> bufferport;
00083 Method<std::string(void)> method;
00084
00089 std::string mymethod() {
00090 return "Hello World";
00091 }
00103 Command<bool(std::string)> command;
00104
00108 bool mycommand(std::string arg) {
00109 log() << "Hello Command: "<< arg << endlog();
00110 if ( arg == "World" )
00111 return true;
00112 else
00113 return false;
00114 }
00115
00119 bool mycomplete(std::string arg) {
00120 log() << "Checking Command: "<< arg <<endlog();
00121 return true;
00122 }
00134 Event<void(std::string)> event;
00135
00139 Handle h;
00140
00144 void mycallback( std::string data )
00145 {
00146 log() << "Receiving Event: " << data << endlog();
00147 }
00150 PeriodicActivity act;
00151
00152 public:
00157 HelloWorld(std::string name)
00158 : TaskContext(name),
00159
00160 property("the_property", "the_property Description", "Hello World"),
00161
00162 attribute("the_attribute", "Hello World"),
00163
00164 constant("the_constant", "Hello World"),
00165
00166 dataport("the_data_port","World"),
00167
00168 bufferport("the_buffer_port",13, "World"),
00169
00170 method("the_method", &HelloWorld::mymethod, this),
00171
00172 command("the_command", &HelloWorld::mycommand, &HelloWorld::mycomplete, this),
00173
00174 event("the_event"),
00175
00176
00177
00178
00179
00180 act(0, 0.01, this->engine() )
00181 {
00182
00183
00184 if ( log().getLogLevel() < Logger::Info ) {
00185 log().setLogLevel( Logger::Info );
00186 log(Info) << "HelloWorld manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
00187 }
00188
00189
00190 assert( property.ready() );
00191 assert( attribute.ready() );
00192 assert( constant.ready() );
00193 assert( method.ready() );
00194 assert( command.ready() );
00195 assert( event.ready() );
00196
00197
00198 this->properties()->addProperty(&property);
00199
00200 this->attributes()->addAttribute(&attribute);
00201 this->attributes()->addConstant(&constant);
00202
00203 this->ports()->addPort(&dataport);
00204 this->ports()->addPort(&bufferport);
00205
00206 this->methods()->addMethod(&method, "'the_method' Description");
00207
00208 this->commands()->addCommand(&command, "'the_command' Description",
00209 "the_arg", "Use 'World' as argument to make the command succeed.");
00210
00211 this->events()->addEvent(&event, "'the_event' Description",
00212 "the_data", "The data of this event.");
00213
00214
00215 h = this->events()->setupConnection("the_event").callback(this, &HelloWorld::mycallback, this->engine()->events() ).handle();
00216 h.connect();
00217
00218 log(Info) << "**** Starting the 'Hello' component ****" <<endlog();
00219
00220 this->start();
00221 }
00222 };
00223 }
00224
00225
00226
00227
00228
00229 #ifndef OCL_COMPONENT_ONLY
00230
00231 int ORO_main(int argc, char** argv)
00232 {
00233 Logger::In in("main()");
00234
00235
00236
00237 if ( log().getLogLevel() < Logger::Info ) {
00238 log().setLogLevel( Logger::Info );
00239 log(Info) << argv[0] << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
00240 }
00241
00242 log(Info) << "**** Creating the 'Hello' component ****" <<endlog();
00243
00244 HelloWorld hello("Hello");
00245
00246 log(Info) << "**** Using the 'Hello' component ****" <<endlog();
00247
00248
00249 log(Info) << "**** Reading a Property: ****" <<endlog();
00250 Property<std::string> p = hello.properties()->getProperty<std::string>("the_property");
00251 assert( p.ready() );
00252 log(Info) << " "<<p.getName() << " = " << p.value() <<endlog();
00253
00254 log(Info) << "**** Sending a Command: ****" <<endlog();
00255 Command<bool(std::string)> c = hello.commands()->getCommand<bool(std::string)>("the_command");
00256 assert( c.ready() );
00257 log(Info) << " Sending Command : " << c("World")<<endlog();
00258
00259 log(Info) << "**** Calling a Method: ****" <<endlog();
00260 Method<std::string(void)> m = hello.methods()->getMethod<std::string(void)>("the_method");
00261 assert( m.ready() );
00262 log(Info) << " Calling Method : " << m() << endlog();
00263
00264 log(Info) << "**** Emitting an Event: ****" <<endlog();
00265 Event<void(std::string)> e = hello.events()->getEvent<void(std::string)>("the_event");
00266 assert( e.ready() );
00267
00268 e("Hello World");
00269
00270 log(Info) << "**** Starting the TaskBrowser ****" <<endlog();
00271
00272 TaskBrowser browser( &hello );
00273
00274
00275 browser.loop();
00276
00277 return 0;
00278 }
00279
00280 #else
00281
00282 #include "ocl/ComponentLoader.hpp"
00283 ORO_CREATE_COMPONENT( OCL::HelloWorld )
00284
00285 #endif