This commit is contained in:
hamza
2024-07-07 18:52:54 +03:00
parent e7b5054e02
commit a4a856eb31
14 changed files with 126 additions and 260 deletions

159
Core/Src/AMS_HighLevel.c Normal file → Executable file
View File

@ -6,29 +6,21 @@
*/
#include "AMS_HighLevel.h"
#include "ADBMS_Abstraction.h"
#include "ADBMS_LL_Driver.h"
#include "TMP1075.h"
#include "can-halal.h"
#include "errors.h"
#include "stm32f3xx_hal.h"
Cell_Module module = {};
uint32_t balancedCells = 0;
uint8_t BalancingActive = 0;
uint8_t stateofcharge = 100;
int64_t currentintegrator = 0;
uint32_t lastticks = 0;
uint32_t currenttick = 0;
uint8_t eepromconfigured = 0;
uint8_t internalbalancingalgo = 1;
uint16_t startbalancingthreshold = 41000;
uint16_t stopbalancingthreshold = 30000;
uint16_t balancingvoltagedelta = 10;
bool balancingActive = false;
uint16_t amsuv = 0;
uint16_t amsov = 0;
uint8_t amserrorcode = 0;
uint8_t amswarningcode = 0;
uint8_t numberofCells = 16;
uint8_t numberofCells = 13;
uint8_t numberofAux = 0;
uint8_t packetChecksumFails = 0;
@ -48,18 +40,9 @@ struct pollingTimes {
struct pollingTimes pollingTimes = {0, 0};
void AMS_Init(SPI_HandleTypeDef* hspi) {
if (eepromconfigured == 1) {
/*amsov = eepromcellovervoltage>>4;
amsuv = (eepromcellundervoltage-1)>>4;
numberofCells = eepromnumofcells;
numberofAux = eepromnumofaux;
initAMS(hspi, eepromnumofcells, eepromnumofaux);*/
amsConfigOverUnderVoltage(amsov, amsuv);
} else {
initAMS(hspi, numberofCells, numberofAux);
amsov = DEFAULT_OV;
amsuv = DEFAULT_UV;
}
initAMS(hspi, numberofCells, numberofAux);
amsov = DEFAULT_OV;
amsuv = DEFAULT_UV;
pollingTimes = (struct pollingTimes) {HAL_GetTick(), HAL_GetTick()};
@ -83,10 +66,8 @@ void AMS_Loop() {
case AMSDISCHARGING:
break;
case AMSWARNING:
writeWarningLog(0x01);
break;
case AMSERROR:
writeErrorLog(amserrorcode);
break;
}
lastAMSState = currentAMSState;
@ -117,8 +98,8 @@ void AMS_Loop() {
uint8_t AMS_Idle_Loop() {
if (!amsWakeUp()) {
//error_data.data_kind = SEK_INTERNAL_BMS_TIMEOUT;
//set_error_source(ERROR_SOURCE_INTERNAL);
error_data.data_kind = SEK_INTERNAL_BMS_TIMEOUT; //we don't receive data for the wakeup command
set_error_source(ERROR_SOURCE_INTERNAL); //so we can't tell if we timed out
}
packetChecksumFails += amsAuxAndStatusMeasurement(&module);
@ -147,15 +128,12 @@ uint8_t AMS_Idle_Loop() {
packetChecksumFails += amsCellMeasurement(&module);
packetChecksumFails += amsCheckUnderOverVoltage(&module);
packetChecksumFails += integrateCurrent();
if (packetChecksumFails > MAX_PACKET_CHECKSUM_FAILS) {
error_data.data_kind = SEK_INTERNAL_BMS_CHECKSUM_FAIL;
set_error_source(ERROR_SOURCE_INTERNAL);
}
tmp1075_measure();
int any_voltage_error = 0;
for (size_t i = 0; i < numberofCells; i++) {
if (module.cellVoltages[i] < 2500) {
@ -190,27 +168,16 @@ uint8_t AMS_Idle_Loop() {
}
mcuDelay(10);
return 0;
}
uint8_t AMS_Warning_Loop() {
amsWakeUp();
amsConfigOverUnderVoltage(amsov, amsuv);
amsClearAux();
amsCellMeasurement(&module);
amsAuxAndStatusMeasurement(&module);
amsCheckUnderOverVoltage(&module);
if (!(module.overVoltage | module.underVoltage)) {
currentAMSState = AMSIDLE;
// amsClearWarning();
if ((module.overVoltage | module.underVoltage)) {
}
amsStopBalancing();
return 0;
}
uint8_t AMS_Warning_Loop() { return 0; }
uint8_t AMS_Error_Loop() { return 0; }
uint8_t AMS_Charging_Loop() { return 0; }
@ -218,96 +185,6 @@ uint8_t AMS_Charging_Loop() { return 0; }
uint8_t AMS_Discharging_Loop() { return 0; }
uint8_t AMS_Balancing_Loop() {
uint8_t balancingdone = 1;
if ((eepromconfigured == 1) && (internalbalancingalgo == 1) &&
(module.internalDieTemp <
28000 /*Thermal Protection 93°C*/)) // If the EEPROM is configured and
// the internal Balancing Algorithm
// should be used
{
uint16_t highestcellvoltage = module.cellVoltages[0];
uint16_t lowestcellvoltage = module.cellVoltages[0];
uint8_t highestcell = 0;
uint8_t lowestcell = 0;
for (uint8_t n = 0; n < numberofCells; n++) {
if (module.cellVoltages[n] > highestcellvoltage) {
highestcellvoltage = module.cellVoltages[n];
highestcell = n;
}
if (module.cellVoltages[n] < lowestcellvoltage) {
lowestcellvoltage = module.cellVoltages[n];
lowestcell = n;
}
}
if (currentAMSState ==
AMSCHARGING) // Balancing is only Active if the BMS is in Charging Mode
{
uint32_t channelstobalance = 0;
if (highestcellvoltage > startbalancingthreshold) {
for (uint8_t n = 0; n < numberofCells; n++) {
if (module.cellVoltages[n] > stopbalancingthreshold) {
uint16_t dv = module.cellVoltages[n] - lowestcellvoltage;
if (dv > (balancingvoltagedelta * 1000)) {
balancingdone = 0;
channelstobalance |= 1 << n;
}
}
}
}
amsConfigBalancing(channelstobalance, 0x0F);
amsStartBalancing(100);
}
else if (currentAMSState == AMSIDLEBALANCING) {
uint32_t channelstobalance = 0;
if (lowestcellvoltage <
stopbalancingthreshold) // If under Voltage of one Cell is reached
{
amsStopBalancing();
balancingdone = 1;
} else // otherwise continue with regular Balancing Algorithm
{
for (uint8_t n = 0; n < numberofCells; n++) {
uint16_t dv = module.cellVoltages[n] - lowestcellvoltage;
if (dv > balancingvoltagedelta) {
balancingdone = 0;
channelstobalance |= 1 << n;
}
}
amsConfigBalancing(channelstobalance, 0x0F);
amsStartBalancing(100);
}
}
} else {
amsStopBalancing();
balancingdone = 1;
}
return balancingdone;
}
uint8_t writeWarningLog(uint8_t warningCode) {
// eepromWriteWarningLog(warningCode);
return 0;
}
uint8_t writeErrorLog(uint8_t errorCode) {
// eepromWriteErrorLog(errorCode);
return 0;
}
uint8_t integrateCurrent() {
lastticks = currenttick;
currenttick = HAL_GetTick();
if (currenttick < lastticks) {
currentintegrator += (module.auxVoltages[0] - module.auxVoltages[2]) *
(currenttick - lastticks);
}
//TODO: implement
return 0;
}