Compare commits

...

88 Commits

Author SHA1 Message Date
70627342f3 Real concurrently 2024-08-30 17:37:53 +08:00
ffa0783cff Don't create report task if not polling for it 2024-08-30 16:55:15 +08:00
ba6b24fe95 PIDAutotuner: Don't use separate sig for interval 2024-08-30 16:55:15 +08:00
2e0ecd9758 aioexample: Show polling while report mode on
WIP: handle KeyboardInterrupt
2024-08-30 16:55:15 +08:00
199aeb3609 aioclient: More accurate DFU docstring 2024-08-30 16:55:15 +08:00
1e81641169 No need for async as well 2024-08-30 16:55:15 +08:00
2c95bd9d09 No need for async 2024-08-30 16:55:15 +08:00
d83c4ed4f6 Remove extra imports 2024-08-30 16:55:15 +08:00
637d8d029a report group 2024-08-30 16:55:15 +08:00
671004703b end_session in thermostat itself afterall 2024-08-30 16:55:15 +08:00
10873933f4 Reorder MainWindow 2024-08-30 16:55:15 +08:00
ec1fffecd1 Rearrange MainWindow.__init__ 2024-08-30 16:55:15 +08:00
36c063d917 Thermostat: Add disconnect callback
For communicating with the autotuner before the client fully
disconnects

Also then there's no need for explicitly resetting autotune elements
2024-08-30 16:55:15 +08:00
65b9202cea Remove info_box_trigger 2024-08-30 16:55:15 +08:00
a72a0606f1 Remove connection errored 2024-08-30 16:55:15 +08:00
81f09a9f0f Simply test for connectivity for turning PID off 2024-08-30 16:55:15 +08:00
f768ea5d4f Fix pressing enter not working in conn menu 2024-08-30 16:55:15 +08:00
e849610658 Assign connection_errored as well 2024-08-30 16:55:15 +08:00
bbe9a60824 Back out 2024-08-30 16:55:15 +08:00
cd07cdeddd Put UI changes into conn_menu 2024-08-30 16:55:15 +08:00
fe3a22a599 ConnectionBtn + StatusLbl 2024-08-30 16:55:15 +08:00
2b35bf57f9 Clear Graph UI changes 2024-08-30 16:55:15 +08:00
8a83bbc1d1 Concentrate ThermostatCtrlMenu UI changes 2024-08-30 16:55:15 +08:00
82315d65b2 Descriptive name 2024-08-30 16:55:15 +08:00
dfe08546fe Refactor repeated stuff 2024-08-30 16:55:15 +08:00
c4309c5ba0 Split PID handler UI up 2024-08-30 16:55:15 +08:00
3387ad67f7 Fix hwrev getting 2024-08-30 16:55:15 +08:00
903f9f8767 hwrev updates 2024-08-30 15:53:05 +08:00
aadf8a2595 Fix info boxes for load/saving from flash 2024-08-30 15:53:05 +08:00
49561a2f5a Move reset request to thermostat control menu
We don't get auto reconnect anymore
2024-08-30 15:53:05 +08:00
b32b38d830 Move pid autotuning request to CtrlPanel
And update autotune UI only on state change instead of every single
report update
2024-08-30 15:53:05 +08:00
fcda46d1f3 Move channelGraph stuff inside LiveDataPlotter 2024-08-30 15:53:05 +08:00
89e9da4499 Move plot_options_menu stuff into menu 2024-08-30 15:53:05 +08:00
87ca0694cd Move autotune ticking connect 2024-08-30 15:53:05 +08:00
03233386d0 Get thermostat_ctrl_menu to subscribe to hwrev updates 2024-08-30 15:53:05 +08:00
242a2b2e69 Save/load info box content 2024-08-30 15:53:05 +08:00
b84af82722 Fix loading all channel settings would bring up 2 info boxes 2024-08-30 15:53:05 +08:00
7997ef2472 Add back asyncSlot to save_cfg_request 2024-08-30 15:53:05 +08:00
911b4c680f hw_rev 2024-08-30 15:53:05 +08:00
19677c2251 fan_update: tec_qt to thermostat_ctrl_menu 2024-08-30 15:53:05 +08:00
5f30f12875 Fix updating status label for hwrev = None 2024-08-30 15:53:05 +08:00
3283b126b8 Put some menu requests in menu itself 2024-08-30 15:53:05 +08:00
59ba8c692d Broadcast ConnectionState changes from Thermostat 2024-08-30 15:53:05 +08:00
e3ef9115a5 Move sigActivatedHandle to CtrlPanel 2024-08-30 15:53:05 +08:00
c5c311ae14 Get rid of timeout on readline 2024-08-30 15:53:05 +08:00
6699458939 Put send_command in CtrlPanel 2024-08-30 15:53:05 +08:00
8a4a963f9b Extra bail removed 2024-08-30 15:53:05 +08:00
2f10ad8056 return hwrev when start_session 2024-08-30 15:53:05 +08:00
3731282146 state str 2024-08-30 15:53:05 +08:00
50ac1c2078 params update concurrently 2024-08-30 15:53:05 +08:00
0eebc0a6df Stuff non-UI changes in Thermostat model 2024-08-30 15:53:05 +08:00
78ba799d56 Base Thermostat connectivity UI changes to state 2024-08-30 15:53:05 +08:00
f9c0f140fa Remove wait_for
OSError raised anyways
2024-08-30 15:53:05 +08:00
93d96401b9 Actually its OSError 2024-08-30 15:53:05 +08:00
881bf6ff6c {start,end}_session -> [dis]connect 2024-08-30 15:53:05 +08:00
7e0dd61f79 Simplify on_connect_btn_clicked
Raise if OSError
2024-08-30 15:53:05 +08:00
ed34eb9e03 Connecting task moved? 2024-08-30 15:53:05 +08:00
387d1226c5 AsyncIO version Client -> AsyncioClient 2024-08-30 15:53:05 +08:00
058e597ef0 Exclusively use the Thermostat object as a medium
All calls to the Thermostat should be forwarded by the medium.
2024-08-30 15:53:05 +08:00
80975f50b1 Integrate WrappedClient into Thermostat model 2024-08-30 15:53:05 +08:00
86afa19085 Should not stop cancelling read if timeout'd 2024-08-30 15:53:05 +08:00
4b6dfdf478 Fix Autotuner state for forceful disconnect 2024-08-30 15:53:05 +08:00
85cb2027a8 _ 2024-08-30 15:53:05 +08:00
4fedb14727 Make connection loss handling more elegant
Show an info box on connection lost informing the user that the
Thermostat was forcefully disconnected.
2024-08-30 15:53:05 +08:00
863920f922 ================gui_dev-fix_asyncio=============== 2024-08-30 15:53:05 +08:00
96b57f199d Use new style super() 2024-08-30 15:53:05 +08:00
f2da457860 Move command line host:port setting handling
To main
2024-08-30 15:40:31 +08:00
066c75fc21 aioclient: Add missing readline for saving
Saving all channels returns multiple JSON objects, read the extra {}.
2024-08-30 15:38:59 +08:00
60b05ddfeb import order rearrange 2024-08-30 15:38:59 +08:00
6a78a2069d This is bail 2024-08-30 15:38:59 +08:00
6eb5b7298e thermostat: Properly register task
Also Thermostat.task -> Thermostat._update_params_task
2024-08-30 15:38:58 +08:00
4a292c19db ip -> host 2024-08-30 15:38:58 +08:00
0b115b3394 Lazy evaluating for debug string command 2024-08-30 15:38:58 +08:00
2e904cb498 Add pytec runnables 2024-08-30 15:38:58 +08:00
d3f378f058 PYTHON shell 2024-08-30 15:38:58 +08:00
d0273cd41b Exactlier wording 2024-08-30 15:38:58 +08:00
0988ed24d6 unused 2024-08-30 15:38:58 +08:00
ab3b26a2a4 encoding 2024-08-30 15:38:58 +08:00
000b97398b Just catch asyncio.TimeoutError
Will just change to TimeoutError once we switch to Python 3.11 in the
flake.
2024-08-30 15:38:58 +08:00
e162bcf210 Remove exception too general 2024-08-30 15:38:58 +08:00
c3d7804898 Use asserts to check for connectivity 2024-08-30 15:38:58 +08:00
9c0977262c Add back the parent 2024-08-30 15:38:58 +08:00
017206bd34 Fix method call 2024-08-30 15:38:58 +08:00
f46bd2b50e README: Proofread 2024-08-30 15:38:58 +08:00
5b48fb2793 Swap order arounda bit more 2024-08-30 15:38:58 +08:00
358403e651 Formatting 2024-08-30 15:38:58 +08:00
4a48379923 Use qtextras 2024-08-30 15:38:58 +08:00
b766e949e8 flake update 2024-08-30 15:38:58 +08:00
21 changed files with 716 additions and 562 deletions

View File

@ -71,9 +71,8 @@ openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "program target/thumbv
A GUI has been developed for easy configuration and plotting of key parameters. A GUI has been developed for easy configuration and plotting of key parameters.
The Python GUI program is located at pytec/tec_qt.py. The Python GUI program is located at pytec/tec_qt.py, and is developed based on the Python library pyqtgraph. The GUI can be configured and
launched automatically by running:
The GUI is developed based on the Python library pyqtgraph. The GUI can be configured and launched automatically by running:
``` ```
nix run .#thermostat_gui nix run .#thermostat_gui

12
flake.lock generated
View File

