MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/MOOSApp.h
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 at MIT 2001-2002 and Oxford
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk.
00011 //
00012 //   This file is part of a  MOOS Core Component.
00013 //
00014 //   This program is free software; you can redistribute it and/or
00015 //   modify it under the terms of the GNU General Public License as
00016 //   published by the Free Software Foundation; either version 2 of the
00017 //   License, or (at your option) any later version.
00018 //
00019 //   This program is distributed in the hope that it will be useful,
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00022 //   General Public License for more details.
00023 //
00024 //   You should have received a copy of the GNU General Public License
00025 //   along with this program; if not, write to the Free Software
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00027 //   02111-1307, USA.
00028 //
00030 // MOOSApp.h: interface for the CMOOSApp class.
00031 //
00033 
00034 #ifndef MOOSAPPH
00035 #define MOOSAPPH
00036 
00037 #include <MOOSGenLib/MOOSGenLib.h>
00038 #include "MOOSCommClient.h"
00039 
00040 
00041 #define DEFAULT_MOOS_APP_COMMS_FREQ 5
00042 #define DEFAULT_MOOS_APP_FREQ 5
00043 #define MOOS_MAX_APP_FREQ 50
00044 #define MOOS_MAX_COMMS_FREQ 200
00045 #define STATUS_PERIOD 2
00046 
00047 #include "MOOSVariable.h"
00048 #include <set>
00049 #include <map>
00050 
00051 typedef std::map<std::string,CMOOSVariable> MOOSVARMAP;
00052 
00058 class CMOOSApp
00059 {
00060 public:
00061 
00062     CMOOSApp();
00063     virtual ~CMOOSApp();
00064 
00065 
00066 protected:
00067 
00068 
00070     //                       THE MOST IMPORTANT AND USEFUL MEMBERS
00072 public:
00078     bool Run( const char * sName,const char * sMissionFile,const char * sSubscribeName);
00079     bool Run( const char * sName,const char * sMissionFile);
00080 
00083     virtual bool OnConnectToServer();
00084 
00086     virtual bool OnDisconnectFromServer();
00087         
00089         bool RequestQuit();
00090     
00091 
00092 protected:
00096     virtual bool Iterate();
00097 
00102     virtual bool OnNewMail(MOOSMSG_LIST & NewMail);
00103 
00106     virtual bool OnCommandMsg(CMOOSMsg Msg);
00107 
00109     virtual std::string MakeStatusString();
00110 
00112     CMOOSCommClient m_Comms;
00113 
00115     bool SetCommsFreq(unsigned int nFreq);
00116 
00118     void SetAppFreq(double dfFreq);
00119 
00121     double GetAppStartTime();
00122 
00124     CProcessConfigReader m_MissionReader;
00125 
00127     bool DoRunWork();
00128     
00130     void SetAppError(bool bFlag, const std::string & sReason);
00131         
00132         
00133 
00135     //                       UTITLITY  METHODS
00137 
00138 
00143     void SetServer(const char * sServerHost="LOCALHOST",long lPort=9000);
00144 
00145 
00148     bool UseMOOSComms(bool bUse);
00149 
00151     void SortMailByTime(bool bSort=true){m_bSortMailByTime = bSort;};
00152     
00154     void SetQuitOnFailedIterate(bool bQuit){m_bQuitOnIterateFail = bQuit;};
00155 
00157     bool MOOSDebugWrite(const std::string & sTxt);
00158 
00160     void EnableCommandMessageFiltering(bool bEnable);
00161     
00163     void EnableIterateWithoutComms(bool bEnable);
00164 
00166     bool LookForAndHandleAppCommand(MOOSMSG_LIST & NewMail);
00167 
00168 
00170     std::string GetAppName();
00171 
00173     std::string GetMissionFileName();
00174 
00175 
00177     //  DYNAMIC VARIABLES  - AN OPTIONAL GARNISH
00179 
00186     bool AddMOOSVariable(std::string sName,std::string sSubscribeName,std::string sPublishName,double dfCommsTime);
00187     
00190     bool SetMOOSVar(const CMOOSVariable& MOOSVar);
00191     
00192 
00194     CMOOSVariable * GetMOOSVar(std::string sName);
00195 
00197     bool RegisterMOOSVariables();
00198 
00199 
00201     bool UpdateMOOSVariables(MOOSMSG_LIST & NewMail);
00202 
00204     bool SetMOOSVar(const std::string & sName,const std::string & sVal,double dfTime);
00205 
00207     bool SetMOOSVar(const std::string & sVarName,double dfVal,double dfTime);
00208 
00209 
00211     bool PublishFreshMOOSVariables();
00212 
00213 
00216     MOOSVARMAP m_MOOSVars;
00217 
00218 
00220     bool IsSimulateMode();
00221 
00224     bool m_bSimMode;
00225 
00226 
00229     virtual bool OnStartUp();
00230 
00232     virtual bool ConfigureComms();
00233 
00235     long m_lServerPort;
00236 
00238     std::string m_sServerHost;
00239 
00241     std::string m_sServerPort;
00242 
00244     bool m_bServerSet;
00245 
00247     bool m_bUseMOOSComms;
00248 
00250     std::string m_sAppName;
00251 
00253     std::string m_sMOOSName;
00254     
00256     int m_nCommsFreq;
00257 
00259     double m_dfFreq;
00260 
00262     std::string m_sMissionFile;
00263 
00265     bool m_bCommandMessageFiltering;
00266     
00268         bool m_bQuitOnIterateFail;
00270     double m_dfAppStartTime;
00271 
00273     double m_dfLastRunTime;
00274 
00275     
00277     bool m_bSortMailByTime;
00278     
00280     std::string m_sAppError;
00281     
00283     bool m_bAppError;
00284     
00285     
00287     double GetTimeSinceIterate();
00288 
00291     double GetLastIterateTime();
00292 
00294     int GetIterateCount();
00295 
00297     bool CanIterateWithoutComms();
00298     
00299     
00300     
00301     
00307     std::string GetCommandKey();
00308 
00309 
00310     bool m_bDebug;
00311 
00312     bool   IsDebug(){return m_bDebug;};
00313 
00314 
00315 public:
00321     void OnDisconnectToServerPrivate();
00322     void OnConnectToServerPrivate();
00323     bool OnMailCallBack();
00324     
00325     /* by calling this function Iterate and OnNewMail will be
00326      called from the thread that is servicing the MOOS Comms client. It
00327      is provided to let really very specialised MOOSApps have very speedy
00328      response times. It is not recommended for general use*/
00329     bool UseMailCallBack();
00330 
00331 private:
00332     /* this function is used to process mail on behalf of the client just before
00333        the derived OnNewMail is invoked - it has no interest to the casual user*/
00334     void OnNewMailPrivate(MOOSMSG_LIST & NewMail);
00335     /* and this is a private iterate - we may need to regularly do things behind the scenes */
00336     void IteratePrivate();
00337 
00339     bool m_bIterateWithoutComms;
00340 
00341 
00342 private:
00343 
00345     int m_nIterateCount;
00346 
00348     int m_nMailCount;
00349 
00351     double m_dfLastStatusTime;
00352 
00355     bool CheckSetUp();
00356         
00358         bool m_bQuitRequested;
00359     
00360     
00361 };
00362 
00363 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines