From 1d55465cb5617984edfa4537c301c094e39718e9 Mon Sep 17 00:00:00 2001 From: Cornelius Claussen Date: Fri, 26 Jan 2024 13:46:57 +0100 Subject: [PATCH] use watchdogs in modules Signed-off-by: Cornelius Claussen --- modules/EnergyManager/EnergyManager.cpp | 3 +++ modules/EvseManager/Charger.cpp | 26 ++++++++++++------- modules/EvseManager/Charger.hpp | 6 ++++- modules/EvseManager/EvseManager.cpp | 9 +++++-- modules/EvseManager/IECStateMachine.hpp | 1 - .../EvseManager/energy_grid/energyImpl.cpp | 3 +++ 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/modules/EnergyManager/EnergyManager.cpp b/modules/EnergyManager/EnergyManager.cpp index 3c9b8f556..7fce3f116 100644 --- a/modules/EnergyManager/EnergyManager.cpp +++ b/modules/EnergyManager/EnergyManager.cpp @@ -25,12 +25,15 @@ void EnergyManager::ready() { // start thread to update energy optimization std::thread([this] { + auto watchdog = watchdog_supervisor.register_watchdog("Energy optimization thread", + std::chrono::seconds(config.update_interval * 10)); while (true) { globals.init(date::utc_clock::now(), config.schedule_interval_duration, config.schedule_total_duration, config.slice_ampere, config.slice_watt, config.debug, energy_flow_request); auto optimized_values = run_optimizer(energy_flow_request); enforce_limits(optimized_values); sleep(config.update_interval); + watchdog(); } }).detach(); } diff --git a/modules/EvseManager/Charger.cpp b/modules/EvseManager/Charger.cpp index 9041dd6a3..7d817d908 100644 --- a/modules/EvseManager/Charger.cpp +++ b/modules/EvseManager/Charger.cpp @@ -20,15 +20,15 @@ namespace module { Charger::Charger(const std::unique_ptr& bsp, const std::unique_ptr& error_handling, - const types::evse_board_support::Connector_type& connector_type) : - bsp(bsp), error_handling(error_handling), connector_type(connector_type) { - -#ifdef EVEREST_USE_BACKTRACES - Everest::install_backtrace_handler(); -#endif - - shared_context.connector_enabled = true; - shared_context.max_current = 6.0; + const types::evse_board_support::Connector_type& connector_type, + Everest::WatchdogSupervisor& _watchdog_supervisor) : + bsp(bsp), + error_handling(error_handling), + connector_type(connector_type), + watchdog_supervisor(_watchdog_supervisor) { + + connectorEnabled = true; + maxCurrent = 6.0; if (connector_type == types::evse_board_support::Connector_type::IEC62196Type2Socket) { shared_context.max_current_cable = bsp->read_pp_ampacity(); } @@ -82,7 +82,11 @@ Charger::~Charger() { pwm_F(); } -void Charger::main_thread() { +void Charger::mainThread() { + + // Register our watchdog for the main loop thread + auto watchdog = watchdog_supervisor.register_watchdog("Charger main loop", std::chrono::seconds(5)); + // Enable CP output bsp->enable(true); @@ -105,6 +109,8 @@ void Charger::main_thread() { // to be done on regular intervals independent from events) run_state_machine(); } + + watchdog(); } } diff --git a/modules/EvseManager/Charger.hpp b/modules/EvseManager/Charger.hpp index b5c137586..7e5dbf655 100644 --- a/modules/EvseManager/Charger.hpp +++ b/modules/EvseManager/Charger.hpp @@ -36,6 +36,8 @@ #include #include +#include + #include "ErrorHandling.hpp" #include "IECStateMachine.hpp" #include "scoped_lock_timeout.hpp" @@ -48,7 +50,8 @@ const std::string IEC62196Type2Socket = "IEC62196Type2Socket"; class Charger { public: Charger(const std::unique_ptr& bsp, const std::unique_ptr& error_handling, - const types::evse_board_support::Connector_type& connector_type); + const types::evse_board_support::Connector_type& connector_type, + Everest::WatchdogSupervisor& watchdog_supervisor); ~Charger(); enum class ChargeMode { @@ -299,6 +302,7 @@ class Charger { const std::unique_ptr& bsp; const std::unique_ptr& error_handling; const types::evse_board_support::Connector_type& connector_type; + Everest::WatchdogSupervisor& watchdog_supervisor; // constants static constexpr float CHARGER_ABSOLUTE_MAX_CURRENT{80.}; diff --git a/modules/EvseManager/EvseManager.cpp b/modules/EvseManager/EvseManager.cpp index fcfd12e3e..af1792d88 100644 --- a/modules/EvseManager/EvseManager.cpp +++ b/modules/EvseManager/EvseManager.cpp @@ -89,7 +89,8 @@ void EvseManager::ready() { hw_capabilities = r_bsp->call_get_hw_capabilities(); - charger = std::unique_ptr(new Charger(bsp, error_handling, hw_capabilities.connector_type)); + charger = + std::unique_ptr(new Charger(bsp, error_handling, hw_capabilities.connector_type, watchdog_supervisor)); if (r_connector_lock.size() > 0) { bsp->signal_lock.connect([this]() { r_connector_lock[0]->call_lock(); }); @@ -743,8 +744,12 @@ void EvseManager::ready() { } telemetryThreadHandle = std::thread([this]() { - while (not telemetryThreadHandle.shouldExit()) { + auto watchdog = watchdog_supervisor.register_watchdog("Telemetry thread", std::chrono::seconds(20)); + + while (!telemetryThreadHandle.shouldExit()) { sleep(10); + watchdog(); + auto p = get_latest_powermeter_data_billing(); Everest::TelemetryMap telemetry_data{{"timestamp", p.timestamp}, {"type", "power_meter"}, diff --git a/modules/EvseManager/IECStateMachine.hpp b/modules/EvseManager/IECStateMachine.hpp index b8c86dda8..fced515e8 100644 --- a/modules/EvseManager/IECStateMachine.hpp +++ b/modules/EvseManager/IECStateMachine.hpp @@ -30,7 +30,6 @@ #include #include "Timeout.hpp" -#include "utils/thread.hpp" #include "scoped_lock_timeout.hpp" diff --git a/modules/EvseManager/energy_grid/energyImpl.cpp b/modules/EvseManager/energy_grid/energyImpl.cpp index 744252475..21bbb5534 100644 --- a/modules/EvseManager/energy_grid/energyImpl.cpp +++ b/modules/EvseManager/energy_grid/energyImpl.cpp @@ -94,9 +94,12 @@ void energyImpl::ready() { // request energy every second std::thread([this] { + auto watchdog = mod->watchdog_supervisor.register_watchdog("Energy request thread", std::chrono::seconds(10)); + while (true) { request_energy_from_energy_manager(); std::this_thread::sleep_for(std::chrono::seconds(1)); + watchdog(); } }).detach();