Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius Claussen <[email protected]>
  • Loading branch information
corneliusclaussen committed Jan 25, 2024
1 parent 3ec2fc8 commit 990142e
Showing 1 changed file with 49 additions and 37 deletions.
86 changes: 49 additions & 37 deletions modules/EvseManager/Charger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ void Charger::run_state_machine() {
// run over state machine loop until currentState does not change anymore
do {
// If a state change happened or an error recovered during a state we reinitialize the state
bool initialize_state = (last_state_detect_state_change != currentState) ||
(last_error_prevent_charging_flag != error_prevent_charging_flag);
bool initialize_state = (last_state_detect_state_change not_eq currentState) or
(last_error_prevent_charging_flag not_eq error_prevent_charging_flag);

if (initialize_state) {
session_log.evse(false,
Expand Down Expand Up @@ -141,8 +141,9 @@ void Charger::run_state_machine() {
if (initialize_state) {
signal_event(types::evse_manager::SessionEventEnum::ReplugStarted);
// start timer in case we need to
if (ac_with_soc_timeout)
if (ac_with_soc_timeout) {
ac_with_soc_timer = 120000;
}
}
// simply wait here until BSP informs us that replugging was finished
break;
Expand Down Expand Up @@ -175,8 +176,9 @@ void Charger::run_state_machine() {
signal_event(types::evse_manager::SessionEventEnum::ReplugFinished);
} else {
// First user interaction was plug in of car? Start session here.
if (not session_active)
if (not session_active) {
start_session(false);
}
// External signal on MQTT
signal_event(types::evse_manager::SessionEventEnum::AuthRequired);
}
Expand Down Expand Up @@ -362,10 +364,11 @@ void Charger::run_state_machine() {
}
if (timeInCurrentState >= t_step_EF) {
session_log.evse(false, "Exit T_step_EF");
if (t_step_EF_return_pwm == 0.)
if (t_step_EF_return_pwm == 0.) {
pwm_off();
else
} else {
update_pwm_now(t_step_EF_return_pwm);
}
currentState = t_step_EF_return_state;
}
break;
Expand All @@ -377,10 +380,11 @@ void Charger::run_state_machine() {
}
if (timeInCurrentState >= t_step_X1) {
session_log.evse(false, "Exit T_step_X1");
if (t_step_X1_return_pwm == 0.)
if (t_step_X1_return_pwm == 0.) {
pwm_off();
else
} else {
update_pwm_now(t_step_X1_return_pwm);
}
currentState = t_step_X1_return_state;
}
break;
Expand Down Expand Up @@ -414,7 +418,7 @@ void Charger::run_state_machine() {
// In AC mode BASIC, iec_allow is sufficient. The same is true for HLC mode when nominal PWM is used as
// the car can do BASIC and HLC charging any time. In AC HLC with 5 percent mode, we need to wait for
// both iec_allow and hlc_allow.
if ((iec_allow_close_contactor and !hlc_use_5percent_current_session) ||
if ((iec_allow_close_contactor and !hlc_use_5percent_current_session) or
(iec_allow_close_contactor and hlc_allow_close_contactor and hlc_use_5percent_current_session)) {

signal_event(types::evse_manager::SessionEventEnum::ChargingStarted);
Expand Down Expand Up @@ -470,7 +474,7 @@ void Charger::run_state_machine() {
}

if (initialize_state) {
if (last_state != EvseState::PrepareCharging) {
if (last_state not_eq EvseState::PrepareCharging) {
signal_event(types::evse_manager::SessionEventEnum::ChargingResumed);
}

Expand All @@ -479,14 +483,16 @@ void Charger::run_state_machine() {

bsp->allow_power_on(true, types::evse_board_support::Reason::FullPowerCharging);
// make sure we are enabling PWM
if (hlc_use_5percent_current_session)
if (hlc_use_5percent_current_session) {
update_pwm_now_if_changed(PWM_5_PERCENT);
else
} else {
update_pwm_now_if_changed(ampere_to_duty_cycle(get_max_current_internal()));
}
} else {
// update PWM if it has changed and 5 seconds have passed since last update
if (!hlc_use_5percent_current_session)
if (!hlc_use_5percent_current_session) {
update_pwm_max_every_5seconds(ampere_to_duty_cycle(get_max_current_internal()));
}
}
}
break;
Expand Down Expand Up @@ -529,12 +535,14 @@ void Charger::run_state_machine() {
if (hlc_charging_terminate_pause == HlcTerminatePause::Terminate) {
// EV wants to terminate session
currentState = EvseState::StoppingCharging;
if (pwm_running)
if (pwm_running) {
pwm_off();
}
} else if (hlc_charging_terminate_pause == HlcTerminatePause::Pause) {
// EV wants an actual pause
if (pwm_running)
if (pwm_running) {
pwm_off();
}
}

} else {
Expand Down Expand Up @@ -576,15 +584,16 @@ void Charger::run_state_machine() {
case EvseState::WaitingForEnergy:
if (initialize_state) {
signal_event(types::evse_manager::SessionEventEnum::WaitingForEnergy);
if (!hlc_use_5percent_current_session)
if (!hlc_use_5percent_current_session) {
pwm_off();
}
}
break;

case EvseState::StoppingCharging:
if (initialize_state) {
bcb_toggle_reset();
if (transaction_active || session_active) {
if (transaction_active or session_active) {
signal_event(types::evse_manager::SessionEventEnum::StoppingCharging);
}

Expand Down Expand Up @@ -624,8 +633,9 @@ void Charger::run_state_machine() {
if (initialize_state) {
// Transaction may already be stopped when it was cancelled earlier.
// In that case, do not sent a second transactionFinished event.
if (transaction_active)
if (transaction_active) {
stop_transaction();
}
// We may come here from an error state, so a session was maybe not active.
if (session_active) {
stop_session();
Expand All @@ -639,7 +649,7 @@ void Charger::run_state_machine() {
currentState = EvseState::Idle;
break;
}
} while (last_state_detect_state_change != currentState);
} while (last_state_detect_state_change not_eq currentState);
}

void Charger::process_event(CPEvent cp_event) {
Expand Down Expand Up @@ -766,7 +776,7 @@ void Charger::process_cp_events_independent(CPEvent cp_event) {
}

void Charger::update_pwm_max_every_5seconds(float dc) {
if (dc != update_pwm_last_dc) {
if (dc not_eq update_pwm_last_dc) {
auto now = date::utc_clock::now();
auto timeSinceLastUpdate = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_pwm_update).count();
if (timeSinceLastUpdate >= 5000) {
Expand All @@ -789,7 +799,7 @@ void Charger::update_pwm_now(float dc) {
}

void Charger::update_pwm_now_if_changed(float dc) {
if (update_pwm_last_dc != dc) {
if (update_pwm_last_dc not_eq dc) {
update_pwm_now(dc);
}
}
Expand Down Expand Up @@ -1033,17 +1043,17 @@ bool Charger::get_authorized_pnc_ready_for_hlc() {
bool auth = false, ready = false;
std::scoped_lock lock(state_machine_mutex);
auth = (authorized and authorized_pnc);
ready = (currentState == EvseState::ChargingPausedEV) || (currentState == EvseState::ChargingPausedEVSE) ||
(currentState == EvseState::Charging) || (currentState == EvseState::WaitingForEnergy);
ready = (currentState == EvseState::ChargingPausedEV) or (currentState == EvseState::ChargingPausedEVSE) or
(currentState == EvseState::Charging) or (currentState == EvseState::WaitingForEnergy);
return (auth and ready);
}

bool Charger::get_authorized_eim_ready_for_hlc() {
bool auth = false, ready = false;
std::scoped_lock lock(state_machine_mutex);
auth = (authorized and !authorized_pnc);
ready = (currentState == EvseState::ChargingPausedEV) || (currentState == EvseState::ChargingPausedEVSE) ||
(currentState == EvseState::Charging) || (currentState == EvseState::WaitingForEnergy);
ready = (currentState == EvseState::ChargingPausedEV) or (currentState == EvseState::ChargingPausedEVSE) or
(currentState == EvseState::Charging) or (currentState == EvseState::WaitingForEnergy);
return (auth and ready);
}

Expand Down Expand Up @@ -1079,7 +1089,7 @@ bool Charger::deauthorize_internal() {
if (session_active) {
auto s = currentState;

if (s == EvseState::Disabled || s == EvseState::Idle || s == EvseState::WaitingForAuthentication) {
if (s == EvseState::Disabled or s == EvseState::Idle or s == EvseState::WaitingForAuthentication) {

// We can safely remove auth as it is not in use right now
if (!authorized) {
Expand All @@ -1096,7 +1106,7 @@ bool Charger::deauthorize_internal() {

bool Charger::disable(int connector_id) {
std::scoped_lock lock(state_machine_mutex);
if (connector_id != 0) {
if (connector_id not_eq 0) {
connector_enabled = false;
}
currentState = EvseState::Disabled;
Expand All @@ -1107,7 +1117,7 @@ bool Charger::disable(int connector_id) {
bool Charger::enable(int connector_id) {
std::scoped_lock lock(state_machine_mutex);

if (connector_id != 0) {
if (connector_id not_eq 0) {
connector_enabled = true;
}

Expand Down Expand Up @@ -1179,8 +1189,8 @@ float Charger::get_max_current() {
float Charger::get_max_current_internal() {
auto maxc = max_current;

if (connector_type == types::evse_board_support::Connector_type::IEC62196Type2Socket and max_current_cable < maxc and
currentState != EvseState::Idle) {
if (connector_type == types::evse_board_support::Connector_type::IEC62196Type2Socket and
max_current_cable < maxc and currentState not_eq EvseState::Idle) {
maxc = max_current_cable;
}

Expand All @@ -1199,15 +1209,16 @@ void Charger::check_soft_over_current() {
float limit = (get_max_current_internal() + soft_over_current_measurement_noise_A) *
(1. + soft_over_current_tolerance_percent / 100.);

if (current_drawn_by_vehicle[0] > limit || current_drawn_by_vehicle[1] > limit || current_drawn_by_vehicle[2] > limit) {
if (current_drawn_by_vehicle[0] > limit or current_drawn_by_vehicle[1] > limit or
current_drawn_by_vehicle[2] > limit) {
if (!over_current) {
over_current = true;
// timestamp when over current happend first
last_over_current_event = date::utc_clock::now();
session_log.evse(false,
fmt::format("Soft overcurrent event (L1:{}, L2:{}, L3:{}, limit {}), starting timer.",
current_drawn_by_vehicle[0], current_drawn_by_vehicle[1], current_drawn_by_vehicle[2],
limit));
current_drawn_by_vehicle[0], current_drawn_by_vehicle[1],
current_drawn_by_vehicle[2], limit));
}
} else {
over_current = false;
Expand All @@ -1216,8 +1227,9 @@ void Charger::check_soft_over_current() {
auto timeSinceOverCurrentStarted =
std::chrono::duration_cast<std::chrono::milliseconds>(now - last_over_current_event).count();
if (over_current and timeSinceOverCurrentStarted >= soft_over_current_timeout) {
auto errstr = fmt::format("Soft overcurrent event (L1:{}, L2:{}, L3:{}, limit {}) triggered",
current_drawn_by_vehicle[0], current_drawn_by_vehicle[1], current_drawn_by_vehicle[2], limit);
auto errstr =
fmt::format("Soft overcurrent event (L1:{}, L2:{}, L3:{}, limit {}) triggered", current_drawn_by_vehicle[0],
current_drawn_by_vehicle[1], current_drawn_by_vehicle[2], limit);
session_log.evse(false, errstr);
// raise the OC error
error_handling->raise_overcurrent_error(errstr);
Expand All @@ -1237,7 +1249,7 @@ bool Charger::power_available() {

void Charger::request_error_sequence() {
std::scoped_lock lock(state_machine_mutex);
if (currentState == EvseState::WaitingForAuthentication || currentState == EvseState::PrepareCharging) {
if (currentState == EvseState::WaitingForAuthentication or currentState == EvseState::PrepareCharging) {
t_step_EF_return_state = currentState;
currentState = EvseState::T_step_EF;
signal_slac_reset();
Expand Down Expand Up @@ -1403,7 +1415,7 @@ void Charger::bcb_toggle_detect_stop_pulse() {
// etc
bool Charger::bcb_toggle_detected() {
auto sequence_length = std::chrono::steady_clock::now() - hlc_ev_pause_start_of_bcb_sequence;
if (hlc_bcb_sequence_started and (sequence_length > TT_EVSE_VALD_TOGGLE || hlc_ev_pause_bcb_count >= 3)) {
if (hlc_bcb_sequence_started and (sequence_length > TT_EVSE_VALD_TOGGLE or hlc_ev_pause_bcb_count >= 3)) {
// no need to wait for further BCB toggles
hlc_ev_pause_bcb_count = 0;
return true;
Expand Down

0 comments on commit 990142e

Please sign in to comment.