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 // ConstantHeadingTask.cpp: implementation of the CConstantHeadingTask class. 00032 // 00034 #ifdef _WIN32 00035 #pragma warning(disable : 4786) 00036 #endif 00037 00038 #include <math.h> 00039 #include <iostream> 00040 using namespace std; 00041 00042 #include "MOOSTaskDefaults.h" 00043 00044 #include "ConstantHeadingTask.h" 00045 00047 // Construction/Destruction 00049 00050 00051 CConstantHeadingTask::CConstantHeadingTask() 00052 { 00053 00054 m_bInitialised = false; 00055 m_YawDOF.SetDesired(0); 00056 m_YawDOF.SetTolerance(0.00); 00057 m_bThrustSet = false; 00058 m_dfThrust = 0; 00059 00060 } 00061 00062 CConstantHeadingTask::~CConstantHeadingTask() 00063 { 00064 00065 } 00066 //returns false if we haven't received data in a while..bad news! 00067 bool CConstantHeadingTask::RegularMailDelivery(double dfTimeNow) 00068 { 00069 // return true; 00070 return !m_YawDOF.IsStale(dfTimeNow,GetStartTime()); 00071 } 00072 00073 00074 bool CConstantHeadingTask::Run(CPathAction &DesiredAction) 00075 { 00076 00077 if(!m_bInitialised) 00078 { 00079 Initialise(); 00080 } 00081 00082 if(m_YawDOF.IsValid()) 00083 { 00084 00085 double dfError = m_YawDOF.GetError(); 00086 00087 if(dfError<-PI) 00088 { 00089 dfError+=2*PI; 00090 } 00091 else if(dfError>PI) 00092 { 00093 dfError-=2*PI; 00094 } 00095 00096 00097 double dfCmd= 0; 00098 00099 //this is for loggin purposes only 00100 m_YawPID.SetGoal(m_YawDOF.GetDesired()); 00101 00102 if(m_YawPID.Run(dfError,m_YawDOF.GetErrorTime(),dfCmd)) 00103 { 00104 //OK we need to change something 00105 DesiredAction.Set( ACTUATOR_RUDDER, 00106 -(dfCmd), 00107 m_nPriority, 00108 "Constant Heading"); 00109 00110 } 00111 00112 if(m_bThrustSet) 00113 { 00114 //OK we need to change something 00115 DesiredAction.Set( ACTUATOR_THRUST, 00116 m_dfThrust, 00117 m_nPriority, 00118 "Constant Heading"); 00119 00120 } 00121 } 00122 return true; 00123 } 00124 00125 bool CConstantHeadingTask::OnNewMail(MOOSMSG_LIST &NewMail) 00126 { 00127 00128 CMOOSMsg Msg; 00129 00130 if(PeekMail(NewMail,"NAV_YAW",Msg)) 00131 { 00132 if(!Msg.IsSkewed(GetTimeNow())) 00133 { 00134 //yaw is in radians 00135 double dfYaw = Msg.m_dfVal; 00136 00137 m_YawDOF.SetCurrent(dfYaw,Msg.m_dfTime); 00138 } 00139 } 00140 00141 //always call base class version 00142 CMOOSBehaviour::OnNewMail(NewMail); 00143 00144 return true; 00145 } 00146 00147 bool CConstantHeadingTask::GetRegistrations(STRING_LIST &List) 00148 { 00149 00150 List.push_front("NAV_YAW"); 00151 00152 //always call base class version 00153 CMOOSBehaviour::GetRegistrations(List); 00154 00155 return true; 00156 } 00157 00158 00159 bool CConstantHeadingTask::SetParam(string sParam, string sVal) 00160 { 00161 MOOSToUpper(sParam); 00162 MOOSToUpper(sVal); 00163 00164 00165 if(!CMOOSBehaviour::SetParam(sParam,sVal)) 00166 { 00167 //this is for us... 00168 if(MOOSStrCmp(sParam,"THRUST")) 00169 { 00170 m_dfThrust = atof(sVal.c_str()); 00171 m_bThrustSet = true; 00172 } 00173 //this is for us... 00174 else if(MOOSStrCmp(sParam,"HEADING")) 00175 { 00176 00177 //now user specifies heading w.r.t to true north indegrees , the compassm or otehr magnetic device 00178 //has already made the corrections... 00179 double dfDesired = atof(sVal.c_str()); 00180 00181 double dfMagneticOffset = 0.0; 00182 00183 if(m_pMissionFileReader) 00184 { 00185 if(!m_pMissionFileReader->GetValue("MAGNETICOFFSET",dfMagneticOffset)) 00186 { 00187 MOOSTrace("WARNING: No magnetic offset specified in Mission file (Field name = \"MagneticOffset\")\n"); 00188 } 00189 } 00190 00191 //we control on *_YAW variables.... 00192 dfDesired*=PI/180.0; 00193 00194 //yaw is in opposite direction to heading... 00195 dfDesired = MOOS_ANGLE_WRAP(-dfDesired); 00196 00197 m_YawDOF.SetDesired(dfDesired); 00198 } 00199 else 00200 { 00201 //hmmm - it wasn't for us at all: base class didn't understand either 00202 MOOSTrace("Param \"%s\" not understood!\n",sParam.c_str()); 00203 return false; 00204 } 00205 } 00206 00207 return true; 00208 00209 } 00210 00211 bool CConstantHeadingTask::Initialise() 00212 { 00213 00214 00215 00216 m_bInitialised = true; 00217 00218 return true; 00219 } 00220 00221