@ -3,11 +3,11 @@
"mozilla-overlay": { "mozilla-overlay": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1690536331, "lastModified": 1704373101,
"narHash": "sha256-aRIf2FB2GTdfF7gl13WyETmiV/J7EhBGkSWXfZvlxcA=", "narHash": "sha256-+gi59LRWRQmwROrmE1E2b3mtocwueCQqZ60CwLG+gbg=",
"owner": "mozilla", "owner": "mozilla",
"repo": "nixpkgs-mozilla", "repo": "nixpkgs-mozilla",
"rev": "db89c8707edcffefcd8e738459d511543a339ff5", "rev": "9b11a87c0cc54e308fa83aac5b4ee1816d5418a2",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -18,11 +18,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1691421349, "lastModified": 1704290814,
"narHash": "sha256-RRJyX0CUrs4uW4gMhd/X4rcDG8PTgaaCQM5rXEJOx6g=", "narHash": "sha256-LWvKHp7kGxk/GEtlrGYV68qIvPHkU9iToomNFGagixU=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "011567f35433879aae5024fc6ec53f2a0568a6c4", "rev": "70bdadeb94ffc8806c0570eb5c2695ad29f0e421",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -85,6 +85,22 @@
propagatedBuildInputs = with pkgs.python3Packages; [ numpy pyqt6 ]; propagatedBuildInputs = with pkgs.python3Packages; [ numpy pyqt6 ];
}; };
qtextras = pkgs.python3Packages.buildPythonPackage rec {
pname = "qtextras";
version = "0.6.8";
format = "pyproject";
src = pkgs.fetchPypi {
inherit pname version;
hash = "sha256-d1ZotSlOI4surUy0H0N4xHoq94IRQvMHunwRH1uubFg=";
};
buildInputs = [ pkgs.python3Packages.hatchling ];
propagatedBuildInputs = with pkgs.python3Packages; [
numpy
pyqtgraph
ruamel-yaml
];
};
pglive = pkgs.python3Packages.buildPythonPackage rec { pglive = pkgs.python3Packages.buildPythonPackage rec {
pname = "pglive"; pname = "pglive";
version = "0.7.2"; version = "0.7.2";
@ -97,6 +113,19 @@
propagatedBuildInputs = [ pyqtgraph pkgs.python3Packages.numpy ]; propagatedBuildInputs = [ pyqtgraph pkgs.python3Packages.numpy ];
}; };
pytec-dev-wrappers = pkgs.runCommandNoCC "pytec-dev-wrappers" { } ''
mkdir -p $out/bin
for program in ${self}/pytec/*.py; do
if [ -x $program ]; then
progname=`basename -s .py $program`
outname=$out/bin/$progname
echo "#!${pkgs.bash}/bin/bash" >> $outname
echo "exec python3 -m pytec.$progname \"\$@\"" >> $outname
chmod 755 $outname
fi
done
'';
thermostat_gui = pkgs.python3Packages.buildPythonPackage { thermostat_gui = pkgs.python3Packages.buildPythonPackage {
pname = "thermostat_gui"; pname = "thermostat_gui";
version = "0.0.0"; version = "0.0.0";
@ -105,7 +134,13 @@
nativeBuildInputs = [ pkgs.qt6.wrapQtAppsHook ]; nativeBuildInputs = [ pkgs.qt6.wrapQtAppsHook ];
propagatedBuildInputs = [ pkgs.qt6.qtbase ] propagatedBuildInputs = [ pkgs.qt6.qtbase ]
++ (with pkgs.python3Packages; [ pyqtgraph pyqt6 qasync pglive ]); ++ (with pkgs.python3Packages; [
pyqtgraph
pyqt6
qasync
pglive
qtextras
]);
dontWrapQtApps = true; dontWrapQtApps = true;
postFixup = '' postFixup = ''
@ -127,7 +162,8 @@
devShell.x86_64-linux = pkgs.mkShell { devShell.x86_64-linux = pkgs.mkShell {
name = "thermostat-dev-shell"; name = "thermostat-dev-shell";
buildInputs = with pkgs; buildInputs = with pkgs;
[ rust openocd dfu-util ] ++ (with python3Packages; [ [ rust openocd dfu-util pytec-dev-wrappers ]
++ (with python3Packages; [
numpy numpy
matplotlib matplotlib
pyqtgraph pyqtgraph
@ -135,7 +171,11 @@
pyqt6 pyqt6
qasync qasync
pglive pglive
qtextras
]); ]);
shellHook = ''
export PYTHONPATH=`pwd`/pytec:$PYTHONPATH
'';
}; };
defaultPackage.x86_64-linux = thermostat; defaultPackage.x86_64-linux = thermostat;
}; };

3
pytec/.flake8 Normal file
View File

@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
extend-ignore = E203,E701

98
pytec/autotune.py Normal file → Executable file
View File

@ -12,16 +12,16 @@ from pytec.client import Client
class PIDAutotuneState(Enum): class PIDAutotuneState(Enum):
STATE_OFF = 'off' STATE_OFF = "off"
STATE_RELAY_STEP_UP = 'relay step up' STATE_RELAY_STEP_UP = "relay step up"
STATE_RELAY_STEP_DOWN = 'relay step down' STATE_RELAY_STEP_DOWN = "relay step down"
STATE_SUCCEEDED = 'succeeded' STATE_SUCCEEDED = "succeeded"
STATE_FAILED = 'failed' STATE_FAILED = "failed"
STATE_READY = 'ready' STATE_READY = "ready"
class PIDAutotune: class PIDAutotune:
PIDParams = namedtuple('PIDParams', ['Kp', 'Ki', 'Kd']) PIDParams = namedtuple("PIDParams", ["Kp", "Ki", "Kd"])
PEAK_AMPLITUDE_TOLERANCE = 0.05 PEAK_AMPLITUDE_TOLERANCE = 0.05
@ -31,13 +31,14 @@ class PIDAutotune:
"ciancone-marlin": [0.303, 0.1364, 0.0481], "ciancone-marlin": [0.303, 0.1364, 0.0481],
"pessen-integral": [0.7, 1.75, 0.105], "pessen-integral": [0.7, 1.75, 0.105],
"some-overshoot": [0.333, 0.667, 0.111], "some-overshoot": [0.333, 0.667, 0.111],
"no-overshoot": [0.2, 0.4, 0.0667] "no-overshoot": [0.2, 0.4, 0.0667],
} }
def __init__(self, setpoint, out_step=10, lookback=60, def __init__(
noiseband=0.5, sampletime=1.2): self, setpoint, out_step=10, lookback=60, noiseband=0.5, sampletime=1.2
):
if setpoint is None: if setpoint is None:
raise ValueError('setpoint must be specified') raise ValueError("setpoint must be specified")
self._inputs = deque(maxlen=round(lookback / sampletime)) self._inputs = deque(maxlen=round(lookback / sampletime))
self._setpoint = setpoint self._setpoint = setpoint
@ -84,7 +85,7 @@ class PIDAutotune:
"""Get a list of all available tuning rules.""" """Get a list of all available tuning rules."""
return self._tuning_rules.keys() return self._tuning_rules.keys()
def get_pid_parameters(self, tuning_rule='ziegler-nichols'): def get_pid_parameters(self, tuning_rule="ziegler-nichols"):
"""Get PID parameters. """Get PID parameters.
Args: Args:
@ -97,7 +98,7 @@ class PIDAutotune:
kd = divisors[2] * self._Ku * self._Pu kd = divisors[2] * self._Ku * self._Pu
return PIDAutotune.PIDParams(kp, ki, kd) return PIDAutotune.PIDParams(kp, ki, kd)
def get_tec_pid (self): def get_tec_pid(self):
divisors = self._tuning_rules["tyreus-luyben"] divisors = self._tuning_rules["tyreus-luyben"]
kp = self._Ku * divisors[0] kp = self._Ku * divisors[0]
ki = divisors[1] * self._Ku / self._Pu ki = divisors[1] * self._Ku / self._Pu
@ -116,28 +117,34 @@ class PIDAutotune:
""" """
now = time_input * 1000 now = time_input * 1000
if (self._state == PIDAutotuneState.STATE_OFF if (
self._state == PIDAutotuneState.STATE_OFF
or self._state == PIDAutotuneState.STATE_SUCCEEDED or self._state == PIDAutotuneState.STATE_SUCCEEDED
or self._state == PIDAutotuneState.STATE_FAILED or self._state == PIDAutotuneState.STATE_FAILED
or self._state == PIDAutotuneState.STATE_READY): or self._state == PIDAutotuneState.STATE_READY
):
self._state = PIDAutotuneState.STATE_RELAY_STEP_UP self._state = PIDAutotuneState.STATE_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 (
and input_val > self._setpoint + self._noiseband): self._state == PIDAutotuneState.STATE_RELAY_STEP_UP
and input_val > self._setpoint + self._noiseband
):
self._state = PIDAutotuneState.STATE_RELAY_STEP_DOWN self._state = PIDAutotuneState.STATE_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 (
and input_val < self._setpoint - self._noiseband): self._state == PIDAutotuneState.STATE_RELAY_STEP_DOWN
and input_val < self._setpoint - self._noiseband
):
self._state = PIDAutotuneState.STATE_RELAY_STEP_UP self._state = PIDAutotuneState.STATE_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.STATE_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.STATE_RELAY_STEP_DOWN:
self._output = self._initial_output + self._outputstep self._output = self._initial_output + self._outputstep
@ -180,8 +187,8 @@ class PIDAutotune:
self._peak_count += 1 self._peak_count += 1
self._peaks.append(input_val) self._peaks.append(input_val)
self._peak_timestamps.append(now) self._peak_timestamps.append(now)
logging.debug('found peak: {0}'.format(input_val)) logging.debug("found peak: {0}".format(input_val))
logging.debug('peak count: {0}'.format(self._peak_count)) logging.debug("peak count: {0}".format(self._peak_count))
# check for convergence of induced oscillation # check for convergence of induced oscillation
# convergence of amplitude assessed on last 4 peaks (1.5 cycles) # convergence of amplitude assessed on last 4 peaks (1.5 cycles)
@ -191,20 +198,19 @@ class PIDAutotune:
abs_max = self._peaks[-2] abs_max = self._peaks[-2]
abs_min = self._peaks[-2] abs_min = self._peaks[-2]
for i in range(0, len(self._peaks) - 2): for i in range(0, len(self._peaks) - 2):
self._induced_amplitude += abs(self._peaks[i] self._induced_amplitude += abs(self._peaks[i] - self._peaks[i + 1])
- self._peaks[i+1])
abs_max = max(self._peaks[i], abs_max) abs_max = max(self._peaks[i], abs_max)
abs_min = min(self._peaks[i], abs_min) abs_min = min(self._peaks[i], abs_min)
self._induced_amplitude /= 6.0 self._induced_amplitude /= 6.0
# check convergence criterion for amplitude of induced oscillation # check convergence criterion for amplitude of induced oscillation
amplitude_dev = ((0.5 * (abs_max - abs_min) amplitude_dev = (
- self._induced_amplitude) 0.5 * (abs_max - abs_min) - self._induced_amplitude
/ self._induced_amplitude) ) / self._induced_amplitude
logging.debug('amplitude: {0}'.format(self._induced_amplitude)) logging.debug("amplitude: {0}".format(self._induced_amplitude))
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.STATE_SUCCEEDED
@ -218,25 +224,24 @@ class PIDAutotune:
if self._state == PIDAutotuneState.STATE_SUCCEEDED: if self._state == PIDAutotuneState.STATE_SUCCEEDED:
self._output = 0 self._output = 0
logging.debug('peak finding successful') logging.debug("peak finding successful")
# calculate ultimate gain # calculate ultimate gain
self._Ku = 4.0 * self._outputstep / \ self._Ku = 4.0 * self._outputstep / (self._induced_amplitude * math.pi)
(self._induced_amplitude * math.pi) logging.debug("Ku: {0}".format(self._Ku))
logging.debug('Ku: {0}'.format(self._Ku))
# calculate ultimate period in seconds # calculate ultimate period in seconds
period1 = self._peak_timestamps[3] - self._peak_timestamps[1] period1 = self._peak_timestamps[3] - self._peak_timestamps[1]
period2 = self._peak_timestamps[4] - self._peak_timestamps[2] period2 = self._peak_timestamps[4] - self._peak_timestamps[2]
self._Pu = 0.5 * (period1 + period2) / 1000.0 self._Pu = 0.5 * (period1 + period2) / 1000.0
logging.debug('Pu: {0}'.format(self._Pu)) logging.debug("Pu: {0}".format(self._Pu))
for rule in self._tuning_rules: for rule in self._tuning_rules:
params = self.get_pid_parameters(rule) params = self.get_pid_parameters(rule)
logging.debug('rule: {0}'.format(rule)) logging.debug("rule: {0}".format(rule))
logging.debug('Kp: {0}'.format(params.Kp)) logging.debug("Kp: {0}".format(params.Kp))
logging.debug('Ki: {0}'.format(params.Ki)) logging.debug("Ki: {0}".format(params.Ki))
logging.debug('Kd: {0}'.format(params.Kd)) logging.debug("Kd: {0}".format(params.Kd))
return True return True
return False return False
@ -263,16 +268,17 @@ def main():
data = next(tec.report_mode()) data = next(tec.report_mode())
ch = data[channel] ch = data[channel]
tuner = PIDAutotune(target_temperature, output_step, tuner = PIDAutotune(
lookback, noiseband, ch['interval']) target_temperature, output_step, lookback, noiseband, ch["interval"]
)
for data in tec.report_mode(): for data in tec.report_mode():
ch = data[channel] ch = data[channel]
temperature = ch['temperature'] temperature = ch["temperature"]
if (tuner.run(temperature, ch['time'])): if tuner.run(temperature, ch["time"]):
break break
tuner_out = tuner.output() tuner_out = tuner.output()

View File

@ -1,16 +1,36 @@
import asyncio import asyncio
from pytec.aioclient import Client from contextlib import suppress
from pytec.aioclient import AsyncioClient
async def poll_for_info(tec):
while True:
print(tec.get_pwm())
print(tec.get_steinhart_hart())
print(tec.get_pid())
print(tec.get_postfilter())
print(tec.get_fan())
await asyncio.sleep(1)
async def main(): async def main():
tec = Client() tec = AsyncioClient()
await tec.start_session() #(host="192.168.1.26", port=23) await tec.connect() # (host="192.168.1.26", port=23)
await tec.set_param("s-h", 1, "t0", 20) await tec.set_param("s-h", 1, "t0", 20)
print(await tec.get_pwm()) print(await tec.get_pwm())
print(await tec.get_pid()) print(await tec.get_pid())
print(await tec.get_pwm()) print(await tec.get_pwm())
print(await tec.get_postfilter()) print(await tec.get_postfilter())
print(await tec.get_steinhart_hart()) print(await tec.get_steinhart_hart())
polling_task = asyncio.create_task(poll_for_info(tec))
async for data in tec.report_mode(): async for data in tec.report_mode():
print(data) print(data)
polling_task.cancel()
with suppress(asyncio.CancelledError):
await polling_task
asyncio.run(main()) asyncio.run(main())

View File

@ -1,6 +1,6 @@
from pytec.client import Client from pytec.client import Client
tec = Client() #(host="localhost", port=6667) tec = Client() # (host="localhost", port=6667)
tec.set_param("s-h", 1, "t0", 20) tec.set_param("s-h", 1, "t0", 20)
print(tec.get_pwm()) print(tec.get_pwm())
print(tec.get_pid()) print(tec.get_pid())

23
pytec/plot.py Normal file → Executable file
View File

@ -7,9 +7,10 @@ from pytec.client import Client
TIME_WINDOW = 300.0 TIME_WINDOW = 300.0
tec = Client() tec = Client()
target_temperature = tec.get_pid()[0]['target'] target_temperature = tec.get_pid()[0]["target"]
print("Channel 0 target temperature: {:.3f}".format(target_temperature)) print("Channel 0 target temperature: {:.3f}".format(target_temperature))
class Series: class Series:
def __init__(self, conv=lambda x: x): def __init__(self, conv=lambda x: x):
self.conv = conv self.conv = conv
@ -27,24 +28,26 @@ class Series:
self.x_data = self.x_data[drop:] self.x_data = self.x_data[drop:]
self.y_data = self.y_data[drop:] self.y_data = self.y_data[drop:]
series = { series = {
# 'adc': Series(), # 'adc': Series(),
# 'sens': Series(lambda x: x * 0.0001), # 'sens': Series(lambda x: x * 0.0001),
'temperature': Series(), "temperature": Series(),
# 'i_set': Series(), # 'i_set': Series(),
'pid_output': Series(), "pid_output": Series(),
# 'vref': Series(), # 'vref': Series(),
# 'dac_value': Series(), # 'dac_value': Series(),
# 'dac_feedback': Series(), # 'dac_feedback': Series(),
# 'i_tec': Series(), # 'i_tec': Series(),
'tec_i': Series(), "tec_i": Series(),
'tec_u_meas': Series(), "tec_u_meas": Series(),
# 'interval': Series(), # 'interval': Series(),
} }
series_lock = Lock() series_lock = Lock()
quit = False quit = False
def recv_data(tec): def recv_data(tec):
global last_packet_time global last_packet_time
for data in tec.report_mode(): for data in tec.report_mode():
@ -55,22 +58,24 @@ def recv_data(tec):
if k in ch0: if k in ch0:
v = ch0[k] v = ch0[k]
if type(v) is float: if type(v) is float:
s.append(ch0['time'], v) s.append(ch0["time"], v)
finally: finally:
series_lock.release() series_lock.release()
if quit: if quit:
break break
thread = Thread(target=recv_data, args=(tec,)) thread = Thread(target=recv_data, args=(tec,))
thread.start() thread.start()
fig, ax = plt.subplots() fig, ax = plt.subplots()
for k, s in series.items(): for k, s in series.items():
s.plot, = ax.plot([], [], label=k) (s.plot,) = ax.plot([], [], label=k)
legend = ax.legend() legend = ax.legend()
def animate(i): def animate(i):
min_x, max_x, min_y, max_y = None, None, None, None min_x, max_x, min_y, max_y = None, None, None, None
@ -120,8 +125,8 @@ def animate(i):
legend.remove() legend.remove()
legend = ax.legend() legend = ax.legend()
ani = animation.FuncAnimation(
fig, animate, interval=1, blit=False, save_count=50) ani = animation.FuncAnimation(fig, animate, interval=1, blit=False, save_count=50)
plt.show() plt.show()
quit = True quit = True

View File

@ -16,3 +16,6 @@ tec_qt = "tec_qt:main"
[tool.setuptools] [tool.setuptools]
packages.find = {} packages.find = {}
py-modules = ["autotune", "plot", "tec_qt"] py-modules = ["autotune", "plot", "tec_qt"]
[tool.pylint.format]
max-line-length = "88"

View File

@ -2,58 +2,34 @@ import asyncio
import json import json
import logging import logging
class CommandError(Exception): class CommandError(Exception):
pass pass
class StoppedConnecting(Exception):
pass
class Client: class AsyncioClient:
def __init__(self): def __init__(self):
self._reader = None self._reader = None
self._writer = None self._writer = None
self._connecting_task = None
self._command_lock = asyncio.Lock() self._command_lock = asyncio.Lock()
self._report_mode_on = False self._report_mode_on = False
self.timeout = None
async def start_session(self, host='192.168.1.26', port=23, timeout=None): async def connect(self, host="192.168.1.26", port=23):
"""Start session to Thermostat at specified host and port. """Connect to Thermostat at specified host and port.
Throws StoppedConnecting if disconnect was called while connecting.
Throws asyncio.TimeoutError if timeout was exceeded.
Example:: Example::
client = Client() client = AsyncioClient()
try: await client.connect()
await client.start_session()
except StoppedConnecting:
print("Stopped connecting")
""" """
self._connecting_task = asyncio.create_task( self._reader, self._writer = await asyncio.open_connection(host, port)
asyncio.wait_for(asyncio.open_connection(host, port), timeout)
)
self.timeout = timeout
try:
self._reader, self._writer = await self._connecting_task
except asyncio.CancelledError:
raise StoppedConnecting
finally:
self._connecting_task = None
await self._check_zero_limits() await self._check_zero_limits()
def connecting(self):
"""Returns True if client is connecting"""
return self._connecting_task is not None
def connected(self): def connected(self):
"""Returns True if client is connected""" """Returns True if client is connected"""
return self._writer is not None return self._writer is not None
async def end_session(self): async def disconnect(self):
"""End session to Thermostat if connected, cancel connection if connecting""" """Disconnect from the Thermostat"""
if self._connecting_task is not None:
self._connecting_task.cancel()
if self._writer is None: if self._writer is None:
return return
@ -69,26 +45,29 @@ class Client:
for pwm_channel in pwm_report: for pwm_channel in pwm_report:
for limit in ["max_i_neg", "max_i_pos", "max_v"]: for limit in ["max_i_neg", "max_i_pos", "max_v"]:
if pwm_channel[limit]["value"] == 0.0: if pwm_channel[limit]["value"] == 0.0:
logging.warning("`{}` limit is set to zero on channel {}".format(limit, pwm_channel["channel"])) logging.warning(
"`{}` limit is set to zero on channel {}".format(
limit, pwm_channel["channel"]
)
)
async def _read_line(self): async def _read_line(self):
# read 1 line # read 1 line
chunk = await asyncio.wait_for(self._reader.readline(), self.timeout) # Only wait for response until timeout chunk = await self._reader.readline()
return chunk.decode('utf-8', errors='ignore') return chunk.decode("utf-8", errors="ignore")
async def _read_write(self, command): async def _read_write(self, command):
self._writer.write(((" ".join(command)).strip() + "\n").encode('utf-8')) self._writer.write(((" ".join(command)).strip() + "\n").encode("utf-8"))
await self._writer.drain() await self._writer.drain()
return await self._read_line() return await self._read_line()
async def _command(self, *command): async def _command(self, *command):
async with self._command_lock: async with self._command_lock:
# protect the read-write process from being cancelled midway line = await self._read_write(command)
line = await asyncio.shield(self._read_write(command))
response = json.loads(line) response = json.loads(line)
logging.debug(f"{command}: {response}") logging.debug("%s: %s", command, response)
if "error" in response: if "error" in response:
raise CommandError(response["error"]) raise CommandError(response["error"])
return response return response
@ -239,6 +218,8 @@ class Client:
async def save_config(self, channel=""): async def save_config(self, channel=""):
"""Save current configuration to EEPROM""" """Save current configuration to EEPROM"""
await self._command("save", str(channel)) await self._command("save", str(channel))
if channel == "":
await self._read_line() # Read the extra {}
async def load_config(self, channel=""): async def load_config(self, channel=""):
"""Load current configuration from EEPROM""" """Load current configuration from EEPROM"""
@ -256,24 +237,24 @@ class Client:
The client is disconnected as the TCP session is terminated. The client is disconnected as the TCP session is terminated.
""" """
async with self._command_lock: async with self._command_lock:
self._writer.write("reset\n".encode('utf-8')) self._writer.write("reset\n".encode("utf-8"))
await self._writer.drain() await self._writer.drain()
await self.end_session() await self.disconnect()
async def dfu(self): async def dfu(self):
"""Put the Thermostat in DFU update mode """Put the Thermostat in DFU mode
The client is disconnected as the Thermostat stops responding to The client is disconnected as the Thermostat stops responding to
TCP commands in DFU update mode. The only way to exit it is by TCP commands in DFU mode. To exit it, submit a DFU leave request
power-cycling. or power-cycle the Thermostat.
""" """
async with self._command_lock: async with self._command_lock:
self._writer.write("dfu\n".encode('utf-8')) self._writer.write("dfu\n".encode("utf-8"))
await self._writer.drain() await self._writer.drain()
await self.end_session() await self.disconnect()
async def ipv4(self): async def ipv4(self):
"""Get the IPv4 settings of the Thermostat""" """Get the IPv4 settings of the Thermostat"""
return await self._command('ipv4') return await self._command("ipv4")

View File

@ -2,9 +2,11 @@ import socket
import json import json
import logging import logging
class CommandError(Exception): class CommandError(Exception):
pass pass
class Client: class Client:
def __init__(self, host="192.168.1.26", port=23, timeout=None): def __init__(self, host="192.168.1.26", port=23, timeout=None):
self._socket = socket.create_connection((host, port), timeout) self._socket = socket.create_connection((host, port), timeout)
@ -20,7 +22,11 @@ class Client:
for pwm_channel in pwm_report: for pwm_channel in pwm_report:
for limit in ["max_i_neg", "max_i_pos", "max_v"]: for limit in ["max_i_neg", "max_i_pos", "max_v"]:
if pwm_channel[limit]["value"] == 0.0: if pwm_channel[limit]["value"] == 0.0:
logging.warning("`{}` limit is set to zero on channel {}".format(limit, pwm_channel["channel"])) logging.warning(
"`{}` limit is set to zero on channel {}".format(
limit, pwm_channel["channel"]
)
)
def _read_line(self): def _read_line(self):
# read more lines # read more lines
@ -28,7 +34,7 @@ class Client:
chunk = self._socket.recv(4096) chunk = self._socket.recv(4096)
if not chunk: if not chunk:
return None return None
buf = self._lines[-1] + chunk.decode('utf-8', errors='ignore') buf = self._lines[-1] + chunk.decode("utf-8", errors="ignore")
self._lines = buf.split("\n") self._lines = buf.split("\n")
line = self._lines[0] line = self._lines[0]
@ -36,7 +42,7 @@ class Client:
return line return line
def _command(self, *command): def _command(self, *command):
self._socket.sendall((" ".join(command) + "\n").encode('utf-8')) self._socket.sendall((" ".join(command) + "\n").encode("utf-8"))
line = self._read_line() line = self._read_line()
response = json.loads(line) response = json.loads(line)

View File

@ -1,13 +1,17 @@
from PyQt6.QtCore import QObject, pyqtSlot from PyQt6.QtCore import QObject, pyqtSlot, pyqtSignal
from qasync import asyncSlot from qasync import asyncSlot
from autotune import PIDAutotuneState, PIDAutotune from autotune import PIDAutotuneState, PIDAutotune
class PIDAutoTuner(QObject): class PIDAutoTuner(QObject):
def __init__(self, parent, client, num_of_channel): autotune_state_changed = pyqtSignal(int, PIDAutotuneState)
super().__init__()
def __init__(self, parent, thermostat, num_of_channel):
super().__init__(parent)
self._thermostat = thermostat
self._thermostat.report_update.connect(self.tick)
self._client = client
self.autotuners = [PIDAutotune(25) for _ in range(num_of_channel)] self.autotuners = [PIDAutotune(25) for _ in range(num_of_channel)]
self.target_temp = [20.0 for _ in range(num_of_channel)] self.target_temp = [20.0 for _ in range(num_of_channel)]
self.test_current = [1.0 for _ in range(num_of_channel)] self.test_current = [1.0 for _ in range(num_of_channel)]
@ -15,10 +19,6 @@ class PIDAutoTuner(QObject):
self.lookback = [3.0 for _ in range(num_of_channel)] self.lookback = [3.0 for _ in range(num_of_channel)]
self.sampling_interval = [1 / 16.67 for _ in range(num_of_channel)] self.sampling_interval = [1 / 16.67 for _ in range(num_of_channel)]
@pyqtSlot(list)
def update_sampling_interval(self, interval):
self.sampling_interval = interval
def set_params(self, params_name, ch, val): def set_params(self, params_name, ch, val):
getattr(self, params_name)[ch] = val getattr(self, params_name)[ch] = val
@ -34,39 +34,51 @@ class PIDAutoTuner(QObject):
self.lookback[ch], self.lookback[ch],
) )
self.autotuners[ch].setReady() self.autotuners[ch].setReady()
self.autotune_state_changed.emit(ch, self.autotuners[ch].state())
async def stop_pid_from_running(self, ch): async def stop_pid_from_running(self, ch):
self.autotuners[ch].setOff() self.autotuners[ch].setOff()
await self._client.set_param("pwm", ch, "i_set", 0) self.autotune_state_changed.emit(ch, self.autotuners[ch].state())
if self._thermostat.connected():
await self._thermostat.set_param("pwm", ch, "i_set", 0)
@asyncSlot(list) @asyncSlot(list)
async def tick(self, report): async def tick(self, report):
for channel_report in report: for channel_report in report:
ch = channel_report["channel"]
self.sampling_interval[ch] = channel_report["interval"]
# TODO: Skip when PID Autotune or emit error message if NTC is not connected # TODO: Skip when PID Autotune or emit error message if NTC is not connected
if channel_report["temperature"] is None: if channel_report["temperature"] is None:
continue continue
ch = channel_report["channel"]
match self.autotuners[ch].state(): match self.autotuners[ch].state():
case PIDAutotuneState.STATE_READY | PIDAutotuneState.STATE_RELAY_STEP_UP | PIDAutotuneState.STATE_RELAY_STEP_DOWN: case (
PIDAutotuneState.STATE_READY
| PIDAutotuneState.STATE_RELAY_STEP_UP
| PIDAutotuneState.STATE_RELAY_STEP_DOWN
):
self.autotuners[ch].run( self.autotuners[ch].run(
channel_report["temperature"], channel_report["time"] channel_report["temperature"], channel_report["time"]
) )
await self._client.set_param( await self._thermostat.set_param(
"pwm", ch, "i_set", self.autotuners[ch].output() "pwm", ch, "i_set", self.autotuners[ch].output()
) )
case PIDAutotuneState.STATE_SUCCEEDED: case PIDAutotuneState.STATE_SUCCEEDED:
kp, ki, kd = self.autotuners[ch].get_tec_pid() kp, ki, kd = self.autotuners[ch].get_tec_pid()
self.autotuners[ch].setOff() self.autotuners[ch].setOff()
self.autotune_state_changed.emit(ch, self.autotuners[ch].state())
await self._client.set_param("pid", ch, "kp", kp) await self._thermostat.set_param("pid", ch, "kp", kp)
await self._client.set_param("pid", ch, "ki", ki) await self._thermostat.set_param("pid", ch, "ki", ki)
await self._client.set_param("pid", ch, "kd", kd) await self._thermostat.set_param("pid", ch, "kd", kd)
await self._client.set_param("pwm", ch, "pid") await self._thermostat.set_param("pwm", ch, "pid")
await self._client.set_param( await self._thermostat.set_param(
"pid", ch, "target", self.target_temp[ch] "pid", ch, "target", self.target_temp[ch]
) )
case PIDAutotuneState.STATE_FAILED: case PIDAutotuneState.STATE_FAILED:
self.autotuners[ch].setOff() self.autotuners[ch].setOff()
await self._client.set_param("pwm", ch, "i_set", 0) self.autotune_state_changed.emit(ch, self.autotuners[ch].state())
await self._thermostat.set_param("pwm", ch, "i_set", 0)

View File

@ -1,20 +1,16 @@
from pytec.aioclient import Client
from PyQt6.QtCore import pyqtSignal, QObject, pyqtSlot from PyQt6.QtCore import pyqtSignal, QObject, pyqtSlot
from qasync import asyncSlot from qasync import asyncSlot
from pytec.gui.model.property import Property, PropertyMeta from pytec.gui.model.property import Property, PropertyMeta
import asyncio import asyncio
import logging import logging
from enum import Enum
from pytec.aioclient import AsyncioClient
class WrappedClient(QObject, Client): class ThermostatConnectionState(Enum):
connection_error = pyqtSignal() DISCONNECTED = "disconnected"
CONNECTING = "connecting"
async def _read_line(self): CONNECTED = "connected"
try:
return await super()._read_line()
except (Exception, TimeoutError, asyncio.exceptions.TimeoutError):
logging.error("Client connection error, disconnecting", exc_info=True)
self.connection_error.emit()
class Thermostat(QObject, metaclass=PropertyMeta): class Thermostat(QObject, metaclass=PropertyMeta):
@ -24,59 +20,81 @@ class Thermostat(QObject, metaclass=PropertyMeta):
pid = Property(list) pid = Property(list)
pwm = Property(list) pwm = Property(list)
postfilter = Property(list) postfilter = Property(list)
interval = Property(list)
report = Property(list) report = Property(list)
info_box_trigger = pyqtSignal(str, str)
def __init__(self, parent, client, update_s): connection_error = pyqtSignal()
connection_state_changed = pyqtSignal(ThermostatConnectionState)
def __init__(self, parent, update_s, disconnect_cb=None):
self._update_s = update_s self._update_s = update_s
self._client = client self._client = AsyncioClient()
self._watch_task = None self._watch_task = None
self._report_mode_task = None self._report_mode_task = None
self._poll_for_report = True self._poll_for_report = True
self._update_params_task = None
self.disconnect_cb = disconnect_cb
super().__init__(parent) super().__init__(parent)
async def start_session(self, host, port):
self.connection_state_changed.emit(ThermostatConnectionState.CONNECTING)
await self._client.connect(host, port)
self.hw_rev = await self.get_hw_rev()
self.connection_state_changed.emit(ThermostatConnectionState.CONNECTED)
self.start_watching()
async def run(self): async def run(self):
self.task = asyncio.create_task(self.update_params()) self._update_params_task = asyncio.create_task(self.update_params())
while True: while True:
if self.task.done(): if self._update_params_task.done():
if self.task.exception() is not None:
try: try:
raise self.task.exception() self._update_params_task.result()
except ( except OSError:
Exception,
TimeoutError,
asyncio.exceptions.TimeoutError,
):
logging.error( logging.error(
"Encountered an error while updating parameter tree.", "Encountered an error while polling for information from Thermostat.",
exc_info=True, exc_info=True,
) )
_ = self.task.result() await self.handle_connection_error()
self.task = asyncio.create_task(self.update_params()) return
self._update_params_task = asyncio.create_task(self.update_params())
await asyncio.sleep(self._update_s) await asyncio.sleep(self._update_s)
async def handle_connection_error(self):
await self.end_session()
self.connection_error.emit()
async def get_hw_rev(self): async def get_hw_rev(self):
self.hw_rev = await self._client.hw_rev() return await self._client.hw_rev()
return self.hw_rev
async def update_params(self): async def update_params(self):
self.fan = await self._client.get_fan()
self.pwm = await self._client.get_pwm()
if self._poll_for_report: if self._poll_for_report:
self.report = await self._client.report() (
self.interval = [ self.fan,
self.report[i]["interval"] for i in range(len(self.report)) self.pwm,
] self.report,
self.pid = await self._client.get_pid() self.pid,
self.thermistor = await self._client.get_steinhart_hart() self.thermistor,
self.postfilter = await self._client.get_postfilter() self.postfilter,
) = await asyncio.gather(
self._client.get_fan(),
self._client.get_pwm(),
self._client.report(),
self._client.get_pid(),
self._client.get_steinhart_hart(),
self._client.get_postfilter(),
)
else:
self.fan, self.pwm, self.pid, self.thermistor, self.postfilter = (
await asyncio.gather(
self._client.get_fan(),
self._client.get_pwm(),
self._client.get_pid(),
self._client.get_steinhart_hart(),
self._client.get_postfilter(),
)
)
def connected(self): def connected(self):
return self._client.connected return self._client.connected()
def connecting(self):
return self._client.connecting
def start_watching(self): def start_watching(self):
self._watch_task = asyncio.create_task(self.run()) self._watch_task = asyncio.create_task(self.run())
@ -87,8 +105,8 @@ class Thermostat(QObject, metaclass=PropertyMeta):
await self.set_report_mode(False) await self.set_report_mode(False)
self._watch_task.cancel() self._watch_task.cancel()
self._watch_task = None self._watch_task = None
self.task.cancel() self._update_params_task.cancel()
self.task = None self._update_params_task = None
async def set_report_mode(self, enabled: bool): async def set_report_mode(self, enabled: bool):
self._poll_for_report = not enabled self._poll_for_report = not enabled
@ -100,12 +118,20 @@ class Thermostat(QObject, metaclass=PropertyMeta):
async def report_mode(self): async def report_mode(self):
async for report in self._client.report_mode(): async for report in self._client.report_mode():
self.report_update.emit(report) self.report_update.emit(report)
self.interval = [
self.report[i]["interval"] for i in range(len(self.report))
]
@asyncSlot()
async def end_session(self): async def end_session(self):
await self._client.end_session() await self.set_report_mode(False)
self.stop_watching()
if self.disconnect_cb is not None:
if asyncio.iscoroutinefunction(self.disconnect_cb):
await self.disconnect_cb()
else:
self.disconnect_cb()
await self._client.disconnect()
self.connection_state_changed.emit(ThermostatConnectionState.DISCONNECTED)
async def set_ipv4(self, ipv4): async def set_ipv4(self, ipv4):
await self._client.set_param("ipv4", ipv4) await self._client.set_param("ipv4", ipv4)
@ -114,18 +140,12 @@ class Thermostat(QObject, metaclass=PropertyMeta):
return await self._client.ipv4() return await self._client.ipv4()
@asyncSlot() @asyncSlot()
async def save_cfg(self, ch): async def save_cfg(self, ch=""):
await self._client.save_config(ch) await self._client.save_config(ch)
self.info_box_trigger.emit(
"Config saved", f"Channel {ch} Config has been saved from flash."
)
@asyncSlot() @asyncSlot()
async def load_cfg(self, ch): async def load_cfg(self, ch=""):
await self._client.load_config(ch) await self._client.load_config(ch)
self.info_box_trigger.emit(
"Config loaded", f"Channel {ch} Config has been loaded from flash."
)
async def dfu(self): async def dfu(self):
await self._client.dfu() await self._client.dfu()
@ -136,3 +156,12 @@ class Thermostat(QObject, metaclass=PropertyMeta):
@pyqtSlot(float) @pyqtSlot(float)
def set_update_s(self, update_s): def set_update_s(self, update_s):
self._update_s = update_s self._update_s = update_s
async def set_fan(self, power="auto"):
await self._client.set_fan(power)
async def get_fan(self):
return await self._client.get_fan()
async def set_param(self, topic, channel, field="", value=""):
await self._client.set_param(topic, channel, field, value)

View File

@ -1,9 +1,17 @@
from PyQt6 import QtWidgets, QtCore from PyQt6 import QtWidgets, QtCore
from PyQt6.QtCore import pyqtSlot
from pytec.gui.model.thermostat import ThermostatConnectionState
class ConnMenu(QtWidgets.QMenu): class ConnMenu(QtWidgets.QMenu):
def __init__(self): def __init__(self, thermostat, connect_btn):
super().__init__() super().__init__()
self._thermostat = thermostat
self._connect_btn = connect_btn
self._thermostat.connection_state_changed.connect(
self.thermostat_state_change_handler
)
self.setTitle("Connection Settings") self.setTitle("Connection Settings")
self.host_set_line = QtWidgets.QLineEdit() self.host_set_line = QtWidgets.QLineEdit()
@ -13,7 +21,7 @@ class ConnMenu(QtWidgets.QMenu):
self.host_set_line.setClearButtonEnabled(True) self.host_set_line.setClearButtonEnabled(True)
def connect_on_enter_press(): def connect_on_enter_press():
self.connect_btn.click() self._connect_btn.click()
self.hide() self.hide()
self.host_set_line.returnPressed.connect(connect_on_enter_press) self.host_set_line.returnPressed.connect(connect_on_enter_press)
@ -54,3 +62,12 @@ class ConnMenu(QtWidgets.QMenu):
exit_action.setDefaultWidget(self.exit_button) exit_action.setDefaultWidget(self.exit_button)
self.addAction(exit_action) self.addAction(exit_action)
self.exit_action = exit_action self.exit_action = exit_action
@pyqtSlot(ThermostatConnectionState)
def thermostat_state_change_handler(self, state):
self.host_set_line.setEnabled(
state == ThermostatConnectionState.DISCONNECTED
)
self.port_set_spin.setEnabled(
state == ThermostatConnectionState.DISCONNECTED
)

View File

@ -1,9 +1,12 @@
from functools import partial
from PyQt6.QtCore import pyqtSignal, QObject, QSignalBlocker, pyqtSlot from PyQt6.QtCore import pyqtSignal, QObject, QSignalBlocker, pyqtSlot
import pyqtgraph.parametertree.parameterTypes as pTypes import pyqtgraph.parametertree.parameterTypes as pTypes
from pyqtgraph.parametertree import ( from pyqtgraph.parametertree import (
Parameter, Parameter,
registerParameterType, registerParameterType,
) )
from qasync import asyncSlot
from autotune import PIDAutotuneState
class MutexParameter(pTypes.ListParameter): class MutexParameter(pTypes.ListParameter):
@ -47,14 +50,18 @@ class CtrlPanel(QObject):
def __init__( def __init__(
self, self,
thermostat,
autotuners,
info_box,
trees_ui, trees_ui,
param_tree, param_tree,
sigTreeStateChanged_handle,
sigActivated_handles,
parent=None, parent=None,
): ):
super().__init__(parent) super().__init__(parent)
self.thermostat = thermostat
self.autotuners = autotuners
self.info_box = info_box
self.trees_ui = trees_ui self.trees_ui = trees_ui
self.NUM_CHANNELS = len(trees_ui) self.NUM_CHANNELS = len(trees_ui)
@ -77,10 +84,24 @@ class CtrlPanel(QObject):
tree.setHeaderHidden(True) tree.setHeaderHidden(True)
tree.setParameters(self.params[i], showTop=False) tree.setParameters(self.params[i], showTop=False)
self.params[i].setValue = self._setValue self.params[i].setValue = self._setValue
self.params[i].sigTreeStateChanged.connect(sigTreeStateChanged_handle) self.params[i].sigTreeStateChanged.connect(self.send_command)
for handle in sigActivated_handles[i]: self.params[i].child("Save to flash").sigActivated.connect(
self.params[i].child(*handle[0]).sigActivated.connect(handle[1]) partial(self.save_settings, i)
)
self.params[i].child("Load from flash").sigActivated.connect(
partial(self.load_settings, i)
)
self.params[i].child(
"PID Config", "PID Auto Tune", "Run"
).sigActivated.connect(partial(self.pid_auto_tune_request, i))
self.thermostat.pid_update.connect(self.update_pid)
self.thermostat.report_update.connect(self.update_report)
self.thermostat.thermistor_update.connect(self.update_thermistor)
self.thermostat.pwm_update.connect(self.update_pwm)
self.thermostat.postfilter_update.connect(self.update_postfilter)
self.autotuners.autotune_state_changed.connect(self.update_pid_autotune)
def _setValue(self, value, blockSignal=None): def _setValue(self, value, blockSignal=None):
""" """
@ -111,6 +132,44 @@ class CtrlPanel(QObject):
def change_params_title(self, channel, path, title): def change_params_title(self, channel, path, title):
self.params[channel].child(*path).setOpts(title=title) self.params[channel].child(*path).setOpts(title=title)
@asyncSlot(object, object)
async def send_command(self, param, changes):
"""Translates parameter tree changes into thermostat set_param calls"""
ch = param.channel
for inner_param, change, data in changes:
if change == "value":
if inner_param.opts.get("param", None) is not None:
if inner_param.opts.get("suffix", None) == "mA":
data /= 1000 # Given in mA
thermostat_param = inner_param.opts["param"]
if thermostat_param[1] == "ch":
thermostat_param[1] = ch
if inner_param.name() == "Postfilter Rate" and data is None:
set_param_args = (*thermostat_param[:2], "off")
else:
set_param_args = (*thermostat_param, data)
param.child(*param.childPath(inner_param)).setOpts(lock=True)
await self.thermostat.set_param(*set_param_args)
param.child(*param.childPath(inner_param)).setOpts(lock=False)
if inner_param.opts.get("pid_autotune", None) is not None:
auto_tuner_param = inner_param.opts["pid_autotune"][0]
if inner_param.opts["pid_autotune"][1] != "ch":
ch = inner_param.opts["pid_autotune"][1]
self.autotuners.set_params(auto_tuner_param, ch, data)
if inner_param.opts.get("activaters", None) is not None:
activater = inner_param.opts["activaters"][
inner_param.opts["limits"].index(data)
]
if activater is not None:
if activater[1] == "ch":
activater[1] = ch
await self.thermostat.set_param(*activater)
@pyqtSlot("QVariantList") @pyqtSlot("QVariantList")
def update_pid(self, pid_settings): def update_pid(self, pid_settings):
for settings in pid_settings: for settings in pid_settings:
@ -200,3 +259,61 @@ class CtrlPanel(QObject):
self.params[channel].child( self.params[channel].child(
"Thermistor Config", "Postfilter Rate" "Thermistor Config", "Postfilter Rate"
).setValue(postfilter_params["rate"]) ).setValue(postfilter_params["rate"])
def update_pid_autotune(self, ch, state):
match state:
case PIDAutotuneState.STATE_OFF:
self.change_params_title(
ch, ("PID Config", "PID Auto Tune", "Run"), "Run"
)
case (
PIDAutotuneState.STATE_READY
| PIDAutotuneState.STATE_RELAY_STEP_UP
| PIDAutotuneState.STATE_RELAY_STEP_DOWN
):
self.change_params_title(
ch, ("PID Config", "PID Auto Tune", "Run"), "Stop"
)
case PIDAutotuneState.STATE_SUCCEEDED:
self.info_box.display_info_box(
"PID Autotune Success",
f"Channel {ch} PID Config has been loaded to Thermostat. Regulating temperature.",
)
case PIDAutotuneState.STATE_FAILED:
self.info_box.display_info_box(
"PID Autotune Failed",
f"Channel {ch} PID Autotune has failed.",
)
@asyncSlot(int)
async def load_settings(self, ch):
await self.thermostat.load_cfg(ch)
self.info_box.display_info_box(
f"Channel {ch} settings loaded",
f"Channel {ch} settings has been loaded from flash.",
)
@asyncSlot(int)
async def save_settings(self, ch):
await self.thermostat.save_cfg(ch)
self.info_box.display_info_box(
f"Channel {ch} settings saved",
f"Channel {ch} settings has been saved to flash.\n"
"It will be loaded on Thermostat reset, or when settings are explicitly loaded.",
)
@asyncSlot()
async def pid_auto_tune_request(self, ch=0):
match self.autotuners.get_state(ch):
case PIDAutotuneState.STATE_OFF | PIDAutotuneState.STATE_FAILED:
self.autotuners.load_params_and_set_ready(ch)
case (
PIDAutotuneState.STATE_READY
| PIDAutotuneState.STATE_RELAY_STEP_UP
| PIDAutotuneState.STATE_RELAY_STEP_DOWN
):
await self.autotuners.stop_pid_from_running(ch)

View File

@ -5,13 +5,21 @@ from pglive.sources.live_plot import LiveLinePlot
from pglive.sources.live_axis import LiveAxis from pglive.sources.live_axis import LiveAxis
from collections import deque from collections import deque
import pyqtgraph as pg import pyqtgraph as pg
from pytec.gui.model.thermostat import ThermostatConnectionState
pg.setConfigOptions(antialias=True) pg.setConfigOptions(antialias=True)
class LiveDataPlotter(QObject): class LiveDataPlotter(QObject):
def __init__(self, live_plots): def __init__(self, thermostat, live_plots):
super().__init__() super().__init__()
self._thermostat = thermostat
self._thermostat.report_update.connect(self.update_report)
self._thermostat.pid_update.connect(self.update_pid)
self._thermostat.connection_state_changed.connect(
self.thermostat_state_change_handler
)
self.NUM_CHANNELS = len(live_plots) self.NUM_CHANNELS = len(live_plots)
self.graphs = [] self.graphs = []
@ -21,6 +29,11 @@ class LiveDataPlotter(QObject):
live_plot[1].setTitle(f"Channel {i} Current") live_plot[1].setTitle(f"Channel {i} Current")
self.graphs.append(_TecGraphs(live_plot[0], live_plot[1])) self.graphs.append(_TecGraphs(live_plot[0], live_plot[1]))
@pyqtSlot(ThermostatConnectionState)
def thermostat_state_change_handler(self, state):
if state == ThermostatConnectionState.DISCONNECTED:
self.clear_graphs()
def _config_connector_max_pts(self, connector, samples): def _config_connector_max_pts(self, connector, samples):
connector.max_points = samples connector.max_points = samples
connector.x = deque(maxlen=int(connector.max_points)) connector.x = deque(maxlen=int(connector.max_points))
@ -67,9 +80,7 @@ class _TecGraphs:
self._t_line = self._t_widget.getPlotItem().addLine(label="{value} °C") self._t_line = self._t_widget.getPlotItem().addLine(label="{value} °C")
self._t_line.setVisible(False) self._t_line.setVisible(False)
# Hack for keeping setpoint line in plot range # Hack for keeping setpoint line in plot range
self._t_setpoint_plot = ( self._t_setpoint_plot = LiveLinePlot()
LiveLinePlot()
)
for graph in t_widget, i_widget: for graph in t_widget, i_widget:
time_axis = LiveAxis( time_axis = LiveAxis(

View File

@ -2,18 +2,22 @@ from PyQt6 import QtWidgets, QtGui
class PlotOptionsMenu(QtWidgets.QMenu): class PlotOptionsMenu(QtWidgets.QMenu):
def __init__(self, max_samples=1000): def __init__(self, channel_graphs, max_samples=1000):
super().__init__() super().__init__()
self.channel_graphs = channel_graphs
self.setTitle("Plot Settings") self.setTitle("Plot Settings")
clear = QtGui.QAction("Clear graphs", self) clear = QtGui.QAction("Clear graphs", self)
self.addAction(clear) self.addAction(clear)
self.clear = clear self.clear = clear
self.clear.triggered.connect(self.channel_graphs.clear_graphs)
self.samples_spinbox = QtWidgets.QSpinBox() self.samples_spinbox = QtWidgets.QSpinBox()
self.samples_spinbox.setRange(2, 100000) self.samples_spinbox.setRange(2, 100000)
self.samples_spinbox.setSuffix(" samples") self.samples_spinbox.setSuffix(" samples")
self.samples_spinbox.setValue(max_samples) self.samples_spinbox.setValue(max_samples)
self.samples_spinbox.valueChanged.connect(self.channel_graphs.set_max_samples)
limit_samples = QtWidgets.QWidgetAction(self) limit_samples = QtWidgets.QWidgetAction(self)
limit_samples.setDefaultWidget(self.samples_spinbox) limit_samples.setDefaultWidget(self.samples_spinbox)

View File

@ -1,24 +1,24 @@
import logging
from PyQt6 import QtWidgets, QtGui, QtCore from PyQt6 import QtWidgets, QtGui, QtCore
from PyQt6.QtCore import pyqtSignal, pyqtSlot from PyQt6.QtCore import pyqtSignal, pyqtSlot, QSignalBlocker
from qasync import asyncSlot
from pytec.gui.view.net_settings_input_diag import NetSettingsInputDiag
from pytec.gui.model.thermostat import ThermostatConnectionState
class ThermostatCtrlMenu(QtWidgets.QMenu): class ThermostatCtrlMenu(QtWidgets.QMenu):
fan_set_act = pyqtSignal(int) def __init__(self, thermostat, info_box, style):
fan_auto_set_act = pyqtSignal(int)
connect_act = pyqtSignal()
reset_act = pyqtSignal(bool)
dfu_act = pyqtSignal(bool)
load_cfg_act = pyqtSignal(int)
save_cfg_act = pyqtSignal(int)
net_cfg_act = pyqtSignal(bool)
def __init__(self, style):
super().__init__() super().__init__()
self._thermostat = thermostat
self._info_box = info_box
self._style = style self._style = style
self.setTitle("Thermostat settings") self.setTitle("Thermostat settings")
self.hw_rev_data = dict() self.hw_rev_data = dict()
self._thermostat.hw_rev_update.connect(self.hw_rev)
self._thermostat.connection_state_changed.connect(
self.thermostat_state_change_handler
)
self.fan_group = QtWidgets.QWidget() self.fan_group = QtWidgets.QWidget()
self.fan_group.setEnabled(False) self.fan_group.setEnabled(False)
@ -45,8 +45,9 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
self.fan_pwm_warning.setMinimumSize(QtCore.QSize(16, 0)) self.fan_pwm_warning.setMinimumSize(QtCore.QSize(16, 0))
self.fan_layout.addWidget(self.fan_pwm_warning) self.fan_layout.addWidget(self.fan_pwm_warning)
self.fan_power_slider.valueChanged.connect(self.fan_set_act) self.fan_power_slider.valueChanged.connect(self.fan_set_request)
self.fan_auto_box.stateChanged.connect(self.fan_auto_set_act) self.fan_auto_box.stateChanged.connect(self.fan_auto_set_request)
self._thermostat.fan_update.connect(self.fan_update)
self.fan_lbl.setToolTip("Adjust the fan") self.fan_lbl.setToolTip("Adjust the fan")
self.fan_lbl.setText("Fan:") self.fan_lbl.setText("Fan:")
@ -58,40 +59,36 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
self.fan = fan self.fan = fan
self.actionReset = QtGui.QAction("Reset Thermostat", self) self.actionReset = QtGui.QAction("Reset Thermostat", self)
self.actionReset.triggered.connect(self.reset_act) self.actionReset.triggered.connect(self.reset_request)
self.addAction(self.actionReset) self.addAction(self.actionReset)
self.actionEnter_DFU_Mode = QtGui.QAction("Enter DFU Mode", self) self.actionEnter_DFU_Mode = QtGui.QAction("Enter DFU Mode", self)
self.actionEnter_DFU_Mode.triggered.connect(self.dfu_act) self.actionEnter_DFU_Mode.triggered.connect(self.dfu_request)
self.addAction(self.actionEnter_DFU_Mode) self.addAction(self.actionEnter_DFU_Mode)
self.actionnet_settings_input_diag = QtGui.QAction("Set IPV4 Settings", self) self.actionnet_settings_input_diag = QtGui.QAction("Set IPV4 Settings", self)
self.actionnet_settings_input_diag.triggered.connect(self.net_cfg_act) self.actionnet_settings_input_diag.triggered.connect(self.net_settings_request)
self.addAction(self.actionnet_settings_input_diag) self.addAction(self.actionnet_settings_input_diag)
@pyqtSlot(bool) @asyncSlot(bool)
def load(_): async def load(_):
self.load_cfg_act.emit(0) await self._thermostat.load_cfg()
self.load_cfg_act.emit(1)
loaded = QtWidgets.QMessageBox(self) self._info_box.display_info_box(
loaded.setWindowTitle("Config loaded") "Config loaded", "All channel configs have been loaded from flash."
loaded.setText("All channel configs have been loaded from flash.") )
loaded.setIcon(QtWidgets.QMessageBox.Icon.Information)
loaded.show()
self.actionLoad_all_configs = QtGui.QAction("Load Config", self) self.actionLoad_all_configs = QtGui.QAction("Load Config", self)
self.actionLoad_all_configs.triggered.connect(load) self.actionLoad_all_configs.triggered.connect(load)
self.addAction(self.actionLoad_all_configs) self.addAction(self.actionLoad_all_configs)
@pyqtSlot(bool) @asyncSlot(bool)
def save(_): async def save(_):
self.save_cfg_act.emit(0) await self._thermostat.save_cfg()
self.save_cfg_act.emit(1)
saved = QtWidgets.QMessageBox(self) self._info_box.display_info_box(
saved.setWindowTitle("Config saved") "Config saved", "All channel configs have been saved to flash."
saved.setText("All channel configs have been saved to flash.") )
saved.setIcon(QtWidgets.QMessageBox.Icon.Information)
saved.show()
self.actionSave_all_configs = QtGui.QAction("Save Config", self) self.actionSave_all_configs = QtGui.QAction("Save Config", self)
self.actionSave_all_configs.triggered.connect(save) self.actionSave_all_configs.triggered.connect(save)
@ -127,6 +124,18 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
self.actionAbout_Thermostat.triggered.connect(about_thermostat) self.actionAbout_Thermostat.triggered.connect(about_thermostat)
self.addAction(self.actionAbout_Thermostat) self.addAction(self.actionAbout_Thermostat)
@pyqtSlot("QVariantMap")
def fan_update(self, fan_settings):
logging.debug(fan_settings)
if fan_settings is None:
return
with QSignalBlocker(self.fan_power_slider):
self.fan_power_slider.setValue(
fan_settings["fan_pwm"] or 100 # 0 = PWM off = full strength
)
with QSignalBlocker(self.fan_auto_box):
self.fan_auto_box.setChecked(fan_settings["auto_mode"])
def set_fan_pwm_warning(self): def set_fan_pwm_warning(self):
if self.fan_power_slider.value() != 100: if self.fan_power_slider.value() != 100:
pixmapi = getattr(QtWidgets.QStyle.StandardPixmap, "SP_MessageBoxWarning") pixmapi = getattr(QtWidgets.QStyle.StandardPixmap, "SP_MessageBoxWarning")
@ -139,7 +148,65 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
self.fan_pwm_warning.setPixmap(QtGui.QPixmap()) self.fan_pwm_warning.setPixmap(QtGui.QPixmap())
self.fan_pwm_warning.setToolTip("") self.fan_pwm_warning.setToolTip("")
@pyqtSlot(ThermostatConnectionState)
def thermostat_state_change_handler(self, state):
if state == ThermostatConnectionState.DISCONNECTED:
self.fan_pwm_warning.setPixmap(QtGui.QPixmap())
self.fan_pwm_warning.setToolTip("")
@pyqtSlot("QVariantMap") @pyqtSlot("QVariantMap")
def hw_rev(self, hw_rev): def hw_rev(self, hw_rev):
self.hw_rev_data = hw_rev self.hw_rev_data = hw_rev
self.fan_group.setEnabled(self.hw_rev_data["settings"]["fan_available"]) self.fan_group.setEnabled(self.hw_rev_data["settings"]["fan_available"])
@asyncSlot(int)
async def fan_set_request(self, value):
assert self._thermostat.connected()
if self.fan_auto_box.isChecked():
with QSignalBlocker(self.fan_auto_box):
self.fan_auto_box.setChecked(False)
await self._thermostat.set_fan(value)
if not self.hw_rev_data["settings"]["fan_pwm_recommended"]:
self.set_fan_pwm_warning()
@asyncSlot(int)
async def fan_auto_set_request(self, enabled):
assert self._thermostat.connected()
if enabled:
await self._thermostat.set_fan("auto")
self.fan_update(await self._thermostat.get_fan())
else:
await self.thermostat.set_fan(
self.fan_power_slider.value()
)
@asyncSlot(bool)
async def reset_request(self, _):
assert self._thermostat.connected()
await self._thermostat.reset()
await self._thermostat.end_session()
@asyncSlot(bool)
async def dfu_request(self, _):
assert self._thermostat.connected()
await self._thermostat.dfu()
await self._thermostat.end_session()
@asyncSlot(bool)
async def net_settings_request(self, _):
assert self._thermostat.connected()
ipv4 = await self._thermostat.get_ipv4()
self.net_settings_input_diag = NetSettingsInputDiag(ipv4["addr"])
self.net_settings_input_diag.set_ipv4_act.connect(self.set_net_settings_request)
@asyncSlot(str)
async def set_net_settings_request(self, ipv4_settings):
assert self._thermostat.connected()
await self._thermostat.set_ipv4(ipv4_settings)
await self._thermostat.end_session()

View File

@ -67,16 +67,30 @@ class QtWaitingSpinner(QWidget):
painter.setPen(Qt.PenStyle.NoPen) painter.setPen(Qt.PenStyle.NoPen)
for i in range(0, self._numberOfLines): for i in range(0, self._numberOfLines):
painter.save() painter.save()
painter.translate(self._innerRadius + self._lineLength, self._innerRadius + self._lineLength) painter.translate(
self._innerRadius + self._lineLength,
self._innerRadius + self._lineLength,
)
rotateAngle = float(360 * i) / float(self._numberOfLines) rotateAngle = float(360 * i) / float(self._numberOfLines)
painter.rotate(rotateAngle) painter.rotate(rotateAngle)
painter.translate(self._innerRadius, 0) painter.translate(self._innerRadius, 0)
distance = self.lineCountDistanceFromPrimary(i, self._currentCounter, self._numberOfLines) distance = self.lineCountDistanceFromPrimary(
color = self.currentLineColor(distance, self._numberOfLines, self._trailFadePercentage, i, self._currentCounter, self._numberOfLines
self._minimumTrailOpacity, self._color) )
color = self.currentLineColor(
distance,
self._numberOfLines,
self._trailFadePercentage,
self._minimumTrailOpacity,
self._color,
)
painter.setBrush(color) painter.setBrush(color)
painter.drawRoundedRect(QRect(0, int(-self._lineWidth / 2), self._lineLength, self._lineWidth), self._roundness, painter.drawRoundedRect(
self._roundness, Qt.SizeMode.RelativeSize) QRect(0, int(-self._lineWidth / 2), self._lineLength, self._lineWidth),
self._roundness,
self._roundness,
Qt.SizeMode.RelativeSize,
)
painter.restore() painter.restore()
def start(self): def start(self):
@ -160,7 +174,9 @@ class QtWaitingSpinner(QWidget):
self.setFixedSize(self.size, self.size) self.setFixedSize(self.size, self.size)
def updateTimer(self): def updateTimer(self):
self._timer.setInterval(int(1000 / (self._numberOfLines * self._revolutionsPerSecond))) self._timer.setInterval(
int(1000 / (self._numberOfLines * self._revolutionsPerSecond))
)
def lineCountDistanceFromPrimary(self, current, primary, totalNrOfLines): def lineCountDistanceFromPrimary(self, current, primary, totalNrOfLines):
distance = primary - current distance = primary - current
@ -168,7 +184,9 @@ class QtWaitingSpinner(QWidget):
distance += totalNrOfLines distance += totalNrOfLines
return distance return distance
def currentLineColor(self, countDistance, totalNrOfLines, trailFadePerc, minOpacity, colorinput): def currentLineColor(
self, countDistance, totalNrOfLines, trailFadePerc, minOpacity, colorinput
):
color = QColor(colorinput) color = QColor(colorinput)
if countDistance == 0: if countDistance == 0:
return color return color
@ -186,7 +204,7 @@ class QtWaitingSpinner(QWidget):
return color return color
if __name__ == '__main__': if __name__ == "__main__":
app = QApplication([]) app = QApplication([])
waiting_spinner = QtWaitingSpinner() waiting_spinner = QtWaitingSpinner()
waiting_spinner.show() waiting_spinner.show()

410
pytec/tec_qt.py Normal file → Executable file
View File

@ -1,5 +1,4 @@
from pytec.gui.view.zero_limits_warning import ZeroLimitsWarningView from pytec.gui.view.zero_limits_warning import ZeroLimitsWarningView
from pytec.gui.view.net_settings_input_diag import NetSettingsInputDiag
from pytec.gui.view.thermostat_ctrl_menu import ThermostatCtrlMenu from pytec.gui.view.thermostat_ctrl_menu import ThermostatCtrlMenu
from pytec.gui.view.conn_menu import ConnMenu from pytec.gui.view.conn_menu import ConnMenu
from pytec.gui.view.plot_options_menu import PlotOptionsMenu from pytec.gui.view.plot_options_menu import PlotOptionsMenu
@ -7,19 +6,16 @@ from pytec.gui.view.live_plot_view import LiveDataPlotter
from pytec.gui.view.ctrl_panel import CtrlPanel from pytec.gui.view.ctrl_panel import CtrlPanel
from pytec.gui.view.info_box import InfoBox from pytec.gui.view.info_box import InfoBox
from pytec.gui.model.pid_autotuner import PIDAutoTuner from pytec.gui.model.pid_autotuner import PIDAutoTuner
from pytec.gui.model.thermostat import WrappedClient, Thermostat from pytec.gui.model.thermostat import Thermostat, ThermostatConnectionState
import json import json
from autotune import PIDAutotuneState from autotune import PIDAutotuneState
from qasync import asyncSlot, asyncClose from qasync import asyncSlot, asyncClose
import qasync import qasync
from pytec.aioclient import StoppedConnecting
import asyncio import asyncio
import logging import logging
import argparse import argparse
from PyQt6 import QtWidgets, QtGui, uic from PyQt6 import QtWidgets, QtGui, uic
from PyQt6.QtCore import QSignalBlocker, pyqtSlot from PyQt6.QtCore import pyqtSlot
import pyqtgraph as pg
from functools import partial
import importlib.resources import importlib.resources
@ -30,9 +26,9 @@ def get_argparser():
"--connect", "--connect",
default=None, default=None,
action="store_true", action="store_true",
help="Automatically connect to the specified Thermostat in IP:port format", help="Automatically connect to the specified Thermostat in host:port format",
) )
parser.add_argument("IP", metavar="ip", default=None, nargs="?") parser.add_argument("HOST", metavar="host", default=None, nargs="?")
parser.add_argument("PORT", metavar="port", default=None, nargs="?") parser.add_argument("PORT", metavar="port", default=None, nargs="?")
parser.add_argument( parser.add_argument(
"-l", "-l",
@ -55,287 +51,126 @@ class MainWindow(QtWidgets.QMainWindow):
NUM_CHANNELS = 2 NUM_CHANNELS = 2
def __init__(self, args): def __init__(self, args):
super(MainWindow, self).__init__() super().__init__()
ui_file_path = importlib.resources.files("pytec.gui.view").joinpath("tec_qt.ui") ui_file_path = importlib.resources.files("pytec.gui.view").joinpath("tec_qt.ui")
uic.loadUi(ui_file_path, self) uic.loadUi(ui_file_path, self)
self.hw_rev_data = None
self.info_box = InfoBox() self.info_box = InfoBox()
self.client = WrappedClient(self) # Models
self.client.connection_error.connect(self.bail) self.thermostat = Thermostat(self, self.report_refresh_spin.value())
self._connecting_task = None
self.thermostat.connection_state_changed.connect(self._on_connection_changed)
self.thermostat = Thermostat( self.autotuners = PIDAutoTuner(self, self.thermostat, 2)
self, self.client, self.report_refresh_spin.value() self.autotuners.autotune_state_changed.connect(self.pid_autotune_handler)
# Handlers for disconnections
async def autotune_disconnect():
for ch in range(self.NUM_CHANNELS):
if self.autotuners.get_state(ch) != PIDAutotuneState.STATE_OFF:
await self.autotuners.stop_pid_from_running(ch)
self.thermostat.disconnect_cb = autotune_disconnect
@pyqtSlot()
def handle_connection_error():
self.info_box.display_info_box(
"Connection Error", "Thermostat connection lost. Is it unplugged?"
) )
self.thermostat.connection_error.connect(handle_connection_error)
self.autotuners = PIDAutoTuner(self, self.client, 2) # Control Panel
def get_ctrl_panel_config(args): def get_ctrl_panel_config(args):
with open(args.param_tree, "r") as f: with open(args.param_tree, "r", encoding="utf-8") as f:
return json.load(f)["ctrl_panel"] return json.load(f)["ctrl_panel"]
param_tree_sigActivated_handles = [
[
[["Save to flash"], partial(self.thermostat.save_cfg, ch)],
[["Load from flash"], partial(self.thermostat.load_cfg, ch)],
[
["PID Config", "PID Auto Tune", "Run"],
partial(self.pid_auto_tune_request, ch),
],
]
for ch in range(self.NUM_CHANNELS)
]
self.thermostat.info_box_trigger.connect(self.info_box.display_info_box)
self.zero_limits_warning = ZeroLimitsWarningView(
self.style(), self.limits_warning
)
self.ctrl_panel_view = CtrlPanel( self.ctrl_panel_view = CtrlPanel(
self.thermostat,
self.autotuners,
self.info_box,
[self.ch0_tree, self.ch1_tree], [self.ch0_tree, self.ch1_tree],
get_ctrl_panel_config(args), get_ctrl_panel_config(args),
self.send_command,
param_tree_sigActivated_handles,
)
self.ctrl_panel_view.set_zero_limits_warning_sig.connect(
self.zero_limits_warning.set_limits_warning
)
self.thermostat.fan_update.connect(self.fan_update)
self.thermostat.report_update.connect(self.ctrl_panel_view.update_report)
self.thermostat.report_update.connect(self.autotuners.tick)
self.thermostat.report_update.connect(self.pid_autotune_handler)
self.thermostat.pid_update.connect(self.ctrl_panel_view.update_pid)
self.thermostat.pwm_update.connect(self.ctrl_panel_view.update_pwm)
self.thermostat.thermistor_update.connect(
self.ctrl_panel_view.update_thermistor
)
self.thermostat.postfilter_update.connect(
self.ctrl_panel_view.update_postfilter
)
self.thermostat.interval_update.connect(
self.autotuners.update_sampling_interval
)
self.report_apply_btn.clicked.connect(
lambda: self.thermostat.set_update_s(self.report_refresh_spin.value())
) )
# Graphs
self.channel_graphs = LiveDataPlotter( self.channel_graphs = LiveDataPlotter(
self.thermostat,
[ [
[getattr(self, f"ch{ch}_t_graph"), getattr(self, f"ch{ch}_i_graph")] [getattr(self, f"ch{ch}_t_graph"), getattr(self, f"ch{ch}_i_graph")]
for ch in range(self.NUM_CHANNELS) for ch in range(self.NUM_CHANNELS)
] ]
) )
self.thermostat.report_update.connect(self.channel_graphs.update_report) # Bottom bar menus
self.thermostat.pid_update.connect(self.channel_graphs.update_pid) self.conn_menu = ConnMenu(self.thermostat, self.connect_btn)
self.plot_options_menu = PlotOptionsMenu()
self.plot_options_menu.clear.triggered.connect(self.clear_graphs)
self.plot_options_menu.samples_spinbox.valueChanged.connect(
self.channel_graphs.set_max_samples
)
self.plot_settings.setMenu(self.plot_options_menu)
self.conn_menu = ConnMenu()
self.connect_btn.setMenu(self.conn_menu) self.connect_btn.setMenu(self.conn_menu)
self.thermostat_ctrl_menu = ThermostatCtrlMenu(self.style()) self.thermostat_ctrl_menu = ThermostatCtrlMenu(
self.thermostat_ctrl_menu.fan_set_act.connect(self.fan_set_request) self.thermostat, self.info_box, self.style()
self.thermostat_ctrl_menu.fan_auto_set_act.connect(self.fan_auto_set_request) )
self.thermostat_ctrl_menu.reset_act.connect(self.reset_request)
self.thermostat_ctrl_menu.dfu_act.connect(self.dfu_request)
self.thermostat_ctrl_menu.save_cfg_act.connect(self.save_cfg_request)
self.thermostat_ctrl_menu.load_cfg_act.connect(self.load_cfg_request)
self.thermostat_ctrl_menu.net_cfg_act.connect(self.net_settings_request)
self.thermostat.hw_rev_update.connect(self.thermostat_ctrl_menu.hw_rev)
self.thermostat_settings.setMenu(self.thermostat_ctrl_menu) self.thermostat_settings.setMenu(self.thermostat_ctrl_menu)
self.plot_options_menu = PlotOptionsMenu(self.channel_graphs)
self.plot_settings.setMenu(self.plot_options_menu)
# Status line
self.zero_limits_warning = ZeroLimitsWarningView(
self.style(), self.limits_warning
)
self.ctrl_panel_view.set_zero_limits_warning_sig.connect(
self.zero_limits_warning.set_limits_warning
)
self.loading_spinner.hide() self.loading_spinner.hide()
if args.connect: self.report_apply_btn.clicked.connect(
if args.IP: lambda: self.thermostat.set_update_s(self.report_refresh_spin.value())
self.host_set_line.setText(args.IP)
if args.PORT:
self.port_set_spin.setValue(int(args.PORT))
self.connect_btn.click()
def clear_graphs(self):
self.channel_graphs.clear_graphs()
async def _on_connection_changed(self, result):
self.graph_group.setEnabled(result)
self.report_group.setEnabled(result)
self.thermostat_settings.setEnabled(result)
self.conn_menu.host_set_line.setEnabled(not result)
self.conn_menu.port_set_spin.setEnabled(not result)
self.connect_btn.setText("Disconnect" if result else "Connect")
if result:
self.hw_rev_data = await self.thermostat.get_hw_rev()
logging.debug(self.hw_rev_data)
self._status(self.hw_rev_data)
self.thermostat.start_watching()
else:
self.status_lbl.setText("Disconnected")
self.background_task_lbl.setText("Ready.")
self.loading_spinner.hide()
self.loading_spinner.stop()
self.thermostat_ctrl_menu.fan_pwm_warning.setPixmap(QtGui.QPixmap())
self.thermostat_ctrl_menu.fan_pwm_warning.setToolTip("")
self.clear_graphs()
self.report_box.setChecked(False)
if not Thermostat.connecting or Thermostat.connected:
for ch in range(self.NUM_CHANNELS):
if self.autotuners.get_state(ch) != PIDAutotuneState.STATE_OFF:
await self.autotuners.stop_pid_from_running(ch)
await self.thermostat.set_report_mode(False)
self.thermostat.stop_watching()
def _status(self, hw_rev_d: dict):
logging.debug(hw_rev_d)
self.status_lbl.setText(
f"Connected to Thermostat v{hw_rev_d['rev']['major']}.{hw_rev_d['rev']['minor']}"
) )
@pyqtSlot("QVariantMap")
def fan_update(self, fan_settings):
logging.debug(fan_settings)
if fan_settings is None:
return
with QSignalBlocker(self.thermostat_ctrl_menu.fan_power_slider):
self.thermostat_ctrl_menu.fan_power_slider.setValue(
fan_settings["fan_pwm"] or 100 # 0 = PWM off = full strength
)
with QSignalBlocker(self.thermostat_ctrl_menu.fan_auto_box):
self.thermostat_ctrl_menu.fan_auto_box.setChecked(fan_settings["auto_mode"])
if not self.hw_rev_data["settings"]["fan_pwm_recommended"]:
self.thermostat_ctrl_menu.set_fan_pwm_warning()
@asyncSlot(int)
async def on_report_box_stateChanged(self, enabled):
await self.thermostat.set_report_mode(enabled)
@asyncClose @asyncClose
async def closeEvent(self, event): async def closeEvent(self, _event):
try: try:
await self.bail() await self.thermostat.end_session()
except: except:
pass pass
@asyncSlot() @pyqtSlot(ThermostatConnectionState)
async def on_connect_btn_clicked(self): def _on_connection_changed(self, state):
host, port = ( self.graph_group.setEnabled(state == ThermostatConnectionState.CONNECTED)
self.conn_menu.host_set_line.text(), self.thermostat_settings.setEnabled(
self.conn_menu.port_set_spin.value(), state == ThermostatConnectionState.CONNECTED
) )
try: self.report_group.setEnabled(state == ThermostatConnectionState.CONNECTED)
if not (self.client.connecting() or self.client.connected()):
self.status_lbl.setText("Connecting...") match state:
case ThermostatConnectionState.CONNECTED:
self.connect_btn.setText("Disconnect")
self.status_lbl.setText(
"Connected to Thermostat v"
f"{self.thermostat.hw_rev['rev']['major']}."
f"{self.thermostat.hw_rev['rev']['minor']}"
)
case ThermostatConnectionState.CONNECTING:
self.connect_btn.setText("Stop") self.connect_btn.setText("Stop")
self.conn_menu.host_set_line.setEnabled(False) self.status_lbl.setText("Connecting...")
self.conn_menu.port_set_spin.setEnabled(False)
try: case ThermostatConnectionState.DISCONNECTED:
await self.client.start_session(host=host, port=port, timeout=5) self.connect_btn.setText("Connect")
except StoppedConnecting: self.status_lbl.setText("Disconnected")
return self.report_box.setChecked(False)
await self._on_connection_changed(True)
else:
await self.bail()
# TODO: Remove asyncio.TimeoutError in Python 3.11 @pyqtSlot(int, PIDAutotuneState)
except (OSError, TimeoutError, asyncio.TimeoutError): def pid_autotune_handler(self, _ch, _state):
try:
await self.bail()
except ConnectionResetError:
pass
@asyncSlot()
async def bail(self):
await self._on_connection_changed(False)
await self.client.end_session()
@asyncSlot(object, object)
async def send_command(self, param, changes):
"""Translates parameter tree changes into thermostat set_param calls"""
ch = param.channel
for inner_param, change, data in changes:
if change == "value":
if inner_param.opts.get("param", None) is not None:
if inner_param.opts.get("suffix", None) == "mA":
data /= 1000 # Given in mA
thermostat_param = inner_param.opts["param"]
if thermostat_param[1] == "ch":
thermostat_param[1] = ch
if inner_param.name() == "Postfilter Rate" and data is None:
set_param_args = (*thermostat_param[:2], "off")
else:
set_param_args = (*thermostat_param, data)
param.child(*param.childPath(inner_param)).setOpts(lock=True)
await self.client.set_param(*set_param_args)
param.child(*param.childPath(inner_param)).setOpts(lock=False)
if inner_param.opts.get("pid_autotune", None) is not None:
auto_tuner_param = inner_param.opts["pid_autotune"][0]
if inner_param.opts["pid_autotune"][1] != "ch":
ch = inner_param.opts["pid_autotune"][1]
self.autotuners.set_params(auto_tuner_param, ch, data)
if inner_param.opts.get("activaters", None) is not None:
activater = inner_param.opts["activaters"][
inner_param.opts["limits"].index(data)
]
if activater is not None:
if activater[1] == "ch":
activater[1] = ch
await self.client.set_param(*activater)
@asyncSlot()
async def pid_auto_tune_request(self, ch=0):
match self.autotuners.get_state(ch):
case PIDAutotuneState.STATE_OFF | PIDAutotuneState.STATE_FAILED:
self.autotuners.load_params_and_set_ready(ch)
case PIDAutotuneState.STATE_READY | PIDAutotuneState.STATE_RELAY_STEP_UP | PIDAutotuneState.STATE_RELAY_STEP_DOWN:
await self.autotuners.stop_pid_from_running(ch)
# To Update the UI elements
self.pid_autotune_handler([])
@asyncSlot(list)
async def pid_autotune_handler(self, _):
ch_tuning = [] ch_tuning = []
for ch in range(self.NUM_CHANNELS): for ch in range(self.NUM_CHANNELS):
match self.autotuners.get_state(ch): if self.autotuners.get_state(ch) in {
case PIDAutotuneState.STATE_OFF: PIDAutotuneState.STATE_READY,
self.ctrl_panel_view.change_params_title( PIDAutotuneState.STATE_RELAY_STEP_UP,
ch, ("PID Config", "PID Auto Tune", "Run"), "Run" PIDAutotuneState.STATE_RELAY_STEP_DOWN,
) }:
case PIDAutotuneState.STATE_READY | PIDAutotuneState.STATE_RELAY_STEP_UP | PIDAutotuneState.STATE_RELAY_STEP_DOWN:
self.ctrl_panel_view.change_params_title(
ch, ("PID Config", "PID Auto Tune", "Run"), "Stop"
)
ch_tuning.append(ch) ch_tuning.append(ch)
case PIDAutotuneState.STATE_SUCCEEDED:
self.info_box.display_info_box(
"PID Autotune Success",
f"Channel {ch} PID Config has been loaded to Thermostat. Regulating temperature.",
)
self.info_box.show()
case PIDAutotuneState.STATE_FAILED:
self.info_box.display_info_box(
"PID Autotune Failed", f"Channel {ch} PID Autotune has failed."
)
self.info_box.show()
if len(ch_tuning) == 0: if len(ch_tuning) == 0:
self.background_task_lbl.setText("Ready.") self.background_task_lbl.setText("Ready.")
self.loading_spinner.hide() self.loading_spinner.hide()
@ -347,61 +182,33 @@ class MainWindow(QtWidgets.QMainWindow):
self.loading_spinner.start() self.loading_spinner.start()
self.loading_spinner.show() self.loading_spinner.show()
@asyncSlot(int) @asyncSlot()
async def fan_set_request(self, value): async def on_connect_btn_clicked(self):
if not self.client.connected(): if (self._connecting_task is None) and (not self.thermostat.connected()):
return self._connecting_task = asyncio.create_task(
if self.thermostat_ctrl_menu.fan_auto_box.isChecked(): self.thermostat.start_session(
with QSignalBlocker(self.thermostat_ctrl_menu.fan_auto_box): host=self.conn_menu.host_set_line.text(),
self.thermostat_ctrl_menu.fan_auto_box.setChecked(False) port=self.conn_menu.port_set_spin.value(),
await self.client.set_fan(value)
if not self.hw_rev_data["settings"]["fan_pwm_recommended"]:
self.thermostat_ctrl_menu.set_fan_pwm_warning()
@asyncSlot(int)
async def fan_auto_set_request(self, enabled):
if not self.client.connected():
return
if enabled:
await self.client.set_fan("auto")
self.fan_update(await self.client.get_fan())
else:
await self.client.set_fan(
self.thermostat_ctrl_menu.fan_power_slider.value()
) )
)
try:
await self._connecting_task
except (OSError, asyncio.CancelledError) as exc:
await self.thermostat.end_session()
if isinstance(exc, asyncio.CancelledError):
return
raise
finally:
self._connecting_task = None
elif self._connecting_task is not None:
self._connecting_task.cancel()
else:
await self.thermostat.end_session()
@asyncSlot(int) @asyncSlot(int)
async def save_cfg_request(self, ch): async def on_report_box_stateChanged(self, enabled):
await self.thermostat.save_cfg(str(ch)) await self.thermostat.set_report_mode(enabled)
@asyncSlot(int)
async def load_cfg_request(self, ch):
await self.thermostat.load_cfg(str(ch))
@asyncSlot(bool)
async def dfu_request(self, _):
await self._on_connection_changed(False)
await self.thermostat.dfu()
@asyncSlot(bool)
async def reset_request(self, _):
await self._on_connection_changed(False)
await self.thermostat.reset()
await asyncio.sleep(0.1) # Wait for the reset to start
self.connect_btn.click() # Reconnect
@asyncSlot(bool)
async def net_settings_request(self, _):
ipv4 = await self.thermostat.get_ipv4()
self.net_settings_input_diag = NetSettingsInputDiag(ipv4["addr"])
self.net_settings_input_diag.set_ipv4_act.connect(self.set_net_settings_request)
@asyncSlot(str)
async def set_net_settings_request(self, ipv4_settings):
await self.thermostat.set_ipv4(ipv4_settings)
await self.thermostat._client.end_session()
await self._on_connection_changed(False)
async def coro_main(): async def coro_main():
@ -414,12 +221,21 @@ async def coro_main():
app = QtWidgets.QApplication.instance() app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(app_quit_event.set) app.aboutToQuit.connect(app_quit_event.set)
app.setWindowIcon( app.setWindowIcon(
QtGui.QIcon(str(importlib.resources.files("pytec.gui.resources").joinpath("artiq.ico"))) QtGui.QIcon(
str(importlib.resources.files("pytec.gui.resources").joinpath("artiq.ico"))
)
) )
main_window = MainWindow(args) main_window = MainWindow(args)
main_window.show() main_window.show()
if args.connect:
if args.HOST:
main_window.conn_menu.host_set_line.setText(args.HOST)
if args.PORT:
main_window.conn_menu.port_set_spin.setValue(int(args.PORT))
main_window.connect_btn.click()
await app_quit_event.wait() await app_quit_event.wait()