Skip to content

Commit a58a02e

Browse files
committedJul 20, 2024
calculate and possibly update limit only every 10s
1 parent c3a20f3 commit a58a02e

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed
 

‎platformio.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extra_configs = inverter.ini
1414

1515
[program]
1616
name = power
17-
version = 6.8
17+
version = 6.9
1818
# adapt these to your environment
1919
instance = 3
2020
serial_speed = 9600

‎src/main.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -251,38 +251,34 @@ void check_limit() {
251251
const uint16_t max_limit = INVERTER_LIMIT; // unthrottled WR while backfeed is small enough
252252
const uint16_t min_aMinus = BACKFEED_MIN; // if actual backfeed is lower, inverter gets less limited
253253
const uint16_t max_aMinus = BACKFEED_MAX; // if actual backfeed is higher, inverter gets more limited
254-
254+
const uint16_t min_check_delay_s = 10; // high enough to make power calc from counter reliable
255+
// low enough to limit time with too high backfeed
255256
static uint32_t uptime = 0;
256257
static uint64_t aMinus = 0;
257258

258259
uint64_t aMinusW = 0;
259260

260-
static uint32_t update_ms = 0;
261-
262261
if( itron.valid == 0x3f ) {
263262
// we have valid backfeed data
264-
if( uptime && uptime != itron.uptime ) {
265-
// and data was updated since last check
266-
uint32_t delta_t = itron.uptime - uptime;
263+
uint32_t delta_t = itron.uptime - uptime;
264+
if( uptime && delta_t > min_check_delay_s ) {
265+
// the last check is more than min_check_delay_s ago
267266
// calculate average backfeed in W from the ever increasing backfeed counter
268267
// and the elapsed time since last check
269268
aMinusW = (itron.aMinus - aMinus) * 360 / delta_t;
270269
/// syslog.logf(LOG_INFO, "Check: Curr A-: %llu W, dt = %u s", aMinusW, delta_t);
271270

272-
uint32_t now = millis();
273-
if( curr_limit != UINT16_MAX && reachable && (now - update_ms) > 30000 ) {
271+
if( curr_limit != UINT16_MAX && reachable ) {
274272
// only try to change the limit if the current limit is known
275-
// and the inverter is reachable and the last change is more than 30s ago
273+
// and the inverter is reachable
276274
if( aMinusW > max_aMinus && curr_limit > 0 ) {
277275
// current backfeed is too high and inverter is not yet fully limited
278-
update_ms = now;
279276
// change of inverter limit to backfeed right in the middle of the desired range
280277
uint16_t delta = aMinusW - (min_aMinus + max_aMinus)/2;
281278
publish_limit(aMinusW, (curr_limit > delta) ? curr_limit - delta : 0);
282279
}
283280
else if( aMinusW < min_aMinus && curr_limit < max_limit ) {
284281
// current backfeed is too low and inverter is not yet fully opened
285-
update_ms = now;
286282
// change of inverter limit to backfeed right in the middle of the desired range
287283
uint16_t delta = (min_aMinus + max_aMinus)/2 - aMinusW;
288284
publish_limit(aMinusW, (curr_limit + delta < max_limit) ? curr_limit + delta : max_limit);
@@ -294,10 +290,9 @@ void check_limit() {
294290
else {
295291
/// syslog.logf(LOG_INFO, "Check: current limit %u W not changed, elapsed since update: %u s", curr_limit, (now - update_ms)/1000);
296292
}
293+
uptime = itron.uptime;
294+
aMinus = itron.aMinus;
297295
}
298-
299-
uptime = itron.uptime;
300-
aMinus = itron.aMinus;
301296
}
302297
}
303298

0 commit comments

Comments
 (0)