diff --git a/Software/src/communication/precharge_control/precharge_control.cpp b/Software/src/communication/precharge_control/precharge_control.cpp index f2942afc..0de3c55b 100644 --- a/Software/src/communication/precharge_control/precharge_control.cpp +++ b/Software/src/communication/precharge_control/precharge_control.cpp @@ -12,8 +12,24 @@ #define Precharge_default_PWM_Freq 11000 #define Precharge_min_PWM_Freq 5000 #define Precharge_max_PWM_Freq 34000 -#define PWM_Res 8 +#define Precharge_PWM_Res 8 + +#define PWM_Freq 20000 // 20 kHz frequency, beyond audible range +#define PWM_Res 10 // 10 Bit resolution 0 to 1023, maps 'nicely' to 0% 100% +#define PWM_HOLD_DUTY 250 #define PWM_OFF_DUTY 0 +#define PWM_ON_DUTY 1023 + +#define ON 0 //Normally closed contactors use inverted logic +#define OFF 1 //Normally closed contactors use inverted logic + +#ifdef INVERTER_DISCONNECT_CONTACTOR_IS_NO +#undef ON +#define ON 1 +#undef OFF +#define OFF 0 +#endif //INVERTER_DISCONNECT_CONTACTOR_IS_NO + #define PWM_Precharge_Channel 0 unsigned long prechargeStartTime = 0; @@ -28,10 +44,10 @@ void init_precharge_control() { #ifdef DEBUG_LOG logging.printf("Precharge control initialised\n"); #endif - pinMode(PRECHARGE_PIN, OUTPUT); - digitalWrite(PRECHARGE_PIN, LOW); - pinMode(POSITIVE_CONTACTOR_PIN, OUTPUT); - digitalWrite(POSITIVE_CONTACTOR_PIN, LOW); + pinMode(HIA4V1_PIN, OUTPUT); + digitalWrite(HIA4V1_PIN, LOW); + pinMode(INVERTER_DISCONNECT_CONTACTOR_PIN, OUTPUT); + digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, ON); } // Main functions @@ -46,33 +62,24 @@ void handle_precharge_control() { #endif #endif - // Handle actual state machine. This first turns on Negative, then Precharge, then Positive, and finally turns OFF precharge switch (datalayer.system.status.precharge_status) { case AUTO_PRECHARGE_IDLE: -#if 0 - if (datalayer.battery.status.bms_status != FAULT && datalayer.battery.status.real_bms_status == BMS_STANDBY && - /*datalayer.system.status.inverter_allows_contactor_closing &&*/ - !datalayer.system.settings.equipment_stop_active) { - datalayer.system.status.precharge_status = AUTO_PRECHARGE_START; - } -#else if (datalayer.system.settings.start_precharging) { datalayer.system.status.precharge_status = AUTO_PRECHARGE_START; } -#endif break; case AUTO_PRECHARGE_START: freq = Precharge_default_PWM_Freq; - ledcAttachChannel(PRECHARGE_PIN, freq, PWM_Res, PWM_Precharge_Channel); - ledcWriteTone(PRECHARGE_PIN, freq); // Set frequency and set dutycycle to 50% + ledcAttachChannel(HIA4V1_PIN, freq, Precharge_PWM_Res, PWM_Precharge_Channel); + ledcWriteTone(HIA4V1_PIN, freq); // Set frequency and set dutycycle to 50% prechargeStartTime = currentTime; datalayer.system.status.precharge_status = AUTO_PRECHARGE_PRECHARGING; #ifdef DEBUG_LOG logging.printf("Precharge: Starting sequence\n"); #endif - digitalWrite(POSITIVE_CONTACTOR_PIN, HIGH); + digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, OFF); break; @@ -103,24 +110,24 @@ void handle_precharge_control() { logging.printf("Precharge: Target: %d V Extern: %d V New frequency: %u Hz\n", target_voltage / 10, external_voltage / 10, freq); #endif - ledcWriteTone(PRECHARGE_PIN, freq); + ledcWriteTone(HIA4V1_PIN, freq); } if ((datalayer.battery.status.real_bms_status != BMS_STANDBY && datalayer.battery.status.real_bms_status != BMS_ACTIVE) || datalayer.battery.status.bms_status != ACTIVE || datalayer.system.settings.equipment_stop_active) { - pinMode(PRECHARGE_PIN, OUTPUT); - digitalWrite(PRECHARGE_PIN, LOW); - digitalWrite(POSITIVE_CONTACTOR_PIN, LOW); + pinMode(HIA4V1_PIN, OUTPUT); + digitalWrite(HIA4V1_PIN, LOW); + digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, ON); datalayer.system.status.precharge_status = AUTO_PRECHARGE_IDLE; #ifdef DEBUG_LOG logging.printf("Precharge: Disabling Precharge bms not standby/active or equipment stop\n"); #endif } else if (currentTime - prechargeStartTime >= MAX_PRECHARGE_TIME_MS || datalayer.battery.status.real_bms_status == BMS_FAULT) { - pinMode(PRECHARGE_PIN, OUTPUT); - digitalWrite(PRECHARGE_PIN, LOW); - digitalWrite(POSITIVE_CONTACTOR_PIN, LOW); + pinMode(HIA4V1_PIN, OUTPUT); + digitalWrite(HIA4V1_PIN, LOW); + digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, ON); datalayer.system.status.precharge_status = AUTO_PRECHARGE_OFF; #ifdef DEBUG_LOG logging.printf("Precharge: Disabled (timeout reached / BMS fault) -> AUTO_PRECHARGE_OFF\n"); @@ -129,9 +136,9 @@ void handle_precharge_control() { // Add event } else if (datalayer.system.status.battery_allows_contactor_closing) { - pinMode(PRECHARGE_PIN, OUTPUT); - digitalWrite(PRECHARGE_PIN, LOW); - digitalWrite(POSITIVE_CONTACTOR_PIN, LOW); + pinMode(HIA4V1_PIN, OUTPUT); + digitalWrite(HIA4V1_PIN, LOW); + digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, ON); datalayer.system.status.precharge_status = AUTO_PRECHARGE_COMPLETED; #ifdef DEBUG_LOG logging.printf("Precharge: Disabled (contacts closed) -> COMPLETED\n"); @@ -153,8 +160,8 @@ void handle_precharge_control() { !datalayer.system.status.inverter_allows_contactor_closing || datalayer.system.settings.equipment_stop_active || datalayer.battery.status.bms_status != FAULT) { datalayer.system.status.precharge_status = AUTO_PRECHARGE_IDLE; - pinMode(PRECHARGE_PIN, OUTPUT); - digitalWrite(PRECHARGE_PIN, LOW); + pinMode(HIA4V1_PIN, OUTPUT); + digitalWrite(HIA4V1_PIN, LOW); #ifdef DEBUG_LOG logging.printf("Precharge: equipment stop activated -> IDLE\n"); #endif diff --git a/Software/src/devboard/hal/hw_lilygo.h b/Software/src/devboard/hal/hw_lilygo.h index 887f925f..8dbf4f74 100644 --- a/Software/src/devboard/hal/hw_lilygo.h +++ b/Software/src/devboard/hal/hw_lilygo.h @@ -53,6 +53,10 @@ #define PRECHARGE_PIN 25 #define BMS_POWER 18 // Note, this pin collides with CAN add-ons and Chademo +// Automatic precharging +#define HIA4V1_PIN 25 +#define INVERTER_DISCONNECT_CONTACTOR_PIN 32 + // SMA CAN contactor pins #define INVERTER_CONTACTOR_ENABLE_PIN 5 diff --git a/Software/src/devboard/hal/hw_stark.h b/Software/src/devboard/hal/hw_stark.h index 064db9da..e5c0f986 100644 --- a/Software/src/devboard/hal/hw_stark.h +++ b/Software/src/devboard/hal/hw_stark.h @@ -51,6 +51,10 @@ GPIOs on extra header #define PRECHARGE_PIN 25 #define BMS_POWER 23 +// Automatic precharging +#define HIA4V1_PIN 19 +#define INVERTER_DISCONNECT_CONTACTOR_PIN 25 + // SMA CAN contactor pins #define INVERTER_CONTACTOR_ENABLE_PIN 19