Skip to content

Commit

Permalink
acc board preliminary pin logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayamelon committed Apr 30, 2024
1 parent 5597aa9 commit 9add346
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
25 changes: 20 additions & 5 deletions bms/src/BmsThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "EnergusTempSensor.h"

BMSThread::BMSThread(LTC681xBus &bus, unsigned int frequency, BmsEventMailbox* mailbox)
BMSThread::BMSThread(LTC681xBus &bus, unsigned int frequency, BmsEventMailbox* bmsEventMailbox, BmsBalanceAllowedMailbox* bmsBalanceAllowedMailbox)
: m_bus(bus) {
for (int i = 0; i < BMS_BANK_COUNT; i++) {
m_chips.push_back(LTC6811(bus, i));
Expand Down Expand Up @@ -89,7 +89,22 @@ void BMSThread::threadWorker() {
std::array<uint16_t, BMS_BANK_COUNT * BMS_BANK_CELL_COUNT> allVoltages;
std::array<int8_t, BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT> allTemps;
while (true) {
printf("\n \n");

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

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

balanceAllowed = balanceAllowedEvent->balanceAllowed;
}



m_bus.WakeupBus();

// Set all status lights high
Expand Down Expand Up @@ -232,7 +247,7 @@ void BMSThread::threadWorker() {
throwBmsFault();
}

if (bmsState == BMSThreadState::BMSIdle) {
if (bmsState == BMSThreadState::BMSIdle && balanceAllowed) {
for (int i = 0; i < BMS_BANK_COUNT; i++) {

LTC6811::Configuration &config = m_chips[i].getConfig();
Expand Down Expand Up @@ -273,7 +288,7 @@ void BMSThread::threadWorker() {
m_chips[i].updateConfig();
}

if (!mailbox->full()) {
if (!bmsEventMailbox->full()) {
BmsEvent* msg = new BmsEvent();
for (int i = 0; i < BMS_BANK_COUNT*BMS_BANK_CELL_COUNT; i++) {
msg->voltageValues[i] = allVoltages[i];
Expand All @@ -282,7 +297,7 @@ void BMSThread::threadWorker() {
msg->temperatureValues[i] = allTemps[i];
}
msg->bmsState = bmsState;
mailbox->put((BmsEvent *)msg);
bmsEventMailbox->put((BmsEvent *)msg);
}


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

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

// Function to allow for starting threads from static context
static void startThread(BMSThread *p) {
p->threadWorker();
}

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

BMSThreadState bmsState = BMSThreadState::BMSStartup;

Expand Down
11 changes: 6 additions & 5 deletions bms/src/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
#include "rtos.h"
#include "Mail.h"

enum class BmsEventType : uint16_t {
VoltageMeasurement,
TemperatureMeasurement
};

class BmsEvent {
public:
uint16_t voltageValues[BMS_BANK_COUNT * BMS_BANK_CELL_COUNT];
int8_t temperatureValues[BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT];
BMSThreadState bmsState;
};

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

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

// Measurement
// - Temp
Expand Down
61 changes: 49 additions & 12 deletions bms/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ bool prechargeDone = false;
bool hasBmsFault = false;

uint32_t dcBusVoltage;
uint32_t tsVoltage;

uint16_t allVoltages[BMS_BANK_COUNT*BMS_BANK_CELL_COUNT];
int8_t allTemps[BMS_BANK_COUNT*BMS_BANK_TEMP_COUNT];
Expand All @@ -61,9 +62,10 @@ int main() {
auto ltcBus = LTC681xParallelBus(spiDriver);

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

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

std::array<int8_t, BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT> allTemps;
Expand All @@ -84,16 +86,29 @@ int main() {
continue;
}



dcBusVoltage = 0;

for (int i = 0; i < BMS_BANK_COUNT*BMS_BANK_CELL_COUNT; i++) {
allVoltages[i] = bmsEvent->voltageValues[i];
dcBusVoltage += allVoltages[i];
}
for (int i = 0; i < BMS_BANK_COUNT*BMS_BANK_TEMP_COUNT; i++) {
allTemps[i] = bmsEvent->temperatureValues[i];
switch (bmsEvent->bmsState) {
case BMSThreadState::BMSStartup:
break;
case BMSThreadState::BMSIdle:
hasBmsFault = false;

tsVoltage = 0;

for (int i = 0; i < BMS_BANK_COUNT*BMS_BANK_CELL_COUNT; i++) {
allVoltages[i] = bmsEvent->voltageValues[i];
tsVoltage += allVoltages[i];
}
for (int i = 0; i < BMS_BANK_COUNT*BMS_BANK_TEMP_COUNT; i++) {
allTemps[i] = bmsEvent->temperatureValues[i];
}

break;
case BMSThreadState::BMSFault:
printf("*** BMS FAULT ***\n");
hasBmsFault = true;
break;
default:
break;
}
}

Expand All @@ -112,6 +127,28 @@ int main() {
break;
}
}

BalanceAllowedEvent* balanceAllowed;
balanceAllowed->balanceAllowed = shutdown_measure_pin;
if (!bmsBalanceAllowedMailbox->full()) {
bmsBalanceAllowedMailbox->put(balanceAllowed);
}


if (dcBusVoltage >= tsVoltage * 0.95) {
prechargeDone = true;
}

precharge_control_pin = prechargeDone;
bms_fault_pin = hasBmsFault;

if (prechargeDone || charge_state_pin) {
fan_control_pin = true;
}

charge_enable_pin = charge_state_pin && !hasBmsFault && shutdown_measure_pin;


ThisThread::sleep_for(50 - (t.read_ms()%50));
}
}
Expand All @@ -123,7 +160,7 @@ void initIO() {

fan_control_pin = 0; // turn fans off at start
charge_enable_pin = 0; // charge not allowed at start
bms_fault_pin = 0; // assume fault at start
bms_fault_pin = 0; // assume fault at start, low means fault
precharge_control_pin = 0; // positive AIR open at start

}
Expand Down

0 comments on commit 9add346

Please sign in to comment.