HelloWorld.cpp
Go to the documentation of this file.
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/OperationCaller.hpp>
00016 #include <rtt/Port.hpp>
00017 #include <rtt/Activity.hpp>
00018 
00019 #include <ocl/OCL.hpp>
00020 
00021 using namespace std;
00022 using namespace RTT;
00023 using namespace RTT::detail; // workaround in 2.0 transition phase.
00024 using namespace Orocos;
00025 
00026 namespace OCL
00027 {
00028 
00034     class HelloWorld
00035         : public RTT::TaskContext
00036     {
00037     protected:
00046         std::string property;
00047 
00052         bool flag;
00056         std::string attribute;
00061         std::string constant;
00072         RTT::OutputPort<std::string> outport;
00076         RTT::InputPort<std::string> bufferport;
00082         std::string mymethod() {
00083             return "Hello World";
00084         }
00085 
00089         bool sayWorld( const std::string& word) {
00090             cout <<"Saying Hello '"<<word<<"' in own thread." <<endl;
00091             if (word == "World")
00092                 return true;
00093             return false;
00094         }
00095 
00096         void updateHook() {
00097                 if (flag) {
00098                         cout <<"flag: " << flag <<endl;
00099                         cout <<"the_property: "<< property <<endl;
00100                         cout <<"the_attribute: "<< attribute <<endl;
00101                         cout <<"the_constant: "<< constant <<endl;
00102                         cout <<"Setting 'flag' back to false."<<endl;
00103                         flag = false;
00104                 }
00105 
00106             outport.write("Hello World!");
00107 
00108             std::string sample;
00109             while(bufferport.read(sample) == NewData) {
00110                 log(Debug) << "Received " << sample << endlog();
00111             }
00112         }
00113     public:
00118         HelloWorld(std::string name)
00119             : RTT::TaskContext(name,PreOperational),
00120               // Name, description, value
00121               property("Hello Property"), flag(false),
00122               attribute("Hello Attribute"),
00123               constant("Hello Constant"),
00124               // Name, initial value
00125               outport("the_results",true),
00126               // Name, policy
00127               bufferport("the_buffer_port",ConnPolicy::buffer(13,ConnPolicy::LOCK_FREE,true) )
00128         {
00129             // New activity with period 0.1s and priority 0.
00130             this->setActivity( new Activity(0, 0.1) );
00131 
00132             // Set log level more verbose than default,
00133             // such that we can see output :
00134             //if ( log().getLogLevel() < RTT::Logger::Info ) {
00135             //    log().setLogLevel( RTT::Logger::Info );
00136             //    log(Info) << "HelloWorld manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
00137             //}
00138 
00139             // Now add member variables to the interface:
00140             this->properties()->addProperty("the_property", property).doc("A friendly property.");
00141 
00142             this->addAttribute("flag", flag);
00143             this->addAttribute("the_attribute", attribute);
00144             this->addConstant("the_constant", constant);
00145 
00146             this->ports()->addPort( outport );
00147             this->ports()->addPort( bufferport );
00148 
00149             this->addOperation( "the_method", &HelloWorld::mymethod, this, ClientThread ).doc("'the_method' Description");
00150 
00151             this->addOperation( "the_command", &HelloWorld::sayWorld, this, OwnThread).doc("'the_command' Description").arg("the_arg", "Use 'World' as argument to make the command succeed.");
00152 
00153             // log(Info) << "**** Starting the 'Hello' component is cancelled ****" <<endlog();
00154             // Start the component's activity:
00155             //this->start();
00156         }
00157     };
00158 }
00159 
00160 
00161 // This define allows to compile the hello world component as a library
00162 // liborocos-helloworld.so or as a program (helloworld). Your component
00163 // should only be compiled as a library.
00164 #ifndef OCL_COMPONENT_ONLY
00165 
00166 int ORO_main(int argc, char** argv)
00167 {
00168     RTT::Logger::In in("main()");
00169 
00170     // Set log level more verbose than default,
00171     // such that we can see output :
00172     if ( log().getLogLevel() < RTT::Logger::Info ) {
00173         log().setLogLevel( RTT::Logger::Info );
00174         log(Info) << argv[0] << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog();
00175     }
00176 
00177     log(Info) << "**** Creating the 'Hello' component ****" <<endlog();
00178     // Create the task:
00179     HelloWorld hello("Hello");
00180 
00181     log(Info) << "**** Using the 'Hello' component    ****" <<endlog();
00182 
00183     // Do some 'client' calls :
00184     log(Info) << "**** Reading a RTT::Property:            ****" <<endlog();
00185     RTT::Property<std::string> p = hello.properties()->getProperty("the_property");
00186     assert( p.ready() );
00187     log(Info) << "     "<<p.getName() << " = " << p.value() <<endlog();
00188 #if 0
00189     log(Info) << "**** Sending a RTT::OperationCaller:             ****" <<endlog();
00190     RTT::OperationCaller<bool(std::string)> c = hello.getOperation<bool(std::string)>("the_command");
00191     assert( c.ready() );
00192     log(Info) << "     Sending RTT::OperationCaller : " << c.send("World")<<endlog();
00193 
00194     log(Info) << "**** Calling a RTT::OperationCaller:              ****" <<endlog();
00195     RTT::OperationCaller<std::string(void)> m = hello.getOperation<std::string(void)>("the_method");
00196     assert( m.ready() );
00197     log(Info) << "     Calling RTT::OperationCaller : " << m() << endlog();
00198 #endif
00199     log(Info) << "**** Starting the TaskBrowser       ****" <<endlog();
00200     // Switch to user-interactive mode.
00201     TaskBrowser browser( &hello );
00202 
00203     // Accept user commands from console.
00204     browser.loop();
00205 
00206     return 0;
00207 }
00208 
00209 #else
00210 
00211 #include "ocl/Component.hpp"
00212 ORO_CREATE_COMPONENT( OCL::HelloWorld )
00213 
00214 #endif


ocl
Author(s): OCL Development Team
autogenerated on Sat Jun 8 2019 18:48:54