Define our own pins for automatic precharge. Add option to use NO contactor for inverter disconnect.

This commit is contained in:
mvgalen 2025-04-15 21:50:24 +02:00
parent 0b615b168d
commit bc1a75a5b6
3 changed files with 44 additions and 29 deletions

View file

@ -12,8 +12,24 @@
#define Precharge_default_PWM_Freq 11000 #define Precharge_default_PWM_Freq 11000
#define Precharge_min_PWM_Freq 5000 #define Precharge_min_PWM_Freq 5000
#define Precharge_max_PWM_Freq 34000 #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_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 #define PWM_Precharge_Channel 0
unsigned long prechargeStartTime = 0; unsigned long prechargeStartTime = 0;
@ -28,10 +44,10 @@ void init_precharge_control() {
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.printf("Precharge control initialised\n"); logging.printf("Precharge control initialised\n");
#endif #endif
pinMode(PRECHARGE_PIN, OUTPUT); pinMode(HIA4V1_PIN, OUTPUT);
digitalWrite(PRECHARGE_PIN, LOW); digitalWrite(HIA4V1_PIN, LOW);
pinMode(POSITIVE_CONTACTOR_PIN, OUTPUT); pinMode(INVERTER_DISCONNECT_CONTACTOR_PIN, OUTPUT);
digitalWrite(POSITIVE_CONTACTOR_PIN, LOW); digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, ON);
} }
// Main functions // Main functions
@ -46,33 +62,24 @@ void handle_precharge_control() {
#endif #endif
#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) { switch (datalayer.system.status.precharge_status) {
case AUTO_PRECHARGE_IDLE: 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) { if (datalayer.system.settings.start_precharging) {
datalayer.system.status.precharge_status = AUTO_PRECHARGE_START; datalayer.system.status.precharge_status = AUTO_PRECHARGE_START;
} }
#endif
break; break;
case AUTO_PRECHARGE_START: case AUTO_PRECHARGE_START:
freq = Precharge_default_PWM_Freq; freq = Precharge_default_PWM_Freq;
ledcAttachChannel(PRECHARGE_PIN, freq, PWM_Res, PWM_Precharge_Channel); ledcAttachChannel(HIA4V1_PIN, freq, Precharge_PWM_Res, PWM_Precharge_Channel);
ledcWriteTone(PRECHARGE_PIN, freq); // Set frequency and set dutycycle to 50% ledcWriteTone(HIA4V1_PIN, freq); // Set frequency and set dutycycle to 50%
prechargeStartTime = currentTime; prechargeStartTime = currentTime;
datalayer.system.status.precharge_status = AUTO_PRECHARGE_PRECHARGING; datalayer.system.status.precharge_status = AUTO_PRECHARGE_PRECHARGING;
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.printf("Precharge: Starting sequence\n"); logging.printf("Precharge: Starting sequence\n");
#endif #endif
digitalWrite(POSITIVE_CONTACTOR_PIN, HIGH); digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, OFF);
break; 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, logging.printf("Precharge: Target: %d V Extern: %d V New frequency: %u Hz\n", target_voltage / 10,
external_voltage / 10, freq); external_voltage / 10, freq);
#endif #endif
ledcWriteTone(PRECHARGE_PIN, freq); ledcWriteTone(HIA4V1_PIN, freq);
} }
if ((datalayer.battery.status.real_bms_status != BMS_STANDBY && if ((datalayer.battery.status.real_bms_status != BMS_STANDBY &&
datalayer.battery.status.real_bms_status != BMS_ACTIVE) || datalayer.battery.status.real_bms_status != BMS_ACTIVE) ||
datalayer.battery.status.bms_status != ACTIVE || datalayer.system.settings.equipment_stop_active) { datalayer.battery.status.bms_status != ACTIVE || datalayer.system.settings.equipment_stop_active) {
pinMode(PRECHARGE_PIN, OUTPUT); pinMode(HIA4V1_PIN, OUTPUT);
digitalWrite(PRECHARGE_PIN, LOW); digitalWrite(HIA4V1_PIN, LOW);
digitalWrite(POSITIVE_CONTACTOR_PIN, LOW); digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, ON);
datalayer.system.status.precharge_status = AUTO_PRECHARGE_IDLE; datalayer.system.status.precharge_status = AUTO_PRECHARGE_IDLE;
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.printf("Precharge: Disabling Precharge bms not standby/active or equipment stop\n"); logging.printf("Precharge: Disabling Precharge bms not standby/active or equipment stop\n");
#endif #endif
} else if (currentTime - prechargeStartTime >= MAX_PRECHARGE_TIME_MS || } else if (currentTime - prechargeStartTime >= MAX_PRECHARGE_TIME_MS ||
datalayer.battery.status.real_bms_status == BMS_FAULT) { datalayer.battery.status.real_bms_status == BMS_FAULT) {
pinMode(PRECHARGE_PIN, OUTPUT); pinMode(HIA4V1_PIN, OUTPUT);
digitalWrite(PRECHARGE_PIN, LOW); digitalWrite(HIA4V1_PIN, LOW);
digitalWrite(POSITIVE_CONTACTOR_PIN, LOW); digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, ON);
datalayer.system.status.precharge_status = AUTO_PRECHARGE_OFF; datalayer.system.status.precharge_status = AUTO_PRECHARGE_OFF;
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.printf("Precharge: Disabled (timeout reached / BMS fault) -> AUTO_PRECHARGE_OFF\n"); logging.printf("Precharge: Disabled (timeout reached / BMS fault) -> AUTO_PRECHARGE_OFF\n");
@ -129,9 +136,9 @@ void handle_precharge_control() {
// Add event // Add event
} else if (datalayer.system.status.battery_allows_contactor_closing) { } else if (datalayer.system.status.battery_allows_contactor_closing) {
pinMode(PRECHARGE_PIN, OUTPUT); pinMode(HIA4V1_PIN, OUTPUT);
digitalWrite(PRECHARGE_PIN, LOW); digitalWrite(HIA4V1_PIN, LOW);
digitalWrite(POSITIVE_CONTACTOR_PIN, LOW); digitalWrite(INVERTER_DISCONNECT_CONTACTOR_PIN, ON);
datalayer.system.status.precharge_status = AUTO_PRECHARGE_COMPLETED; datalayer.system.status.precharge_status = AUTO_PRECHARGE_COMPLETED;
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.printf("Precharge: Disabled (contacts closed) -> COMPLETED\n"); 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.status.inverter_allows_contactor_closing ||
datalayer.system.settings.equipment_stop_active || datalayer.battery.status.bms_status != FAULT) { datalayer.system.settings.equipment_stop_active || datalayer.battery.status.bms_status != FAULT) {
datalayer.system.status.precharge_status = AUTO_PRECHARGE_IDLE; datalayer.system.status.precharge_status = AUTO_PRECHARGE_IDLE;
pinMode(PRECHARGE_PIN, OUTPUT); pinMode(HIA4V1_PIN, OUTPUT);
digitalWrite(PRECHARGE_PIN, LOW); digitalWrite(HIA4V1_PIN, LOW);
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.printf("Precharge: equipment stop activated -> IDLE\n"); logging.printf("Precharge: equipment stop activated -> IDLE\n");
#endif #endif

View file

@ -53,6 +53,10 @@
#define PRECHARGE_PIN 25 #define PRECHARGE_PIN 25
#define BMS_POWER 18 // Note, this pin collides with CAN add-ons and Chademo #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 // SMA CAN contactor pins
#define INVERTER_CONTACTOR_ENABLE_PIN 5 #define INVERTER_CONTACTOR_ENABLE_PIN 5

View file

@ -51,6 +51,10 @@ GPIOs on extra header
#define PRECHARGE_PIN 25 #define PRECHARGE_PIN 25
#define BMS_POWER 23 #define BMS_POWER 23
// Automatic precharging
#define HIA4V1_PIN 19
#define INVERTER_DISCONNECT_CONTACTOR_PIN 25
// SMA CAN contactor pins // SMA CAN contactor pins
#define INVERTER_CONTACTOR_ENABLE_PIN 19 #define INVERTER_CONTACTOR_ENABLE_PIN 19