PyThermostat: Modify PIDAutotune for GUI usage #162

Open
atse wants to merge 3 commits from atse/thermostat:autotune-state into master
Showing only changes of commit f4c1dab00f - Show all commits

View File

@ -13,12 +13,12 @@ from pythermostat.client import Client
class PIDAutotuneState(Enum): class PIDAutotuneState(Enum):
STATE_OFF = auto() OFF = auto()
STATE_RELAY_STEP_UP = auto() RELAY_STEP_UP = auto()
STATE_RELAY_STEP_DOWN = auto() RELAY_STEP_DOWN = auto()
STATE_SUCCEEDED = auto() SUCCEEDED = auto()
STATE_FAILED = auto() FAILED = auto()
STATE_READY = auto() READY = auto()
Outdated
Review

Inconsistent string delimiter.

Can't it just use auto() anyway?

Inconsistent string delimiter. Can't it just use ``auto()`` anyway?
Outdated
Review

Actually yes, switched to auto() in force-push to 828648ed76.

Then I believe #71 would be unnecessary since the string values aren't used anywhere.

Actually yes, switched to `auto()` in force-push to 828648ed76. Then I believe #71 would be unnecessary since the string values aren't used anywhere.
Outdated
Review

Also just realised this highlights the verbosity of the STATE_ prefix.

Also just realised this highlights the verbosity of the `STATE_` prefix.
Outdated
Review

Refactored in force-push to 869922c928.

Refactored in force-push to 869922c928.
class PIDAutotune: class PIDAutotune:
@ -46,7 +46,7 @@ class PIDAutotune:
self._noiseband = noiseband self._noiseband = noiseband
self._out_min = -out_step self._out_min = -out_step
self._out_max = out_step self._out_max = out_step
self._state = PIDAutotuneState.STATE_OFF self._state = PIDAutotuneState.OFF
self._peak_timestamps = deque(maxlen=5) self._peak_timestamps = deque(maxlen=5)
self._peaks = deque(maxlen=5) self._peaks = deque(maxlen=5)
self._output = 0 self._output = 0
@ -67,11 +67,11 @@ class PIDAutotune:
self._inputs = deque(maxlen=round(lookback / sampletime)) self._inputs = deque(maxlen=round(lookback / sampletime))
def set_ready(self): def set_ready(self):
self._state = PIDAutotuneState.STATE_READY self._state = PIDAutotuneState.READY
self._peak_count = 0 self._peak_count = 0
def set_off(self): def set_off(self):
self._state = PIDAutotuneState.STATE_OFF self._state = PIDAutotuneState.OFF
def state(self): def state(self):
"""Get the current state.""" """Get the current state."""
@ -111,29 +111,29 @@ class PIDAutotune:
now = time_input * 1000 now = time_input * 1000
if self._state not in { if self._state not in {

Isn't there a way to shorten it?

Isn't there a way to shorten it?
Outdated
Review

Yep, did it in the latest force-push to 828648ed76.

Yep, did it in the latest force-push to 828648ed76.

Wouldn't the negative logic be applicable here? Like not in (STEP_UP, STEP_DOWN).

Wouldn't the negative logic be applicable here? Like `not in (STEP_UP, STEP_DOWN)`.
Outdated
Review

Humm, that's true. Was worried about self._state being set to a non-PIDAutotuneState value, but that shouldn't happen under normal circumstances.

Humm, that's true. Was worried about `self._state` being set to a non-`PIDAutotuneState` value, but that shouldn't happen under normal circumstances.
Outdated
Review

Done in force-push to f4c1dab00f.

Done in force-push to f4c1dab00f.
PIDAutotuneState.STATE_RELAY_STEP_DOWN, PIDAutotuneState.RELAY_STEP_DOWN,
PIDAutotuneState.STATE_RELAY_STEP_UP, PIDAutotuneState.RELAY_STEP_UP,
}: }:
self._state = PIDAutotuneState.STATE_RELAY_STEP_UP self._state = PIDAutotuneState.RELAY_STEP_UP
self._last_run_timestamp = now self._last_run_timestamp = now
# check input and change relay state if necessary # check input and change relay state if necessary
if self._state == PIDAutotuneState.STATE_RELAY_STEP_UP if self._state == PIDAutotuneState.RELAY_STEP_UP
and input_val > self._setpoint + self._noiseband: and input_val > self._setpoint + self._noiseband:
self._state = PIDAutotuneState.STATE_RELAY_STEP_DOWN self._state = PIDAutotuneState.RELAY_STEP_DOWN
logging.debug('switched state: {0}'.format(self._state)) logging.debug('switched state: {0}'.format(self._state))
logging.debug('input: {0}'.format(input_val)) logging.debug('input: {0}'.format(input_val))
elif self._state == PIDAutotuneState.STATE_RELAY_STEP_DOWN elif self._state == PIDAutotuneState.RELAY_STEP_DOWN
and input_val < self._setpoint - self._noiseband: and input_val < self._setpoint - self._noiseband:
self._state = PIDAutotuneState.STATE_RELAY_STEP_UP self._state = PIDAutotuneState.RELAY_STEP_UP
logging.debug('switched state: {0}'.format(self._state)) logging.debug('switched state: {0}'.format(self._state))
logging.debug('input: {0}'.format(input_val)) logging.debug('input: {0}'.format(input_val))
# set output # set output
if self._state == PIDAutotuneState.STATE_RELAY_STEP_UP: if self._state == PIDAutotuneState.RELAY_STEP_UP:
self._output = self._initial_output - self._outputstep self._output = self._initial_output - self._outputstep
elif self._state == PIDAutotuneState.STATE_RELAY_STEP_DOWN: elif self._state == PIDAutotuneState.RELAY_STEP_DOWN:
self._output = self._initial_output + self._outputstep self._output = self._initial_output + self._outputstep
# respect output limits # respect output limits
@ -201,16 +201,16 @@ class PIDAutotune:
logging.debug('amplitude deviation: {0}'.format(amplitude_dev)) logging.debug('amplitude deviation: {0}'.format(amplitude_dev))
if amplitude_dev < PIDAutotune.PEAK_AMPLITUDE_TOLERANCE: if amplitude_dev < PIDAutotune.PEAK_AMPLITUDE_TOLERANCE:
self._state = PIDAutotuneState.STATE_SUCCEEDED self._state = PIDAutotuneState.SUCCEEDED
# if the autotune has not already converged # if the autotune has not already converged
# terminate after 10 cycles # terminate after 10 cycles
if self._peak_count >= 20: if self._peak_count >= 20:
self._output = 0 self._output = 0
self._state = PIDAutotuneState.STATE_FAILED self._state = PIDAutotuneState.FAILED
return True return True
if self._state == PIDAutotuneState.STATE_SUCCEEDED: if self._state == PIDAutotuneState.SUCCEEDED:
self._output = 0 self._output = 0
logging.debug('peak finding successful') logging.debug('peak finding successful')