Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Libs/CoulombCounter/CoulombCounter.cpp

Committer:
pspatel321
Date:
2015-01-07
Revision:
34:18bcf276d3bf
Parent:
33:6bc82b6b62e5
Child:
36:0afc0fc8f86b

File content as of revision 34:18bcf276d3bf:

#include "CoulombCounter.h"

const float MSEC_HRS = 2.77778e-7;                          // Multiplier to convert milliseconds to hours
const float BAT_ISENSE_MULTIPLIER = 6.2299;                 // Multiplier to convert float to amps
const float BAT_ISENSE_OFFSET = -0.5*BAT_ISENSE_MULTIPLIER; // Offset to convert float to amps
const float BAT_ISENSE_LIMS = 3.0;                          // Over-current limit = +/- 3A

CoulombCounter::CoulombCounter(PinName _pin, int _mSec) : BatISense(_pin) {
    mSec = _mSec;
    
    // Default capacity if blank or corrupted
    float capReg = store.read(rtcGPREG_capacity);
    if (capReg < 1.0 || capReg > 5.0) {             // Bad, write default
        store.write(defaultAh, rtcGPREG_capacity);
    }
    
    // Default SOC if blank or corrupted
    float Ah = store.read(rtcGPREG_counter);
    if (Ah < 0 || Ah > defaultAh) {                    // Bad, write default
        store.write(defaultSOC*defaultAh, rtcGPREG_counter);
    }
    
    // Take the initial readings, fill the buffer
    for (int i = 0; i < CC_FILTER_TAPS; i++) {
        buffArr[i] = BatISense.read() * BAT_ISENSE_MULTIPLIER + BAT_ISENSE_OFFSET;
    }
    current();      // Get avg and fill out overCurrent flag
    tracker=0;
    
    // Start counting
    // sampler.attach_us(this, &CoulombCounter::sample, mSec*1000);
}

void CoulombCounter::sample() {
    // Take the reading
    currentSample = BatISense.read()*BAT_ISENSE_MULTIPLIER+BAT_ISENSE_OFFSET;
        
    // Integrate
    float f = ampHours()-currentSample*mSec*MSEC_HRS;
    
    // Bound to valid range
    float cap = capacity();
    if (f > cap) f = cap;
    if (f < 0) f = 0;
    
    // Write
    store.write(f, rtcGPREG_counter);
    
    // Add to filter
    buffArr[tracker] = currentSample;
    tracker++;
    if (tracker >= CC_FILTER_TAPS) tracker = 0;
}

float CoulombCounter::current() { 
    float avg = 0;
    for (int i = 0; i < CC_FILTER_TAPS; i++) {
        avg += buffArr[i];   
    }
    avg /= CC_FILTER_TAPS;
    if (abs(avg) > BAT_ISENSE_LIMS) overCurrent = true;
    return avg;
}

bool CoulombCounter::overCurrentDetected() {
    return overCurrent;
}