From 828648ed7651e3047376046cb0ce85252de520e1 Mon Sep 17 00:00:00 2001 From: atse Date: Mon, 4 Nov 2024 12:54:58 +0800 Subject: [PATCH] PyThermostat: Modify PIDAutotune for GUI usage Co-authored-by: topquark12 --- pythermostat/pythermostat/autotune.py | 37 ++++++++++++++++++++------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/pythermostat/pythermostat/autotune.py b/pythermostat/pythermostat/autotune.py index 0cd45a4..7f20b50 100644 --- a/pythermostat/pythermostat/autotune.py +++ b/pythermostat/pythermostat/autotune.py @@ -2,7 +2,7 @@ import math import logging import time from collections import deque, namedtuple -from enum import Enum +from enum import Enum, auto from pythermostat.client import Client @@ -13,11 +13,12 @@ from pythermostat.client import Client class PIDAutotuneState(Enum): - STATE_OFF = 'off' - STATE_RELAY_STEP_UP = 'relay step up' - STATE_RELAY_STEP_DOWN = 'relay step down' - STATE_SUCCEEDED = 'succeeded' - STATE_FAILED = 'failed' + STATE_OFF = auto() + STATE_RELAY_STEP_UP = auto() + STATE_RELAY_STEP_DOWN = auto() + STATE_SUCCEEDED = auto() + STATE_FAILED = auto() + STATE_READY = auto() class PIDAutotune: @@ -57,6 +58,21 @@ class PIDAutotune: self._Ku = 0 self._Pu = 0 + def set_param(self, target, step, noiseband, sampletime, lookback): + self._setpoint = target + self._outputstep = step + self._out_max = step + self._out_min = -step + self._noiseband = noiseband + self._inputs = deque(maxlen=round(lookback / sampletime)) + + def set_ready(self): + self._state = PIDAutotuneState.STATE_READY + self._peak_count = 0 + + def set_off(self): + self._state = PIDAutotuneState.STATE_OFF + def state(self): """Get the current state.""" return self._state @@ -94,9 +110,12 @@ class PIDAutotune: """ now = time_input * 1000 - if (self._state == PIDAutotuneState.STATE_OFF - or self._state == PIDAutotuneState.STATE_SUCCEEDED - or self._state == PIDAutotuneState.STATE_FAILED): + if self._state in ( + PIDAutotuneState.STATE_OFF, + PIDAutotuneState.STATE_SUCCEEDED, + PIDAutotuneState.STATE_FAILED, + PIDAutotuneState.STATE_READY, + ): self._state = PIDAutotuneState.STATE_RELAY_STEP_UP self._last_run_timestamp = now