From cb4a830a5730df3ce6fefb7c313fc1f3b38d7d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20=C3=96ster?= Date: Fri, 15 Nov 2024 20:54:37 +0200 Subject: [PATCH] Add second contactor closing function --- Software/Software.ino | 53 +++++++++++++++++++----------- Software/USER_SETTINGS.h | 9 +++-- Software/src/devboard/hal/hw_3LB.h | 6 ++-- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/Software/Software.ino b/Software/Software.ino index bba1d277..234fd5ab 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -123,11 +123,11 @@ State contactorStatus = DISCONNECTED; #define ON 1 #define OFF 0 -#ifdef NC_CONTACTORS //Normally closed contactors use inverted logic - #undef ON - #define ON 0 - #undef OFF - #define OFF 1 +#ifdef NC_CONTACTORS //Normally closed contactors use inverted logic +#undef ON +#define ON 0 +#undef OFF +#define OFF 1 #endif #define MAX_ALLOWED_FAULT_TICKS 1000 @@ -149,18 +149,16 @@ unsigned long negativeStartTime = 0; unsigned long timeSpentInFaultedMode = 0; #endif -void set(uint8_t pin, bool direction, uint32_t pwm_freq = 0xFFFFFFFFFF){ - #ifdef PWM_CONTACTOR_CONTROL - if(pwm_freq != 0xFFFFFFFFFF) - { +void set(uint8_t pin, bool direction, uint32_t pwm_freq = 0xFFFFFFFFFF) { +#ifdef PWM_CONTACTOR_CONTROL + if (pwm_freq != 0xFFFFFFFFFF) { ledcWrite(pin, pwm_freq); return; } - #endif - if(direction == 1){ +#endif + if (direction == 1) { digitalWrite(pin, HIGH); - } - else{ // 0 + } else { // 0 digitalWrite(pin, LOW); } } @@ -528,7 +526,7 @@ void init_contactors() { NEGATIVE_PWM_Ch); // Setup PWM Channel Frequency and Resolution ledcWrite(POSITIVE_CONTACTOR_PIN, PWM_OFF_DUTY); // Set Positive PWM to 0% ledcWrite(NEGATIVE_CONTACTOR_PIN, PWM_OFF_DUTY); // Set Negative PWM to 0% -#else //Normal CONTACTOR_CONTROL +#else //Normal CONTACTOR_CONTROL pinMode(POSITIVE_CONTACTOR_PIN, OUTPUT); set(POSITIVE_CONTACTOR_PIN, OFF); pinMode(NEGATIVE_CONTACTOR_PIN, OUTPUT); @@ -536,12 +534,18 @@ void init_contactors() { #endif pinMode(PRECHARGE_PIN, OUTPUT); set(PRECHARGE_PIN, OFF); -#endif //CONTACTOR_CONTROL +#endif //CONTACTOR_CONTROL +#ifdef CONTACTOR_CONTROL_DOUBLE_BATTERY + pinMode(SECOND_POSITIVE_CONTACTOR_PIN, OUTPUT); + set(SECOND_POSITIVE_CONTACTOR_PIN, OFF); + pinMode(SECOND_NEGATIVE_CONTACTOR_PIN, OUTPUT); + set(SECOND_NEGATIVE_CONTACTOR_PIN, OFF); +#endif //CONTACTOR_CONTROL_DOUBLE_BATTERY // Init BMS contactor #ifdef HW_STARK // TODO: Rewrite this so LilyGo can also handle this BMS contactor pinMode(BMS_POWER, OUTPUT); - set(BMS_POWER, ON); -#endif //HW_STARK + digitalWrite(BMS_POWER, HIGH); +#endif //HW_STARK } void init_rs485() { @@ -797,9 +801,18 @@ void handle_contactors() { // In case the inverter requests contactors to open, set the state accordingly if (contactorStatus == COMPLETED) { - if (!datalayer.system.status.inverter_allows_contactor_closing){ + if (!datalayer.system.status.inverter_allows_contactor_closing) { contactorStatus = DISCONNECTED; } +#ifdef CONTACTOR_CONTROL_DOUBLE_BATTERY + if (datalayer.system.status.battery2_allows_contactor_closing) { + set(SECOND_NEGATIVE_CONTACTOR_PIN, ON); + set(SECOND_POSITIVE_CONTACTOR_PIN, ON); + } else { + set(SECOND_NEGATIVE_CONTACTOR_PIN, OFF); + set(SECOND_POSITIVE_CONTACTOR_PIN, OFF); + } +#endif //CONTACTOR_CONTROL_DOUBLE_BATTERY // Skip running the state machine below if it has already completed return; } @@ -831,8 +844,8 @@ void handle_contactors() { case PRECHARGE_OFF: if (currentTime - negativeStartTime >= POSITIVE_CONTACTOR_TIME_MS) { set(PRECHARGE_PIN, OFF); - set(NEGATIVE_CONTACTOR_PIN, PWM_HOLD_DUTY); - set(POSITIVE_CONTACTOR_PIN, PWM_HOLD_DUTY); + set(NEGATIVE_CONTACTOR_PIN, ON, PWM_HOLD_DUTY); + set(POSITIVE_CONTACTOR_PIN, ON, PWM_HOLD_DUTY); contactorStatus = COMPLETED; datalayer.system.status.contactor_control_closed = true; } diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index be9dfece..392bd661 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -53,13 +53,16 @@ //#define HW_STARK //#define HW_3LB +/* Contactor settings. If you have a battery that does not activate contactors via CAN, configure this section */ +//#define CONTACTOR_CONTROL //Enable this line to have the emulator handle automatic precharge/contactor+/contactor- closing sequence (See wiki for pins) +//#define CONTACTOR_CONTROL_DOUBLE_BATTERY //Enable this line to have the emulator hardware control secondary set of contactors for double battery setups (See wiki for pins) +//#define PWM_CONTACTOR_CONTROL //Enable this line to use PWM for CONTACTOR_CONTROL, which lowers power consumption and heat generation. CONTACTOR_CONTROL must be enabled. +//#define NC_CONTACTORS //Enable this line to control normally closed contactors. CONTACTOR_CONTROL must be enabled for this option. Extremely rare setting! + /* Other options */ //#define DEBUG_VIA_USB //Enable this line to have the USB port output serial diagnostic data while program runs (WARNING, raises CPU load, do not use for production) //#define DEBUG_CAN_DATA //Enable this line to print incoming/outgoing CAN & CAN-FD messages to USB serial (WARNING, raises CPU load, do not use for production) //#define INTERLOCK_REQUIRED //Nissan LEAF specific setting, if enabled requires both high voltage conenctors to be seated before starting -//#define CONTACTOR_CONTROL //Enable this line to have pins 25,32,33 handle automatic precharge/contactor+/contactor- closing sequence -//#define PWM_CONTACTOR_CONTROL //Enable this line to use PWM for CONTACTOR_CONTROL, which lowers power consumption and heat generation. CONTACTOR_CONTROL must be enabled. -//#define NC_CONTACTORS //Enable this line to control normally closed contactors. CONTACTOR_CONTROL must be enabled for this option. Extremely rare setting! //#define DUAL_CAN //Enable this line to activate an isolated secondary CAN Bus using add-on MCP2515 chip (Needed for some inverters / double battery) #define CRYSTAL_FREQUENCY_MHZ 8 //DUAL_CAN option, what is your MCP2515 add-on boards crystal frequency? //#define CAN_FD //Enable this line to activate an isolated secondary CAN-FD bus using add-on MCP2518FD chip / Native CANFD on Stark board diff --git a/Software/src/devboard/hal/hw_3LB.h b/Software/src/devboard/hal/hw_3LB.h index 137d83f3..a54de2a4 100644 --- a/Software/src/devboard/hal/hw_3LB.h +++ b/Software/src/devboard/hal/hw_3LB.h @@ -52,9 +52,9 @@ #define NEGATIVE_CONTACTOR_PIN 33 #define PRECHARGE_PIN 25 -#define 2ND_POSITIVE_CONTACTOR_PIN 13 -#define 2ND_NEGATIVE_CONTACTOR_PIN 16 -#define 2ND_PRECHARGE_PIN 18 +#define SECOND_POSITIVE_CONTACTOR_PIN 13 +#define SECOND_NEGATIVE_CONTACTOR_PIN 16 +#define SECOND_PRECHARGE_PIN 18 // SMA CAN contactor pins #define INVERTER_CONTACTOR_ENABLE_PIN 36