MOOS 0.2375
|
00001 00002 // 00003 // MOOS - Mission Oriented Operating Suite 00004 // 00005 // A suit of Applications and Libraries for Mobile Robotics Research 00006 // Copyright (C) 2001-2005 Massachusetts Institute of Technology and 00007 // Oxford University. 00008 // 00009 // This software was written by Paul Newman and others 00010 // at MIT 2001-2002 and Oxford University 2003-2005. 00011 // email: pnewman@robots.ox.ac.uk. 00012 // 00013 // This file is part of a MOOS Instrument. 00014 // 00015 // This program is free software; you can redistribute it and/or 00016 // modify it under the terms of the GNU General Public License as 00017 // published by the Free Software Foundation; either version 2 of the 00018 // License, or (at your option) any later version. 00019 // 00020 // This program is distributed in the hope that it will be useful, 00021 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00023 // General Public License for more details. 00024 // 00025 // You should have received a copy of the GNU General Public License 00026 // along with this program; if not, write to the Free Software 00027 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00028 // 02111-1307, USA. 00029 // 00031 #include <mexVNLHelpers.h> 00032 #include <string> 00033 #include <map> 00034 00035 #include <fstream> 00036 00037 00038 00039 00040 00043 #ifdef _WIN32 00044 CMOOSNTSerialPort gPort; 00045 #else 00046 CMOOSLinuxSerialPort gPort; 00047 #endif 00048 00051 CMOOSCommClient gComms; 00052 00053 // a parameter holding class 00054 class Param 00055 { 00056 00057 public: 00058 enum Type 00059 { 00060 STR, 00061 DBL, 00062 VEC, 00063 MAT, 00064 UNK, 00065 }; 00066 double dfVal; 00067 std::string sVal; 00068 Matrix M; 00069 Vector V; 00070 Type m_eType; 00071 Param() 00072 { 00073 m_eType=UNK; 00074 dfVal = -1; 00075 } 00076 std::string Str() 00077 { 00078 switch(m_eType) 00079 { 00080 case STR: 00081 return "Str: "+sVal; 00082 case DBL: 00083 return MOOSFormat("Dbl: %.3f",dfVal); 00084 case VEC: 00085 return MOOSFormat("Vec: %d",V.rows()); 00086 case MAT: 00087 return MOOSFormat("Mat: %dx%d",M.rows(),M.cols()); 00088 case UNK: 00089 return MOOSFormat("NOT SET! "); 00090 } 00091 } 00092 00093 }; 00094 typedef std::map<std::string,Param> ARGMAP; 00095 00096 ARGMAP gArgMap; 00097 00098 00099 00100 bool Initialise(const mxArray *prhs[], int nrhs) 00101 { 00102 mexPrintf("*********** iMatlab Initialising ***********\n"); 00103 mexPrintf("* A box of MOOS accesories *\n"); 00104 mexPrintf("* P. Newman *\n"); 00105 mexPrintf("* Oxford 2005 *\n"); 00106 mexPrintf("********************************************\n"); 00107 00108 00109 00110 if(prhs!=NULL) 00111 { 00112 for(int i = 1;i<nrhs;i+=2) 00113 { 00114 if(i<nrhs-1) 00115 { 00116 std::string sParam; 00117 double dfVal; 00118 if(!Matlab2String(sParam,prhs[i])) 00119 { 00120 mexErrMsgTxt("Incorrect param value pair (not a string)"); 00121 } 00122 00123 const mxArray *p = prhs[i+1]; 00124 Matrix M; 00125 Param NewParam; 00126 NewParam.Type = Param::UNK; 00127 switch(mxGetClassID(p)) 00128 { 00129 case mxCHAR_CLASS: 00130 { 00131 if(Matlab2String(sParam,p)) 00132 { 00133 NewParam.Type=Param::STR; 00134 } 00135 } 00136 break 00137 case mxDOUBLE_CLASS: 00138 if(MatrixMatlab2MatrixVNL(p,NewParam.M)) 00139 { 00140 if(M.rows()==1 && M.cols()==1) 00141 { 00142 NewParam.Type = Param::DBL; 00143 NewParam.dfVal = M[0][0]; 00144 //a scalar 00145 } 00146 else if (M.rows()==1 || M.cols()==1) 00147 { 00148 NewParam.Type = Param::VEC; 00149 //a vector 00150 if(M.rows()==1) 00151 { 00152 NewParam.V = M.transpose().column(0).as_vector(); 00153 } 00154 if(M.cols()==1) 00155 { 00156 NewParam.V = M.column(0).as_vector(); 00157 } 00158 } 00159 else 00160 { 00161 NewParam.Type = Param::MAT; 00162 NewParam.M = M; 00163 } 00164 } 00165 00166 break; 00167 default: 00168 break; 00169 } 00170 00171 //did we parse it OK? 00172 if(NewParam.Type==Param::UNK) 00173 { 00174 mexPrintf("PROBLEM : can't parse parameter value %s\n",sParam.c_str()); 00175 } 00176 else 00177 { 00178 ARGMAP::iterator p = ArgMap.find(sParam); 00179 00180 if(p==ArgMap.end()) 00181 { 00182 mexPrintf("warning no such parameter exists: %s\n",sParam.c_str()); 00183 } 00184 else 00185 { 00186 *(p->second) = NewParam; 00187 } 00188 } 00189 } 00190 } 00191 } 00192 00193 00194 ARGMAP::iterator p; 00195 00196 for(p = ArgMap.begin();p!=ArgMap.end();p++) 00197 { 00198 mexPrintf("Property %-25s %s\n",p->first.c_str(),(p->second).Str().c_str()); 00199 } 00200 00201 00202 bInitialised = true; 00203 00204 return bInitialised; 00205 } 00206 00207 00208 void PrintHelp() 00209 { 00210 00211 std::ifstream HelpFile("OX2DLS.help"); 00212 00213 if(HelpFile.is_open()) 00214 { 00215 while(!HelpFile.eof()) 00216 { 00217 char Line[1024]; 00218 HelpFile.getline(Line,sizeof(Line)); 00219 mexPrintf("%s\n",Line); 00220 } 00221 } 00222 else 00223 { 00224 mexPrintf("help file \"OX2DLS.help\" not found\n"); 00225 } 00226 00227 } 00228 00231 void mexFunction(int nlhs, 00232 mxArray *plhs[], 00233 int nrhs, 00234 const mxArray *prhs[]) 00235 { 00236 00237 //local variables 00238 vnl_matrix<double> Sxy; 00239 std::string sCmd; 00240 00241 //lets begin by parsing the input (RHS) 00242 if(nrhs<1) 00243 { 00244 PrintHelp(); 00245 return; 00246 } 00247 00248 //first parameter is always a string command 00249 if(!Matlab2String(sCmd,prhs[0])) 00250 { 00251 mexErrMsgTxt("Param 1 (cmd) must be a string "); 00252 } 00253 00254 00255 // MOOS_MAIL_FETCH returns String 00256 // MOOS_MAIL_SEND 00257 // SERIAL_READ_ASCII 00258 00259 00260 if(sCmd=="INITIALISE") 00261 { 00262 Initialise(prhs,nrhs); 00263 } 00264 else if(sCmd=="RUN") 00265 { 00266 00267 switch(nrhs) 00268 { 00269 case 2: 00270 { 00271 if( !mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) ) 00272 { 00273 mexErrMsgTxt("Param 2 vehicle must be a 2 by N scan in sensor centric x-y space"); 00274 } 00275 00276 MatrixMatlab2MatrixVNL(prhs[1],Sxy); 00277 00278 int nPoints = Sxy.columns(); 00279 if(nPoints<2) 00280 { 00281 mexErrMsgTxt("Need at least two points to extract a segment!\n"); 00282 } 00283 00284 double * pX = new double[nPoints]; 00285 double * pY = new double[nPoints]; 00286 00287 for(int i = 0;i<nPoints;i++) 00288 { 00289 pX[i] = Sxy(0,i); 00290 pY[i] = Sxy(1,i); 00291 } 00292 00293 00294 00295 if(!bInitialised) 00296 { 00297 mexPrintf("Initialising with default settings...\n"); 00298 Initialise(NULL,0); 00299 } 00300 00301 std::vector<CSegment2D> ResultSegments; 00302 if(TheRansacGadget.Run(pX,pY,nPoints,ResultSegments)) 00303 { 00304 vnl_matrix<double> Results(ResultSegments.size(),4); 00305 00306 mexPrintf("Found %d segments\n",ResultSegments.size()); 00307 for(int i = 0;i<ResultSegments.size();i++) 00308 { 00309 mexPrintf("S[%d] -> %s",i,ResultSegments[i].Describe().c_str()); 00310 Results(i,0) = ResultSegments[i].x1; 00311 Results(i,1) = ResultSegments[i].y1; 00312 Results(i,2) = ResultSegments[i].x2; 00313 Results(i,3) = ResultSegments[i].y2; 00314 } 00315 00316 00317 //the return result is a matrix of x1 y1 x2 y2 line segments 00318 MatrixVNL2MatrixMatlab(plhs[0],Results); 00319 00320 } 00321 00322 delete pX; 00323 delete pY; 00324 00325 } 00326 break; 00327 00328 00329 00330 default: 00331 mexErrMsgTxt(" \"RUN\" needs a sacn as a second parameter\n"); 00332 } 00333 } 00334 else 00335 { 00336 mexErrMsgTxt("Unknown command string\n"); 00337 } 00338 00339 00340 }