MOOS 0.2375
|
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 // BatteryInstrument.cpp: implementation of the CBatteryInstrument class. 00032 // 00034 #include <MOOSLIB/MOOSLib.h> 00035 00036 00037 #include <iostream> 00038 #include <strstream> 00039 #include <math.h> 00040 using namespace std; 00041 00042 00043 #include "BatteryDriver.h" 00044 #include "BluefinBatteryDriverV1.h" 00045 #include "BluefinBatteryDriverV2.h" 00046 #include "KeithleyBatteryDriver.h" 00047 #include "BatteryInstrument.h" 00048 00049 #define DEFAULT_FULL_VOLTS 48.0 00050 #define DEFAULT_EMPTY_VOLTS 40.0 00051 #define BATTERY_PERIOD 5.0 00052 00054 // Construction/Destruction 00056 00057 CBatteryInstrument::CBatteryInstrument() 00058 { 00059 m_dfLastIteration=0; 00060 m_pBatterySensor = NULL; 00061 } 00062 00063 CBatteryInstrument::~CBatteryInstrument() 00064 { 00065 if(m_pBatterySensor!=NULL) 00066 { 00067 delete m_pBatterySensor; 00068 } 00069 } 00070 bool CBatteryInstrument::OnNewMail(MOOSMSG_LIST &NewMail) 00071 { 00072 CMOOSMsg Msg; 00073 if(m_Comms.PeekMail(NewMail,"BATTERY_CONTROL",Msg,true)) 00074 { 00075 if(m_pBatterySensor!=NULL && !Msg.IsSkewed(MOOSTime())) 00076 { 00077 bool bSuccess = true; 00078 bool bOn = MOOSStrCmp(Msg.m_sVal,"ON"); 00079 00080 bSuccess = m_pBatterySensor->Switch(bOn); 00081 00082 if(bSuccess) 00083 { 00084 string sState = (bOn?"ON":"OFF"); 00085 string sFeedback = "Battery is "+ sState; 00086 this->MOOSDebugWrite(sFeedback); 00087 } 00088 00089 00090 } 00091 } 00092 return true; 00093 00094 } 00095 bool CBatteryInstrument::OnConnectToServer() 00096 { 00097 m_Comms.Register("BATTERY_CONTROL",0); 00098 00099 return true; 00100 } 00101 bool CBatteryInstrument::Iterate() 00102 { 00103 if(!IsSimulateMode()) 00104 { 00105 if(MOOSTime()-m_dfLastIteration<BATTERY_PERIOD) 00106 { 00107 return true; 00108 } 00109 else 00110 { 00111 m_dfLastIteration = MOOSTime(); 00112 } 00113 00114 if(GetData()) 00115 { 00116 PublishData(); 00117 } 00118 } 00119 return true; 00120 } 00121 00122 bool CBatteryInstrument::OnStartUp() 00123 { 00124 00125 CMOOSInstrument::OnStartUp(); 00126 00127 //here we make the variables that we are managing 00128 double dfKeithleyPeriod = 2; 00129 00130 //Keithley update @ 1Hz 00131 AddMOOSVariable("Keithley","SIM_VOLTAGE","KEITHLEY_BATT_VOLTAGE",dfKeithleyPeriod); 00132 AddMOOSVariable("KEITHLEY_RAW","","KEITHLEY_RAW",dfKeithleyPeriod); 00133 00134 if(IsSimulateMode()) 00135 { 00136 RegisterMOOSVariables(); 00137 } 00138 else 00139 { 00140 //try to open 00141 if(!SetupPort()) 00142 { 00143 return false; 00144 } 00145 00146 if(!MakeDriver()) 00147 return false; 00148 00149 //what is the fully charged battery voltage? 00150 double dfFull=DEFAULT_FULL_VOLTS; 00151 m_MissionReader.GetConfigurationParam("FULLVOLTS",dfFull); 00152 m_pBatterySensor->SetFullVolts(dfFull); 00153 00154 double dfEmpty=DEFAULT_EMPTY_VOLTS; 00155 m_MissionReader.GetConfigurationParam("EMPTYVOLTS",dfEmpty); 00156 m_pBatterySensor->SetEmptyVolts(dfEmpty); 00157 00158 //figure out at what voltage we should issue a batery low message! 00159 string sWarning; 00160 m_MissionReader.GetConfigurationParam("LOWFLAG",sWarning); 00161 string sFlag = MOOSChomp(sWarning,"@"); 00162 string sLevel = sWarning; 00163 00164 if(!sLevel.empty()) 00165 { 00166 double dfTmp = atof(sLevel.c_str()); 00167 if(dfTmp>0) 00168 { 00169 m_sWarningFlag = sFlag; 00170 m_pBatterySensor->SetLowVolts(dfTmp); 00171 } 00172 } 00173 00174 //tell teh driver what port to use.. 00175 m_pBatterySensor->SetSerialPort(&m_Port); 00176 00177 //try 10 times to initialise sensor 00178 if(!InitialiseSensorN(10,"Battery")) 00179 { 00180 return false; 00181 } 00182 00183 } 00184 00185 return true; 00186 } 00187 00188 00189 bool CBatteryInstrument::InitialiseSensor() 00190 { 00191 return m_pBatterySensor->Initialise(); 00192 } 00193 00194 bool CBatteryInstrument::GetData() 00195 { 00196 return m_pBatterySensor->GetData(); 00197 } 00198 00199 bool CBatteryInstrument::PublishData() 00200 { 00201 CBatteryDriver::CBatteryStatus CurrentStatus; 00202 00203 if(m_pBatterySensor->GetStatus(CurrentStatus)) 00204 { 00205 m_Comms.Notify("BATTERY_VOLTAGE",CurrentStatus.GetVoltage()); 00206 m_Comms.Notify("BATTERY_CHARGE",CurrentStatus.GetPercentCharge()); 00207 m_Comms.Notify("BATTERY_STATUS",CurrentStatus.GetStatusAsString()); 00208 00209 if(CurrentStatus.GetStatus()==CBatteryDriver::CBatteryStatus::LOW) 00210 { 00211 MOOSTrace("Battery is low: %f V!\n",CurrentStatus.GetVoltage()); 00212 if(!m_sWarningFlag.empty()) 00213 { 00214 m_Comms.Notify(m_sWarningFlag,CurrentStatus.GetVoltage()); 00215 } 00216 } 00217 00218 //publish cell details 00219 string sCells = m_pBatterySensor->GetCellsString(); 00220 if(!sCells.empty()) 00221 { 00222 m_Comms.Notify("BATTERY_CELL_DETAILS",sCells); 00223 } 00224 00225 //publish all errors 00226 string sError = m_pBatterySensor->GetErrorString(); 00227 if(!sError.empty()) 00228 { 00229 m_Comms.Notify("BATTERY_ERROR",sError); 00230 } 00231 00232 //publish cell comments 00233 string sComment = m_pBatterySensor->GetCommentString(); 00234 if(!sComment.empty()) 00235 { 00236 m_Comms.Notify("BATTERY_COMMENT",sComment); 00237 } 00238 00239 00240 return true; 00241 } 00242 else 00243 { 00244 return false; 00245 } 00246 00247 } 00248 00249 bool CBatteryInstrument::MakeDriver() 00250 { 00251 m_pBatterySensor=NULL; 00252 00253 string sType; 00254 m_MissionReader.GetConfigurationParam("TYPE",sType); 00255 00256 if(MOOSStrCmp(sType,"KEITHLEY")) 00257 { 00258 m_pBatterySensor = new CKeithleyBatteryDriver; 00259 MOOSTrace("Making Keithley battery driver\n"); 00260 } 00261 else if (MOOSStrCmp(sType,"BLUEFIN_V1")) 00262 { 00263 m_pBatterySensor = new CBluefinBatteryDriverV1; 00264 MOOSTrace("Making Bluefin \"binary\" battery driver\n"); 00265 } 00266 else if (MOOSStrCmp(sType,"BLUEFIN_V2")) 00267 { 00268 m_pBatterySensor = new CBluefinBatteryDriverV2; 00269 MOOSTrace("Making Bluefin \"smart\" battery driver\n"); 00270 } 00271 else 00272 { 00273 MOOSTrace("Battery type must be one of KEITHLEY or BLUEFIN_V1 or BLUEFIN_V2 \n"); 00274 MOOSTrace("Assuming KEITHLEY...! \n"); 00275 m_pBatterySensor = new CKeithleyBatteryDriver; 00276 } 00277 00278 return m_pBatterySensor!=NULL; 00279 }