Skip to content

Commit

Permalink
Added acc board pin IO and started CAN logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayamelon committed Apr 30, 2024
1 parent 044151c commit 7f46a08
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 75 deletions.
1 change: 1 addition & 0 deletions bms/.mbed
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ROOT=.
TARGET=NUCLEO_L432KC
TARGET_CODE=0770
TARGET_SERIAL=0671FF555185754867150637
146 changes: 96 additions & 50 deletions bms/src/BmsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,95 +107,141 @@ const int BMS_CELL_MAP[12] = {0, 1, 2, 3, -1, -1, 4, 5, 6, -1, -1, -1};
#endif


// Acc board shutdown measurement
//
// To measure the state of the shutdown circuit
#ifndef ACC_SHUTDOWN_MEASURE

#ifdef TARGET_NUCLEO_L432KC
#define ACC_SHUTDOWN_MEASURE PA_2
#else
#error "Unknown board for ACC_SHUTDOWN_MEASURE"
#endif

#endif


/*
// BMS fault latch
// Acc board fan control
//
// Readback from BMS fault relay to be broadcasted on CAN bus
#ifndef BMS_PIN_BMS_FLT_LAT
// To be set high or low to enable/disable fans
#ifndef ACC_FAN_CONTROL

#ifdef TARGET_LPC1768
#define BMS_PIN_BMS_FLT_LAT p12
#elif TARGET_NUCLEO_F303K8
#define BMS_PIN_BMS_FLT_LAT PF_1
#ifdef TARGET_NUCLEO_L432KC
#define ACC_FAN_CONTROL PA_7
#else
#error "Unknown board for BMS_PIN_BMS_FLT_LAT"
#error "Unknown board for ACC_FAN_CONTROL"
#endif

#endif


// Acc board current sensor vref read
//
// To measure the vref on the current sensor
#ifndef ACC_BUFFERED_C_VREF

#ifdef TARGET_NUCLEO_L432KC
#define ACC_BUFFERED_C_VREF PA_3
#else
#error "Unknown board for ACC_BUFFERED_C_VREF"
#endif
*/

#endif


/*
// IMD status input
// Acc board current sensor output read
//
// Reads PWM output from IMD board
#ifndef BMS_PIN_IMD_STATUS
// To measure the output on the current sensor
#ifndef ACC_BUFFERED_C_OUT

#ifdef TARGET_LPC1768
#define BMS_PIN_IMD_STATUS p14
#elif TARGET_NUCLEO_F303K8
#define BMS_PIN_IMD_STATUS PF_0
#ifdef TARGET_NUCLEO_L432KC
#define ACC_BUFFERED_C_OUT PA_1
#else
#error "Unknown board for ACC_BUFFERED_C_OUT"
#endif

#endif


// Acc board GLV voltage read
//
// To measure the voltage of the GLV system
#ifndef ACC_GLV_VOLTAGE

#ifdef TARGET_NUCLEO_L432KC
#define ACC_GLV_VOLTAGE PA_0
#else
#error "Unknown board for BMS_PIN_IMD_STATUS"
#error "Unknown board for ACC_GLV_VOLTAGE"
#endif

#endif


// Acc charge enable output
//
// To be set high when charging is allowed
#ifndef ACC_CHARGE_ENABLE

#ifdef TARGET_NUCLEO_L432KC
#define ACC_CHARGE_ENABLE PA_9
#else
#error "Unknown board for ACC_CHARGE_ENABLE"
#endif
*/

#endif


/*
// IMD fault latch
// Acc charge state input
//
// Readback from IMD fault relay to be broadcasted on CAN bus
#ifndef BMS_PIN_IMD_FLT_LAT
// To measure if the acc is connected to the charger
#ifndef ACC_CHARGE_STATE

#ifdef TARGET_LPC1768
#define BMS_PIN_IMD_FLT_LAT p13
#elif TARGET_NUCLEO_F303K8
#define BMS_PIN_IMD_FLT_LAT PA_8
#ifdef TARGET_NUCLEO_L432KC
#define ACC_CHARGE_STATE PA_10
#else
#error "Unknown board for BMS_PIN_IMD_FLT_LAT"
#error "Unknown board for ACC_CHARGE_STATE"
#endif

#endif


// Acc IMD status input
//
// To measure the state of the IMD status pin
#ifndef ACC_IMD_STATUS

#ifdef TARGET_NUCLEO_L432KC
#define ACC_IMD_STATUS PB_0
#else
#error "Unknown board for ACC_IMD_STATUS"
#endif
*/

#endif


// Charger output
// Acc BMS Fault Output
//
// To be pulled high to enable charger
#ifndef BMS_PIN_CHARGER_CONTROL
// To be set low when there is a BMS fault
#ifndef ACC_BMS_FAULT

