Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Committer:
pspatel321
Date:
Wed Jan 07 03:25:50 2015 +0000
Revision:
34:18bcf276d3bf
Parent:
33:6bc82b6b62e5
Child:
36:0afc0fc8f86b
Added serial input.  Updated glvBat coulomb counter to match AMS, brought in changes to outDiag and inCommands from AMS.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pspatel321 30:91af74a299e1 1 #include "CoulombCounter.h"
pspatel321 30:91af74a299e1 2
pspatel321 30:91af74a299e1 3 const float MSEC_HRS = 2.77778e-7; // Multiplier to convert milliseconds to hours
pspatel321 30:91af74a299e1 4 const float BAT_ISENSE_MULTIPLIER = 6.2299; // Multiplier to convert float to amps
pspatel321 30:91af74a299e1 5 const float BAT_ISENSE_OFFSET = -0.5*BAT_ISENSE_MULTIPLIER; // Offset to convert float to amps
pspatel321 30:91af74a299e1 6 const float BAT_ISENSE_LIMS = 3.0; // Over-current limit = +/- 3A
pspatel321 30:91af74a299e1 7
pspatel321 30:91af74a299e1 8 CoulombCounter::CoulombCounter(PinName _pin, int _mSec) : BatISense(_pin) {
pspatel321 30:91af74a299e1 9 mSec = _mSec;
pspatel321 30:91af74a299e1 10
pspatel321 30:91af74a299e1 11 // Default capacity if blank or corrupted
pspatel321 30:91af74a299e1 12 float capReg = store.read(rtcGPREG_capacity);
pspatel321 30:91af74a299e1 13 if (capReg < 1.0 || capReg > 5.0) { // Bad, write default
pspatel321 30:91af74a299e1 14 store.write(defaultAh, rtcGPREG_capacity);
pspatel321 30:91af74a299e1 15 }
pspatel321 30:91af74a299e1 16
pspatel321 30:91af74a299e1 17 // Default SOC if blank or corrupted
pspatel321 30:91af74a299e1 18 float Ah = store.read(rtcGPREG_counter);
pspatel321 33:6bc82b6b62e5 19 if (Ah < 0 || Ah > defaultAh) { // Bad, write default
pspatel321 33:6bc82b6b62e5 20 store.write(defaultSOC*defaultAh, rtcGPREG_counter);
pspatel321 30:91af74a299e1 21 }
pspatel321 30:91af74a299e1 22
pspatel321 30:91af74a299e1 23 // Take the initial readings, fill the buffer
pspatel321 30:91af74a299e1 24 for (int i = 0; i < CC_FILTER_TAPS; i++) {
pspatel321 30:91af74a299e1 25 buffArr[i] = BatISense.read() * BAT_ISENSE_MULTIPLIER + BAT_ISENSE_OFFSET;
pspatel321 30:91af74a299e1 26 }
pspatel321 30:91af74a299e1 27 current(); // Get avg and fill out overCurrent flag
pspatel321 30:91af74a299e1 28 tracker=0;
pspatel321 30:91af74a299e1 29
pspatel321 30:91af74a299e1 30 // Start counting
pspatel321 30:91af74a299e1 31 // sampler.attach_us(this, &CoulombCounter::sample, mSec*1000);
pspatel321 30:91af74a299e1 32 }
pspatel321 30:91af74a299e1 33
pspatel321 30:91af74a299e1 34 void CoulombCounter::sample() {
pspatel321 30:91af74a299e1 35 // Take the reading
pspatel321 30:91af74a299e1 36 currentSample = BatISense.read()*BAT_ISENSE_MULTIPLIER+BAT_ISENSE_OFFSET;
pspatel321 30:91af74a299e1 37
pspatel321 30:91af74a299e1 38 // Integrate
pspatel321 30:91af74a299e1 39 float f = ampHours()-currentSample*mSec*MSEC_HRS;
pspatel321 30:91af74a299e1 40
pspatel321 30:91af74a299e1 41 // Bound to valid range
pspatel321 30:91af74a299e1 42 float cap = capacity();
pspatel321 30:91af74a299e1 43 if (f > cap) f = cap;
pspatel321 30:91af74a299e1 44 if (f < 0) f = 0;
pspatel321 30:91af74a299e1 45
pspatel321 30:91af74a299e1 46 // Write
pspatel321 30:91af74a299e1 47 store.write(f, rtcGPREG_counter);
pspatel321 30:91af74a299e1 48
pspatel321 30:91af74a299e1 49 // Add to filter
pspatel321 30:91af74a299e1 50 buffArr[tracker] = currentSample;
pspatel321 30:91af74a299e1 51 tracker++;
pspatel321 30:91af74a299e1 52 if (tracker >= CC_FILTER_TAPS) tracker = 0;
pspatel321 30:91af74a299e1 53 }
pspatel321 34:18bcf276d3bf 54
pspatel321 30:91af74a299e1 55 float CoulombCounter::current() {
pspatel321 30:91af74a299e1 56 float avg = 0;
pspatel321 30:91af74a299e1 57 for (int i = 0; i < CC_FILTER_TAPS; i++) {
pspatel321 30:91af74a299e1 58 avg += buffArr[i];
pspatel321 30:91af74a299e1 59 }
pspatel321 30:91af74a299e1 60 avg /= CC_FILTER_TAPS;
pspatel321 30:91af74a299e1 61 if (abs(avg) > BAT_ISENSE_LIMS) overCurrent = true;
pspatel321 30:91af74a299e1 62 return avg;
pspatel321 30:91af74a299e1 63 }
pspatel321 34:18bcf276d3bf 64
pspatel321 30:91af74a299e1 65 bool CoulombCounter::overCurrentDetected() {
pspatel321 34:18bcf276d3bf 66 return overCurrent;
pspatel321 30:91af74a299e1 67 }