hakkoso/control/controllers.py

57 lines
1.8 KiB
Python

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
self.input_label = "WARNING: NO INPUT LABEL SET"
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
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
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