-
Notifications
You must be signed in to change notification settings - Fork 118
/
current_shaper.cpp
213 lines (186 loc) · 5.22 KB
/
current_shaper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "current_shaper.h"
#include "input_filter.h"
//global instance
CurrentShaperTask shaper;
CurrentShaperTask::CurrentShaperTask() : MicroTasks::Task() {
_changed = false;
_enabled = false;
_max_pwr = 0;
_live_pwr = 0;
_smoothed_live_pwr = 0;
_chg_cur = 0;
_max_cur = 0;
_pause_timer = 0;
_timer = 0;
_updated = false;
}
CurrentShaperTask::~CurrentShaperTask() {
// should be useless but just in case
evse.release(EvseClient_OpenEVSE_Shaper);
}
void CurrentShaperTask::setup() {
}
unsigned long CurrentShaperTask::loop(MicroTasks::WakeReason reason) {
if (_enabled) {
EvseProperties props;
if (_changed) {
props.setMaxCurrent(floor(_max_cur));
if (_max_cur < evse.getMinCurrent()) {
// pause temporary, not enough amps available
props.setState(EvseState::Disabled);
if (!_pause_timer)
{
_pause_timer = millis();
}
}
else if (millis() - _pause_timer >= current_shaper_min_pause_time * 1000 && (_max_cur - evse.getMinCurrent() >= EVSE_SHAPER_HYSTERESIS))
{
_pause_timer = 0;
props.setState(EvseState::None);
}
_timer = millis();
_changed = false;
// 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;
event["shaper_smoothed_live_pwr"] = _smoothed_live_pwr;
event["shaper_max_pwr"] = _max_pwr;
event["shaper_cur"] = _max_cur;
event["shaper_updated"] = _updated;
event_send(event);
}
}
else if ( !_updated || millis() - _timer > current_shaper_data_maxinterval * 1000 )
{
//available power has not been updated since EVSE_SHAPER_FAILSAFE_TIME, pause charge
DBUGF("shaper_live_pwr has not been updated in time, pausing charge");
if (_updated)
{
_pause_timer = millis();
_updated = false;
_smoothed_live_pwr = _live_pwr;
}
if (evse.getState(EvseClient_OpenEVSE_Shaper) != EvseState::Disabled)
{
props.setState(EvseState::Disabled);
evse.claim(EvseClient_OpenEVSE_Shaper, EvseManager_Priority_Limit, props);
StaticJsonDocument<128> event;
event["shaper"] = 1;
event["shaper_live_pwr"] = _live_pwr;
event["shaper_smoothed_live_pwr"] = _smoothed_live_pwr;
event["shaper_max_pwr"] = _max_pwr;
event["shaper_cur"] = _max_cur;
event["shaper_updated"] = _updated;
event_send(event);
}
}
}
else {
//remove shaper claim
if (_evse->clientHasClaim(EvseClient_OpenEVSE_Shaper)) {
_evse->release(EvseClient_OpenEVSE_Shaper);
_smoothed_live_pwr = 0;
}
}
return EVSE_SHAPER_LOOP_TIME;
}
void CurrentShaperTask::begin(EvseManager &evse) {
this -> _timer = millis();
this -> _enabled = config_current_shaper_enabled();
this -> _evse = &evse;
this -> _max_pwr = current_shaper_max_pwr;
this -> _live_pwr = 0;
this -> _smoothed_live_pwr = 0;
this -> _max_cur = 0;
this -> _updated = false;
MicroTask.startTask(this);
StaticJsonDocument<128> event;
event["shaper"] = 1;
event_send(event);
}
void CurrentShaperTask::notifyConfigChanged( bool enabled, uint32_t max_pwr) {
DBUGF("CurrentShaper: got config changed");
_enabled = enabled;
_max_pwr = max_pwr;
if (!enabled) evse.release(EvseClient_OpenEVSE_Shaper);
StaticJsonDocument<128> event;
event["shaper"] = enabled == true ? 1 : 0;
event["shaper_max_pwr"] = max_pwr;
event_send(event);
}
void CurrentShaperTask::setMaxPwr(int max_pwr) {
_max_pwr = max_pwr;
shapeCurrent();
}
void CurrentShaperTask::setLivePwr(int live_pwr) {
_live_pwr = live_pwr;
shapeCurrent();
}
// temporary change Current Shaper state without changing configuration
void CurrentShaperTask::setState(bool state) {
_enabled = state;
if (!_enabled) {
//remove claim
evse.release(EvseClient_OpenEVSE_Shaper);
}
StaticJsonDocument<128> event;
event["shaper"] = state?1:0;
event_send(event);
}
void CurrentShaperTask::shapeCurrent() {
_updated = true;
// adding self produced energy to total
int max_pwr = _max_pwr;
int livepwr;
DBUGVAR(_pause_timer);
if (_pause_timer == 0) {
_smoothed_live_pwr = _live_pwr;
livepwr = _live_pwr;
}
else {
if (_live_pwr > _smoothed_live_pwr) {
_smoothed_live_pwr = _live_pwr;
}
else {
_smoothed_live_pwr = _inputFilter.filter(_live_pwr, _smoothed_live_pwr, current_shaper_smoothing_time);
}
livepwr = _smoothed_live_pwr;
}
if (config_divert_enabled() == true) {
if ( divert_type == DIVERT_TYPE_SOLAR ) {
max_pwr += solar;
}
}
if (livepwr > max_pwr) {
livepwr = max_pwr;
}
if(!config_threephase_enabled()) {
_max_cur = ((max_pwr - livepwr) / evse.getVoltage()) + evse.getAmps();
}
else {
_max_cur = ((max_pwr - livepwr) / evse.getVoltage() / 3.0) + evse.getAmps();
}
_changed = true;
}
int CurrentShaperTask::getMaxPwr() {
return _max_pwr;
}
int CurrentShaperTask::getLivePwr() {
return _live_pwr;
}
double CurrentShaperTask::getMaxCur() {
return _max_cur;
}
bool CurrentShaperTask::getState() {
return _enabled;
}
bool CurrentShaperTask::isActive() {
return _evse->clientHasClaim(EvseClient_OpenEVSE_Shaper);
}
bool CurrentShaperTask::isUpdated() {
return _updated;
}