Skip to content

Commit

Permalink
Changed cell balance duty cycle when charging
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayamelon committed May 3, 2024
1 parent af55f8b commit 8fb0323
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
7 changes: 7 additions & 0 deletions bms/src/BmsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ extern DigitalOut* chargerControl;
#define BMS_FAULT_TEMP_THRESHOLD_HIGH 55
#endif

// Upper threshold when fault will be thrown for cell temperature when charging
//
// Units: degrees celcius
#ifndef BMS_FAULT_TEMP_THRESHOLD_CHARING_HIGH
#define BMS_FAULT_TEMP_THRESHOLD_CHARING_HIGH 45
#endif

// Upper threshold when fault will be thrown for cell temperature
//
// Units: degrees celcius
Expand Down
32 changes: 19 additions & 13 deletions bms/src/BmsThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include "EnergusTempSensor.h"

BMSThread::BMSThread(LTC681xBus &bus, unsigned int frequency, BmsEventMailbox* bmsEventMailbox, BmsBalanceAllowedMailbox* bmsBalanceAllowedMailbox)
: m_bus(bus), bmsEventMailbox(bmsEventMailbox), bmsBalanceAllowedMailbox(bmsBalanceAllowedMailbox) {
BMSThread::BMSThread(LTC681xBus &bus, unsigned int frequency, BmsEventMailbox* bmsEventMailbox, MainToBMSMailbox* mainToBMSMailbox)
: m_bus(bus), bmsEventMailbox(bmsEventMailbox), mainToBMSMailbox(mainToBMSMailbox) {
for (int i = 0; i < BMS_BANK_COUNT; i++) {
m_chips.push_back(LTC6811(bus, i));
}
Expand Down Expand Up @@ -91,18 +91,19 @@ void BMSThread::threadWorker() {
std::array<int8_t, BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT> allTemps;
while (true) {

while(!bmsBalanceAllowedMailbox->empty()) {
BalanceAllowedEvent *balanceAllowedEvent;
while(!mainToBMSMailbox->empty()) {
MainToBMSEvent *mainToBMSEvent;

osEvent evt = bmsBalanceAllowedMailbox->get();
osEvent evt = mainToBMSMailbox->get();
if (evt.status == osEventMessage) {
balanceAllowedEvent = (BalanceAllowedEvent*)evt.value.p;
mainToBMSEvent = (MainToBMSEvent*)evt.value.p;
} else {
continue;
}

balanceAllowed = balanceAllowedEvent->balanceAllowed;
delete balanceAllowedEvent;
balanceAllowed = mainToBMSEvent->balanceAllowed;
charging = mainToBMSEvent->charging;
delete mainToBMSEvent;
}


Expand Down Expand Up @@ -232,20 +233,21 @@ void BMSThread::threadWorker() {
}
}

uint16_t minTemp = allTemps[0];
uint16_t maxTemp = 0;
int8_t minTemp = allTemps[0];
int8_t maxTemp = 0;
for (int i = 0; i < BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT; i++) {
if (allTemps[i] < minTemp) {
minTemp = allTemps[i];
} else if (allTemps[i] > maxTemp) {
maxTemp = allTemps[i];
}
}
// printf("min temp: %d, max temp: %d\nmin volt: %d, max volt %d\n", minTemp, maxTemp, minVoltage, maxVoltage);

if (minVoltage <= BMS_FAULT_VOLTAGE_THRESHOLD_LOW ||
maxVoltage >= BMS_FAULT_VOLTAGE_THRESHOLD_HIGH ||
minTemp <= BMS_FAULT_TEMP_THRESHOLD_LOW ||
maxTemp >= BMS_FAULT_TEMP_THRESHOLD_HIGH) {
maxTemp >= ((charging) ? BMS_FAULT_TEMP_THRESHOLD_CHARING_HIGH : BMS_FAULT_TEMP_THRESHOLD_HIGH)) {
throwBmsFault();
}

Expand All @@ -264,7 +266,7 @@ void BMSThread::threadWorker() {
uint16_t cellVoltage = allVoltages[i * BMS_BANK_CELL_COUNT + cellNum];
if (cellVoltage >= BMS_BALANCE_THRESHOLD &&
cellVoltage >= minVoltage + BMS_DISCHARGE_THRESHOLD) {
//printf("Balancing cell %d\n", cellNum);
// printf("Balancing cell %d\n", cellNum);
dischargeValue |= (0x1 << j);
}
}
Expand Down Expand Up @@ -303,7 +305,11 @@ void BMSThread::threadWorker() {
}


ThisThread::sleep_for(100ms); // TODO: change to 100 or lower
if (charging) {
ThisThread::sleep_for(200ms); // longer duty cycle when charging
} else {
ThisThread::sleep_for(100ms);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions bms/src/BmsThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class BMSThread {
public:

BMSThread(LTC681xBus& bus, unsigned int frequency, BmsEventMailbox* bmsEventMailbox, BmsBalanceAllowedMailbox* bmsBalanceAllowedMailbox);
BMSThread(LTC681xBus& bus, unsigned int frequency, BmsEventMailbox* bmsEventMailbox, MainToBMSMailbox* mainToBMSMailbox);

// Function to allow for starting threads from static context
static void startThread(BMSThread *p) {
Expand All @@ -31,10 +31,11 @@ class BMSThread {

private:
bool balanceAllowed = false;
bool charging = false;
LTC681xBus& m_bus;
std::vector<LTC6811> m_chips;
BmsEventMailbox* bmsEventMailbox;
BmsBalanceAllowedMailbox* bmsBalanceAllowedMailbox;
MainToBMSMailbox* mainToBMSMailbox;

BMSThreadState bmsState = BMSThreadState::BMSStartup;

Expand Down
5 changes: 3 additions & 2 deletions bms/src/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class BmsEvent {
BMSThreadState bmsState;
};

class BalanceAllowedEvent {
class MainToBMSEvent {
public:
bool balanceAllowed = false;
bool charging = false;
};

static constexpr auto mailboxSize = 4;
using BmsEventMailbox = Queue<BmsEvent, mailboxSize>;
using BmsBalanceAllowedMailbox = Queue<BalanceAllowedEvent, mailboxSize>;
using MainToBMSMailbox = Queue<MainToBMSEvent, mailboxSize>;

// Measurement
// - Temp
Expand Down
20 changes: 12 additions & 8 deletions bms/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ AnalogIn glv_voltage_pin(ACC_GLV_VOLTAGE);

bool prechargeDone = false;
bool hasBmsFault = false;
bool isCharging = false;

uint32_t dcBusVoltage;
uint32_t tsVoltage;
Expand All @@ -62,10 +63,10 @@ int main() {
auto ltcBus = LTC681xParallelBus(spiDriver);

BmsEventMailbox* bmsMailbox = new BmsEventMailbox();
BmsBalanceAllowedMailbox* bmsBalanceAllowedMailbox = new BmsBalanceAllowedMailbox();
MainToBMSMailbox* mainToBMSMailbox = new MainToBMSMailbox();

Thread bmsThreadThread;
BMSThread bmsThread(ltcBus, 1, bmsMailbox, bmsBalanceAllowedMailbox);
BMSThread bmsThread(ltcBus, 1, bmsMailbox, mainToBMSMailbox);
bmsThreadThread.start(callback(&BMSThread::startThread, &bmsThread));

std::array<int8_t, BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT> allTemps;
Expand All @@ -74,7 +75,7 @@ int main() {
t.start();
while (1) {
int glv_voltage = glv_voltage_pin * 18530; // in mV
printf("GLV voltage: %d mV\n", glv_voltage);
//printf("GLV voltage: %d mV\n", glv_voltage);

while (!bmsMailbox->empty()) {
BmsEvent *bmsEvent;
Expand Down Expand Up @@ -129,10 +130,11 @@ int main() {
}
}

if (!bmsBalanceAllowedMailbox->full()) {
BalanceAllowedEvent* balanceAllowed = new BalanceAllowedEvent();
balanceAllowed->balanceAllowed = shutdown_measure_pin;
bmsBalanceAllowedMailbox->put(balanceAllowed);
if (!mainToBMSMailbox->full()) {
MainToBMSEvent* mainToBMSEvent = new MainToBMSEvent();
mainToBMSEvent->balanceAllowed = shutdown_measure_pin;
mainToBMSEvent->charging = isCharging;
mainToBMSMailbox->put(mainToBMSEvent);
}


Expand All @@ -147,7 +149,9 @@ int main() {
fan_control_pin = true;
}

charge_enable_pin = charge_state_pin && !hasBmsFault && shutdown_measure_pin;
isCharging = charge_state_pin;

charge_enable_pin = isCharging && !hasBmsFault && shutdown_measure_pin;


ThisThread::sleep_for(50 - (t.read_ms()%50));
Expand Down

0 comments on commit 8fb0323

Please sign in to comment.