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/Activity.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(Info) << "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(Info) << "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(Info) << "Receiving Event: " << data << endlog();
00147 }
00150 public:
00155 HelloWorld(std::string name)
00156 : TaskContext(name),
00157
00158 property("the_property", "the_property Description", "Hello World"),
00159
00160 attribute("the_attribute", "Hello World"),
00161
00162 constant("the_constant", "Hello World"),
00163
00164 dataport("the_data_port","World"),
00165
00166 bufferport("the_buffer_port",13, "World"),
00167
00168 method("the_method", &HelloWorld::mymethod, this),
00169
00170 command("the_command", &HelloWorld::mycommand, &HelloWorld::mycomplete, this),
00171
00172 event("the_event")
00173 {
00174
00175 this->setActivity( new Activity(0, 0.01) );
00176
00177
00178
00179 if ( log().getLogLevel() < Logger::Info ) {
00180 log().setLogLevel( Logger::Info );
00181 log(Info) << "HelloWorld manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
00182 }
00183
00184
00185 assert( property.ready() );
00186 assert( attribute.ready() );
00187 assert( constant.ready() );
00188 assert( method.ready() );
00189 assert( command.ready() );
00190 assert( event.ready() );
00191
00192
00193 this->properties()->addProperty(&property);
00194
00195 this->attributes()->addAttribute(&attribute);
00196 this->attributes()->addConstant(&constant);
00197
00198 this->ports()->addPort(&dataport);
00199 this->ports()->addPort(&bufferport);
00200
00201 this->methods()->addMethod(&method, "'the_method' Description");
00202
00203 this->commands()->addCommand(&command, "'the_command' Description",
00204 "the_arg", "Use 'World' as argument to make the command succeed.");
00205
00206 this->events()->addEvent(&event, "'the_event' Description",
00207 "the_data", "The data of this event.");
00208
00209
00210 h = this->events()->setupConnection("the_event").callback(this, &HelloWorld::mycallback, this->engine()->events() ).handle();
00211 h.connect();
00212
00213 log(Info) << "**** Starting the 'Hello' component ****" <<endlog();
00214
00215 this->start();
00216 }
00217 };
00218 }
00219
00220
00221
00222
00223
00224 #ifndef OCL_COMPONENT_ONLY
00225
00226 int ORO_main(int argc, char** argv)
00227 {
00228 Logger::In in("main()");
00229
00230
00231
00232 if ( log().getLogLevel() < Logger::Info ) {
00233 log().setLogLevel( Logger::Info );
00234 log(Info) << argv[0] << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
00235 }
00236
00237 log(Info) << "**** Creating the 'Hello' component ****" <<endlog();
00238
00239 HelloWorld hello("Hello");
00240
00241 log(Info) << "**** Using the 'Hello' component ****" <<endlog();
00242
00243
00244 log(Info) << "**** Reading a Property: ****" <<endlog();
00245 Property<std::string> p = hello.properties()->getProperty<std::string>("the_property");
00246 assert( p.ready() );
00247 log(Info) << " "<<p.getName() << " = " << p.value() <<endlog();
00248
00249 log(Info) << "**** Sending a Command: ****" <<endlog();
00250 Command<bool(std::string)> c = hello.commands()->getCommand<bool(std::string)>("the_command");
00251 assert( c.ready() );
00252 log(Info) << " Sending Command : " << c("World")<<endlog();
00253
00254 log(Info) << "**** Calling a Method: ****" <<endlog();
00255 Method<std::string(void)> m = hello.methods()->getMethod<std::string(void)>("the_method");
00256 assert( m.ready() );
00257 log(Info) << " Calling Method : " << m() << endlog();
00258
00259 log(Info) << "**** Emitting an Event: ****" <<endlog();
00260 Event<void(std::string)> e = hello.events()->getEvent<void(std::string)>("the_event");
00261 assert( e.ready() );
00262
00263 e("Hello World");
00264
00265 log(Info) << "**** Starting the TaskBrowser ****" <<endlog();
00266
00267 TaskBrowser browser( &hello );
00268
00269
00270 browser.loop();
00271
00272 return 0;
00273 }
00274
00275 #else
00276
00277 #include "ocl/ComponentLoader.hpp"
00278 ORO_CREATE_COMPONENT( OCL::HelloWorld )
00279
00280 #endif