Compare commits

...

8 Commits

Author SHA1 Message Date
a6c852369f ctrl_panel: Reformat SpinBox text always if valid
The parameter SpinBoxes previously would only update if the interpreted
value was changed, missing cases where the text would have changed but
the value stays the same, e.g. removing trailing decimal zeros.
2024-08-07 17:59:32 +08:00
3ed181403f ctrl_panel: Move postfilter into its own group 2024-08-07 17:59:27 +08:00
205f5c0ae9 ctrl_panel: Use new locking mechanism from Kirdy 2024-08-07 17:59:07 +08:00
3c4bc4b50c ctrl_panel: Fix editing fields with unit "°C"
A faulty regular expression within PyQtGraph causes any Parameter with a
suffix that doesn't begin with an alphanumeric character (as matched
with \w) to act abnormally. For instance, entering "100 °C" into the
input boxes gets interpreted as 10 °C.

Patch the FLOAT_REGEX in PyQtGraph to simply match for any character in
the suffix group.
2024-08-07 17:59:06 +08:00
3a0bb66212 ctrl_panel: More appropriate steps and fixes 2024-08-07 17:59:06 +08:00
9ebdcfc54f ctrl_panel: Put plotted values into readings group
For more intuitiveness to first-time users
2024-08-07 17:59:06 +08:00
980cabefb5 ctrl_panel: Fix max_v to only have unit "V"
As most users do not need to limit TEC voltage with accuracy of less
than 1mV.
2024-08-07 17:59:06 +08:00
e1861263b2 ctrl_panel: Indicate active parameter of control
Instead of hiding the inactive control parameter, underline and bold the
active control parameter title, e.g. "Set Current" when control method
is constant current, and "Setpoint" when it is temperature PID.
2024-08-07 17:59:06 +08:00
4 changed files with 122 additions and 114 deletions

View File

