Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CoulombCounter.h Source File

CoulombCounter.h

00001 #ifndef _FILE_CURRENTMONITOR_H
00002 #define _FILE_CURRENTMONITOR_H
00003 
00004 #include "mbed.h"
00005 #include "RTCStore.h"
00006 
00007 #define GPREG_AH_COUNTER    0                                // rtc GPREG offset for the coulomb counter
00008 #define GPREG_AH_CAPACITY   1                                // rtc GPREG offset for the capacity spec
00009 
00010 const float DEFAULT_AH = 1.5;                                // Default amphours of battery, in case store read is bad/empty
00011 const float DEFAULT_SOC = 0.5;                               // Defualt SOC, in case of bad read/store
00012 
00013 const float MIN_CAPACITY_SETTING = 0.5;                      // Lowest allowable capacity setting
00014 const float MAX_CAPACITY_SETTING = 10;                       // Largest allowable capacity setting
00015 
00016 enum GLV_BAT_ERROR {
00017     OVER_CHARGE_I       = 1<<0,     // Charge current exceeded (after filter)
00018     OVER_DISCHARGE_I    = 1<<1,     // Discharge current exceeded (after filter)
00019     CAP_INIT            = 1<<2,     // Capacity RTC register was invalid on startup
00020     SOC_INIT            = 1<<3,     // SOC RTC register was invalid on startup
00021 };
00022 
00023 class CoulombCounter
00024 {
00025 
00026 public:
00027 
00028     // Configures for a certain pin, millisecond sample period, and which GPREG in store to use to store the ampHours
00029     CoulombCounter(PinName _pin, int _mSec, unsigned int size=50);
00030     void setup(float* overChargeCurrent, float* overDischargeCurrent);
00031     bool size(unsigned int size);
00032     char readError() {
00033         return errorPacket;
00034     }
00035     void clearError() {
00036         errorPacket = 0;
00037     }
00038     float current() {
00039         return currentFiltered;    // Last current reading in Amps
00040     }
00041     void sample();
00042 
00043     float capacity() {
00044         return store.read(GPREG_AH_CAPACITY);
00045     }
00046     float SOC() {
00047         return ampHours()/capacity();
00048     }
00049     float ampHours() {
00050         return store.read(GPREG_AH_COUNTER);
00051     }
00052     bool changeCapacity(float _ampHours) {
00053         if (_ampHours < MIN_CAPACITY_SETTING || _ampHours > MAX_CAPACITY_SETTING) return false;
00054         store.write(_ampHours, GPREG_AH_CAPACITY);
00055         return true;
00056     }
00057     bool resetToSOC(float _SOC) {
00058         if (_SOC < 0 || _SOC > 1) return false;
00059         store.write(_SOC*capacity(), GPREG_AH_COUNTER);
00060         return true;
00061     }
00062     bool resetToAh(float _ampHours) {
00063         if (_ampHours < 0 || _ampHours > capacity()) return false;
00064         store.write(_ampHours, GPREG_AH_COUNTER);
00065         return true;
00066     }
00067 
00068 private:
00069     RTCStore store;                 // Access to the RTC registers (battery-backed)
00070     unsigned int _size;             // Size of buffer
00071     void updateAvg();               // Used to get average current and update flags
00072     int mSec;                       // Integration time
00073     float currentFiltered;          // Filtered current, last result of updateAvg()
00074     AnalogIn BatISense;             // Analog input pin
00075     char errorPacket;               // Errors (over-current)
00076     float *buffArr;                 // The buffer itself
00077     unsigned int tracker;           // Position to load next sample
00078     
00079     float* overChargeCurrent;       // Charge current limit
00080     float* overDischargeCurrent;    // Discharge current limit
00081 };
00082 #endif