Skip to content

Commit

Permalink
Halt only CAN sending towards battery while BMSResetInProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
dalathegreat committed Jan 22, 2025
1 parent 854e909 commit cc97642
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
8 changes: 6 additions & 2 deletions Software/src/communication/can/comm_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ void init_CAN() {
// Transmit functions
void transmit_can() {
if (!allowed_to_send_CAN) {
return;
return; //Global block of CAN messages
}

if (!datalayer.system.status.BMS_reset_in_progress) {
//Transmitting towards battery is halted while BMS is being reset
transmit_can_battery();
}
transmit_can_battery();

#ifdef CAN_INVERTER_SELECTED
transmit_can_inverter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ unsigned long currentTime = 0;
unsigned long lastPowerRemovalTime = 0;
const unsigned long powerRemovalInterval = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const unsigned long powerRemovalDuration = 30000; // 30 seconds in milliseconds
bool isBMSResetActive = false;

void set(uint8_t pin, bool direction, uint32_t pwm_freq = 0xFFFF) {
#ifdef PWM_CONTACTOR_CONTROL
Expand Down Expand Up @@ -256,48 +255,38 @@ void handle_BMSpower() {
#endif //PERIODIC_BMS_RESET

// If power has been removed for 30 seconds, restore the power and resume the emulator
if (isBMSResetActive && currentTime - lastPowerRemovalTime >= powerRemovalDuration) {
if (datalayer.system.status.BMS_reset_in_progress && currentTime - lastPowerRemovalTime >= powerRemovalDuration) {
// Reapply power to the BMS
digitalWrite(BMS_POWER, HIGH);
#ifdef BMS_2_POWER
digitalWrite(BMS_2_POWER, HIGH); // Same for battery 2
#endif

//Resume the battery pause and CAN communication
//Resume from the power pause
setBatteryPause(false, false, false, false);

isBMSResetActive = false; // Reset the power removal flag
datalayer.system.status.BMS_reset_in_progress = false; // Reset the power removal flag
}
#endif //defined(PERIODIC_BMS_RESET) || defined(REMOTE_BMS_RESET)
}

void start_bms_reset() {
#if defined(PERIODIC_BMS_RESET) || defined(REMOTE_BMS_RESET)
if (!isBMSResetActive) {
if (!datalayer.system.status.BMS_reset_in_progress) {
lastPowerRemovalTime = currentTime; // Record the time when BMS reset was started

// Set a flag to let the rest of the system know we are cutting power to the BMS.
// The battery CAN sending routine will then know not to try to send anything towards battery while active
datalayer.system.status.BMS_reset_in_progress = true;

// Set emulator state to paused (Max Charge/Discharge = 0 & CAN = stop)
// TODO: We try to keep contactors engaged during this pause, and just ramp power down to 0.
// If this turns out to not work properly, set also the third option to true to open contactors
setBatteryPause(true, true, false, false);
// We try to keep contactors engaged during this pause, and just ramp power down to 0.
setBatteryPause(true, false, false, false);

digitalWrite(BMS_POWER, LOW); // Remove power by setting the BMS power pin to LOW
#ifdef BMS_2_POWER
digitalWrite(BMS_2_POWER, LOW); // Same for battery 2
#endif

isBMSResetActive = true; // Set a flag to indicate power removal is active
}

// If power has been removed for 30 seconds, restore the power and resume the emulator
if (isBMSResetActive && currentTime - lastPowerRemovalTime >= powerRemovalDuration) {
// Reapply power to the BMS
digitalWrite(BMS_POWER, HIGH);
#ifdef BMS_2_POWER
digitalWrite(BMS_2_POWER, HIGH); // Same for battery 2
#endif

//Resume the battery pause and CAN communication
setBatteryPause(false, false, false, false);

isBMSResetActive = false; // Reset the power removal flag
}
#endif //PERIODIC_BMS_RESET
#endif //defined(PERIODIC_BMS_RESET) || defined(REMOTE_BMS_RESET)
}
2 changes: 2 additions & 0 deletions Software/src/datalayer/datalayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ typedef struct {
/** True if the contactor controlled by battery-emulator is closed. Determined by check_interconnect_available(); if voltage is OK */
bool contactors_battery2_engaged = false;
#endif
/** True if the BMS is being reset, by cutting power towards it */
bool BMS_reset_in_progress = false;
} DATALAYER_SYSTEM_STATUS_TYPE;

typedef struct {
Expand Down

0 comments on commit cc97642

Please sign in to comment.