Skip to content

Commit

Permalink
prevent relay switching too much with shaper
Browse files Browse the repository at this point in the history
  • Loading branch information
KipK committed Apr 1, 2023
1 parent 4be7f09 commit de02af9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/current_shaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ CurrentShaperTask shaper;
CurrentShaperTask::CurrentShaperTask() : MicroTasks::Task() {
_changed = false;
_enabled = false;
_max_pwr = 0;
_live_pwr = 0;
_chg_cur = 0;
_max_cur = 0;
_pause_timer = 0;
_timer = 0;
_updated = false;
}

CurrentShaperTask::~CurrentShaperTask() {
Expand All @@ -18,21 +25,30 @@ void CurrentShaperTask::setup() {
}

unsigned long CurrentShaperTask::loop(MicroTasks::WakeReason reason) {

if (_enabled) {
EvseProperties props;
if (_changed) {
props.setMaxCurrent(_max_cur);
if (_max_cur < evse.getMinCurrent() ) {
// pause temporary, not enough amps available
// this timer will also be reseted while in pause, this should waits
// enough time to get a stable power available
props.setState(EvseState::Disabled);
_pause_timer = millis();
}
else {
else if (!_pause_timer || millis() - _pause_timer >= EVSE_SHAPER_PAUSE_TIME)
{
_pause_timer = 0;
props.setState(EvseState::None);
}
_changed = false;
_updated = true;
_timer = millis();
evse.claim(EvseClient_OpenEVSE_Shaper,EvseManager_Priority_Safety, props);
// claim only if we have change
if (evse.getState() != props.getState() || evse.getChargeCurrent() != props.getChargeCurrent()) {
evse.claim(EvseClient_OpenEVSE_Shaper, EvseManager_Priority_Safety, props);
}
StaticJsonDocument<128> event;
event["shaper"] = 1;
event["shaper_live_pwr"] = _live_pwr;
Expand Down Expand Up @@ -130,8 +146,10 @@ void CurrentShaperTask::shapeCurrent() {
else {
_max_cur = round(((max_pwr - _live_pwr) / evse.getVoltage() / 3) + (evse.getAmps()));
}


if (_max_cur > evse.getMaxConfiguredCurrent()) {
_max_cur = evse.getMaxConfiguredCurrent()
}

_changed = true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/current_shaper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
#ifndef EVSE_SHAPER_LOOP_TIME
#define EVSE_SHAPER_LOOP_TIME 2000
#endif
// Maximum allowed time between data before suspending charge
#ifndef EVSE_SHAPER_FAILSAFE_TIME
#define EVSE_SHAPER_FAILSAFE_TIME 360000
#endif
// Time to waits after a pause before starting to charge again
#ifndef EVSE_SHAPER_PAUSE_TIME
#define EVSE_SHAPER_PAUSE_TIME 360000
#endif

#include "emonesp.h"
#include <MicroTasks.h>
Expand All @@ -30,6 +35,7 @@ class CurrentShaperTask: public MicroTasks::Task
uint8_t _chg_cur; // calculated charge current to claim
uint8_t _max_cur; // shaper calculated max current
uint32_t _timer;
uint32_t _pause_timer;
bool _updated;

protected:
Expand Down

0 comments on commit de02af9

Please sign in to comment.