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 // DepthInstrument.cpp: implementation of the CDepthInstrument class. 00032 // 00034 #include <MOOSLIB/MOOSLib.h> 00035 00036 #include "DepthInstrument.h" 00037 #include "MOOSConsiDepthSensor.h" 00038 #include "MOOSParaSciDepthSensor.h" 00039 00040 #include <iostream> 00041 #include <sstream> 00042 #include <math.h> 00043 00044 #include <algorithm> 00045 00046 #define MAX_SANE_DEPTH 4000.0 00047 #define MAX_SANE_DELTA_DEPTH 2.0 00048 #define DEPTH_HISTORY_LENGTH 10 00049 using namespace std; 00050 00051 00053 // Construction/Destruction 00055 00056 CDepthInstrument::CDepthInstrument() 00057 { 00058 //some sensible defaults (missionfile can overwrite this) 00059 SetAppFreq(2); 00060 SetCommsFreq(8); 00061 m_nHistoryLength = DEPTH_HISTORY_LENGTH; 00062 m_bFilter = true; 00063 } 00064 00065 CDepthInstrument::~CDepthInstrument() 00066 { 00067 00068 } 00069 00070 bool CDepthInstrument::OnNewMail(MOOSMSG_LIST &NewMail) 00071 { 00072 00073 if(IsSimulateMode()) 00074 { 00075 return UpdateMOOSVariables(NewMail); 00076 } 00077 else 00078 { 00079 CMOOSMsg Msg; 00080 if(m_Comms.PeekMail(NewMail,"ZERO_DEPTH",Msg)) 00081 { 00082 m_pDepthSensor->Zero(); 00083 } 00084 } 00085 00086 00087 return true; 00088 } 00089 00090 00093 bool CDepthInstrument::Iterate() 00094 { 00095 00096 if(GetDepth()) 00097 { 00098 PublishDepth(); 00099 } 00100 return true; 00101 } 00102 00103 00104 bool CDepthInstrument::OnStartUp() 00105 { 00106 CMOOSInstrument::OnStartUp(); 00107 00108 m_MissionReader.GetConfigurationParam("FILTER",m_bFilter); 00109 00110 //what is our MOOS variable? 00111 AddMOOSVariable("Depth", //locally called depth 00112 "SIM_DEPTH",//subscribed to as SIM_DEPTH (iProcesses subscribed via the simulator! 00113 "DEPTH_DEPTH",// 00114 0.5); //can update @10Hz 00115 00116 AddMOOSVariable("DEPTH_RAW","","DEPTH_RAW",0.1); 00117 00118 if(IsSimulateMode()) 00119 { 00120 RegisterMOOSVariables(); 00121 SetAppFreq(5); 00122 SetCommsFreq(10); 00123 } 00124 00125 else 00126 { 00127 //try to open 00128 if(!SetupPort()) 00129 { 00130 return false; 00131 } 00132 00133 string sType; 00134 m_MissionReader.GetConfigurationParam("TYPE",sType); 00135 00136 if(MOOSStrCmp(sType,"CONSI")) 00137 { 00138 m_pDepthSensor = new CMOOSConsiDepthSensor; 00139 } 00140 else if (MOOSStrCmp(sType,"PARASCI")) 00141 { 00142 m_pDepthSensor = new CMOOSParaSciDepthSensor; 00143 } 00144 else 00145 { 00146 MOOSTrace("Depth sensor type must be one of PARASCI or CONSI \n"); 00147 MOOSTrace("Assuming ParaSci...! \n"); 00148 m_pDepthSensor = new CMOOSParaSciDepthSensor; 00149 } 00150 00151 00152 if(dynamic_cast<CMOOSParaSciDepthSensor *> (m_pDepthSensor) ) 00153 { 00154 double dfResolution; 00155 00156 if(m_MissionReader.GetConfigurationParam("RESOLUTION",dfResolution)) 00157 { 00158 00159 ((CMOOSParaSciDepthSensor *)m_pDepthSensor)->SetResolution(dfResolution); 00160 } 00161 } 00162 00163 //tell teh driver what port to use.. 00164 m_pDepthSensor->SetSerialPort(&m_Port); 00165 00166 //try 10 times to initialise sensor 00167 if(!InitialiseSensorN(10,"Depth")) 00168 { 00169 return false; 00170 } 00171 00172 } 00173 00174 return true; 00175 } 00176 00177 00178 00179 00180 00181 bool CDepthInstrument::OnConnectToServer() 00182 { 00183 00184 if(IsSimulateMode()) 00185 { 00186 //we only subscribe to things if we are in simulator mode 00187 RegisterMOOSVariables(); 00188 } 00189 else 00190 { 00191 m_Comms.Register("ZERO_DEPTH",0.5); 00192 } 00193 00194 return true; 00195 } 00196 00197 bool CDepthInstrument::GetDepth() 00198 { 00199 00200 //we only do this if there is no simulator connected and 00201 if(!IsSimulateMode()) 00202 { 00203 if(m_pDepthSensor->GetDepth()) 00204 { 00205 double dfDepth = m_pDepthSensor->GetDepthValue(); 00206 if(fabs(dfDepth)<MAX_SANE_DEPTH) 00207 { 00208 if(Filter(dfDepth)) 00209 { 00210 SetMOOSVar("Depth",dfDepth,MOOSTime()); 00211 } 00212 } 00213 else 00214 { 00215 string sCrazy = MOOSFormat("Crazy Depth: %fm",dfDepth); 00216 MOOSDebugWrite(sCrazy); 00217 } 00218 } 00219 } 00220 else 00221 { 00222 /*testing filtering offline 00223 CMOOSVariable * pVar = GetMOOSVar("Depth"); 00224 if(pVar->IsFresh()) 00225 { 00226 double dfDepth = pVar->GetDoubleVal(); 00227 Filter(dfDepth); 00228 }*/ 00229 } 00230 00231 00232 return true; 00233 } 00234 00236 // tell the world 00237 bool CDepthInstrument::PublishDepth() 00238 { 00239 return PublishFreshMOOSVariables(); 00240 00241 } 00242 00244 // here we initialise the sensor, giving it start up values 00245 bool CDepthInstrument::InitialiseSensor() 00246 { 00247 return m_pDepthSensor->Initialise(); 00248 00249 } 00250 00251 00252 00253 bool CDepthInstrument::Filter(double dfDepth) 00254 { 00255 //no filtering -> accept 00256 if(!m_bFilter) 00257 return true; 00258 00259 00260 DEPTH_HISTORY::iterator p; 00261 00262 m_DepthHistory.push_front(dfDepth); 00263 00264 //not enough history -> reject 00265 if(m_DepthHistory.size()<m_nHistoryLength) 00266 { 00267 return false; 00268 } 00269 00270 //don't grow for too long 00271 while(m_DepthHistory.size()>m_nHistoryLength) 00272 { 00273 m_DepthHistory.pop_back(); 00274 } 00275 00276 //copy list to vector 00277 vector<double> LocalVec; 00278 LocalVec.resize(m_DepthHistory.size()); 00279 00280 00281 00282 copy(m_DepthHistory.begin(),m_DepthHistory.end(),LocalVec.begin()); 00283 00284 sort(LocalVec.begin(),LocalVec.end()); 00285 00286 00287 double dfMedian = LocalVec[m_DepthHistory.size()/2]; 00288 00289 bool bAccept = fabs(dfDepth-dfMedian)<MAX_SANE_DELTA_DEPTH; 00290 00291 if(!bAccept) 00292 { 00293 string sText = MOOSFormat("Depth Filter rejects %f m",dfDepth); 00294 MOOSDebugWrite(sText); 00295 } 00296 return bAccept; 00297 00298 }