Skip to content

Commit 9e2f400

Browse files
committed
Add debounce for contactor check error
1 parent 298257f commit 9e2f400

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

warp/contactor_check.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ void contactor_check_tick(void) {
120120
}
121121
#ifdef HAS_HARDWARE_VERSION
122122
} else {
123-
// TODO: Do we need debouce here?
124123
const bool check_n_l1 = XMC_GPIO_GetInput(CONTACTOR_CHECK_FB1_PIN); // low = contactor active, high = contactor not active
125124
const bool check_l2_l3 = XMC_GPIO_GetInput(CONTACTOR_CHECK_FB2_PIN); // low = contactor active, high = contactor not active
126-
const bool check_pe = XMC_GPIO_GetInput(CONTACTOR_CHECK_PE_PIN); // low = PE check OK, high = PE check fail
125+
const bool check_pe = XMC_GPIO_GetInput(CONTACTOR_CHECK_PE_PIN); // 50hz = PE check OK, constant = PE check fail
127126

128127
const bool contactor = XMC_GPIO_GetInput(EVSE_CONTACTOR_PIN); // low = contactor aux active, high = contactor aux not active
129128
const bool phase_switch = XMC_GPIO_GetInput(EVSE_PHASE_SWITCH_PIN); // low = contactor aux active, high = contactor aux not active
@@ -146,11 +145,11 @@ void contactor_check_tick(void) {
146145
contactor_check.error = contactor_check.pe_edge_count > 10 ? 0 : 1;
147146
}
148147
contactor_check.pe_edge_count = 0;
149-
} else {
150-
// If we are not PE check interval, we keep that last value of PE check
151-
contactor_check.error = contactor_check.error & 1;
152148
}
153149

150+
// Only keep the last value of PE check
151+
contactor_check.error = contactor_check.error & 1;
152+
154153
// N/L1 and L2/L3 check
155154
uint8_t error = 0;
156155
switch((contactor << 3) | (phase_switch << 2) | (check_n_l1 << 1) | (check_l2_l3 << 0)) {
@@ -179,8 +178,18 @@ void contactor_check_tick(void) {
179178
default: error = 13; break; // Impossible
180179
}
181180

182-
// First bit used for independed PE check error
183-
contactor_check.error |= (error << 1);
181+
if(error == 0) {
182+
contactor_check.last_error_time = 0;
183+
} else {
184+
if(contactor_check.last_error_time == 0) {
185+
contactor_check.last_error_time = system_timer_get_ms();
186+
} else if(system_timer_is_time_elapsed_ms(contactor_check.last_error_time, 250)) { // 250ms error debounce
187+
contactor_check.error |= (error << 1);
188+
189+
// Make sure we reach here again if the error persists
190+
contactor_check.last_error_time = system_timer_get_ms() - 251;
191+
}
192+
}
184193

185194
// The data that we send to the Brick uses "active high", so we invert the inputs here
186195
contactor_check.state = (!check_n_l1) | ((!check_l2_l3) << 1) | ((!(contactor_check.error & 1)) << 2) | ((!contactor) << 3) | ((!phase_switch) << 4);

warp/contactor_check.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ typedef struct {
5050

5151
bool pe_last_value;
5252

53-
uint8_t last_error;
54-
uint8_t last_error_time;
53+
uint32_t last_error_time;
5554
} ContactorCheck;
5655

5756
extern ContactorCheck contactor_check;

0 commit comments

Comments
 (0)