#ifdef TARGET_LPC1768
#define BMS_PIN_CHARGER_CONTROL p11
#elif TARGET_NUCLEO_F303K8
#define BMS_PIN_CHARGER_CONTROL PB_1
#elif TARGET_NUCLEO_L432KC
#define BMS_PIN_CHARGER_CONTROL PA_9
#ifdef TARGET_NUCLEO_L432KC
#define ACC_BMS_FAULT PB_7
#else
#error "Unknown board for BMS_PIN_CHARGER_CONTROL"
#error "Unknown board for ACC_BMS_FAULT"
#endif

#endif

// Current input

// Acc Precharge Control
//
// Input from analog current sensor
#ifndef BMS_PIN_SIG_CURRENT
// To be set high when precharging is done to allow the positive AIR to close
#ifndef ACC_PRECHARGE_CONTROL

#ifdef TARGET_LPC1768
#define BMS_PIN_SIG_CURRENT p15
#elif TARGET_NUCLEO_F303K8
#define BMS_PIN_SIG_CURRENT PA_0
#elif TARGET_NUCLEO_L432KC
#define BMS_PIN_SIG_CURRENT PA_1
#ifdef TARGET_NUCLEO_L432KC
#define ACC_PRECHARGE_CONTROL PB_1
#else
#error "Unknown board for BMS_PIN_SIG_CURRENT"
#error "Unknown board for ACC_PRECHARGE_CONTROL"
#endif

#endif
Expand Down
25 changes: 1 addition & 24 deletions bms/src/BmsThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ void BMSThread::threadWorker() {
throwBmsFault();
}

// TODO: DON'T FORGET TO REMOVE THE '!'
if (!m_discharging) {
if (m_discharging) {
for (int i = 0; i < BMS_BANK_COUNT; i++) {

LTC6811::Configuration &config = m_chips[i].getConfig();
Expand All @@ -264,28 +263,6 @@ void BMSThread::threadWorker() {
config.dischargeState.value = dischargeValue;

m_chips[i].updateConfig();

// uint8_t buf[8];

// uint8_t sendBuf[6] = {0x78, 0x00, 0x00, 0x00, 0xff, 0x0f};

// ThisThread::sleep_for(5ms);

// m_bus.SendDataCommand(LTC681xBus::BuildAddressedBusCommand(WriteConfigurationGroupA(),
// i), sendBuf);

// ThisThread::sleep_for(15ms);

// m_bus.SendReadCommand(
// LTC681xBus::BuildAddressedBusCommand(ReadConfigurationGroupA(), i),
// buf);

// ThisThread::sleep_for(5ms);

// printf("conf group a: %x %x %x %x %x %x\n", buf[0], buf[1],
// buf[2], buf[3], buf[4], buf[5]);

// ThisThread::sleep_for(5ms);
}
} else {
for (int i = 0; i < BMS_BANK_COUNT; i++) {
Expand Down
2 changes: 1 addition & 1 deletion bms/src/BmsThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class BMSThread {
std::vector<BmsEventMailbox*> mailboxes;

// Things that need to go away
bool m_discharging = false;
bool m_discharging = true;

void throwBmsFault();
void threadWorker();
Expand Down
39 changes: 39 additions & 0 deletions bms/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,33 @@
CAN* canBus;

void initIO();
void canRX();

CircularBuffer<CANMessage, 32> canqueue;

uint8_t canCount;


DigitalIn shutdown_measure_pin(ACC_SHUTDOWN_MEASURE);
DigitalIn imd_status_pin(ACC_IMD_STATUS);
DigitalIn charge_state_pin(ACC_CHARGE_STATE);


DigitalOut fan_control_pin(ACC_FAN_CONTROL);
DigitalOut charge_enable_pin(ACC_CHARGE_ENABLE);
DigitalOut bms_fault_pin(ACC_BMS_FAULT);
DigitalOut precharge_control_pin(ACC_PRECHARGE_CONTROL);


AnalogIn current_vref_pin(ACC_BUFFERED_C_VREF);
AnalogIn current_sense_pin(ACC_BUFFERED_C_OUT);
AnalogIn glv_voltage_pin(ACC_GLV_VOLTAGE);

bool prechargeDone = false;


int main() {

printf("main\n");
initIO();
printf("initIO\n");
Expand All @@ -43,6 +64,8 @@ int main() {
Timer t;
t.start();
while (1) {
int glv_voltage = glv_voltage_pin * 18530; // in mV
//printf("GLV voltage: %d mV\n", glv);

while (!canqueue.empty()) {
CANMessage msg;
Expand All @@ -58,4 +81,20 @@ int main() {

void initIO() {
canBus = new CAN(BMS_PIN_CAN_RX, BMS_PIN_CAN_TX, BMS_CAN_FREQUENCY);
canBus->attach(canRX);


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
precharge_control_pin = 0; // positive AIR open at start

}

void canRX() {
CANMessage msg;

if (canBus->read(msg)) {
canqueue.push(msg);
}
}

0 comments on commit 7f46a08

Please sign in to comment.