Skip to content

Commit

Permalink
use watchdogs in modules
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius Claussen <[email protected]>
  • Loading branch information
corneliusclaussen committed Feb 9, 2024
1 parent c5e0ede commit 1d55465
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
3 changes: 3 additions & 0 deletions modules/EnergyManager/EnergyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
26 changes: 16 additions & 10 deletions modules/EvseManager/Charger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
namespace module {

Charger::Charger(const std::unique_ptr<IECStateMachine>& bsp, const std::unique_ptr<ErrorHandling>& 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();
}
Expand Down Expand Up @@ -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);

Expand All @@ -105,6 +109,8 @@ void Charger::main_thread() {
// to be done on regular intervals independent from events)
run_state_machine();
}

watchdog();
}
}

Expand Down
6 changes: 5 additions & 1 deletion modules/EvseManager/Charger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <queue>
#include <sigslot/signal.hpp>

#include <utils/watchdog.hpp>

#include "ErrorHandling.hpp"
#include "IECStateMachine.hpp"
#include "scoped_lock_timeout.hpp"
Expand All @@ -48,7 +50,8 @@ const std::string IEC62196Type2Socket = "IEC62196Type2Socket";
class Charger {
public:
Charger(const std::unique_ptr<IECStateMachine>& bsp, const std::unique_ptr<ErrorHandling>& 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 {
Expand Down Expand Up @@ -299,6 +302,7 @@ class Charger {
const std::unique_ptr<IECStateMachine>& bsp;
const std::unique_ptr<ErrorHandling>& error_handling;
const types::evse_board_support::Connector_type& connector_type;
Everest::WatchdogSupervisor& watchdog_supervisor;

// constants
static constexpr float CHARGER_ABSOLUTE_MAX_CURRENT{80.};
Expand Down
9 changes: 7 additions & 2 deletions modules/EvseManager/EvseManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ void EvseManager::ready() {

hw_capabilities = r_bsp->call_get_hw_capabilities();

charger = std::unique_ptr<Charger>(new Charger(bsp, error_handling, hw_capabilities.connector_type));
charger =
std::unique_ptr<Charger>(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(); });
Expand Down Expand Up @@ -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"},
Expand Down
1 change: 0 additions & 1 deletion modules/EvseManager/IECStateMachine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <sigslot/signal.hpp>

#include "Timeout.hpp"
#include "utils/thread.hpp"

#include "scoped_lock_timeout.hpp"

Expand Down
3 changes: 3 additions & 0 deletions modules/EvseManager/energy_grid/energyImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 1d55465

Please sign in to comment.