MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/MOOSInstrument.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 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 // MOOSInstrument.cpp: implementation of the CMOOSInstrument class.
00031 //
00033 #ifdef _WIN32
00034     #pragma warning(disable : 4786)
00035     #pragma warning(disable : 4503)
00036 #endif
00037 #include <MOOSLIB/MOOSLib.h>
00038 #include <iostream> 
00039 #include <sstream>
00040 #include <iomanip>
00041 #include <cctype>
00042 using namespace std;
00043 #include "MOOSInstrument.h"
00044 
00046 // Construction/Destruction
00048 
00049 CMOOSInstrument::CMOOSInstrument()
00050 {
00051     m_bPublishRaw = false;
00052     m_dfMagneticOffset = 0;
00053     m_sPrompt = "";
00054     m_sInstrumentErrorMessage = "";
00055     
00056 }
00057 
00058 CMOOSInstrument::~CMOOSInstrument()
00059 {
00060 
00061 }
00062 
00063 bool CMOOSInstrument::SetupPort()
00064 {
00065     STRING_LIST sParams;
00066 
00067     if(!m_MissionReader.GetConfiguration(m_sAppName,sParams))
00068     {
00069     MOOSTrace("%s ReadConfiguration() failed to read configuration\n",m_sAppName.c_str());
00070     MOOSPause(3000);
00071     return false;
00072     }
00073     
00075     // create the port....
00076     
00077     if(!m_Port.Configure(sParams))
00078     {
00079     MOOSTrace("failed port creation\n");
00080     return false;
00081     }
00082     
00083     m_Port.Flush();
00084     return true;
00085 }
00086 
00087 bool CMOOSInstrument::InitialiseSensorN(int nAttempts, string sSensorName)
00088 {
00089     int i = 0;
00090     while(!InitialiseSensor())
00091     {
00092     if(++i>nAttempts)
00093     {
00094             MOOSTrace("tried %d time to initialise \"%s\"...giving up\n",i,sSensorName.c_str());
00095         
00096         return false;
00097     }
00098     else
00099     {
00100         MOOSTrace("Attempting to initialise %s\n",sSensorName.c_str());
00101     }
00102     }
00103     
00104     return true;
00105 }
00106 
00107 bool CMOOSInstrument::OnStartUp()
00108 {
00109     string sRaw;
00110     m_bPublishRaw = false;
00111     if(m_MissionReader.GetConfigurationParam("PUBLISHRAW",sRaw))
00112     {
00113         m_bPublishRaw = MOOSStrCmp(sRaw,"TRUE");
00114     }
00115 
00116     m_MissionReader.GetValue("CIRCUITNAME",m_sResourceName);
00117 
00118     return true;
00119 }
00120 
00121 bool CMOOSInstrument::InitialiseSensor()
00122 {
00123     MOOSTrace("warning base class CMOOSInstrument::InitialiseSensor() called - NULL action\n");
00124     return false;
00125 }
00126 
00127 double CMOOSInstrument::GetMagneticOffset()
00128 {
00129     double dfTmp=0;
00130     if(m_MissionReader.GetValue("MAGNETICOFFSET",dfTmp))
00131     {
00132         m_dfMagneticOffset=dfTmp;
00133     }
00134     else
00135     {
00136         MOOSTrace("WARNING: No magnetic offset specified  in Mission file (Field name = \"MagneticOffset\")\n");
00137         m_dfMagneticOffset =  0;
00138     }
00139     return m_dfMagneticOffset;
00140 }
00141 
00142 void CMOOSInstrument::SetPrompt(string sPrompt)
00143 {
00144     m_sPrompt = sPrompt;
00145 }
00146 
00147 void CMOOSInstrument::SetInstrumentErrorMessage(string sError)
00148 {
00149     m_sInstrumentErrorMessage = sError;
00150 }
00151 
00152 
00153 bool CMOOSInstrument::DoNMEACheckSum(string sNMEA)
00154 {
00155     unsigned char xCheckSum=0;
00156 
00157     string sToCheck;
00158     MOOSChomp(sNMEA,"$");
00159     sToCheck = MOOSChomp(sNMEA,"*");
00160     string sRxCheckSum = sNMEA;
00161 
00162     //now calculate what we think check sum should be...
00163     string::iterator p;
00164     for(p = sToCheck.begin();p!=sToCheck.end();p++)
00165     {
00166         xCheckSum^=*p;
00167     }
00168 
00169     ostringstream os;
00170     
00171     os.flags(ios::hex);
00172     os<<(int)xCheckSum;//<<ends;
00173     string sExpected = os.str();
00174     
00175     if (sExpected.length() < 2) 
00176     {
00177         sExpected = "0" + sExpected;
00178     }
00179     
00181 
00182     return MOOSStrCmp(sExpected,sRxCheckSum);
00183 
00184 
00185 }
00190 //changed June 4th after Scott R. Sideleau, emailed me that
00191 //it isn't quite NMEA compliant
00192 string CMOOSInstrument::Message2NMEA(string sMsg)
00193 {
00194     unsigned char xCheckSum=0;
00195     //now calculate what we think check sum should be...
00196     string::iterator p;
00197     for(p = sMsg.begin(); p != sMsg.end(); p++)
00198     {
00199         xCheckSum ^= *p;
00200     }
00201     
00202     ostringstream os;
00203     
00204     os.flags(ios::hex);
00205     os<<(int)xCheckSum; //<<ends;
00206     string sChkSum = os.str();
00207     
00208     if (sChkSum.length() < 2) 
00209     {
00210         sChkSum = "0" + sChkSum;
00211     }
00212     
00213     std::transform(sChkSum.begin(), sChkSum.end(), sChkSum.begin(), \
00214                    (int(*)(int)) std::toupper);
00215     
00216     string sOutput = "$" + sMsg + "*" + sChkSum + "\r\n";
00217     
00218     return sOutput;
00219 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines