LTC2943.cpp
Go to the documentation of this file.
00001 /*
00002  * LTC2943.cpp
00003  *
00004  *  Created on: Oct 20, 2015
00005  *      Author: veli
00006  */
00007 
00008 #include "evarobot_battery/LTC2943.h"
00009 #include <inttypes.h>
00010 #include <stdint.h>
00011 
00012 using namespace std;
00013 
00014 void LTC2943::resetAlertStatus()
00015 {
00016         try
00017         {
00018                 this->batteryReader->WriteByte(LTC2943_ALERT_RESPONSE_ADDRESS);
00019         }catch(int e)
00020         {
00021                 throw e;
00022         } 
00023 }
00024 
00025 void LTC2943::setTemperatureThresholdHigh(uint16_t _maxTemp)
00026 {
00027         uint16_t maxTemperature;
00028 
00029         maxTemperature = (_maxTemp + 273.15)*(0xFFFF)/(FULLSCALE_TEMPERATURE);
00030 
00031         union
00032         {
00033                 uint8_t b[2];
00034                 uint16_t w;
00035         } data;
00036 
00037         data.w = maxTemperature;
00038 
00039         try
00040         {
00041                 this->batteryReader->WriteTwoDataByte(TEMPERATURE_THRESH_HIGH, data.b[1], data.b[0]);
00042         }catch(int e)
00043         {
00044                 throw e;
00045         } 
00046 
00047 }
00048 
00049 void LTC2943::setTemperatureThresholdLow(uint16_t _minTemp)
00050 {
00051         uint16_t minTemperature;
00052 
00053         minTemperature = (_minTemp + 273.15)*(0xFFFF)/(FULLSCALE_TEMPERATURE);
00054 
00055         union
00056         {
00057                 uint8_t b[2];
00058                 uint16_t w;
00059         } data;
00060 
00061         data.w = minTemperature;
00062 
00063         try
00064         {
00065                 this->batteryReader->WriteTwoDataByte(TEMPERATURE_THRESH_LOW, data.b[1], data.b[0]);
00066         }catch(int e)
00067         {
00068                 throw e;
00069         }
00070 
00071 }
00072 
00073 void LTC2943::setCurrentThresholdHigh(uint16_t _maxCurrent)
00074 {
00075         uint16_t maxCurrentCode;
00076 
00077         maxCurrentCode = this->senseResistorValue * _maxCurrent *(0x7FFF)/(FULLSCALE_CURRENT) + 0x7FFF;
00078 
00079         union
00080         {
00081                 uint8_t b[2];
00082                 uint16_t w;
00083         } data;
00084 
00085         data.w = maxCurrentCode;
00086 
00087         try
00088         {
00089                 this->batteryReader->WriteTwoDataByte(CURRENT_THRESH_HIGH_MSB, data.b[1], data.b[0]);
00090         }catch(int e)
00091         {
00092                 throw e;
00093         }
00094 
00095 }
00096 
00097 void LTC2943::setCurrentThresholdLow(uint16_t _minCurrent)
00098 {
00099         uint16_t minCurrentCode;
00100 
00101         minCurrentCode = this->senseResistorValue * _minCurrent *(0x7FFF)/(FULLSCALE_CURRENT) + 0x7FFF;
00102 
00103         union
00104         {
00105                 uint8_t b[2];
00106                 uint16_t w;
00107         } data;
00108 
00109         data.w = minCurrentCode;
00110 
00111         try
00112         {
00113                 this->batteryReader->WriteTwoDataByte(CURRENT_THRESH_LOW_MSB, data.b[1], data.b[0]);
00114         }catch(int e)
00115         {
00116                 throw e;
00117         }
00118 }
00119 
00120 void LTC2943::setChargeThresholdHigh(uint16_t _maxCharge)
00121 {
00122         union
00123         {
00124                 uint8_t b[2];
00125                 uint16_t w;
00126         } data;
00127 
00128         data.w = _maxCharge;
00129 
00130         try
00131         {
00132                 this->batteryReader->WriteTwoDataByte(CHARGE_THRESH_HIGH_MSB, data.b[1], data.b[0]);
00133         }catch(int e)
00134         {
00135                 throw e;
00136         }
00137 }
00138 
00139 void LTC2943::setChargeThresholdLow(uint16_t _minCharge)
00140 {
00141         union
00142         {
00143                 uint8_t b[2];
00144                 uint16_t w;
00145         } data;
00146 
00147         data.w = _minCharge;
00148 
00149         try
00150         {
00151                 this->batteryReader->WriteTwoDataByte(CHARGE_THRESH_LOW_MSB, data.b[1], data.b[0]);
00152         }catch(int e)
00153         {
00154                 throw e;
00155         }
00156 }
00157 
00158 void LTC2943::setVoltageThresholdHigh(float _maxVoltage)
00159 {
00160         uint16_t maxVoltageCode;
00161 
00162         maxVoltageCode = _maxVoltage *(65535.0) / (FULLSCALE_VOLTAGE);
00163 
00164         union
00165         {
00166                 uint8_t b[2];
00167                 uint16_t w;
00168         } data;
00169 
00170         data.w = maxVoltageCode;
00171 
00172         try
00173         {
00174                 this->batteryReader->WriteTwoDataByte(VOLTAGE_THRESH_HIGH_MSB, data.b[1], data.b[0]);
00175         }catch(int e)
00176         {
00177                 throw e;
00178         }
00179 }
00180 
00181 void LTC2943::setVoltageThresholdLow(float _minVoltage)
00182 {
00183         uint16_t minVoltageCode;
00184 
00185         minVoltageCode = _minVoltage * 65535.0 / (FULLSCALE_VOLTAGE);
00186 
00187         union
00188         {
00189                 uint8_t b[2];
00190                 uint16_t w;
00191         } data;
00192 
00193         data.w = minVoltageCode;
00194 
00195         try
00196         {
00197                 this->batteryReader->WriteTwoDataByte(VOLTAGE_THRESH_LOW_MSB, data.b[1], data.b[0]);
00198         }catch(int e)
00199         {
00200                 throw e;
00201         }
00202 }
00203 
00204 int LTC2943::readStatusRegister()
00205 {
00206         char statusReg;
00207         int i_status = 1;
00208         
00209         statusReg = c_register_data[STATUS];
00210         
00211         return (int)statusReg;
00212 }
00213 
00214 float LTC2943::readTemperature()
00215 {
00216         float f_temperature = 0.0;
00217         uint16_t temperatureADCCode;
00218 
00219         temperatureADCCode = (uint16_t)c_register_data[TEMPERATURE_MSB] * 256 + (uint16_t)c_register_data[TEMPERATURE_LSB];
00220 
00221         f_temperature = calculateTemperatureFromADCCode(temperatureADCCode);
00222 
00223         return f_temperature;
00224 }
00225 
00226 float LTC2943::calculateTemperatureFromADCCode(uint16_t _tempCode)
00227 {
00228         float calculatedTempValue = 0.0;
00229 
00230         calculatedTempValue = _tempCode * ((float)(FULLSCALE_TEMPERATURE)/65535) - 273.15;
00231 
00232         return calculatedTempValue;
00233 }
00234 
00235 float LTC2943::readCurrent()
00236 {
00237         float f_current = 0.0;
00238         uint16_t currentADCCode;
00239 
00240         currentADCCode = (uint16_t)c_register_data[CURRENT_MSB] * 256 + (uint16_t)c_register_data[CURRENT_LSB];
00241 
00242         f_current = calculateCurrentFromADCCode(currentADCCode);
00243 
00244         return f_current;
00245 }
00246 
00247 float LTC2943::calculateCurrentFromADCCode(uint16_t _currentCode)
00248 {
00249         float calculatedCurrentValue = 0.0;
00250 
00251         calculatedCurrentValue = (((float)_currentCode-32767)/(32767))*((float)(FULLSCALE_CURRENT)/this->senseResistorValue);
00252 
00253         return calculatedCurrentValue;
00254 }
00255 
00256 float LTC2943::readVoltage()
00257 {
00258         float f_voltage = 0.0;
00259         uint16_t voltageADCCode;
00260 
00261         voltageADCCode = (uint16_t)c_register_data[VOLTAGE_MSB] * 256 + (uint16_t)c_register_data[VOLTAGE_LSB];
00262 
00263         f_voltage = calculateVoltageFromADCCode(voltageADCCode);
00264 
00265         return f_voltage;
00266 
00267 }
00268 
00269 float LTC2943::calculateVoltageFromADCCode(uint16_t _voltageCode)
00270 {
00271         float calculatedVoltageValue = 0.0;
00272 
00273         calculatedVoltageValue = ((float)_voltageCode / (65535)) * FULLSCALE_VOLTAGE;
00274 
00275         return calculatedVoltageValue;
00276 }
00277 
00278 float LTC2943::readAccumulatedCharge()
00279 {
00280         float f_accumulatedCharge = 0.0;
00281         uint16_t accumulatedChargeADCCode;
00282         int i_status = 1;
00283 
00284         accumulatedChargeADCCode = (uint16_t)c_register_data[ACC_CHARGE_MSB] * 256 + (uint16_t)c_register_data[ACC_CHARGE_LSB];
00285 
00286         f_accumulatedCharge = calculateAccumulatedChargeFromADCCode(accumulatedChargeADCCode);
00287 
00288         return f_accumulatedCharge;
00289 }
00290 
00291 float LTC2943::calculateAccumulatedChargeFromADCCode(uint16_t _accChargeCode)
00292 {
00293         float calculatedAccValue = 0.0;
00294 
00295         calculatedAccValue = 1000 * (float)(_accChargeCode * CHARGE_LEAST * this->prescalerValue * 0.05) / (this->senseResistorValue * 4096);
00296 
00297         return calculatedAccValue;
00298 }
00299 
00300 void LTC2943::setControlRegister(unsigned char _mode, unsigned char _prescalerCode, unsigned char _alccConfiguration)
00301 {
00302         int8_t configuration = 0x00;
00303 
00304         configuration = _mode | _prescalerCode | _alccConfiguration;
00305 
00306         try
00307         {
00308                 this->batteryReader->WriteDataByte(CONTROL, configuration);
00309         }catch(int e)
00310         {
00311                 throw e;
00312         }
00313 
00314         switch(int(_prescalerCode))
00315         {
00316                 case 0x00:
00317                         this->prescalerValue = 1;
00318                         break;
00319                 case 0x08:
00320                         this->prescalerValue = 4;
00321                         break;
00322                 case 0x10:
00323                         this->prescalerValue = 16;
00324                         break;
00325                 case 0x18:
00326                         this->prescalerValue = 64;
00327                         break;
00328                 case 0x20:
00329                         this->prescalerValue = 256;
00330                         break;
00331                 case 0x28:
00332                         this->prescalerValue = 1024;
00333                         break;
00334                 case 0x30:
00335                         this->prescalerValue = 4096;
00336                         break;
00337                 case 0x31:
00338                         this->prescalerValue = 4096;
00339                         break;
00340                 default:
00341                         this->prescalerValue = 4096;
00342         }
00343 
00344 }
00345 
00346 LTC2943::LTC2943(unsigned char u_c_device_address,
00347                                  string str_i2c_file_name,
00348                                  sem_t * mutex)
00349 {
00350         // TODO Auto-generated constructor stub
00351         senseResistorValue = 0.05; // 50 mohm
00352 
00353         prescalerValue = 4096; // varsayılan entegre değeri
00354         prescalerMode = PRESCALER_4096;
00355         alccMode = ALERT_MODE;
00356 
00357         this->batteryReader = new IMI2C(u_c_device_address, str_i2c_file_name, mutex);
00358 
00359 }
00360 
00361 LTC2943::~LTC2943()
00362 {
00363         // TODO Auto-generated destructor stub
00364         delete this->batteryReader;
00365 }
00366 
00367 void LTC2943::readRegisters()
00368 {
00369         try
00370         {
00371                 this->batteryReader->ReadMultipleRegister(c_register_data, 24); 
00372         }catch(int e)
00373         {
00374                 throw e;
00375         }       
00376 }


evarobot_battery
Author(s): Mehmet Akcakoca
autogenerated on Fri Feb 12 2016 01:15:13