A public repository for BMS algorithms for a NUCLEO BOARD.
Hi Everyone!
Welcome to this repository from Howey's Research Group at the University of Oxford.
The code published here incorporates BMS algorithms for diagnosis functions such as SOC, SOH and Power estimation on a Kokam 53Ah Li-ion battery. This code was designed to work with a NUCLEO F401-RE board and to be tested with a dSPACE HIL Simulator. A short guide on how the set up works is available at https://bitbucket.org/ff95/bms .
The code is made up of three key parts. "Headers" and "Source" folders and the "main.cpp" file. As the code was generated by converting a Simulink model ( available on the BitBucket page), the headers and source code files generated by the conversion are in the corresponding "Headers" and "Source" folders. The "main.cpp" file sets up the ADC, the USB data transmission and starts the estimation (once a character "y" has been received by the computer it is connected to). It also transmits the data from the estimation via USB. Explanation on how to set up the communication with the board is available at BitBucket webpage, from where a MATLAB file can be downloaded which allows real time communication.
For any questions you can contact the author at federicomariaferrari@gmail.com .
The Simulink and Matlab files, together with a short guide, are all available at: https://bitbucket.org/ff95/bms.
Thanks for trying this out!
Federico
Diff: main.cpp
- Revision:
- 14:4b5df635f248
- Parent:
- 13:831eab218c33
- Child:
- 15:b39f568faa27
--- a/main.cpp Mon Jan 30 19:31:17 2017 +0000 +++ b/main.cpp Tue Feb 07 12:20:57 2017 +0000 @@ -1,35 +1,4 @@ -/** -* @author Damien Frost -* -* @section LICENSE -* -* Copyright (c) 2016 Damien Frost -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -* THE SOFTWARE. -* -* @file "main.cpp" -* -* @section DESCRIPTION -* Simple Internet of Things main program. The device sends data every 3 -* seconds, and can receive data from a server. -* -*/ + #include "mbed.h" #include "EKF.h" @@ -37,97 +6,124 @@ Timer OutputTimer; -Timer SampleTime; Timer MacroTime; +Ticker TimerforIteration; + Serial TRANSMIT(SERIAL_TX, SERIAL_RX); AnalogIn VoltageSensor(PA_0); AnalogIn CurrentSensor(PA_1); +AnalogIn TemperatureSensor(PA_4); DigitalOut DATATRANSMISSION(LED1); -DigitalIn OnOrOff(USER_BUTTON); +InterruptIn OnOrOff(USER_BUTTON); + float VoltageMeas = 0; float CurrentMeas = 0; +float TemperatureMeas=0; + float Result_V = 0; float Result_I = 0; +float Result_T = 0; + float MinStepRes = 0; float SocOutput = 0.0f; float R0Output=0.0f; float Q0Output=0.0f; +float mVoltRange = 3300; // This is the supplay voltage of ADC (or MCU) +float ADCres = 65536; // This is the ADC resolution oxFFFF +int Count=0; + +float MaxCurrentAmp=150; +float MaxTemperature=80; +float MaxdSpaceOut=6; +float CircuitGain=0.546; + +bool ButtonPressed=0.0f; +bool StateEKF=1.0f; + +//TURN THE PROCESS OR ON OFF +void pressed() +{ + EKF_initialize(); + OutputTimer.reset(); + MacroTime.reset(); + + if (StateEKF) { + StateEKF =0; + + } else { + StateEKF = 1; + + + } +} -float mVoltRange = 3300; // This is the supplay voltage of ADC (or MCU) -float ADCres = 65536; // This is the ADC resolution oxFFFF +//USE THE FUNCTION WITH A TIMER TO STEP AT KNOWN TIME LENGTHS +void ITERATE() +{ + + // SAMPLE THE ADCs + VoltageMeas = VoltageSensor.read_u16(); + CurrentMeas = CurrentSensor.read_u16(); + TemperatureMeas=TemperatureSensor.read_u16(); + + //CALCULATE THE RESOLUTION OF EACH BIT + MinStepRes = (mVoltRange / ADCres); + + // CONVERT MEASURED VALUES TO ACTUAL SCALED VALUES + Result_V = ((MinStepRes * VoltageMeas))/1000; + Result_I = ((MinStepRes * CurrentMeas))/1000; + Result_T = ((MinStepRes * TemperatureMeas))/1000; + + // INPUT THE DATA TO THE EKF_STEP + + //ADJUST FOR THE CIRCUIT GAIN + EKF_U.Voltage=Result_V/CircuitGain; + EKF_U.Current=(Result_I/CircuitGain-MaxdSpaceOut/2)*(2*MaxCurrentAmp/MaxdSpaceOut); + EKF_U.TemperatureIn=Result_T*(MaxTemperature/MaxdSpaceOut)/CircuitGain; + // EKF_U.TemperatureIn=25; + EKF_U.TimeStep=MacroTime.read()-EKF_U.MacroTime; + EKF_U.MacroTime=MacroTime.read(); + + // STEP THE EKF MODEL + EKF_step(); + + //FLASH THE LIGHT + DATATRANSMISSION=1; + + //TRANSMIT DATA + TRANSMIT.printf("%f \n", EKF_Y.SOC); + TRANSMIT.printf("%f \n", EKF_Y.R0); + TRANSMIT.printf("%f \n", EKF_Y.Q0); + + TRANSMIT.printf(" %f \n",EKF_U.Voltage); + TRANSMIT.printf("%f \n", EKF_U.Current); + TRANSMIT.printf("%f \n", EKF_U.TemperatureIn); + + TRANSMIT.printf("%f \n", EKF_U.MacroTime); + TRANSMIT.printf("%f \n", EKF_U.TimeStep); + +} int main() { - - OutputTimer.start(); - EKF_initialize(); - TRANSMIT.printf("Starting up...\n\r" ); - - // Inifinite main loop: while(1) { - DATATRANSMISSION=1; - - // START TIMER FOR STEPPING FUNCTION - SampleTime.start(); - MacroTime.start(); + + OnOrOff.fall(&pressed); - // SAMPLE THE ADCs - VoltageMeas = VoltageSensor.read_u16(); - CurrentMeas = CurrentSensor.read_u16(); - - //CALCULATE THE RESOLUTION OF EACH BIT - MinStepRes = (mVoltRange / ADCres); - - // CONVERT MEASURED VALUES TO ACTUAL SCALED VALUES - Result_V = ((MinStepRes * VoltageMeas))/1000; - Result_I = ((MinStepRes * CurrentMeas))/1000; - - - // INPUT THE DATA TO THE EKF_STEP - EKF_U.Voltage=Result_V; - EKF_U.Current=Result_I*50/6; - EKF_U.MacroTime=MacroTime.read(); - - + - //RESET SAMPLE TIMER - //SampleTime.reset(); - - // EKF_U.Current=Result_I; - - //STEP THE MODEL EVERY 0.0001S - - if (SampleTime.read()>0.01f) { - EKF_step(); - SampleTime.reset(); - - if(OutputTimer.read()>(0.1f)) { - DATATRANSMISSION=0; -//EKF_Y.SOC=1; -//EKF_Y.R0=2; -//EKF_Y.Q0=3; -//EKF_U.Voltage=4; -//EKF_U.Current=5; -//EKF_U.MacroTime=6; - - TRANSMIT.printf("%f \n", EKF_Y.SOC); - TRANSMIT.printf("%f \n", EKF_Y.R0); - TRANSMIT.printf("%f \n\r", EKF_Y.Q0); - - TRANSMIT.printf(" %f \n", EKF_U.Voltage); - TRANSMIT.printf("%f \n\r", EKF_U.Current); - TRANSMIT.printf("%f \n\r", EKF_U.MacroTime); - // Reset the timer: - //OutputTimer.reset(); - } + // MAIN LOOP ACTIVATED FROM BUTTON BEING PRESSED + while(StateEKF) { + TimerforIteration.attach(&ITERATE,0.1); + //START TIMER AND LED + MacroTime.start(); + OutputTimer.start(); + DATATRANSMISSION=0; } - - } } -