From 316615105daddf53b1123c8e920524b152794148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Balzarotti?= Date: Sat, 1 Apr 2023 13:28:22 +0200 Subject: [PATCH] cooldown controller threshold to prevent fast on/off --- control/fixed_duty.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/control/fixed_duty.py b/control/fixed_duty.py index 6040030..abde64c 100644 --- a/control/fixed_duty.py +++ b/control/fixed_duty.py @@ -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)