@ -7,42 +7,6 @@ from pyqtgraph.parametertree import (
import pytec.gui.view.unitful import pytec.gui.view.unitful
class MutexParameter(pTypes.ListParameter):
"""
Mutually exclusive parameter where only one of its children is visible at a time, list selectable.
The ordering of the list items determines which children will be visible.
"""
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)
def set_tree_label_tips(tree): def set_tree_label_tips(tree):
for item in tree.listAllItems(): for item in tree.listAllItems():
p = item.param p = item.param
@ -66,6 +30,11 @@ 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)
def _set_value_with_lock(self, value):
if not self.opts.get("lock"):
self.setValue(value)
Parameter.set_value_with_lock = _set_value_with_lock
self.params = [ self.params = [
Parameter.create( Parameter.create(
name=f"Thermostat Channel {ch} Parameters", name=f"Thermostat Channel {ch} Parameters",
@ -83,37 +52,28 @@ class CtrlPanel(QObject):
set_tree_label_tips(tree) set_tree_label_tips(tree)
for ch, param in enumerate(self.params): for ch, param in enumerate(self.params):
self.params[ch].setValue = self._setValue
param.sigTreeStateChanged.connect(sigTreeStateChanged_handle) param.sigTreeStateChanged.connect(sigTreeStateChanged_handle)
for handle in sigActivated_handles[ch]: for handle in sigActivated_handles[ch]:
param.child(*handle[0]).sigActivated.connect(handle[1]) param.child(*handle[0]).sigActivated.connect(handle[1])
def _setValue(self, value, blockSignal=None): def _highlight_usage(param, control_method):
""" for item in param.child("i_set").items:
Implement 'lock' mechanism for Parameter Type font = item.font(0)
font.setUnderline(control_method == "constant_current")
font.setBold(control_method == "constant_current")
item.setFont(0, font)
for item in param.child("target").items:
font = item.font(0)
font.setUnderline(control_method == "temperature_pid")
font.setBold(control_method == "temperature_pid")
item.setFont(0, font)
Modified from the source param.child("output", "control_method").sigValueChanged.connect(_highlight_usage)
""" for item in param.child("output", "control_method").items:
try: font = item.font(0)
if blockSignal is not None: font.setBold(True)
self.sigValueChanged.disconnect(blockSignal) item.setFont(0, font)
value = self._interpretValue(value)
if fn.eq(self.opts["value"], value):
return value
if "lock" in self.opts.keys():
if self.opts["lock"]:
return value
self.opts["value"] = value
self.sigValueChanged.emit(
self, value
) # value might change after signal is received by tree item
finally:
if blockSignal is not None:
self.sigValueChanged.connect(blockSignal)
return self.opts["value"]
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)
@ -123,55 +83,59 @@ class CtrlPanel(QObject):
for settings in pid_settings: for settings in pid_settings:
channel = settings["channel"] channel = settings["channel"]
with QSignalBlocker(self.params[channel]): with QSignalBlocker(self.params[channel]):
self.params[channel].child("pid", "kp").setValue( self.params[channel].child("pid", "kp").set_value_with_lock(
settings["parameters"]["kp"] settings["parameters"]["kp"]
) )
self.params[channel].child("pid", "ki").setValue( self.params[channel].child("pid", "ki").set_value_with_lock(
settings["parameters"]["ki"] settings["parameters"]["ki"]
) )
self.params[channel].child("pid", "kd").setValue( self.params[channel].child("pid", "kd").set_value_with_lock(
settings["parameters"]["kd"] settings["parameters"]["kd"]
) )
self.params[channel].child( self.params[channel].child(
"pid", "pid_output_clamping", "output_min" "pid", "pid_output_clamping", "output_min"
).setValue(settings["parameters"]["output_min"]) ).set_value_with_lock(settings["parameters"]["output_min"])
self.params[channel].child( self.params[channel].child(
"pid", "pid_output_clamping", "output_max" "pid", "pid_output_clamping", "output_max"
).setValue(settings["parameters"]["output_max"]) ).set_value_with_lock(settings["parameters"]["output_max"])
self.params[channel].child( self.params[channel].child(
"output", "control_method", "target" "output", "control_method", "target"
).setValue(settings["target"]) ).set_value_with_lock(settings["target"])
@pyqtSlot("QVariantList") @pyqtSlot("QVariantList")
def update_report(self, report_data): def update_report(self, report_data):
for settings in report_data: for settings in report_data:
channel = settings["channel"] channel = settings["channel"]
with QSignalBlocker(self.params[channel]): with QSignalBlocker(self.params[channel]):
self.params[channel].child("output", "control_method").setValue( self.params[channel].child(
"output", "control_method"
).set_value_with_lock(
"temperature_pid" if settings["pid_engaged"] else "constant_current" "temperature_pid" if settings["pid_engaged"] else "constant_current"
) )
self.params[channel].child( self.params[channel].child(
"output", "control_method", "i_set" "output", "control_method", "i_set"
).setValue(settings["i_set"]) ).set_value_with_lock(settings["i_set"])
if settings["temperature"] is not None: if settings["temperature"] is not None:
self.params[channel].child("temperature").setValue( self.params[channel].child(
settings["temperature"] "readings", "temperature"
) ).set_value_with_lock(settings["temperature"])
if settings["tec_i"] is not None: if settings["tec_i"] is not None:
self.params[channel].child("tec_i").setValue(settings["tec_i"]) self.params[channel].child(
"readings", "tec_i"
).set_value_with_lock(settings["tec_i"])
@pyqtSlot("QVariantList") @pyqtSlot("QVariantList")
def update_thermistor(self, sh_data): def update_thermistor(self, sh_data):
for sh_param in sh_data: for sh_param in sh_data:
channel = sh_param["channel"] channel = sh_param["channel"]
with QSignalBlocker(self.params[channel]): with QSignalBlocker(self.params[channel]):
self.params[channel].child("thermistor", "t0").setValue( self.params[channel].child("thermistor", "t0").set_value_with_lock(
sh_param["params"]["t0"] - 273.15 sh_param["params"]["t0"] - 273.15
) )
self.params[channel].child("thermistor", "r0").setValue( self.params[channel].child("thermistor", "r0").set_value_with_lock(
sh_param["params"]["r0"] sh_param["params"]["r0"]
) )
self.params[channel].child("thermistor", "b").setValue( self.params[channel].child("thermistor", "b").set_value_with_lock(
sh_param["params"]["b"] sh_param["params"]["b"]
) )
@ -182,15 +146,15 @@ class CtrlPanel(QObject):
for pwm_params in pwm_data: for pwm_params in pwm_data:
channel = pwm_params["channel"] channel = pwm_params["channel"]
with QSignalBlocker(self.params[channel]): with QSignalBlocker(self.params[channel]):
self.params[channel].child("output", "limits", "max_v").setValue( self.params[channel].child(
pwm_params["max_v"]["value"] "output", "limits", "max_v"
) ).set_value_with_lock(pwm_params["max_v"]["value"])
self.params[channel].child("output", "limits", "max_i_pos").setValue( self.params[channel].child(
pwm_params["max_i_pos"]["value"] "output", "limits", "max_i_pos"
) ).set_value_with_lock(pwm_params["max_i_pos"]["value"])
self.params[channel].child("output", "limits", "max_i_neg").setValue( self.params[channel].child(
pwm_params["max_i_neg"]["value"] "output", "limits", "max_i_neg"
) ).set_value_with_lock(pwm_params["max_i_neg"]["value"])
for limit in "max_i_pos", "max_i_neg", "max_v": for limit in "max_i_pos", "max_i_neg", "max_v":
if pwm_params[limit]["value"] == 0.0: if pwm_params[limit]["value"] == 0.0:
@ -202,6 +166,6 @@ class CtrlPanel(QObject):
for postfilter_params in postfilter_data: for postfilter_params in postfilter_data:
channel = postfilter_params["channel"] channel = postfilter_params["channel"]
with QSignalBlocker(self.params[channel]): with QSignalBlocker(self.params[channel]):
self.params[channel].child("thermistor", "rate").setValue( self.params[channel].child("postfilter", "rate").set_value_with_lock(
postfilter_params["rate"] postfilter_params["rate"]
) )

View File

@ -1,5 +1,11 @@
{ {
"ctrl_panel": [ "ctrl_panel": [
{
"name": "readings",
"title": "Readings",
"type": "group",
"tip": "Thermostat readings",
"children": [
{ {
"name": "temperature", "name": "temperature",
"title": "Temperature", "title": "Temperature",
@ -18,6 +24,8 @@
"decimals": 6, "decimals": 6,
"readonly": true, "readonly": true,
"tip": "The measured current through the TEC" "tip": "The measured current through the TEC"
}
]
}, },
{ {
"name": "output", "name": "output",
@ -29,7 +37,7 @@
{ {
"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"
@ -54,7 +62,6 @@
-2, -2,
2 2
], ],
"triggerOnShow": true,
"decimals": 6, "decimals": 6,
"pinSiPrefix": "m", "pinSiPrefix": "m",
"suffix": "A", "suffix": "A",
@ -154,11 +161,11 @@
"type": "float", "type": "float",
"value": 0, "value": 0,
"step": 0.1, "step": 0.1,
"decimals": 3,
"limits": [ "limits": [
0, 0,
4 4
], ],
"siPrefix": true,
"suffix": "V", "suffix": "V",
"noUnitEditing": true, "noUnitEditing": true,
"compactHeight": false, "compactHeight": false,
@ -179,7 +186,7 @@
"title": "Thermistor Settings", "title": "Thermistor Settings",
"expanded": true, "expanded": true,
"type": "group", "type": "group",
"tip": "Settings of the connected thermistor\n- Parameters for the resistance to temperature conversion (with the B-Parameter equation)\n- Settings for the 50/60 Hz filter with the thermistor", "tip": "Parameters for the resistance to temperature conversion with the B-Parameter equation",
"children": [ "children": [
{ {
"name": "t0", "name": "t0",
@ -208,7 +215,7 @@
"title": "R₀", "title": "R₀",
"type": "float", "type": "float",
"value": 10000, "value": 10000,
"step": 1, "step": 100,
"min": 0, "min": 0,
"siPrefix": true, "siPrefix": true,
"pinSiPrefix": "k", "pinSiPrefix": "k",
@ -228,7 +235,7 @@
"title": "B", "title": "B",
"type": "float", "type": "float",
"value": 3950, "value": 3950,
"step": 1, "step": 10,
"suffix": "K", "suffix": "K",
"noUnitEditing": true, "noUnitEditing": true,
"decimals": 4, "decimals": 4,
@ -240,10 +247,18 @@
], ],
"tip": "The Beta Parameter", "tip": "The Beta Parameter",
"lock": false "lock": false
}
]
}, },
{
"name": "postfilter",
"title": "50/60 Hz filter settings",
"type": "group",
"tip": "Settings for the 50/60 Hz filter with the thermistor",
"children": [
{ {
"name": "rate", "name": "rate",
"title": "50/60 Hz filter rejection", "title": "Rejection",
"type": "list", "type": "list",
"value": 16.67, "value": 16.67,
"param": [ "param": [
@ -275,7 +290,6 @@
"title": "Kp", "title": "Kp",
"type": "float", "type": "float",
"step": 0.1, "step": 0.1,
"suffix": "",
"compactHeight": false, "compactHeight": false,
"param": [ "param": [
"pid", "pid",
@ -423,9 +437,8 @@
"type": "float", "type": "float",
"value": 1.5, "value": 1.5,
"step": 0.1, "step": 0.1,
"prefix": "±",
"format": "{value:.4f} {suffix}", "format": "{value:.4f} {suffix}",
"suffix": "°C", "suffix": "K",
"noUnitEditing": true, "noUnitEditing": true,
"compactHeight": false, "compactHeight": false,
"pid_autotune": [ "pid_autotune": [

View File

@ -1,3 +1,5 @@
import re
from PyQt6.QtCore import QSignalBlocker from PyQt6.QtCore import QSignalBlocker
from PyQt6.QtGui import QValidator from PyQt6.QtGui import QValidator
@ -7,6 +9,13 @@ from pyqtgraph.parametertree import registerParameterItemType
from pyqtgraph.parametertree.parameterTypes import SimpleParameter, NumericParameterItem from pyqtgraph.parametertree.parameterTypes import SimpleParameter, NumericParameterItem
# See https://github.com/pyqtgraph/pyqtgraph/issues/3115
fn.FLOAT_REGEX = re.compile(
r"(?P<number>[+-]?((((\d+(\.\d*)?)|(\d*\.\d+))([eE][+-]?\d+)?)|((?i:nan)|(inf))))\s*"
+ r"((?P<siPrefix>[u" + fn.SI_PREFIXES + r"]?)(?P<suffix>.*))?$"
)
class UnitfulSpinBox(SpinBox): class UnitfulSpinBox(SpinBox):
""" """
Extension of PyQtGraph's SpinBox widget. Extension of PyQtGraph's SpinBox widget.
@ -82,6 +91,24 @@ class UnitfulSpinBox(SpinBox):
super().setOpts(**opts) super().setOpts(**opts)
def editingFinishedEvent(self):
# Modified from pyqtgraph.SpinBox.editingFinishedEvent source
new_text = self.lineEdit().text()
if new_text == self.lastText:
return
try:
val = self.interpret()
except Exception:
return
if val is False:
return
if val == self.val:
self.updateText() # still update text so that values are reformatted pretty-like
return
self.setValue(val, delaySignal=False) ## allow text update so that values are reformatted pretty-like
def formatText(self, prev=None): def formatText(self, prev=None):
""" """
In addition to pyqtgraph.SpinBox's formatting, incorporate the In addition to pyqtgraph.SpinBox's formatting, incorporate the

View File

@ -293,6 +293,10 @@ class MainWindow(QtWidgets.QMainWindow):
if activater[1] == "ch": if activater[1] == "ch":
activater[1] = ch activater[1] = ch
await self.client.set_param(*activater) await self.client.set_param(*activater)
else:
await self.client.set_param(
"pwm", ch, "i_set", inner_param.child("i_set").value()
)
@asyncSlot() @asyncSlot()
async def pid_autotune_request(self, ch=0): async def pid_autotune_request(self, ch=0):