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)