2022-11-19 18:52:01 +01:00
|
|
|
from time import perf_counter
|
|
|
|
|
|
|
|
class Controller():
|
|
|
|
def __init__(self,
|
|
|
|
# Actions
|
|
|
|
# Target value we want to reach, in what period ()
|
|
|
|
target=None, reach_period_s=None,
|
|
|
|
# Timeframe we need to wait before changing the
|
|
|
|
# system output (means: how fast we can trigger
|
|
|
|
# on/off states). Used to prevent breaking fridge or
|
|
|
|
# other actuators.
|
|
|
|
refractary_period_s=10,
|
|
|
|
# Send notifications outside this range?
|
|
|
|
warning_range=(None, None)):
|
|
|
|
self.input = None
|
|
|
|
self.target = target
|
|
|
|
self.range = warning_range
|
2023-03-30 16:02:59 +02:00
|
|
|
self.input_label = "WARNING: NO INPUT LABEL SET"
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
self.reach_period_s = reach_period_s
|
|
|
|
self.refractary_period_s = refractary_period_s
|
|
|
|
|
|
|
|
def set_reach_period(self, reach_period_s):
|
|
|
|
self.reach_period_s = reach_period_s
|
|
|
|
|
|
|
|
def set_input(self, input):
|
|
|
|
self.input = input
|
|
|
|
|
|
|
|
def set_input_label(self, label):
|
|
|
|
self.input_label = label
|
|
|
|
|
|
|
|
def set_target(self, target):
|
|
|
|
print('Setting target', target)
|
|
|
|
self.target = target
|
|
|
|
|
2023-02-22 12:12:44 +01:00
|
|
|
def get_target(self):
|
|
|
|
return self.target
|
|
|
|
|
|
|
|
def set_warning_range(self, range):
|
|
|
|
print('Setting range', range)
|
|
|
|
self.range = range
|
|
|
|
|
|
|
|
def get_warning_range(self):
|
|
|
|
return self.range
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
def one_cycle(self, time=None):
|
|
|
|
print("ERROR: This function must be implemented by the controller")
|
|
|
|
|
|
|
|
"Set the input, run `one_cycle' and return the required actuator action."
|
|
|
|
def apply(self, input, time=perf_counter):
|
|
|
|
# Set the current input
|
|
|
|
self.set_input(input)
|
|
|
|
# Call one cycle
|
|
|
|
action = self.one_cycle(time())
|
|
|
|
# TODO: Check refractary period?
|
|
|
|
return action
|