MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/NavigationAndControl/MOOSTaskLib/LimitBox.cpp
Go to the documentation of this file.
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 // LimitBox.cpp: implementation of the CLimitBox class.
00032 //
00034 
00035 #include "LimitBox.h"
00036 
00038 // Construction/Destruction
00040 
00041 #define DEFAULT_BOX_X  500
00042 #define DEFAULT_BOX_Y  500
00043 #define DEFAULT_BOX_DEPTH  20
00044 #define DEFAULT_IGNORE_TIME 10.0
00045 #define ON_BOX_EXCEEDED_ELEVATOR MOOSDeg2Rad(-30.00)
00046 
00047 CLimitBox::CLimitBox()
00048 {
00049     m_dfXMax = DEFAULT_BOX_X;
00050     m_dfXMin = -DEFAULT_BOX_X;
00051     m_dfYMax = DEFAULT_BOX_Y;
00052     m_dfYMin = -DEFAULT_BOX_Y;
00053     m_dfDepthMax = DEFAULT_BOX_DEPTH;
00054     m_dfIgnoreTime = DEFAULT_IGNORE_TIME;
00055     m_bWatching = false;
00056     m_dfWatchStart = 0;
00057 
00058     m_nPriority = 0;
00059     m_bDoneVerbalNotify = false;
00060 
00061 }
00062 
00063 CLimitBox::~CLimitBox()
00064 {
00065 
00066 }
00067 
00068 bool CLimitBox::SetParam(string sParam, string sVal)
00069 {
00070     MOOSToUpper(sParam);
00071     MOOSToUpper(sVal);
00072 
00073     if(!CMOOSBehaviour::SetParam(sParam,sVal))
00074     {
00075         //alwasy make it very important
00076         m_nPriority = 0;
00077 
00078 
00079         //this is for us...
00080         if(MOOSStrCmp(sParam,"XMIN"))
00081         {
00082             m_dfXMin=atof(sVal.c_str());
00083         }
00084         else if(MOOSStrCmp(sParam,"XMAX"))
00085         {
00086             m_dfXMax=atof(sVal.c_str());
00087         }
00088         else if(MOOSStrCmp(sParam,"YMIN"))
00089         {
00090             m_dfYMin=atof(sVal.c_str());
00091         }
00092         else if(MOOSStrCmp(sParam,"YMAX"))
00093         {
00094             m_dfYMax=atof(sVal.c_str());
00095         }
00096         else if(MOOSStrCmp(sParam,"DEPTHMAX"))
00097         {
00098             m_dfDepthMax=atof(sVal.c_str());
00099         }
00100         else if(MOOSStrCmp(sParam,"IGNORETIME"))
00101         {
00102             m_dfIgnoreTime=atof(sVal.c_str());
00103         }
00104         else
00105         {
00106             MOOSTrace("CLimitBox::unknown param %s",sParam.c_str());
00107             return false;
00108         }
00109     }    
00110     return true;
00111 }
00112 
00113 //returns false if we haven't received data in a while..bad news!
00114 bool CLimitBox::RegularMailDelivery(double dfTimeNow)
00115 {
00116  
00117     return (!m_DepthDOF.IsStale(dfTimeNow,GetStartTime())&&
00118             !m_XDOF.IsStale(dfTimeNow,GetStartTime()) &&
00119             !m_YDOF.IsStale(dfTimeNow,GetStartTime()));
00120     
00121 }
00122 
00123 
00124 bool CLimitBox::Run(CPathAction &DesiredAction)
00125 {
00126     if(m_DepthDOF.IsValid())
00127     {
00128         if(m_DepthDOF.IsValid() && m_DepthDOF.GetCurrent()>m_dfDepthMax  ||
00129            m_XDOF.IsValid()     && m_XDOF.GetCurrent()>m_dfXMax  ||
00130            m_XDOF.IsValid()     && m_XDOF.GetCurrent()<m_dfXMin  ||
00131            m_YDOF.IsValid()     && m_YDOF.GetCurrent()>m_dfYMax  ||
00132            m_YDOF.IsValid()     && m_YDOF.GetCurrent()<m_dfYMin)
00133         {
00134 
00135             if(m_bWatching == false)
00136             {
00137                 m_bWatching = true;
00138                 m_dfWatchStart = MOOSTime();
00139             }
00140             else
00141             {
00142                 if(MOOSTime()-m_dfWatchStart>m_dfIgnoreTime)
00143                 {
00144                     DesiredAction.Set(  ACTUATOR_ELEVATOR,
00145                                         ON_BOX_EXCEEDED_ELEVATOR,
00146                                         m_nPriority,
00147                                         "Bounding Box");
00148 
00149                     DesiredAction.Set(  ACTUATOR_THRUST,
00150                                         0,
00151                                         m_nPriority,
00152                                         "Bounding Box");
00153 
00154                     DesiredAction.Set(  ACTUATOR_RUDDER,
00155                                         0,
00156                                         m_nPriority,
00157                                         "Bounding Box");
00158 
00159                     //we may have some flags to set...
00160                     string sTxt = MOOSFormat("Box Limits exceeded for more than %f seconds",m_dfIgnoreTime);
00161                     OnEvent(sTxt,!m_bDoneVerbalNotify);
00162                     m_bDoneVerbalNotify = true;
00163                 }
00164             }
00165             
00166         }
00167         else
00168         {
00169             m_bDoneVerbalNotify = false;
00170             m_bWatching = false;
00171         }
00172     }
00173     return true;
00174 }
00175 
00176 
00177 bool CLimitBox::OnNewMail(MOOSMSG_LIST &NewMail)
00178 {
00179     CMOOSMsg Msg;
00180     if(PeekMail(NewMail,"NAV_X",Msg))
00181     {
00182         if(!Msg.IsSkewed(GetTimeNow()))
00183         {
00184             m_XDOF.SetCurrent(Msg.m_dfVal,Msg.m_dfTime);
00185         }
00186     }
00187     if(PeekMail(NewMail,"NAV_Y",Msg))
00188     {
00189         if(!Msg.IsSkewed(GetTimeNow()))
00190         {
00191             m_YDOF.SetCurrent(Msg.m_dfVal,Msg.m_dfTime);
00192         }
00193     }
00194     if(PeekMail(NewMail,"NAV_DEPTH",Msg))
00195     {
00196         if(!Msg.IsSkewed(GetTimeNow()))
00197         {
00198             m_DepthDOF.SetCurrent(Msg.m_dfVal,Msg.m_dfTime);
00199         }
00200     }
00201 
00202     //always call base class version
00203     CMOOSBehaviour::OnNewMail(NewMail);
00204 
00205     return true;
00206 }
00207 
00208 bool CLimitBox::GetRegistrations(STRING_LIST &List)
00209 {
00210     List.push_front("NAV_DEPTH");
00211     List.push_front("NAV_X");
00212     List.push_front("NAV_Y");
00213 
00214 
00215     //always call base class version
00216     CMOOSBehaviour::GetRegistrations(List);
00217 
00218     return true;
00219 }
00220 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines