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 Basic (Common) Application. 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 // ZPatternTask.cpp: implementation of the CZPatternTask class. 00032 // 00034 #ifdef _WIN32 00035 #pragma warning(disable : 4786) 00036 #endif 00037 00038 00039 #include "MOOSTaskDefaults.h" 00040 #include "math.h" 00041 #include "ZPatternTask.h" 00042 00043 00044 00046 // Construction/Destruction 00048 00049 CZPatternTask::CZPatternTask() 00050 { 00051 m_ePatternType = MOOS_Z_PATTERN_ERROR; 00052 m_eGoal = MOOS_Z_PATTERN_GOAL_MIN_DEPTH; 00053 m_dfTolerance = 0.3; 00054 m_dfMaxDepth = 1.0; 00055 m_dfMinDepth = 1.0; 00056 00057 m_dfLevelStartTime = -1; 00058 m_dfLevelDuration = 20.0; 00059 00060 00061 m_bInitialised = false; 00062 m_DepthDOF.SetDesired(0.0); 00063 00064 00065 } 00066 00067 CZPatternTask::~CZPatternTask() 00068 { 00069 00070 } 00071 00072 00073 00074 00075 bool CZPatternTask::SetParam(string sParam, string sVal) 00076 { 00077 00078 MOOSToUpper(sParam); 00079 MOOSToUpper(sVal); 00080 00081 00082 if(!CMOOSBehaviour::SetParam(sParam,sVal)) 00083 { 00084 if(MOOSStrCmp(sParam,"THRUST")) 00085 { 00086 m_Thrust.SetCurrent(atof(sVal.c_str()),MOOSTime()); 00087 } 00088 else if(MOOSStrCmp(sParam,"TOLERANCE")) 00089 { 00090 m_dfTolerance = atof(sVal.c_str()); 00091 } 00092 else if(MOOSStrCmp(sParam,"PATTERN")) 00093 { 00094 if(MOOSStrCmp(sVal,"YOYO")) 00095 { 00096 m_ePatternType = MOOS_Z_PATTERN_YOYO; 00097 } 00098 else if(MOOSStrCmp(sVal,"SQUARE")) 00099 { 00100 m_ePatternType = MOOS_Z_PATTERN_SQUARE; 00101 } 00102 } 00103 else if(MOOSStrCmp(sParam,"LEVELTIME")) 00104 { 00105 m_dfLevelDuration = atof(sVal.c_str()); 00106 } 00107 00108 else if(MOOSStrCmp(sParam,"MAXDEPTH")) 00109 { 00110 m_dfMaxDepth = atof(sVal.c_str()); 00111 } 00112 else if(MOOSStrCmp(sParam,"MINDEPTH")) 00113 { 00114 m_dfMinDepth = atof(sVal.c_str()); 00115 } 00116 00117 else 00118 { 00119 //hmmm - it wasn't for us at all: base class didn't understand either 00120 MOOSTrace("Param \"%s\" not understood!\n",sParam.c_str()); 00121 return false; 00122 } 00123 } 00124 00125 return true; 00126 00127 } 00128 00129 00130 //returns false if we haven't received data in a while..bad news! 00131 bool CZPatternTask::RegularMailDelivery(double dfTimeNow) 00132 { 00133 return !( 00134 m_DepthDOF.IsStale(dfTimeNow,GetStartTime()) || 00135 m_PitchDOF.IsStale(dfTimeNow,GetStartTime()) 00136 ); 00137 } 00138 00139 00140 00141 bool CZPatternTask::Initialise() 00142 { 00143 //set a pitch driven depth controller 00144 m_ZPID.SetAsDepthController(true); 00145 00146 m_DepthDOF.SetDesired(m_dfMinDepth); 00147 00148 m_bInitialised = true; 00149 return true; 00150 } 00151 00152 bool CZPatternTask::Run(CPathAction &DesiredAction) 00153 { 00154 00155 if(!m_bInitialised) 00156 { 00157 Initialise(); 00158 } 00159 00160 if(m_DepthDOF.IsValid() && m_PitchDOF.IsValid()) 00161 { 00162 00163 00164 00165 double dfError = m_DepthDOF.GetError(); 00166 00167 //modfied by David Battle Dec 05 00168 if (fabs(dfError)<=m_dfTolerance) 00169 { 00170 SetNextSetPoint(); 00171 } 00172 00173 00174 00175 double dfCmd= 0; 00176 00177 //this is for loggin purposes only 00178 m_ZPID.SetGoal(m_DepthDOF.GetDesired()); 00179 00180 00181 if(m_ZPID.Run( dfError, 00182 m_DepthDOF.GetErrorTime(), 00183 dfCmd, 00184 m_PitchDOF.GetCurrent(), 00185 m_PitchDOF.GetErrorTime())) 00186 { 00187 00188 00189 00190 //OK we need to change something 00191 /* DesiredAction.Set( ACTUATOR_THRUST, 00192 m_Thrust.GetCurrent(), 00193 m_nPriority, 00194 "Z Pattern"); 00195 */ 00196 00197 DesiredAction.Set( ACTUATOR_ELEVATOR, 00198 dfCmd, 00199 m_nPriority, 00200 "Z Pattern"); 00201 00202 00203 } 00204 } 00205 return true; 00206 } 00207 00208 bool CZPatternTask::OnNewMail(MOOSMSG_LIST &NewMail) 00209 { 00210 CMOOSMsg Msg; 00211 00212 if(PeekMail(NewMail,"NAV_DEPTH",Msg)) 00213 { 00214 if(!Msg.IsSkewed(GetTimeNow())) 00215 { 00216 m_DepthDOF.SetCurrent(Msg.m_dfVal,Msg.m_dfTime); 00217 } 00218 } 00219 00220 if(PeekMail(NewMail,"NAV_PITCH",Msg)) 00221 { 00222 if(!Msg.IsSkewed(GetTimeNow())) 00223 { 00224 m_PitchDOF.SetCurrent(Msg.m_dfVal,Msg.m_dfTime); 00225 } 00226 } 00227 00228 00229 //always call base class version 00230 CMOOSBehaviour::OnNewMail(NewMail); 00231 00232 return true; 00233 } 00234 00235 bool CZPatternTask::GetRegistrations(STRING_LIST &List) 00236 { 00237 00238 List.push_front("NAV_DEPTH"); 00239 List.push_front("NAV_PITCH"); 00240 00241 //always call base class version 00242 CMOOSBehaviour::GetRegistrations(List); 00243 00244 return true; 00245 } 00246 00247 00248 00249 bool CZPatternTask::SetNextSetPoint() 00250 { 00251 00252 00253 if(m_DepthDOF.GetError()>m_dfTolerance) 00254 return true; 00255 00256 00257 00258 switch(m_ePatternType) 00259 { 00260 00261 case MOOS_Z_PATTERN_SQUARE: 00262 00263 switch(m_eGoal) 00264 { 00265 00266 case MOOS_Z_PATTERN_GOAL_WAIT_MIN: 00267 case MOOS_Z_PATTERN_GOAL_WAIT_MAX: 00268 if(MOOSTime()-m_dfLevelStartTime>=m_dfLevelDuration) 00269 { 00270 if(m_eGoal==MOOS_Z_PATTERN_GOAL_WAIT_MAX) 00271 { 00272 m_eGoal = MOOS_Z_PATTERN_GOAL_MIN_DEPTH; 00273 m_DepthDOF.SetDesired(m_dfMinDepth); 00274 } 00275 else if(m_eGoal==MOOS_Z_PATTERN_GOAL_WAIT_MIN) 00276 { 00277 m_eGoal = MOOS_Z_PATTERN_GOAL_MAX_DEPTH; 00278 m_DepthDOF.SetDesired(m_dfMaxDepth); 00279 } 00280 } 00281 break; 00282 00283 case MOOS_Z_PATTERN_GOAL_MIN_DEPTH: 00284 m_dfLevelStartTime = MOOSTime(); 00285 m_eGoal = MOOS_Z_PATTERN_GOAL_WAIT_MIN; 00286 break; 00287 00288 case MOOS_Z_PATTERN_GOAL_MAX_DEPTH: 00289 m_dfLevelStartTime = MOOSTime(); 00290 m_eGoal = MOOS_Z_PATTERN_GOAL_WAIT_MAX; 00291 break; 00292 } 00293 00294 break; 00295 00296 case MOOS_Z_PATTERN_YOYO: 00297 switch(m_eGoal) 00298 { 00299 case MOOS_Z_PATTERN_GOAL_MIN_DEPTH: 00300 m_DepthDOF.SetDesired(m_dfMaxDepth); 00301 m_eGoal = MOOS_Z_PATTERN_GOAL_MAX_DEPTH; 00302 break; 00303 case MOOS_Z_PATTERN_GOAL_MAX_DEPTH: 00304 m_DepthDOF.SetDesired(m_dfMinDepth); 00305 m_eGoal = MOOS_Z_PATTERN_GOAL_MIN_DEPTH; 00306 break; 00307 } 00308 break; 00309 00310 default: 00311 return false; 00312 break; 00313 } 00314 00315 return true; 00316 }