Code to assist in measuring a battery's Chemical ID.

Dependencies:   BQ34Z100G1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ChemIDMeasurer.cpp Source File

ChemIDMeasurer.cpp

00001 #include "ChemIDMeasurer.h"
00002 #include <cinttypes>
00003 
00004 namespace mbed
00005 {
00006     FileHandle *mbed_override_console(int)
00007     {
00008         static BufferedSerial serial(USBTX, USBRX, 115200);
00009         return &serial;
00010     }
00011 }
00012 
00013 ChemIDMeasurer::ChemIDMeasurer():
00014 soc(I2C_SDA, I2C_SCL, 100000)
00015 {
00016 }
00017 
00018 void ChemIDMeasurer::activateCharger()
00019 {
00020     // If you have code to activate the charger, put it here
00021 }
00022 
00023 void ChemIDMeasurer::deactivateCharger()
00024 {
00025     // If you have code to deactivate the charger, put it here
00026 }
00027 
00028 void ChemIDMeasurer::activateLoad()
00029 {
00030     // If you have code to activate the dummy load, put it here
00031 }
00032 
00033 void ChemIDMeasurer::deactivateLoad()
00034 {
00035     // If you have code to deactivate the dummy load, put it here
00036 }
00037 
00038 void ChemIDMeasurer::setState(ChemIDMeasurer::State newState)
00039 {
00040     state = newState;
00041     stateTimer.reset();
00042 }
00043 
00044 void ChemIDMeasurer::runMeasurement()
00045 {
00046     totalTimer.start();
00047     stateTimer.start();
00048 
00049     while(state != State::DONE)
00050     {
00051         // read data
00052         uint16_t voltage_mV = soc.getVoltage();
00053         int32_t current_mA = soc.getCurrent();
00054         double temperatureC = soc.getTemperature();
00055         char const * comment = "";
00056 
00057         // update based on state
00058         switch (state)
00059         {
00060             case State::INIT:
00061                 // Print header
00062                 printf("Elapsed Time (s), Voltage (mV), Current (mA), Temperature (deg C), SoC (%%), Comments\n");
00063                 setState(State::CHARGE);
00064                 activateCharger();
00065                 comment = "Activating charger and entering CHARGE";
00066                 break;
00067 
00068             case State::CHARGE:
00069                 // threshold current = C/10
00070                 if(current_mA < DESIGNCAP/10)
00071                 {
00072                     deactivateCharger();
00073                     setState(State::RELAX_CHARGED);
00074                     comment = "Deactivating charger and entering RELAX_CHARGED";
00075                 }
00076                 break;
00077 
00078             case State::RELAX_CHARGED:
00079                 if(stateTimer.elapsed_time() > 2h)
00080                 {
00081                     activateLoad();
00082                     setState(State::DISCHARGE);
00083                     comment = "Done relaxing and entering discharge -- please disconnect charger and connect C/10 load now.";
00084                 }
00085                 break;
00086 
00087             case State::DISCHARGE:
00088                 // Change states once we hit the termination voltage
00089                 if(voltage_mV < ZEROCHARGEVOLT * CELLCOUNT)
00090                 {
00091                     deactivateLoad();
00092                     setState(State::RELAX_DISCHARGED);
00093                     comment = "Done discharging -- please remove C/10 load now.";
00094                 }
00095                 // BQ34Z100 reports positive current always, but TI's tool expects discharging to
00096                 // be negative current.
00097                 current_mA *= -1;
00098                 break;
00099 
00100             case State::RELAX_DISCHARGED:
00101                 if(stateTimer.elapsed_time() > 5h)
00102                 {
00103                     setState(State::DONE);
00104                     comment = "Done!";
00105                 }
00106                 break;
00107 
00108             default:
00109                 // will never be hit but here to silence warning
00110                 break;
00111         }
00112 
00113 
00114         // print data column
00115         printf("%" PRIi64 ", %" PRIi16 ", %" PRIi32 ", %f, %" PRIu8 ", %s\n",
00116             std::chrono::duration_cast<std::chrono::seconds>(totalTimer.elapsed_time()).count(),
00117             voltage_mV, current_mA, temperatureC, soc.getSOC(), comment);
00118 
00119         // wait, update freq is every 5 seconds
00120         ThisThread::sleep_for(5s);
00121     }
00122 }
00123 
00124 ChemIDMeasurer measurer;
00125 
00126 int main()
00127 {
00128     measurer.runMeasurement();
00129     return 0;
00130 }