Compare commits

..

9 Commits

Author SHA1 Message Date
3552a582f8 ctrl_panel: Keep i_set visible when PID engaged
Since i_set is also plotted, we would want to see its precise value too.
2024-07-19 15:55:43 +08:00
d7defecaaa ctrl_panel: Remove MutexParameter
Use the standard ListParamenter instead, and hook up UI changes
elsewhere.
2024-07-19 15:49:19 +08:00
282f3d1e82 ctrl_panel: Limits fixes
* PID Autotune test current should be positive

* Maximum absolute voltage should be 4 V not 5 V
2024-07-19 15:49:19 +08:00
d4171f002e ctrl_panel: Code cleanup
* Remove unnecessary duplication of `THERMOSTAT_PARAMETERS`

* i -> ch

* Separate ParameterTree and Parameter initiation

* Remove extra "channel" option to root parameters, as the "value"
option is already the channel number
2024-07-19 15:49:19 +08:00
1221ac4436 ctrl_panel: PID Auto Tune -> PID Autotune 2024-07-19 15:49:19 +08:00
abecac44d6 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-19 15:49:19 +08:00
8902052ee0 ctrl_panel: Appropriate 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-19 15:49:19 +08:00
fd7e22fd23 ctrl_panel: Pin down units for editable fields
Avoids awkward value editing
2024-07-19 15:49:19 +08:00
681a7539fe ctrl_panel: Improve postfilter description 2024-07-19 15:49:19 +08:00
4 changed files with 73 additions and 288 deletions

View File

