MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Instruments/Ocean/iBattery/KeithleyBatteryDriver.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 // KeithleyBatteryDriver.cpp: implementation of the CKeithleyBatteryDriver class.
00032 //
00034 
00035 #include "KeithleyBatteryDriver.h"
00036 #include <MOOSGenLib/MOOSGenLib.h>
00038 // Construction/Destruction
00040 
00041 CKeithleyBatteryDriver::CKeithleyBatteryDriver()
00042 {
00043     m_bError = false;
00044 }
00045 
00046 CKeithleyBatteryDriver::~CKeithleyBatteryDriver()
00047 {
00048     
00049 }
00050 
00051 bool CKeithleyBatteryDriver::Initialise()
00052 {
00053     //nothing to do
00054     return true;
00055 }
00056 
00057 
00058 bool CKeithleyBatteryDriver::GetData()
00059 {
00060 
00061     //The basic communication
00062     //$ starts the string
00063     //1 is the address of the device (if other Keithleys
00064     //are in the chain, each must be addressed separately
00065     //RD reads data, end the string with a <cr>
00066     const char * sGetKeithleyString = "$1RD\r";
00067 
00068     m_pPort->Flush();
00069 
00070     if( m_pPort->Write(sGetKeithleyString,strlen(sGetKeithleyString)) )
00071     {
00072         string sWhat;
00073 
00074         char Tmp[20];
00075         char * pStr = Tmp;
00076         int nRead = m_pPort->ReadNWithTimeOut(Tmp,20,1.5);
00077 
00078         if(nRead>0)
00079         {
00080             if(Tmp[0]=='\0')
00081             {
00082                 pStr+=1;
00083             }
00084             Tmp[nRead]='\0';
00085 
00086             sWhat = pStr;
00087 
00088             ParseKeithleyString(sWhat);
00089 
00090 
00091         }
00092 
00093 
00094     }
00095     else
00096     {
00097         return false;
00098     }
00099 
00100     return true;
00101 }
00102 
00103 bool CKeithleyBatteryDriver::ParseKeithleyString(string &sReply)
00104 {
00105     m_bError = false;    
00106     string sCopy = sReply;
00107 
00108     //Error messages are in the form ?<address#> MESSAGE<cr>
00109     //Example:  ?1 SYNTAX ERROR
00110     //If the message doesn't contain a "?", assume a valid message
00111     if (sReply.find("?") == string::npos)
00112     {
00113         //Valid messages are in the form *+00000.00<cr>
00114         //The * starts the message
00115         //+ could be - for negative readings
00116         //(Since we only remove the "+", negative values should be read correctly)
00117         MOOSRemoveChars(sReply, "*+");
00118 
00119         m_Status.m_dfVoltage = atof(sReply.c_str());
00120         m_Status.m_eStatus   = CBatteryDriver::CBatteryStatus::GOOD;
00121         
00122     }
00123     else
00124     {
00125         m_Status.m_eStatus = CBatteryDriver::CBatteryStatus::SENSOR_ERROR;
00126         m_sError           = "Keithley saw ?";
00127         m_bError           = true;
00128     }
00129 
00130     return true;
00131 }
00132 
00133 
00134 bool CKeithleyBatteryDriver::IsError()
00135 {
00136     return m_bError;
00137 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines