00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <rtt/Attribute.hpp>
00030 #include <rtt/Ports.hpp>
00031 #include <rtt/Logger.hpp>
00032 #include <rtt/Command.hpp>
00033 #include <hardware/lias/BaseVelocityController.hpp>
00034
00035 using namespace RTT;
00036 using namespace std;
00037
00038
00039 BaseVelocityController::BaseVelocityController(const std::string& name,const std::string& propfile)
00040 : RTT::TaskContext(name),
00041 velocity("basevelocity"),
00042 rotvel ("baserotvel"),
00043 x_world_pf("x_world_pf"),
00044 y_world_pf("y_world_pf"),
00045 theta_world_pf("theta_world_pf"),
00046 hostname("hostname","The host name to connect to","10.33.172.172"),
00047 port("port","The port of the host computer", 9999 ),
00048 logging("log","Enable logging",false)
00049 {
00053 this->ports()->addPort( &velocity );
00054 this->ports()->addPort( &rotvel );
00055 this->ports()->addPort( &x_world_pf );
00056 this->ports()->addPort( &y_world_pf );
00057 this->ports()->addPort( &theta_world_pf );
00058 this->properties()->addProperty( &hostname );
00059 this->properties()->addProperty( &port );
00060 this->properties()->addProperty( &logging );
00061
00062 if (!marshalling()->readProperties(propfile)) {
00063 log(Error) << "Failed to read the property file." << endlog();
00064 assert(0);
00065 }
00066
00071 this->commands()->addCommand( command( "initialise", &BaseVelocityController::initialise,
00072 &BaseVelocityController::initialiseDone, this),
00073 "Command to initialises the coordinates of the robot.","x","position","y","position","t","position" );
00074
00075 this->commands()->addCommand( command( "setVelocity", &BaseVelocityController::setVelocity,
00076 &BaseVelocityController::setVelocityDone, this),
00077 "Command to set a velocity of X,T for the robot.","l","linear","r","rotational" );
00078
00079 this->commands()->addCommand( command( "stopnow", &BaseVelocityController::stopnow,
00080 &BaseVelocityController::stopnowDone, this),
00081 "Command to stop the robot." );
00082
00083 this->commands()->addCommand( command( "startLaserScanner", &BaseVelocityController::startLaserScanner,
00084 &BaseVelocityController::startLaserScannerDone, this),
00085 "Command to start the laser scanner." );
00086
00087 this->commands()->addCommand( command( "stopLaserScanner", &BaseVelocityController::stopLaserScanner,
00088 &BaseVelocityController::stopLaserScannerDone, this),
00089 "Command to stop the laser scanner." );
00090
00091 cl=0;
00092 connected = false;
00093 }
00094
00095 bool BaseVelocityController::initialise(double v1,double v2,double v3)
00096 {
00097
00098 stringstream sstr;
00099 if(!connected) return false;
00100
00101 initialiseC=false;
00102 sstr << "Initialise "<<v1<<" "<<v2<<" "<<v3<<" \n" ;
00103
00104 cl->sendCommand(sstr.str());
00105 if(cl->receiveData()!="") initialiseC=true;
00106 return true;
00107
00108 }
00109
00110 bool BaseVelocityController::initialiseDone(double v1,double v2,double v3) const
00111 {
00112 return true;
00113 }
00114
00115 bool BaseVelocityController::setVelocity(double v1,double v2)
00116 {
00117 stringstream sstr;
00118 if(!connected) return false;
00119
00120 setVelocityC=false;
00121 sstr << "SetVelocity "<<v1<<" "<<v2<<" \n" ;
00122
00123 cl->sendCommand(sstr.str());
00124 if(cl->receiveData()!="")
00125 return false;
00126 else
00127 return true;
00128 }
00129
00130 bool BaseVelocityController::setVelocityDone() const
00131 {
00132 return true;
00133 }
00134
00135 bool BaseVelocityController::stopnow()
00136 {
00137 stringstream sstr;
00138 if(!connected) return false;
00139
00140 stopnowC=false;
00141 sstr << "Stop \n" ;
00142
00143 cl->sendCommand(sstr.str());
00144 if(cl->receiveData()!="")
00145 return false;
00146 else
00147 return true;
00148 }
00149
00150 bool BaseVelocityController::stopnowDone() const
00151 {
00152 return true;
00153 }
00154
00155 bool BaseVelocityController::startLaserScanner()
00156 {
00157 stringstream sstr;
00158 if(!connected) return false;
00159
00160 startLaserScannerC=false;
00161 sstr << "StartScanner \n" ;
00162
00163 cl->sendCommand(sstr.str());
00164 if(cl->receiveData()!="")
00165 return false;
00166 else
00167 return true;
00168 }
00169
00170 bool BaseVelocityController::startLaserScannerDone() const
00171 {
00172 return true;
00173 }
00174
00175 bool BaseVelocityController::stopLaserScanner()
00176 {
00177 stringstream sstr;
00178
00179 if(!connected) return false;
00180 stopLaserScannerC=false;
00181 sstr << "StartScanner \n" ;
00182
00183 cl->sendCommand(sstr.str());
00184 if(cl->receiveData()!="")
00185 return false;
00186 else
00187 return true;
00188 }
00189
00190 bool BaseVelocityController::stopLaserScannerDone() const
00191 {
00192 return true;
00193 }
00198 bool BaseVelocityController::startup() {
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 cl=new LiasClientN::Client;
00209
00210 if (cl->connect(hostname,port)!=-1) {
00211 connected=true;
00212 Logger::log() << "Connection to "<<hostname<<":"<<port<<" established."<<endlog();
00213 } else {
00214 connected=false;
00215 Logger::log() << "Could not connect to "<<hostname<<":"<<port<<endlog();
00216 }
00217
00218 return connected;
00219
00220 }
00221
00225 void BaseVelocityController::update() {
00226
00227 if (!connected) return;
00228
00229 double v = velocity.Get()*100;
00230 double w = rotvel.Get()*180.0/3.14159265358979;
00231
00232 stringstream sstr;
00233 sstr << "SetVelocityGetPosition "<< v << " " << w << " \n" ;
00234 if (logging.value()) {
00235 Logger::log() << "(BaseVelocityController) Sending "<< sstr.str() <<endlog();
00236 }
00237 cl->sendCommand(sstr.str());
00238 string str = cl->receiveData();
00239 if (logging.value()) {
00240 Logger::log() << "(BaseVelocityController) Receiving "<< str <<endlog();
00241 }
00242 stringstream sstr2(str);
00243 double x,y,theta;
00244 sstr2 >> x;
00245 sstr2 >> y;
00246 sstr2 >> theta;
00247
00248
00249 x_world_pf.Set(x/100.0);
00250 y_world_pf.Set(y/100.0);
00251 theta_world_pf.Set(theta/180.0*3.14159265358979);
00252 }
00253
00257 void BaseVelocityController::shutdown() {
00258 if (connected) {
00259 if (cl) {
00260 delete cl;
00261 cl=0;
00262 }
00263 Logger::log() << "Disconnected."<<endlog();
00264 connected=false;
00265 }
00266 }
00267
00268