MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Instruments/Ocean/iLBL/LBLInstrument.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 Instrument. 
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 // LBLInstrument.cpp: implementation of the CLBLInstrument class.
00032 //
00034 #include <MOOSLIB/MOOSLib.h>
00035 #include <iostream>
00036 #include <math.h>
00037 using namespace std;
00038 
00039 
00040 #include "LBLInstrument.h"
00041 
00043 // Construction/Destruction
00045 
00046 CLBLInstrument::CLBLInstrument()
00047 {
00048     m_bInhibit = false;
00049     m_dfLastPingTime = 0;
00050     m_dfPingPeriod = 1.5;
00051 }
00052 
00053 CLBLInstrument::~CLBLInstrument()
00054 {
00055 
00056 }
00057 
00058 
00059 
00060 
00061 
00064 bool CLBLInstrument::Iterate()
00065 {
00066 
00067  
00068     if(GetData())
00069     {
00070         PublishData();
00071     }
00072 
00073     return true;
00074 }
00075 
00076 
00077 bool CLBLInstrument::GetData()
00078 {
00079     if(!IsSimulateMode())
00080     {
00081         if(MOOSTime()-m_dfLastPingTime<m_dfPingPeriod)
00082         {
00083             return false;
00084         }
00085         
00086         m_dfLastPingTime = MOOSTime();
00087 
00088         if(!m_bInhibit)
00089         {
00090             if(m_AVTRAK.GetRanges())
00091             {
00092                 string sResult;
00093                 if(m_AVTRAK.GetTOFString(sResult))
00094                 {
00095                     SetMOOSVar("LBL_TOF",sResult,MOOSTime());
00096                 }
00097                 else
00098                 {
00099                     return false;
00100                 }
00101 
00102                 for(int i = 1;i<=MAX_AVTRAK_CHANNELS;i++)
00103                 {
00104                     string sName;
00105                     double dfTimeRx;
00106                     double dfTOF;
00107                     if(m_AVTRAK.GetTOFByChannel(i,sName,dfTimeRx,dfTOF))
00108                     {
00109                         m_Comms.Notify(sName,dfTOF,dfTimeRx);
00110                     }
00111                 }
00112             }
00113         }
00114     }
00115     return true;
00116 }
00117 
00118 
00120 // tell the world
00121 bool CLBLInstrument::PublishData()
00122 {
00123     return PublishFreshMOOSVariables();
00124 }
00125 
00126 
00127 bool CLBLInstrument::OnStartUp()
00128 {
00129     //call base class member first
00130     CMOOSInstrument::OnStartUp();
00131     
00132     //here we make the variables that we are managing
00133     AddMOOSVariable("LBL_TOF",  "SIM_LBL_TOF",  "LBL_TOF",  0);
00134 
00135 
00136     if(IsSimulateMode())
00137     {
00138         //not much to do...othe than register for input from
00139         //simulator ...
00140         RegisterMOOSVariables();
00141 
00142         //we want to be very responsive....
00143         SetCommsFreq(20);
00144         SetAppFreq(10);
00145         
00146     }
00147     else
00148     {
00149         //try to open 
00150         if(!SetupPort())
00151         {
00152             return false;
00153         }
00154 
00155         m_AVTRAK.SetSerialPort(&m_Port);
00156 
00157         //try 10 times to initialise sensor
00158         if(!InitialiseSensorN(2,"LBL"))
00159         {
00160     //        return false;
00161         }          
00162 
00163 
00164         //now set stuff up
00165         string sChannels;
00166         if(m_MissionReader.GetConfigurationParam("RXCHANNELS",sChannels))
00167         {
00168             INT_VECTOR Chans;
00169             while(!sChannels.empty())
00170             {
00171                 int nChan = atoi(MOOSChomp(sChannels,",").c_str());
00172                 Chans.push_back(nChan);
00173             }
00174 
00175             m_AVTRAK.SetRxChannel(Chans);
00176         }
00177 
00178         double dfRxTimeOut = 2.0;
00179         m_MissionReader.GetConfigurationParam("RXTIMEOUT",dfRxTimeOut);
00180         m_AVTRAK.SetAcousticTimeOut(dfRxTimeOut);
00181 
00182 
00183         m_MissionReader.GetConfigurationParam("PINGEVERY",m_dfPingPeriod);
00184 
00185 
00186     }
00187 
00188 
00189 
00190   
00191 
00192 
00193 
00194     return true;
00195 }
00196 
00197 
00198 
00199 bool CLBLInstrument::OnNewMail(MOOSMSG_LIST &NewMail)
00200 {
00201     CMOOSMsg Msg;
00202 
00203     if(m_Comms.PeekMail(NewMail,"LBL_INHIBIT",Msg,true))
00204     {
00205         if(MOOSStrCmp(Msg.m_sVal,"TRUE"))
00206         {
00207             m_bInhibit  = true;
00208         }
00209         else
00210         {
00211             m_bInhibit  = false;
00212         }
00213     }
00214     if(IsSimulateMode())
00215     {
00216         //we want a very quick response here - don't update a moos
00217         //variable which may be wriiten over again before the next
00218         //mailout is perfomed - forward it now instead
00219         while(m_Comms.PeekMail(NewMail,"SIM_LBL_TOF",Msg,true))
00220         {
00221             //this message has now beed removed from the NewMail list 
00222             //(true passed in PeekMail)
00223             m_Comms.Notify("LBL_TOF",Msg.m_sVal,Msg.m_dfTime);
00224             /*MOOSTrace("LBLMsg: MOOStime = %.3f MsgTime = %.3f Data = %s\n",
00225                         MOOSTime(),
00226                         Msg.m_dfTime,
00227                         Msg.m_sVal.c_str());*/
00228         //    Msg.Trace();
00229         }
00230     }
00231 
00232     return UpdateMOOSVariables(NewMail);
00233 }
00234 
00235 
00236 
00237 
00238 bool CLBLInstrument::OnConnectToServer()
00239 {
00240 
00241     m_Comms.Register("LBL_INHIBIT",0);
00242 
00243 
00244     if(IsSimulateMode())
00245     {
00246         //not much to do...
00247         m_Comms.Register("SIM_LBL_TOF",0);
00248         
00249         return RegisterMOOSVariables();
00250         
00251     }
00252     
00253     return true;
00254 }
00255 
00256 
00257 
00258 
00259 
00260 // here we initialise the sensor, giving it start up values
00261 bool CLBLInstrument::InitialiseSensor()
00262 {    
00263     return m_AVTRAK.Reset();    
00264 }
00265 
00266 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines