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 // AVTRAKDriver.cpp: implementation of the CAVTRAKDriver class. 00032 // 00034 00035 #include "AVTRAKDriver.h" 00036 #include <MOOSLIB/MOOSLib.h> 00037 #include <MOOSGenLib/MOOSGenLib.h> 00038 #include <sstream> 00039 #include <iomanip> 00040 #include <algorithm> 00042 // Construction/Destruction 00044 00045 00046 00047 CAVTRAKDriver::CAVTRAKDriver() 00048 { 00049 00050 00051 m_dfAcousticTimeOut = 5.0; 00052 00053 00054 } 00055 00056 CAVTRAKDriver::~CAVTRAKDriver() 00057 { 00058 00059 } 00060 00061 bool CAVTRAKDriver::SetRxChannel(INT_VECTOR Channels) 00062 { 00063 m_RxOrder = Channels; 00064 sort(m_RxOrder.begin(),m_RxOrder.end()); 00065 00066 return SetRangingParams(); 00067 00068 } 00069 00070 bool CAVTRAKDriver::SetRangingParams() 00071 { 00072 INT_VECTOR::iterator p; 00073 00074 stringstream os; 00075 00076 os.setf(ios::fixed); 00077 00078 os<<">SR"<<setprecision(1)<<m_dfAcousticTimeOut; 00079 00080 for(p = m_RxOrder.begin();p!=m_RxOrder.end();p++) 00081 { 00082 os<<","<<*p; 00083 } 00084 os<<"\r\n"<<ends; 00085 00086 00087 string sCmd = os.str(); 00088 00089 00090 m_pPort->Write((char *)sCmd.c_str(),sCmd.length()); 00091 00092 string sReply; 00093 if(!m_pPort->GetTelegram(sReply,2.0)) 00094 { 00095 MOOSTrace("Failed AVTRAK Ranging Configuration, no reply\n"); 00096 return false; 00097 } 00098 else 00099 { 00100 00101 if(sReply.find("OK")!=string::npos) 00102 { 00103 MOOSChomp(sReply,"<SR"); 00104 00105 m_dfAcousticTimeOut = atof(MOOSChomp(sReply,",").c_str()); 00106 00107 m_RxOrder.clear(); 00108 while(!sReply.empty()) 00109 { 00110 int nChan = atoi(MOOSChomp(sReply,",").c_str()); 00111 m_RxOrder.push_back(nChan); 00112 } 00113 } 00114 else 00115 { 00116 MOOSTrace("Failed AVTRAK Ranging Configuration : %s \n",sReply.c_str()); 00117 return false; 00118 } 00119 00120 } 00121 00122 00123 return true; 00124 } 00125 00126 bool CAVTRAKDriver::GetRanges() 00127 { 00128 00129 m_TOFs.clear(); 00130 00131 stringstream os; 00132 00133 os<<">MR\r\n"<<ends; 00134 00135 string sCmd = os.str(); 00136 00137 00138 m_pPort->Write((char *)sCmd.c_str(),sCmd.length()); 00139 00140 00141 double dfLastTxTime = MOOSTime()+AVTRAK_RANGING_TX_DELAY; 00142 00143 string sReply; 00144 if(!m_pPort->GetTelegram(sReply,m_dfAcousticTimeOut+1.0)) 00145 { 00146 MOOSTrace("Failed AVTRAK Ranging Command, no reply\n"); 00147 return false; 00148 } 00149 else 00150 { 00151 if(m_pPort->IsVerbose()) 00152 { 00153 00154 } 00155 MOOSChomp(sReply,"<MR"); 00156 00157 INT_VECTOR::iterator p = m_RxOrder.begin(); 00158 00159 while(!sReply.empty() && p!=m_RxOrder.end()) 00160 { 00161 int nChan = *p++; 00162 double dfTOF = atof(MOOSChomp(sReply,",").c_str()); 00163 00164 CTwoWayTOF TOF; 00165 00166 TOF.m_nChannel = nChan; 00167 TOF.m_dfTOF = dfTOF/1000000; 00168 00169 //here we apply the sonardyne correction!! 00170 //specila lover insider information!! 00171 TOF.m_dfTOF-=0.8e-3; 00172 00173 if(TOF.m_dfTOF>0) 00174 { 00175 00176 TOF.m_dfTxTime = dfLastTxTime; 00177 00178 m_TOFs.push_back(TOF); 00179 00180 MOOSTrace("Chan[%d] = %f s %f m\n", 00181 TOF.m_nChannel, 00182 TOF.m_dfTOF, 00183 (TOF.m_dfTOF-GetTAT(TOF.m_nChannel))/2*1500); 00184 } 00185 } 00186 } 00187 00188 return true; 00189 00190 } 00191 00192 bool CAVTRAKDriver::GetTOFString(string & sResult) 00193 { 00194 00195 //format is Tx=tt.tt,Ch[i]=tt.tt,Ch[j]=tt.tt.... 00196 if(m_TOFs.empty()) 00197 return false; 00198 00199 stringstream os; 00200 00201 os.setf(ios::fixed); 00202 os.precision(3); 00203 00204 os<<"Tx="<<m_TOFs.front().m_dfTxTime; 00205 00206 os.precision(6); 00207 00208 TOF_VECTOR::iterator p; 00209 00210 for(p = m_TOFs.begin();p!=m_TOFs.end();p++) 00211 { 00212 CTwoWayTOF TOF = *p; 00213 if(TOF.m_dfTOF!=0) 00214 { 00215 os<<",Ch["<<TOF.m_nChannel<<"]="<<TOF.m_dfTOF; 00216 } 00217 } 00218 os<<ends; 00219 00220 sResult = os.str(); 00221 00222 return true; 00223 } 00224 00225 00226 00227 bool CAVTRAKDriver::SetTransceiverMode() 00228 { 00229 string sReply,sCmd; 00230 00231 sCmd = ">TC\r\n", 00232 m_pPort->Write((char*)sCmd.c_str(),sCmd.size()); 00233 00234 00235 if(m_pPort->GetTelegram(sReply,2.0)) 00236 { 00237 if(sReply.find("OK")!=string::npos) 00238 { 00239 return true; 00240 } 00241 } 00242 00243 return false; 00244 } 00245 00246 bool CAVTRAKDriver::Reset() 00247 { 00248 //locals 00249 string sReply,sCmd; 00250 00251 //break comms lines to reset via hardware 00252 m_pPort->Break(); 00253 00254 if(m_pPort->GetTelegram(sReply,4.0)) 00255 { 00256 if(sReply.find(" TYPE 7995 AVTRAK OK")==string::npos) 00257 { 00258 return false; 00259 } 00260 } 00261 else 00262 { 00263 MOOSTrace("AVTRAK Hardware Reset Failed\n"); 00264 return false; 00265 } 00266 00267 MOOSTrace("Waiting..."); 00268 MOOSPause(1000); 00269 MOOSTrace("OK\n"); 00270 00271 00272 //sofware reset... 00273 00274 /* 00275 sCmd = ">RS\r\n", 00276 m_pPort->Write((char*)sCmd.c_str(),sCmd.size()); 00277 00278 if(m_pPort->GetTelegram(sReply,2.0)) 00279 { 00280 if(sReply.find("OK")!=string::npos) 00281 { 00282 return true; 00283 } 00284 } 00285 */ 00286 return true; 00287 } 00288 00289 bool CAVTRAKDriver::SetSerialPort(CMOOSSerialPort * pPort) 00290 { 00291 #ifndef _WIN32 00292 m_pPort = (CMOOSLinuxSerialPort*) pPort; 00293 #else 00294 m_pPort = (CMOOSNTSerialPort*) pPort; 00295 #endif 00296 return true; 00297 } 00298 00299 00300 bool CAVTRAKDriver::SetAcousticTimeOut(double dfTimeOut) 00301 { 00302 if(dfTimeOut>0) 00303 { 00304 m_dfAcousticTimeOut = dfTimeOut; 00305 00306 return SetRangingParams(); 00307 00308 } 00309 return false; 00310 } 00311 00312 double CAVTRAKDriver::GetTAT(int nChan) 00313 { 00314 switch(nChan) 00315 { 00316 case 4: return 0.625; break; 00317 case 5: return 0.5625; break; 00318 case 6: return 0.5; break; 00319 case 7: return 0.4375; break; 00320 case 8: return 0.375; break; 00321 case 9: return 0.3125; break; 00322 case 10: return 0.25; break; 00323 case 11: return 0.1875; break; 00324 case 12: return 0.1250; break; 00325 case 13: return 0.0625; break; 00326 default: 00327 return 0; 00328 } 00329 } 00330 00331 bool CAVTRAKDriver::GetTOFByChannel(int nChan, string &sName, double &dfTime, double &dfTOF) 00332 { 00333 TOF_VECTOR::iterator p; 00334 00335 for(p = m_TOFs.begin();p!=m_TOFs.end();p++) 00336 { 00337 CTwoWayTOF TOF = *p; 00338 if(TOF.m_dfTOF>0) 00339 { 00340 if(TOF.m_nChannel == nChan) 00341 { 00342 sName = MOOSFormat("LBL_CH_%d",nChan); 00343 dfTime = TOF.m_dfTxTime+TOF.m_dfTOF; 00344 dfTOF = TOF.m_dfTOF; 00345 return true; 00346 } 00347 } 00348 } 00349 00350 return false; 00351 }