MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/NavigationAndControl/MOOSTaskLib/ThirdPartyTask.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 // ThirdpartyTask.cpp: implementation of the CThirdPartyTask class.
00032 //
00034 
00035 #include "ThirdPartyTask.h"
00036 
00037 #define THIRDPARTY_STALE 10.0
00038 
00039 // Construction/Destruction
00041 
00042 CThirdPartyTask::CThirdPartyTask()
00043 {
00044     m_bInitialised = false;
00045     m_bEnabled = false;
00046 }
00047 
00048 CThirdPartyTask::~CThirdPartyTask()
00049 {
00050 
00051 }
00052 
00053 //returns false if we haven't received data in a while..bad news!
00054 bool CThirdPartyTask::RegularMailDelivery(double dfTimeNow)
00055 {
00056     return true;
00057 
00058     
00059 }
00060 bool CThirdPartyTask::SetParam(string sParam, string sVal)
00061 {
00062 
00063     if(!CMOOSBehaviour::SetParam(sParam,sVal))
00064     {
00065         //this is for us...
00066         if(MOOSStrCmp(sParam,"CLIENT"))
00067         {
00068             //format is what@who            
00069             m_sClient = sVal;
00070         }
00071         else if(MOOSStrCmp(sParam,"JOB"))
00072         {
00073             m_sJob = sVal;
00074         }
00075     }
00076     return true;
00077 }
00078 
00079 bool CThirdPartyTask::OnNewMail(MOOSMSG_LIST &NewMail)
00080 {
00081     CMOOSMsg Msg;
00082 
00083     if(PeekMail(NewMail,m_sJob,Msg) && Msg.m_sSrc==m_sClient)
00084     {
00085         if(!Msg.IsSkewed(GetTimeNow()))
00086         {
00087             return OnNewInstruction(Msg.m_sVal,Msg.m_dfTime);
00088         }
00089     }
00090 
00091     return true;
00092 }
00093 
00094 bool CThirdPartyTask::GetNotifications(MOOSMSG_LIST & List)
00095 {
00096     if(m_bActive)
00097     {
00098     }
00099 
00100     return CMOOSBehaviour::GetNotifications(List);
00101 }
00102 
00103 
00104 
00105 bool CThirdPartyTask::GetRegistrations(STRING_LIST &List)
00106 {
00107 
00108     List.push_front(m_sJob);
00109 
00110     //always call base class version
00111     CMOOSBehaviour::GetRegistrations(List);
00112 
00113     return true;
00114 }
00115 
00116 
00117 bool CThirdPartyTask::Initialise()
00118 {
00119     Enable(true);
00120     m_bInitialised = true;
00121     return true;
00122 }
00123 
00124 
00125 bool CThirdPartyTask::Run(CPathAction &DesiredAction)
00126 {
00127 
00128     if(!m_bInitialised)
00129     {
00130         Initialise();
00131     }
00132 
00133     if(!m_bEnabled)
00134         return true;
00135 
00136 
00137     if(m_Rudder.GetAge(GetTimeNow())<THIRDPARTY_STALE)
00138     {
00139         DesiredAction.Set(  ACTUATOR_RUDDER,
00140                             m_Rudder.GetDoubleVal(),
00141                             m_nPriority,
00142                             m_sJob.c_str());
00143     }
00144 
00145     if(m_Elevator.GetAge(GetTimeNow())<THIRDPARTY_STALE)
00146     {
00147         DesiredAction.Set(  ACTUATOR_ELEVATOR,
00148                             m_Elevator.GetDoubleVal(),
00149                             m_nPriority,
00150                             m_sJob.c_str());
00151     }
00152 
00153     if(m_Thrust.GetAge(GetTimeNow())<THIRDPARTY_STALE)
00154     {
00155         DesiredAction.Set(  ACTUATOR_THRUST,
00156                             m_Thrust.GetDoubleVal(),
00157                             m_nPriority,
00158                             m_sJob.c_str());
00159     }
00160 
00161     return true;
00162 }
00163 
00164 bool CThirdPartyTask::OnNewInstruction(string sInstruction,double dfTimeNow)
00165 {
00166     MOOSToUpper(sInstruction);
00167     MOOSRemoveChars(sInstruction," \t");
00168 
00169     if(sInstruction.find("ACTUATION:")!=string::npos)
00170     {
00171         return OnActuationInstruction(sInstruction,dfTimeNow);
00172     }
00173     else if(sInstruction.find("STATUS:")!=string::npos)
00174     {
00175         //eg "STATUS:ENABLE=TRUE"
00176         return OnStatusInstruction(sInstruction,dfTimeNow);
00177     }
00178     else
00179     {
00180         MOOSTrace("Unknown thirdparty command! : %s",sInstruction.c_str());
00181         return false;
00182     }
00183 
00184  
00185 }
00186 
00187 bool CThirdPartyTask::OnActuationInstruction(string sInstruction, double dfTimeNow)
00188 {
00189    //instruction is of form
00190     //"RUDDER=9,ELEVATOR = 0,THRUST = 90"
00191     string sCopy = sInstruction;
00192 
00193     MOOSChomp(sCopy,"RUDDER=");
00194     if(!sCopy.empty())
00195     {
00196         string sRudder = MOOSChomp(sCopy,",");
00197         if(!sRudder.empty())
00198         {
00199             double dfRudder = atof(sRudder.c_str());
00200             m_Rudder.Set(dfRudder,dfTimeNow);        
00201         }
00202     }
00203 
00204     sCopy = sInstruction;
00205     MOOSChomp(sCopy,"ELEVATOR=");
00206     if(!sCopy.empty())
00207     {
00208         string sElevator = MOOSChomp(sCopy,",");
00209         if(!sElevator.empty())
00210         {
00211             double dfElevator = atof(sElevator.c_str());
00212             m_Elevator.Set(dfElevator,dfTimeNow);        
00213         }
00214     }
00215 
00216     sCopy = sInstruction;
00217     MOOSChomp(sCopy,"THRUST=");
00218     if(!sCopy.empty())
00219     {
00220         string sThrust = MOOSChomp(sCopy,",");
00221         if(!sThrust.empty())
00222         {
00223             double dfThrust = atof(sThrust.c_str());
00224             m_Thrust.Set(dfThrust,dfTimeNow);        
00225         }
00226     }
00227 
00228 
00229     return true;
00230 }
00231 
00232 bool CThirdPartyTask::OnStatusInstruction(string sInstruction, double dfTimeNow)
00233 {
00234     if(sInstruction.find("ENABLE=TRUE")!=string::npos)
00235     {
00236       Enable(true);
00237     }
00238     if(sInstruction.find("ENABLE=FALSE")!=string::npos)
00239     {
00240       Enable(false);
00241     }
00242 
00243     return true;
00244 }
00245 
00246 bool CThirdPartyTask::Enable(bool bEnable)
00247 {
00248     string sMsg = MOOSFormat("%s Thridparty Task %s job %s\n",
00249                             bEnable?"Enabling":"Disabling",
00250                             GetName().c_str(),
00251                             m_sJob.c_str());
00252         
00253      DebugNotify(sMsg);
00254         
00255      m_bEnabled = bEnable;
00256 
00257      return true;
00258 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines