diff --git a/modules/MicroMegaWattBSP/dc_supply/power_supply_DCImpl.cpp b/modules/MicroMegaWattBSP/dc_supply/power_supply_DCImpl.cpp index 1b543320f..335caedec 100644 --- a/modules/MicroMegaWattBSP/dc_supply/power_supply_DCImpl.cpp +++ b/modules/MicroMegaWattBSP/dc_supply/power_supply_DCImpl.cpp @@ -25,6 +25,38 @@ void power_supply_DCImpl::init() { p.voltage_V = v; mod->p_powermeter->publish_powermeter(p); }); + + std::thread([this]() { + float low_pass_voltage = 0.; + + while (true) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // prevent overshoot + if (low_pass_voltage > req_voltage) { + // step down immediately + low_pass_voltage = req_voltage; + } else { + float delta = req_voltage - low_pass_voltage; + if (delta > 500) { + low_pass_voltage += 100; + } else { + if (delta > 50) { + low_pass_voltage += 25; + } else { + low_pass_voltage = req_voltage; + } + } + } + + if (is_on) { + mod->serial.setOutputVoltageCurrent(low_pass_voltage, req_current); + + } else { + mod->serial.setOutputVoltageCurrent(0, 0); + low_pass_voltage = 0.; + } + } + }).detach(); } void power_supply_DCImpl::ready() { @@ -57,10 +89,6 @@ void power_supply_DCImpl::handle_setMode(types::power_supply_DC::Mode& mode, void power_supply_DCImpl::handle_setExportVoltageCurrent(double& voltage, double& current) { req_voltage = voltage; req_current = current; - - if (is_on) { - mod->serial.setOutputVoltageCurrent(req_voltage, req_current); - } }; void power_supply_DCImpl::handle_setImportVoltageCurrent(double& voltage, double& current){ diff --git a/modules/MicroMegaWattBSP/dc_supply/power_supply_DCImpl.hpp b/modules/MicroMegaWattBSP/dc_supply/power_supply_DCImpl.hpp index e89fdfe88..112395f8b 100644 --- a/modules/MicroMegaWattBSP/dc_supply/power_supply_DCImpl.hpp +++ b/modules/MicroMegaWattBSP/dc_supply/power_supply_DCImpl.hpp @@ -51,8 +51,9 @@ class power_supply_DCImpl : public power_supply_DCImplBase { // ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1 // insert your private definitions here - float req_voltage{0}, req_current{0}; - bool is_on{false}; + std::atomic req_voltage{0}; + std::atomic req_current{0}; + std::atomic_bool is_on{false}; // ev@3370e4dd-95f4-47a9-aaec-ea76f34a66c9:v1 };