cooldown controller threshold to prevent fast on/off

master
Nicolò Balzarotti 2023-04-01 13:28:22 +02:00
parent d50db773bd
commit 316615105d
1 changed files with 13 additions and 4 deletions

View File

@ -13,9 +13,11 @@ class FixedDutyCycle(controllers.Controller):
'>': lambda a, b: a > b,
'==': lambda a, b: a == b,
}
self.offset = 0.1 if operator == "<" else -0.1
self.operator = functions[operator]
super().__init__(*args, **kwargs)
self.last_time = None
self.last_time = None
self.last_output = False
self.total_failed_read = 0
self.failed_read = 0
@ -45,11 +47,18 @@ class FixedDutyCycle(controllers.Controller):
if self.input is None:
self.total_failed_read += 1
self.failed_read += 1
# TODO: WARN/EXIT IF WE HAVE TOO MANY FAILED READS?
# TODO: find a sane default failed read value
if self.failed_read > 100:
print("ERROR: TOO MANY CONSEQUTIVE FAILED READ; DISABLED")
return False
if self.input is None:
print("WARNING: READ FAILED, KEEPING LAST ESTIMATE")
return self.last_output
self.failed_read = 0
return (self.timepoint() < self.duty) if (
self.operator(self.input, self.target)) else False
target = self.target + self.offset * (1 if self.last_output else -1)
self.last_output = (self.timepoint() < self.duty) if (
self.operator(self.input, target)) else False
return self.last_output
# 0. refractary_period is fixed (e.g. 30s, depends on the hardware)