Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions HVC/firmware/Core/Src/hvc_state_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void update_state_machine(bool any_faults) {
// Transition to precharging
precharge_start_time = current_time;
current_state = HVC_STATE_PRECHARGING;
} else if (is_charge_enable_active()) {
} else if (is_shutdown_closed() && is_charge_enable_active()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The function is_shutdown_closed() contains stateful debouncing logic (lines 193-212) that increments a counter on every call if the input differs from the current state. By adding this call here, it is now called twice within the HVC_STATE_NOT_ENERGIZED case (once at line 69 and once at line 73). This causes the debounce logic to process multiple times per task cycle, effectively reducing the debounce period and making the system more susceptible to noise.

Furthermore, the logic is redundant as is_shutdown_closed() is checked in both branches. It is highly recommended to capture the result of is_shutdown_closed() into a local variable at the start of update_state_machine() and use that variable throughout the function to ensure a consistent state snapshot and correct debouncing behavior.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to what Gemini suggested, consider creating a variable bool is_shutdown_closed_var = is_shutdown_closed() or move the debouncing out of the function

// Transition to charging precharge
precharge_start_time = current_time;
current_state = HVC_STATE_CHARGING_PRECHARGING;
Expand Down Expand Up @@ -133,15 +133,15 @@ void update_state_machine(bool any_faults) {
}

// Check if charge enable released
if (!is_charge_enable_active()) {
if (!is_charge_enable_active() || !is_shutdown_closed()) { //false = not charging
set_positive_contactor(false);
current_state = HVC_STATE_NOT_ENERGIZED;
}
break;

case HVC_STATE_CHARGING:
// Stay in charging while charge enable is active
if (!is_charge_enable_active()) {
if (!is_charge_enable_active() || !is_shutdown_closed()) {
set_positive_contactor(false);
current_state = HVC_STATE_NOT_ENERGIZED;
}
Expand Down
Loading