MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/NavigationAndControl/pNav/MOOSPriorityInput.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 
00032 
00033 #include <MOOSLIB/MOOSLib.h>
00034 #include <iostream>
00035 #include "MOOSPriorityInput.h"
00036 
00037 #define DEFAULT_PRIORITY_TIMEOUT 3.0
00038 
00039 using namespace std;
00040 
00042 // Construction/Destruction
00044 CMOOSPriorityInput::CMOOSPriorityInput()
00045 {
00046     m_dfLastValue = 0;
00047     m_dfLastTimeSet = -1;
00048 }
00049 
00050 CMOOSPriorityInput::~CMOOSPriorityInput()
00051 {
00052 
00053 }
00054 
00055 bool CMOOSPriorityInput::Initialise(string sName,string sSources, string sStem,STRING_LIST & SubscribeTo)
00056 {
00057 
00058 
00059     //what is our name?
00060     m_sName = sName;
00061 
00062     SubscribeTo.clear();
00063 
00064     //what named data can we be set from...
00065     while(!sSources.empty())
00066     {
00067         string sInput = MOOSChomp(sSources,",");
00068         string sWho = MOOSChomp(sInput,"@");
00069 
00070         //default time out
00071         double dfTimeOut = DEFAULT_PRIORITY_TIMEOUT;
00072 
00073         if(!sInput.empty())
00074         {
00075             //specified time out
00076             dfTimeOut = atof(sInput.c_str());
00077             if(dfTimeOut==0)
00078             {
00079                 dfTimeOut = DEFAULT_PRIORITY_TIMEOUT;
00080             }
00081         }
00082 
00083         //give the stem and the header whatr is the variable name ?
00084         // eg if we are given stem = "X_E" and who = GPS then we are
00085         //expected to be updated by GPS_X_E        
00086         string sMOOSVariable = sWho+"_"+sStem;
00087 
00088         //alwaye upper case
00089         MOOSToUpper(sMOOSVariable);
00090 
00091         //we shall want to subscribe to this...
00092         SubscribeTo.push_front(sMOOSVariable);
00093 
00094 
00095         //OK so save this input source in our priority stack
00096         CPrioritySource PSrc;
00097         PSrc.Initialise(sMOOSVariable,dfTimeOut);
00098 
00099         //store this input..
00100         m_Sources.push_back(PSrc);
00101     }
00102 
00103     //a little bit of debug info to show what is going on..
00104     MOOSTrace("Variable \"%s\" has inputs from (decrasing order):\n",m_sName.c_str());
00105     
00106     PRIORITY_SOURCE_VECTOR::iterator p;
00107 
00108     for(p = m_Sources.begin(); p!=m_Sources.end();p++)
00109     {
00110         MOOSTrace("%s\n",p->GetSource().c_str());
00111     }
00112     MOOSTrace("\n");
00113 
00114     //current source is top most in list (higher priority)
00115     m_nCurrentSourceNdx =0;
00116 
00117 
00118     return true;
00119 }
00120 
00121 
00122 bool CMOOSPriorityInput::SetInput(CMOOSMsg &InMsg,double dfTimeNow)
00123 {
00124 
00125     unsigned int i;
00126     for(i = 0; i<m_Sources.size();i++)
00127     {
00128         //does this Msg come the input source monitored by m_Sources[i]?
00129         if(m_Sources[i].GetSource() == InMsg.m_sKey)
00130         {
00131             if(InMsg.IsDataType(MOOS_DOUBLE))
00132                 m_Sources[i].Set(InMsg.m_dfVal,InMsg.m_dfTime);
00133             else
00134                m_Sources[i].Set(InMsg.GetString(),InMsg.GetTime());   
00135         }
00136     }
00137 
00138     return true;
00139 }
00140 
00141 bool CMOOSPriorityInput::GetOutput(CMOOSMsg &OutMsg,double dfTimeNow)
00142 {
00143 
00144     for(unsigned int i = 0; i<m_Sources.size();i++)
00145     {
00146         if(m_Sources[i].HasExpired(dfTimeNow) ==false)
00147         {
00148             //upgrade path
00149 
00150             if(static_cast<int>(i)<m_nCurrentSourceNdx)
00151             {
00152                 MOOSTrace("Variable %s upgraded now deriving from %s\n",
00153                         m_sName.c_str(),
00154                         m_Sources[i].GetSource().c_str());
00155             }
00156 
00157             SetActiveSource(i);
00158 
00159 
00160             if(m_Sources[i].IsFresh())
00161             {
00162 
00163                 //rename sKey to our name
00164                 //eg DEPTH_DEPTH -> NAV_DEPTH
00165                 OutMsg.m_sKey = m_sName;
00166 
00167                 //mapped names come from us.."pNav"
00168                 OutMsg.m_sSrc = "pNav";
00169 
00170 
00171                 //fill in a message
00172                 OutMsg.m_cDataType = MOOS_DOUBLE;
00173                 OutMsg.m_cMsgType = MOOS_NOTIFY;
00174 
00175 
00176                 //fill in data...
00177                 double dfVal,dfTime;
00178                std::string sVal;
00179                 m_Sources[i].Get(dfVal,dfTime);
00180                 m_Sources[i].Get(sVal,dfTime);
00181 
00182                 OutMsg.m_dfVal = dfVal;
00183                 OutMsg.m_sVal = sVal;
00184                 OutMsg.m_cDataType = m_Sources[i].GetDataType(); 
00185                OutMsg.m_dfTime = dfTime;
00186 
00187                
00188                 //remeber our last value
00189                 m_dfLastValue = dfVal;
00190 
00191                 //and the time we set it
00192                 m_dfLastTimeSet = dfTime;
00193 
00194                 return true;
00195             }
00196             else
00197             {
00198                 return false;
00199             }
00200         }
00201         else
00202         {
00203             if(i+1<m_Sources.size())
00204             {
00205                 //down-grade path
00206                 if(static_cast<int> (i+1) > m_nCurrentSourceNdx)
00207                 {
00208 
00209                     MOOSTrace("Variable %s downgraded now deriving from %s\n",
00210                         m_sName.c_str(),
00211                         m_Sources[i+1].GetSource().c_str());
00212 
00213 
00214                     SetActiveSource(i+1);
00215                 }
00216             }
00217         }
00218         
00219     }
00220 
00221 
00222     //if we got here there is nothing to do        
00223     return false;
00224 
00225 }
00226 
00227 CMOOSPriorityInput::CPrioritySource::CPrioritySource()
00228 {
00229     m_dfVal         = 0.0;
00230     m_dfTimeOut        = DEFAULT_PRIORITY_TIMEOUT;
00231     m_dfLastInputTime    = -1;
00232     m_bFresh = false;
00233 
00234 }
00235 
00236 bool CMOOSPriorityInput::CPrioritySource::HasExpired(double dfTimeNow)
00237 {
00238     if(m_dfLastInputTime==-1)
00239     {
00240         m_dfLastInputTime = dfTimeNow;
00241     }
00242     return dfTimeNow-m_dfLastInputTime>m_dfTimeOut;
00243 }
00244 
00245 bool CMOOSPriorityInput::CPrioritySource::Set(double dfVal, double dfTime)
00246 {
00247     if(dfTime>m_dfLastInputTime)
00248     {
00249         m_dfVal = dfVal;
00250         m_dfDataTime = dfTime;
00251         m_dfLastInputTime = dfTime;
00252         m_bFresh = true;
00253         m_cDataType = MOOS_DOUBLE;
00254     }
00255     return true;
00256 }
00257 
00258                
00259                bool CMOOSPriorityInput::CPrioritySource::Set( const std::string & sVal, double dfTime)
00260 {
00261         if(dfTime>m_dfLastInputTime)
00262     {
00263        m_sVal = sVal;
00264        m_dfDataTime = dfTime;
00265        m_dfLastInputTime = dfTime;
00266        m_bFresh = true;
00267        m_cDataType = MOOS_STRING;
00268         }
00269     return true;
00270 }
00271                
00272 bool CMOOSPriorityInput::CPrioritySource::Get(double &dfVal, double &dfTime)
00273 {
00274     dfVal = m_dfVal;
00275     dfTime = m_dfDataTime;
00276     m_bFresh = false;
00277 
00278     return true;
00279     
00280 }
00281 
00282 bool CMOOSPriorityInput::CPrioritySource::Get(std::string &sVal, double &dfTime)
00283 {
00284     sVal = m_sVal;
00285     dfTime = m_dfDataTime;
00286     m_bFresh = false;
00287     return true;
00288 }
00289 
00290 char CMOOSPriorityInput::CPrioritySource::GetDataType()
00291 {
00292     return m_cDataType;
00293 }
00294 
00295 bool CMOOSPriorityInput::CPrioritySource::Initialise(string &sSourceName,double dfTimeOut)
00296 {
00297     m_sSource = sSourceName;
00298     m_dfTimeOut = dfTimeOut;
00299     return true;
00300 }
00301 
00302 string & CMOOSPriorityInput::CPrioritySource::GetSource()
00303 {
00304     return m_sSource;
00305 }
00306 
00307 double CMOOSPriorityInput::CPrioritySource::GetLastInputTime()
00308 {
00309     return m_dfLastInputTime;
00310 }
00311 
00312 
00313 
00314 
00315 bool CMOOSPriorityInput::SetActiveSource(int nSrcNdx)
00316 {
00317 
00318     if(nSrcNdx<0 || nSrcNdx>= static_cast<int> (m_Sources.size()))
00319     {
00320         return false;
00321     }
00322 
00323     m_nCurrentSourceNdx = nSrcNdx;
00324 
00325     return true;
00326 }
00327 
00328 bool CMOOSPriorityInput::Clear()
00329 {
00330     m_Sources.clear();
00331     //current source is top most in list (higher priority)
00332     m_nCurrentSourceNdx =0;
00333     return true;
00334 }
00335 
00336 bool CMOOSPriorityInput::GetLastValue(double & dfTime, double & dfVal)
00337 {
00338     if(m_dfLastTimeSet==-1)
00339         return false;
00340 
00341     dfTime = m_dfLastTimeSet;
00342     dfVal =  m_dfLastValue;
00343     return true;
00344 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines