Compare commits

...

19 Commits

Author SHA1 Message Date
6dcd1b6275 Keep i_set visible even when PID is engaged
Since i_set is also plotted, we would also want to see its precise value
too.
2024-07-18 18:08:39 +08:00
c75c8f2da0 Remove MutexParameter
Use the standard ListParamenter instead, and hook up UI changes
elsewhere.
2024-07-18 18:08:39 +08:00
c754dcfb18 Remove "activater" option
Interpret commands directly in send_command
2024-07-18 18:08:38 +08:00
0b0bf26244 PID Autotune test current should only be positive 2024-07-18 18:08:38 +08:00
487bf24ffe More straightforward mapping to thermostat command
New tag "thermostat:set_param" used in JSON.
2024-07-18 18:08:36 +08:00
6372e03dff Separate tree and param changes 2024-07-18 18:08:10 +08:00
dbee457c09 i -> ch
and others
2024-07-18 18:08:10 +08:00
97c5f2a201 Remove unnessarily duplicated parameter tree
Huge source of confusion and easy bugs!
2024-07-18 18:08:10 +08:00
fad9008707 Remove unneccessary redirection
The child at inner_param's childpath to the root parameter... is just
inner_param.
2024-07-18 18:08:10 +08:00
bbd6774c75 Make send_command more readable 2024-07-18 18:08:10 +08:00
405d40c543 Remove extra "channel" option to root parameters
Their corresponding channel numbers are already represented with their
"value" options.
2024-07-18 18:08:10 +08:00
56655854cf Fix grammar in info box on PID Autotune failure 2024-07-18 18:08:10 +08:00
37e888846b Don't say settings loaded on settings save 2024-07-18 18:08:09 +08:00
301db2e003 ctrl_panel: PID Auto Tune -> PID Autotune 2024-07-18 18:08:09 +08:00
f2ca607b8b ctrl_panel: Stop crushing spinboxes
It might not be the case on some themes, but on the default Qt theme the
spinbox are a bit too short for the containing numbers. See
https://github.com/pyqtgraph/pyqtgraph/issues/701.
2024-07-18 18:08:09 +08:00
a18d6fb4d0 ctrl_panel: Approriate units for measured current
Allow the readonly display of current to vary its SI prefix in the unit,
since as a display entry it won't have the unit adjustment problem.
2024-07-18 18:08:09 +08:00
0e39b29c5d ctrl_panel: Pin down units for editable fields
Avoids awkward value editing
2024-07-18 18:08:09 +08:00
ce93004368 ctrl_panel: Improve postfilter description 2024-07-18 18:08:07 +08:00
a57c365b8d ctrl_panel: Add tooltips
For users' better understanding of what the parameters do
2024-07-18 18:07:43 +08:00
4 changed files with 209 additions and 216 deletions

View File

@ -117,7 +117,7 @@ class Thermostat(QObject, metaclass=PropertyMeta):
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( self.info_box_trigger.emit(
"Settings loaded", f"Channel {ch} Settings has been loaded to flash." "Settings saved", f"Channel {ch} Settings has been saved to flash."
) )
@asyncSlot() @asyncSlot()

View File

@ -6,40 +6,11 @@ from pyqtgraph.parametertree import (
) )
class MutexParameter(pTypes.ListParameter): def set_tree_label_tips(tree):
""" for item in tree.listAllItems():
Mutually exclusive parameter where only one of its children is visible at a time, list selectable. p = item.param
if "tip" in p.opts:
The ordering of the list items determines which children will be visible. item.setToolTip(0, p.opts["tip"])
"""
def __init__(self, **opts):
super().__init__(**opts)
self.sigValueChanged.connect(self.show_chosen_child)
self.sigValueChanged.emit(self, self.opts["value"])
def _get_param_from_value(self, value):
if isinstance(self.opts["limits"], dict):
values_list = list(self.opts["limits"].values())
else:
values_list = self.opts["limits"]
return self.children()[values_list.index(value)]
@pyqtSlot(object, object)
def show_chosen_child(self, value):
for param in self.children():
param.hide()
child_to_show = self._get_param_from_value(value.value())
child_to_show.show()
if child_to_show.opts.get("triggerOnShow", None):
child_to_show.sigValueChanged.emit(child_to_show, child_to_show.value())
registerParameterType("mutex", MutexParameter)
class CtrlPanel(QObject): class CtrlPanel(QObject):
@ -58,29 +29,40 @@ class CtrlPanel(QObject):
self.trees_ui = trees_ui self.trees_ui = trees_ui
self.NUM_CHANNELS = len(trees_ui) self.NUM_CHANNELS = len(trees_ui)
self.THERMOSTAT_PARAMETERS = [param_tree for i in range(self.NUM_CHANNELS)]
self.params = [ self.params = [
Parameter.create( Parameter.create(
name=f"Thermostat Channel {ch} Parameters", name=f"Thermostat Channel {ch} Parameters",
type="group", type="group",
value=ch, value=ch,
children=self.THERMOSTAT_PARAMETERS[ch], children=param_tree,
) )
for ch in range(self.NUM_CHANNELS) for ch in range(self.NUM_CHANNELS)
] ]
for i, param in enumerate(self.params): for ch, tree in enumerate(self.trees_ui):
param.channel = i
for i, tree in enumerate(self.trees_ui):
tree.setHeaderHidden(True) tree.setHeaderHidden(True)
tree.setParameters(self.params[i], showTop=False) tree.setParameters(self.params[ch], showTop=False)
self.params[i].setValue = self._setValue
self.params[i].sigTreeStateChanged.connect(sigTreeStateChanged_handle)
for handle in sigActivated_handles[i]: set_tree_label_tips(tree)
self.params[i].child(*handle[0]).sigActivated.connect(handle[1])
for ch, param in enumerate(self.params):
self.params[ch].setValue = self._setValue
param.sigTreeStateChanged.connect(sigTreeStateChanged_handle)
for handle in sigActivated_handles[ch]:
param.child(*handle[0]).sigActivated.connect(handle[1])
param.child("output", "control_method").sigValueChanged.connect(
lambda param, value: param.child("i_set").setWritable(
value == "constant_current"
)
)
param.child("output", "control_method").sigValueChanged.connect(
lambda param, value: param.child("target").show(
value == "temperature_pid"
)
)
def _setValue(self, value, blockSignal=None): def _setValue(self, value, blockSignal=None):
""" """
@ -152,7 +134,7 @@ class CtrlPanel(QObject):
) )
if settings["tec_i"] is not None: if settings["tec_i"] is not None:
self.params[channel].child("tec_i").setValue( self.params[channel].child("tec_i").setValue(
settings["tec_i"] * 1000 settings["tec_i"]
) )
@pyqtSlot("QVariantList") @pyqtSlot("QVariantList")

View File