@ -4,7 +4,6 @@ from pyqtgraph.parametertree import (
Parameter,
registerParameterType,
)
import pytec.gui.view.unitful
def set_tree_label_tips(tree):
@ -110,10 +109,10 @@ class CtrlPanel(QObject):
)
self.params[channel].child(
"pid", "pid_output_clamping", "output_min"
).setValue(settings["parameters"]["output_min"])
).setValue(settings["parameters"]["output_min"] * 1000)
self.params[channel].child(
"pid", "pid_output_clamping", "output_max"
).setValue(settings["parameters"]["output_max"])
).setValue(settings["parameters"]["output_max"] * 1000)
self.params[channel].child(
"output", "control_method", "target"
).setValue(settings["target"])
@ -128,13 +127,13 @@ class CtrlPanel(QObject):
)
self.params[channel].child(
"output", "control_method", "i_set"
).setValue(settings["i_set"])
).setValue(settings["i_set"] * 1000)
if settings["temperature"] is not None:
self.params[channel].child("readings", "temperature").setValue(
self.params[channel].child("temperature").setValue(
settings["temperature"]
)
if settings["tec_i"] is not None:
self.params[channel].child("readings", "tec_i").setValue(
self.params[channel].child("tec_i").setValue(
settings["tec_i"]
)
@ -164,10 +163,10 @@ class CtrlPanel(QObject):
pwm_params["max_v"]["value"]
)
self.params[channel].child("output", "limits", "max_i_pos").setValue(
pwm_params["max_i_pos"]["value"]
pwm_params["max_i_pos"]["value"] * 1000
)
self.params[channel].child("output", "limits", "max_i_neg").setValue(
pwm_params["max_i_neg"]["value"]
pwm_params["max_i_neg"]["value"] * 1000
)
for limit in "max_i_pos", "max_i_neg", "max_v":

View File

@ -1,17 +1,10 @@
{
"ctrl_panel": [
{
"name": "readings",
"title": "Readings",
"type": "group",
"tip": "Thermostat readings",
"children": [
{
"name": "temperature",
"title": "Temperature",
"type": "float",
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"format": "{value:.4f} °C",
"readonly": true,
"tip": "The measured temperature at the thermistor"
},
@ -24,8 +17,6 @@
"decimals": 6,
"readonly": true,
"tip": "The measured current through the TEC"
}
]
},
{
"name": "output",
@ -54,19 +45,16 @@
"children": [
{
"name": "i_set",
"title": "Set Current",
"title": "Set Current (mA)",
"type": "float",
"value": 0,
"step": 0.1,
"step": 100,
"limits": [
-2,
2
-2000,
2000
],
"triggerOnShow": true,
"decimals": 6,
"pinSiPrefix": "m",
"suffix": "A",
"siPrefix": true,
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pwm",
@ -78,7 +66,7 @@
},
{
"name": "target",
"title": "Setpoint",
"title": "Setpoint (°C)",
"type": "float",
"visible": false,
"value": 25,
@ -87,10 +75,7 @@
-273,
300
],
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"regex": "(?P<number>[+-]?((((\\d+(\\.\\d*)?)|(\\d*\\.\\d+))([eE][+-]?\\d+)?)|((?i:nan)|(inf))))\\s*((?P<siPrefix>[uyzafpnµm kMGTPEZY]?)(?P<suffix>.*))?$",
"noUnitEditing": true,
"format": "{value:.4f}",
"compactHeight": false,
"param": [
"pid",
@ -111,20 +96,16 @@
"children": [
{
"name": "max_i_pos",
"title": "Max Cooling Current",
"title": "Max Cooling Current (mA)",
"type": "float",
"value": 0,
"step": 0.1,
"step": 100,
"decimals": 6,
"compactHeight": false,
"limits": [
0,
2
2000
],
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pwm",
"ch",
@ -135,20 +116,16 @@
},
{
"name": "max_i_neg",
"title": "Max Heating Current",
"title": "Max Heating Current (mA)",
"type": "float",
"value": 0,
"step": 0.1,
"step": 100,
"decimals": 6,
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"limits": [
0,
2
2000
],
"compactHeight": false,
"param": [
"pwm",
"ch",
@ -159,17 +136,15 @@
},
{
"name": "max_v",
"title": "Max Absolute Voltage",
"title": "Max Absolute Voltage (V)",
"type": "float",
"value": 0,
"step": 0.1,
"decimals": 3,
"limits": [
0,
4
],
"suffix": "V",
"noUnitEditing": true,
"siPrefix": true,
"compactHeight": false,
"param": [
"pwm",
@ -192,7 +167,7 @@
"children": [
{
"name": "t0",
"title": "T₀",
"title": "T₀ (°C)",
"type": "float",
"value": 25,
"step": 0.1,
@ -200,10 +175,7 @@
-100,
100
],
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"regex": "(?P<number>[+-]?((((\\d+(\\.\\d*)?)|(\\d*\\.\\d+))([eE][+-]?\\d+)?)|((?i:nan)|(inf))))\\s*((?P<siPrefix>[uyzafpnµm kMGTPEZY]?)(?P<suffix>.*))?$",
"noUnitEditing": true,
"format": "{value:.4f}",
"compactHeight": false,
"param": [
"s-h",
@ -215,13 +187,11 @@
},
{
"name": "r0",
"title": "R₀",
"title": "R₀ (Ω)",
"type": "float",
"value": 10000,
"step": 100,
"step": 1,
"siPrefix": true,
"suffix": "Ω",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"s-h",
@ -233,12 +203,10 @@
},
{
"name": "b",
"title": "B",
"title": "B (K)",
"type": "float",
"value": 3950,
"step": 10,
"suffix": "K",
"noUnitEditing": true,
"step": 1,
"decimals": 4,
"compactHeight": false,
"param": [
@ -262,8 +230,8 @@
"limits": {
"Off": null,
"47 dB @ 10.41 Hz": 27.0,
"62 dB @ 10 Hz": 21.25,
"86 dB @ 9.1 Hz": 20.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",
@ -283,6 +251,7 @@
"title": "Kp",
"type": "float",
"step": 0.1,
"suffix": "",
"compactHeight": false,
"param": [
"pid",
@ -294,11 +263,9 @@
},
{
"name": "ki",
"title": "Ki",
"title": "Ki (Hz)",
"type": "float",
"step": 0.1,
"suffix": "Hz",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
@ -310,11 +277,9 @@
},
{
"name": "kd",
"title": "Kd",
"title": "Kd (s)",
"type": "float",
"step": 0.1,
"suffix": "s",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
@ -333,18 +298,14 @@
"children": [
{
"name": "output_min",
"title": "Minimum",
"title": "Minimum (mA)",
"type": "float",
"step": 0.1,
"step": 100,
"limits": [
-2,
2
-2000,
2000
],
"decimals": 6,
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
@ -356,18 +317,14 @@
},
{
"name": "output_max",
"title": "Maximum",
"title": "Maximum (mA)",
"type": "float",
"step": 0.1,
"step": 100,
"limits": [
-2,
2
-2000,
2000
],
"decimals": 6,
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
@ -388,14 +345,11 @@
"children": [
{
"name": "target_temp",
"title": "Target Temperature",
"title": "Target Temperature (°C)",
"type": "float",
"value": 20,
"step": 0.1,
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"regex": "(?P<number>[+-]?((((\\d+(\\.\\d*)?)|(\\d*\\.\\d+))([eE][+-]?\\d+)?)|((?i:nan)|(inf))))\\s*((?P<siPrefix>[uyzafpnµm kMGTPEZY]?)(?P<suffix>.*))?$",
"noUnitEditing": true,
"format": "{value:.4f}",
"compactHeight": false,
"pid_autotune": [
"target_temp",
@ -405,20 +359,16 @@
},
{
"name": "test_current",
"title": "Test Current",
"title": "Test Current (mA)",
"type": "float",
"value": 0,
"decimals": 6,
"step": 0.1,
"compactHeight": false,
"step": 100,
"limits": [
0,
2
2000
],
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"pid_autotune": [
"test_current",
"ch"
@ -427,14 +377,12 @@
},
{
"name": "temp_swing",
"title": "Temperature Swing",
"title": "Temperature Swing (°C)",
"type": "float",
"value": 1.5,
"step": 0.1,
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"regex": "(?P<number>[+-]?((((\\d+(\\.\\d*)?)|(\\d*\\.\\d+))([eE][+-]?\\d+)?)|((?i:nan)|(inf))))\\s*((?P<siPrefix>[uyzafpnµm kMGTPEZY]?)(?P<suffix>.*))?$",
"noUnitEditing": true,
"prefix": "±",
"format": "{value:.4f}",
"compactHeight": false,
"pid_autotune": [
"temp_swing",
@ -444,13 +392,11 @@
},
{
"name": "lookback",
"title": "Lookback",
"title": "Lookback (s)",
"type": "float",
"value": 3.0,
"step": 0.1,
"format": "{value:.4f} {suffix}",
"noUnitEditing": true,
"suffix": "s",
"format": "{value:.4f}",
"compactHeight": false,
"pid_autotune": [
"lookback",

View File

@ -1,159 +0,0 @@
from PyQt6.QtCore import QSignalBlocker
from PyQt6.QtGui import QValidator
from pyqtgraph import SpinBox
import pyqtgraph.functions as fn
from pyqtgraph.parametertree.parameterTypes import (
SimpleParameter,
NumericParameterItem,
registerParameterItemType,
)
class UnitfulSpinBox(SpinBox):
"""
Extension of PyQtGraph's SpinBox widget.
Adds:
* The "pinSiPrefix" option, where the siPrefix could be fixed to a
particular scale instead of as determined by its value.
* The "noUnitEditing" option, where the unit portion of the
SpinBox text, including the siPrefix, is fixed and uneditable.
"""
def __init__(self, parent=None, value=0.0, **kwargs):
super().__init__(parent, value, **kwargs)
self._current_si_prefix = ""
self.lineEdit().cursorPositionChanged.connect(
self.editor_cursor_position_changed
)
def validate(self, strn, pos):
ret, strn, pos = super().validate(strn, pos)
if self.opts.get("noUnitEditing") is True:
suffix = self.opts["suffix"]
# When the unit is edited / removed
if not (
strn.endswith(suffix)
and strn.removesuffix(suffix).endswith(self._current_si_prefix)
):
# Then the input is invalid instead of incomplete, reject this change
ret = QValidator.State.Invalid
return ret, strn, pos
def editor_cursor_position_changed(self, oldpos, newpos):
"""
Modified from the original Qt C++ source,
QAbstractSpinBox::editorCursorPositionChanged
Their suffix is different than our suffix; there's no obvious
way to set that one here.
"""
if self.opts.get("noUnitEditing") is True:
edit = self.lineEdit()
if edit.hasSelectedText():
return # Allow for selecting units, for copy-and-paste
unit_len = len(self._current_si_prefix) + len(self.opts["suffix"])
text_len = len(edit.text())
pos = -1
# Cursor in unit
if text_len - unit_len < newpos < text_len:
if oldpos == text_len:
pos = text_len - unit_len
else:
pos = text_len
if pos != -1:
with QSignalBlocker(edit):
edit.setCursorPosition(pos)
def setOpts(self, **opts):
if "pinSiPrefix" in opts:
self.opts["pinSiPrefix"] = opts.pop("pinSiPrefix")
if "noUnitEditing" in opts:
self.opts["noUnitEditing"] = opts.pop("noUnitEditing")
super().setOpts(**opts)
def formatText(self, prev=None):
"""
Implement 'pinSiPrefix' mechanism for pyqtgraph.SpinBox, where
SI prefixes could be pinned down.
Code modified from the PyQtGraph source
"""
# get the number of decimal places to print
decimals = self.opts['decimals']
suffix = self.opts['suffix']
prefix = self.opts['prefix']
pin_si_prefix = self.opts.get("pinSiPrefix")
# format the string
val = self.value()
if self.opts['siPrefix'] is True:
# SI prefix was requested, so scale the value accordingly
if pin_si_prefix is not None and pin_si_prefix in fn.SI_PREFIX_EXPONENTS:
# fixed scale
s = 10**-fn.SI_PREFIX_EXPONENTS[pin_si_prefix]
p = pin_si_prefix
elif self.val == 0 and prev is not None:
# special case: if it's zero use the previous prefix
(s, p) = fn.siScale(prev)
else:
(s, p) = fn.siScale(val)
parts = {'value': val, 'suffix': suffix, 'decimals': decimals, 'siPrefix': p, 'scaledValue': s*val, 'prefix':prefix}
self._current_si_prefix = p
else:
# no SI prefix /suffix requested; scale is 1
parts = {'value': val, 'suffix': suffix, 'decimals': decimals, 'siPrefix': '', 'scaledValue': val, 'prefix':prefix}
parts['prefixGap'] = '' if parts['prefix'] == '' else ' '
parts['suffixGap'] = '' if (parts['suffix'] == '' and parts['siPrefix'] == '') else ' '
return self.opts['format'].format(**parts)
class UnitfulNumericParameterItem(NumericParameterItem):
"""
Subclasses PyQtGraph's `NumericParameterItem` and uses
UnitfulSpinBox for editing.
"""
def makeWidget(self):
opts = self.param.opts
t = opts['type']
defs = {
'value': 0, 'min': None, 'max': None,
'step': 1.0, 'dec': False,
'siPrefix': False, 'suffix': '', 'decimals': 3,
'pinSiPrefix': None, 'noUnitEditing': False,
}
if t == 'int':
defs['int'] = True
defs['minStep'] = 1.0
for k in defs:
if k in opts:
defs[k] = opts[k]
if 'limits' in opts:
defs['min'], defs['max'] = opts['limits']
w = UnitfulSpinBox()
w.setOpts(**defs)
w.sigChanged = w.sigValueChanged
w.sigChanging = w.sigValueChanging
return w
registerParameterItemType(
"float", UnitfulNumericParameterItem, SimpleParameter, override=True
)
registerParameterItemType(
"int", UnitfulNumericParameterItem, SimpleParameter, override=True
)

View File

@ -267,6 +267,9 @@ class MainWindow(QtWidgets.QMainWindow):
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("title", None).endswith(" (mA)"):
data /= 1000 # Given in mA
thermostat_param = inner_param.opts["param"]
if thermostat_param[1] == "ch":
thermostat_param[1] = ch
@ -293,10 +296,6 @@ class MainWindow(QtWidgets.QMainWindow):
if activater[1] == "ch":
activater[1] = ch
await self.client.set_param(*activater)
else:
await self.client.set_param(
"pwm", ch, "i_set", inner_param.child("i_set").value()
)
@asyncSlot()
async def pid_autotune_request(self, ch=0):