OrocosComponentLibrary
2.7.0
|
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 public: 00111 HelloWorld(std::string name) 00112 : RTT::TaskContext(name,PreOperational), 00113 // Name, description, value 00114 property("Hello Property"), flag(false), 00115 attribute("Hello Attribute"), 00116 constant("Hello Constant"), 00117 // Name, initial value 00118 outport("the_results",true), 00119 // Name, policy 00120 bufferport("the_buffer_port",ConnPolicy::buffer(13,ConnPolicy::LOCK_FREE,true) ) 00121 { 00122 // New activity with period 0.1s and priority 0. 00123 this->setActivity( new Activity(0, 0.1) ); 00124 00125 // Set log level more verbose than default, 00126 // such that we can see output : 00127 //if ( log().getLogLevel() < RTT::Logger::Info ) { 00128 // log().setLogLevel( RTT::Logger::Info ); 00129 // log(Info) << "HelloWorld manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog(); 00130 //} 00131 00132 // Now add member variables to the interface: 00133 this->properties()->addProperty("the_property", property).doc("A friendly property."); 00134 00135 this->addAttribute("flag", flag); 00136 this->addAttribute("the_attribute", attribute); 00137 this->addConstant("the_constant", constant); 00138 00139 this->ports()->addPort( outport ); 00140 this->ports()->addPort( bufferport ); 00141 00142 this->addOperation( "the_method", &HelloWorld::mymethod, this, ClientThread ).doc("'the_method' Description"); 00143 00144 this->addOperation( "the_command", &HelloWorld::sayWorld, this, OwnThread).doc("'the_command' Description").arg("the_arg", "Use 'World' as argument to make the command succeed."); 00145 00146 // log(Info) << "**** Starting the 'Hello' component is cancelled ****" <<endlog(); 00147 // Start the component's activity: 00148 //this->start(); 00149 } 00150 }; 00151 } 00152 00153 00154 // This define allows to compile the hello world component as a library 00155 // liborocos-helloworld.so or as a program (helloworld). Your component 00156 // should only be compiled as a library. 00157 #ifndef OCL_COMPONENT_ONLY 00158 00159 int ORO_main(int argc, char** argv) 00160 { 00161 RTT::Logger::In in("main()"); 00162 00163 // Set log level more verbose than default, 00164 // such that we can see output : 00165 if ( log().getLogLevel() < RTT::Logger::Info ) { 00166 log().setLogLevel( RTT::Logger::Info ); 00167 log(Info) << argv[0] << " manually raises LogLevel to 'Info' (5). See also file 'orocos.log'."<<endlog(); 00168 } 00169 00170 log(Info) << "**** Creating the 'Hello' component ****" <<endlog(); 00171 // Create the task: 00172 HelloWorld hello("Hello"); 00173 00174 log(Info) << "**** Using the 'Hello' component ****" <<endlog(); 00175 00176 // Do some 'client' calls : 00177 log(Info) << "**** Reading a RTT::Property: ****" <<endlog(); 00178 RTT::Property<std::string> p = hello.properties()->getProperty("the_property"); 00179 assert( p.ready() ); 00180 log(Info) << " "<<p.getName() << " = " << p.value() <<endlog(); 00181 #if 0 00182 log(Info) << "**** Sending a RTT::OperationCaller: ****" <<endlog(); 00183 RTT::OperationCaller<bool(std::string)> c = hello.getOperation<bool(std::string)>("the_command"); 00184 assert( c.ready() ); 00185 log(Info) << " Sending RTT::OperationCaller : " << c.send("World")<<endlog(); 00186 00187 log(Info) << "**** Calling a RTT::OperationCaller: ****" <<endlog(); 00188 RTT::OperationCaller<std::string(void)> m = hello.getOperation<std::string(void)>("the_method"); 00189 assert( m.ready() ); 00190 log(Info) << " Calling RTT::OperationCaller : " << m() << endlog(); 00191 #endif 00192 log(Info) << "**** Starting the TaskBrowser ****" <<endlog(); 00193 // Switch to user-interactive mode. 00194 TaskBrowser browser( &hello ); 00195 00196 // Accept user commands from console. 00197 browser.loop(); 00198 00199 return 0; 00200 } 00201 00202 #else 00203 00204 #include "ocl/Component.hpp" 00205 ORO_CREATE_COMPONENT( OCL::HelloWorld ) 00206 00207 #endif