@ -5,42 +5,43 @@
"title": "Temperature", "title": "Temperature",
"type": "float", "type": "float",
"format": "{value:.4f} °C", "format": "{value:.4f} °C",
"readonly": true "readonly": true,
"tip": "The measured temperature at the thermistor"
}, },
{ {
"name": "tec_i", "name": "tec_i",
"title": "Current through TEC", "title": "Current through TEC",
"type": "float", "type": "float",
"suffix": "mA", "siPrefix": true,
"suffix": "A",
"decimals": 6, "decimals": 6,
"readonly": true "readonly": true,
"tip": "The measured current through the TEC"
}, },
{ {
"name": "output", "name": "output",
"title": "Output Settings", "title": "Output Settings",
"expanded": true, "expanded": true,
"type": "group", "type": "group",
"tip": "Settings of the output to the TEC",
"children": [ "children": [
{ {
"name": "control_method", "name": "control_method",
"title": "Control Method", "title": "Control Method",
"type": "mutex", "type": "list",
"limits": { "limits": {
"Constant Current": "constant_current", "Constant Current": "constant_current",
"Temperature PID": "temperature_pid" "Temperature PID": "temperature_pid"
}, },
"activaters": [ "thermostat:set_param": {
null, "topic": "pwm",
[ "field": "pid"
"pwm", },
"ch", "tip": "Select control method of output",
"pid"
]
],
"children": [ "children": [
{ {
"name": "i_set", "name": "i_set",
"title": "Set Current", "title": "Set Current (mA)",
"type": "float", "type": "float",
"value": 0, "value": 0,
"step": 100, "step": 100,
@ -50,30 +51,32 @@
], ],
"triggerOnShow": true, "triggerOnShow": true,
"decimals": 6, "decimals": 6,
"suffix": "mA", "compactHeight": false,
"param": [ "thermostat:set_param": {
"pwm", "topic": "pwm",
"ch", "field": "i_set"
"i_set" },
], "tip": "The set current through TEC",
"lock": false "lock": false
}, },
{ {
"name": "target", "name": "target",
"title": "Set Temperature", "title": "Set Temperature (°C)",
"type": "float", "type": "float",
"visible": false,
"value": 25, "value": 25,
"step": 0.1, "step": 0.1,
"limits": [ "limits": [
-273, -273,
300 300
], ],
"format": "{value:.4f} °C", "format": "{value:.4f}",
"param": [ "compactHeight": false,
"pid", "thermostat:set_param": {
"ch", "topic": "pid",
"target" "field": "target"
], },
"tip": "The target temperature of the TEC",
"lock": false "lock": false
} }
] ]
@ -83,48 +86,49 @@
"title": "Limits", "title": "Limits",
"expanded": true, "expanded": true,
"type": "group", "type": "group",
"tip": "The limits of output, with the polarity at the front panel as reference",
"children": [ "children": [
{ {
"name": "max_i_pos", "name": "max_i_pos",
"title": "Max Cooling Current", "title": "Max Cooling Current (mA)",
"type": "float", "type": "float",
"value": 0, "value": 0,
"step": 100, "step": 100,
"decimals": 6, "decimals": 6,
"compactHeight": false,
"limits": [ "limits": [
0, 0,
2000 2000
], ],
"suffix": "mA", "thermostat:set_param": {
"param": [ "topic": "pwm",
"pwm", "field": "max_i_pos"
"ch", },
"max_i_pos" "tip": "The maximum cooling (+ve) current through the output pins",
],
"lock": false "lock": false
}, },
{ {
"name": "max_i_neg", "name": "max_i_neg",
"title": "Max Heating Current", "title": "Max Heating Current (mA)",
"type": "float", "type": "float",
"value": 0, "value": 0,
"step": 100, "step": 100,
"decimals": 6, "decimals": 6,
"compactHeight": false,
"limits": [ "limits": [
0, 0,
2000 2000
], ],
"suffix": "mA", "thermostat:set_param": {
"param": [ "topic": "pwm",
"pwm", "field": "max_i_neg"
"ch", },
"max_i_neg" "tip": "The maximum heating (-ve) current through the output pins",
],
"lock": false "lock": false
}, },
{ {
"name": "max_v", "name": "max_v",
"title": "Max Voltage Difference", "title": "Max Absolute Voltage (V)",
"type": "float", "type": "float",
"value": 0, "value": 0,
"step": 0.1, "step": 0.1,
@ -133,12 +137,12 @@
5 5
], ],
"siPrefix": true, "siPrefix": true,
"suffix": "V", "compactHeight": false,
"param": [ "thermostat:set_param": {
"pwm", "topic": "pwm",
"ch", "field": "max_v"
"max_v" },
], "tip": "The maximum voltage across the output pins",
"lock": false "lock": false
} }
] ]
@ -150,11 +154,11 @@
"title": "Thermistor Settings", "title": "Thermistor Settings",
"expanded": true, "expanded": true,
"type": "group", "type": "group",
"tip": "Settings of the connected Thermistor", "tip": "Settings of the connected Thermistor, including for the B-Parameter conversion",
"children": [ "children": [
{ {
"name": "t0", "name": "t0",
"title": "T₀", "title": "T₀ (°C)",
"type": "float", "type": "float",
"value": 25, "value": 25,
"step": 0.1, "step": 0.1,
@ -162,61 +166,62 @@
-100, -100,
100 100
], ],
"format": "{value:.4f} °C", "format": "{value:.4f}",
"param": [ "compactHeight": false,
"s-h", "thermostat:set_param": {
"ch", "topic": "s-h",
"t0" "field": "t0"
], },
"tip": "The origin temperature",
"lock": false "lock": false
}, },
{ {
"name": "r0", "name": "r0",
"title": "R₀", "title": "R₀ (Ω)",
"type": "float", "type": "float",
"value": 10000, "value": 10000,
"step": 1, "step": 1,
"siPrefix": true, "siPrefix": true,
"suffix": "Ω", "compactHeight": false,
"param": [ "thermostat:set_param": {
"s-h", "topic": "s-h",
"ch", "field": "r0"
"r0" },
], "tip": "The resistance of the thermistor at origin temperature T₀",
"lock": false "lock": false
}, },
{ {
"name": "b", "name": "b",
"title": "B", "title": "B (K)",
"type": "float", "type": "float",
"value": 3950, "value": 3950,
"step": 1, "step": 1,
"suffix": "K",
"decimals": 4, "decimals": 4,
"param": [ "compactHeight": false,
"s-h", "thermostat:set_param": {
"ch", "topic": "s-h",
"b" "field": "b"
], },
"tip": "The B (or Beta) Parameter",
"lock": false "lock": false
}, },
{ {
"name": "rate", "name": "rate",
"title": "Postfilter Rate", "title": "Postfilter Rejection",
"type": "list", "type": "list",
"value": 16.67, "value": 16.67,
"param": [ "thermostat:set_param": {
"postfilter", "topic": "postfilter",
"ch", "field": "rate"
"rate"
],
"limits": {
"Off": null,
"16.67 Hz": 16.67,
"20 Hz": 20.0,
"21.25 Hz": 21.25,
"27 Hz": 27.0
}, },
"limits": {
"Off": "",
"47 dB @ 10.41 Hz": 27.0,
"62 dB @ 9.1 Hz": 21.25,
"86 dB @ 10 Hz": 20.0,
"92 dB @ 8.4 Hz": 16.67
},
"tip": "Trade off effective sampling rate and rejection of (50±1) Hz and (60±1) Hz",
"lock": false "lock": false
} }
] ]
@ -226,6 +231,7 @@
"title": "PID Settings", "title": "PID Settings",
"expanded": true, "expanded": true,
"type": "group", "type": "group",
"tip": "Settings of PID parameters and clamping",
"children": [ "children": [
{ {
"name": "kp", "name": "kp",
@ -233,11 +239,12 @@
"type": "float", "type": "float",
"step": 0.1, "step": 0.1,
"suffix": "", "suffix": "",
"param": [ "compactHeight": false,
"pid", "thermostat:set_param": {
"ch", "topic": "pid",
"kp" "field": "kp"
], },
"tip": "Proportional gain",
"lock": false "lock": false
}, },
{ {
@ -246,11 +253,12 @@
"type": "float", "type": "float",
"step": 0.1, "step": 0.1,
"suffix": "Hz", "suffix": "Hz",
"param": [ "compactHeight": false,
"pid", "thermostat:set_param": {
"ch", "topic": "pid",
"ki" "field": "ki"
], },
"tip": "Integral gain",
"lock": false "lock": false
}, },
{ {
@ -259,11 +267,12 @@
"type": "float", "type": "float",
"step": 0.1, "step": 0.1,
"suffix": "s", "suffix": "s",
"param": [ "compactHeight": false,
"pid", "thermostat:set_param": {
"ch", "topic": "pid",
"kd" "field": "kd"
], },
"tip": "Differential gain",
"lock": false "lock": false
}, },
{ {
@ -271,10 +280,11 @@
"title": "PID Output Clamping", "title": "PID Output Clamping",
"expanded": true, "expanded": true,
"type": "group", "type": "group",
"tip": "Clamps PID outputs to specified range\nCould be different than output limits",
"children": [ "children": [
{ {
"name": "output_min", "name": "output_min",
"title": "Minimum", "title": "Minimum (mA)",
"type": "float", "type": "float",
"step": 100, "step": 100,
"limits": [ "limits": [
@ -282,17 +292,17 @@
2000 2000
], ],
"decimals": 6, "decimals": 6,
"suffix": "mA", "compactHeight": false,
"param": [ "thermostat:set_param": {
"pid", "topic": "pid",
"ch", "field": "output_min"
"output_min" },
], "tip": "Minimum PID output",
"lock": false "lock": false
}, },
{ {
"name": "output_max", "name": "output_max",
"title": "Maximum", "title": "Maximum (mA)",
"type": "float", "type": "float",
"step": 100, "step": 100,
"limits": [ "limits": [
@ -300,81 +310,85 @@
2000 2000
], ],
"decimals": 6, "decimals": 6,
"suffix": "mA", "compactHeight": false,
"param": [ "thermostat:set_param": {
"pid", "topic": "pid",
"ch", "field": "output_max"
"output_max" },
], "tip": "Maximum PID output",
"lock": false "lock": false
} }
] ]
}, },
{ {
"name": "pid_autotune", "name": "pid_autotune",
"title": "PID Auto Tune", "title": "PID Autotune",
"expanded": false, "expanded": false,
"type": "group", "type": "group",
"tip": "Automatically tune PID parameters",
"children": [ "children": [
{ {
"name": "target_temp", "name": "target_temp",
"title": "Target Temperature", "title": "Target Temperature (°C)",
"type": "float", "type": "float",
"value": 20, "value": 20,
"step": 0.1, "step": 0.1,
"format": "{value:.4f} °C", "format": "{value:.4f}",
"pid_autotune": [ "compactHeight": false,
"target_temp", "pid_autotune": {
"ch" "params_name": "target_temp"
] },
"tip": "The target temperature to autotune for"
}, },
{ {
"name": "test_current", "name": "test_current",
"title": "Test Current", "title": "Test Current (mA)",
"type": "float", "type": "float",
"value": 0, "value": 0,
"decimals": 6, "decimals": 6,
"compactHeight": false,
"step": 100, "step": 100,
"limits": [ "limits": [
-2000, 0,
2000 2000
], ],
"suffix": "mA", "pid_autotune": {
"pid_autotune": [ "params_name": "test_current"
"test_current", },
"ch" "tip": "The testing current when autotuning"
]
}, },
{ {
"name": "temp_swing", "name": "temp_swing",
"title": "Temperature Swing", "title": "Temperature Swing (°C)",
"type": "float", "type": "float",
"value": 1.5, "value": 1.5,
"step": 0.1, "step": 0.1,
"prefix": "±", "prefix": "±",
"format": "{value:.4f} °C", "format": "{value:.4f}",
"pid_autotune": [ "compactHeight": false,
"temp_swing", "pid_autotune": {
"ch" "params_name": "temp_swing"
] },
"tip": "The temperature swing around the target"
}, },
{ {
"name": "lookback", "name": "lookback",
"title": "Lookback", "title": "Lookback (s)",
"type": "float", "type": "float",
"value": 3.0, "value": 3.0,
"step": 0.1, "step": 0.1,
"format": "{value:.4f} s", "format": "{value:.4f}",
"pid_autotune": [ "compactHeight": false,
"lookback", "pid_autotune": {
"ch" "params_name": "lookback"
] },
"tip": "Amount of time to lookback"
}, },
{ {
"name": "run_pid", "name": "run_pid",
"title": "Run", "title": "Run",
"type": "action", "type": "action",
"tip": "Run" "tip": "Run PID Autotune with above settings"
} }
] ]
} }

View File

@ -82,7 +82,7 @@ class MainWindow(QtWidgets.QMainWindow):
[["load"], partial(self.thermostat.load_cfg, ch)], [["load"], partial(self.thermostat.load_cfg, ch)],
[ [
["pid", "pid_autotune", "run_pid"], ["pid", "pid_autotune", "run_pid"],
partial(self.pid_auto_tune_request, ch), partial(self.pid_autotune_request, ch),
], ],
] ]
for ch in range(self.NUM_CHANNELS) for ch in range(self.NUM_CHANNELS)
@ -262,43 +262,40 @@ class MainWindow(QtWidgets.QMainWindow):
@asyncSlot(object, object) @asyncSlot(object, object)
async def send_command(self, param, changes): async def send_command(self, param, changes):
"""Translates parameter tree changes into thermostat set_param calls""" """Translates parameter tree changes into thermostat set_param calls"""
ch = param.channel ch = param.value()
for inner_param, change, data in changes: for inner_param, change, data in changes:
if change == "value": if change == "value":
if inner_param.opts.get("param", None) is not None: new_value = data
if inner_param.opts.get("suffix", None) == "mA": if "thermostat:set_param" in inner_param.opts:
data /= 1000 # Given in mA thermostat_param = inner_param.opts["thermostat:set_param"]
thermostat_param = inner_param.opts["param"] # Handle thermostat command irregularities
if thermostat_param[1] == "ch": match inner_param.name(), new_value:
thermostat_param[1] = ch case "rate", "":
thermostat_param = thermostat_param.copy()
thermostat_param["field"] = "off"
case "control_method", "constant_current":
inner_param = inner_param.child("i_set")
thermostat_param = inner_param.opts["thermostat:set_param"]
new_value = inner_param.value()
case "control_method", "temperature_pid":
new_value = ""
if inner_param.name() == "rate" and data is None: if inner_param.opts.get("title", None).endswith(" (mA)"):
set_param_args = (*thermostat_param[:2], "off") new_value /= 1000 # Given in mA
else: inner_param.setOpts(lock=True)
set_param_args = (*thermostat_param, data) await self.client.set_param(
param.child(*param.childPath(inner_param)).setOpts(lock=True) channel=ch, value=new_value, **thermostat_param
await self.client.set_param(*set_param_args) )
param.child(*param.childPath(inner_param)).setOpts(lock=False) inner_param.setOpts(lock=False)
if inner_param.opts.get("pid_autotune", None) is not None: if "pid_autotune" in inner_param.opts:
auto_tuner_param = inner_param.opts["pid_autotune"][0] autotuner_param = inner_param.opts["pid_autotune"]
if inner_param.opts["pid_autotune"][1] != "ch": self.autotuners.set_params(ch=ch, val=new_value, **autotuner_param)
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.reverse[0].index(data) # ListParameter.reverse = list of codename values
]
if activater is not None:
if activater[1] == "ch":
activater[1] = ch
await self.client.set_param(*activater)
@asyncSlot() @asyncSlot()
async def pid_auto_tune_request(self, ch=0): async def pid_autotune_request(self, ch=0):
match self.autotuners.get_state(ch): match self.autotuners.get_state(ch):
case PIDAutotuneState.STATE_OFF | PIDAutotuneState.STATE_FAILED: case PIDAutotuneState.STATE_OFF | PIDAutotuneState.STATE_FAILED:
self.autotuners.load_params_and_set_ready(ch) self.autotuners.load_params_and_set_ready(ch)
@ -332,7 +329,7 @@ class MainWindow(QtWidgets.QMainWindow):
case PIDAutotuneState.STATE_FAILED: case PIDAutotuneState.STATE_FAILED:
self.info_box.display_info_box( self.info_box.display_info_box(
"PID Autotune Failed", f"Channel {ch} PID Autotune is failed." "PID Autotune Failed", f"Channel {ch} PID Autotune has failed."
) )
self.info_box.show() self.info_box.show()