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:
- 10:c4f0c3a5223f
- Parent:
- 9:17c258c67c33
- Child:
- 11:e39c490420d2
--- a/main.cpp Fri Dec 09 17:23:26 2016 +0000 +++ b/main.cpp Fri Dec 09 18:27:15 2016 +0000 @@ -33,11 +33,8 @@ #include "mbed.h" #include "globals.h" -#include "WiflyInterface.h" #include "Commands.h" -#include "Websocket.h" #include "ADC.h" -#include "pwm.h" #include "EKF.h" //#define DEBUG @@ -48,118 +45,45 @@ #include "messages.h" - - // Main Loop! int main() { - unsigned int wifi_cmd = NO_WIFI_CMD; - float wifi_data = 0.0f; - unsigned int ADCRaw; - char msg[128]; - - // Set the IoT ID: - IoT_ID = 5; - - // Set the Auto reconnect flag: - IotStatus.SetFlag(SF_AUTOCONNECT); - - // Send a startup message to serial port: - INFO(""); - INFO(""); - INFO("Starting up..."); - INFO("CPU SystemCoreClock is %d Hz", SystemCoreClock); - - // Configure the ADC to sample the internal temperature sensor. You cannot - // use AnalogIn() unfortunately... - ConfigureADC(); - - // Check for the wifi module - WifiPresent.mode(PullDown); - if(WifiPresent == 1){ - IotStatus.SetFlag(SS_WIFIPRESENT); - } - - if(IotStatus.CheckFlag(SS_WIFIPRESENT)){ - - // Connect to the wifi network. It will basically get stuck here until it - // connects to the network. - SetupNetwork(5000); - - // Configure the baud rate of the wifi shield: - // This will make our wireless transmissions much faster. - ws.setBaud(115200); - wait(0.5f); - } - - // Configure the PWM module: - ConfigurePWM(Duty_us, PwmPeriod_us); - // Turn on the PWM: - TurnOnPWM(true); - - // Check to see we are connected to the network: - if(IotStatus.CheckFlag(SF_WIRELESSCONNECTED)){ - // Try to connect to the WebSocket server: - sprintf(msg, "ws://%s:%d/ws", SERVER_IP, WS_PORT); - ws.Initialize(msg); - INFO("Connecting to Websocket Server on %s...", msg); - if(ws.connect()){ - // Set a status flag: - INFO("Connected."); - IotStatus.SetFlag(SF_SERVERCONNECTED); - }else{ - // We could not connect right now.. - IotStatus.ClearFlag(SF_SERVERCONNECTED); - INFO("Could not connect to server, will try again later."); - ReconnectAttempts++; - } - } - - // Start the display timer which will send data to the server every - // 3 seconds. + + DisplayTimer.start(); - + // Inifinite main loop: while(1) { - // Process the wifi command: - if(wifi_cmd > NO_WIFI_CMD){ - // Modify the desired variable: - ModifyVariable(wifi_cmd, wifi_data); - // Reset the command: - wifi_cmd = NO_WIFI_CMD; - } - - // Check for new wifi data: - if((wifi_cmd == NO_WIFI_CMD)){ - ReceiveNetworkData(&wifi_cmd, &wifi_data); - } + db = 1; + // Sample the ADCs: + VoltageMeasurement = VoltageSensor.read(); + CurrentMeasurement = CurrentSensor.read(); + db = 0; + // Set battery model inputs: + EKF_U.Voltage = VoltageMeasurement; + EKF_U.Current = CurrentMeasurement; + // Set a debug pin high: + db = 1; // Probe this pin to see how long the battery model algorithm takes to run. + // Run battery model here: + EKF_step(); + // Clear the debug pin: + db = 0; + // Read battery model outputs: + SocOutput = EKF_Y.SOC; - // Send the network data every 3 seconds: - if(DisplayTimer.read()>(3.0f)){ - // Sample the internal temperature sensor: - STARTADCCONVERSION; - // Wait for the conversion to complete: - while(!ADCCONVERSIONCOMPLETE); - // Save the raw value from the ADC: - ADCRaw = ADC1->DR; - // Calculate the temperature using information from the datasheet: - TempSensor = ((((float)ADCRaw)/ADC_MAX)*IT_VMAX - IT_V25)/IT_AVG_SLOPE + 25.0f; - // Output the result: - DBG("TempSensor = %.5f", TempSensor); - DBG("ADC1->DR = %d", ADCRaw); - + + + + if(DisplayTimer.read()>(3.0f)){ INFO("SOC is, %f", EKF_Y.SOC); INFO("VOLTAGE is, %f", EKF_U.Voltage); INFO("CURRENT is, %f", EKF_U.Current); - // Send data over network: - SendNetworkData(); - - // Increment a counter: - SendCounter++; - // Reset the timer: DisplayTimer.reset(); } - } // while(1) -} // main() + + } + + } +