Compare commits

...

17 Commits

Author SHA1 Message Date
c6fa3cdb80 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 18:32:36 +08:00
93af2d2893 ctrl_panel: Move postfilter into its own group 2024-08-07 18:32:36 +08:00
4ed135518b ctrl_panel: Use new locking mechanism from Kirdy 2024-08-07 18:32:32 +08:00
4c1c22430b 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 18:31:33 +08:00
08b3eb8114 ctrl_panel: More appropriate steps and fixes 2024-08-07 18:31:33 +08:00
bb4b7ae597 ctrl_panel: Put plotted values into readings group
For more intuitiveness to first-time users
2024-08-07 18:31:33 +08:00
5f60b44895 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 18:31:33 +08:00
31c27c6a76 ctrl_panel: Bold "Control Method" text 2024-08-07 18:30:59 +08:00
81ee2d0b27 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 18:29:54 +08:00
dbbf30bfeb ctrl_panel: Limits fixes
* PID Autotune test current should be positive

* max_v should be 4 V not 5 V

* r0 should not be negative
2024-08-07 17:59:06 +08:00
159c70972e 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-08-07 17:59:06 +08:00
651e323206 ctrl_panel: PID Auto Tune -> PID Autotune 2024-08-07 17:59:06 +08:00
d6ba9be20c 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-08-07 17:59:06 +08:00
66556f4c29 ctrl_panel: Pin down units for editable fields
User input always has the same order of magnitude, so allowing multiple
siPrefixes would be unwanted complexity. Don't allow them to be changed.

The Parameter option "noUnitEditing" is added to do so by the following
measures:

1. Don't validate for changed siPrefix (if pinned) and suffix, which
avoids their removal.

2. Avoid getting the cursor embedded within the unit.
2024-08-07 17:59:06 +08:00
c59e3e7ac4 ctrl_panel: Remove need for "mA" hack
Remove all instances of mA scaling scattered all around the code and
specify it in the parameter tree with a single source of truth.

Done by adding the option "pinSiPrefix" for all Parameters of type `int`
or `float`, and using it for current Parameters with unit "mA".
2024-08-07 17:59:06 +08:00
d96d36fb0c 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-08-07 17:59:06 +08:00
ef7dbf9b5e ctrl_panel: Improve postfilter description 2024-08-07 17:59:03 +08:00
4 changed files with 379 additions and 163 deletions

View File

@ -4,42 +4,7 @@ from pyqtgraph.parametertree import (
Parameter,
registerParameterType,
)
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)
import pytec.gui.view.unitful
def set_tree_label_tips(tree):
@ -65,57 +30,53 @@ class CtrlPanel(QObject):
self.trees_ui = trees_ui
self.NUM_CHANNELS = len(trees_ui)
self.THERMOSTAT_PARAMETERS = [param_tree for i in range(self.NUM_CHANNELS)]
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 = [
Parameter.create(
name=f"Thermostat Channel {ch} Parameters",
type="group",
value=ch,
children=self.THERMOSTAT_PARAMETERS[ch],
children=param_tree,
)
for ch in range(self.NUM_CHANNELS)
]
for i, param in enumerate(self.params):
param.channel = i
for i, tree in enumerate(self.trees_ui):
for ch, tree in enumerate(self.trees_ui):
tree.setHeaderHidden(True)
tree.setParameters(self.params[i], showTop=False)
self.params[i].setValue = self._setValue
self.params[i].sigTreeStateChanged.connect(sigTreeStateChanged_handle)
tree.setParameters(self.params[ch], showTop=False)
set_tree_label_tips(tree)
for handle in sigActivated_handles[i]:
self.params[i].child(*handle[0]).sigActivated.connect(handle[1])
for ch, param in enumerate(self.params):
param.sigTreeStateChanged.connect(sigTreeStateChanged_handle)
def _setValue(self, value, blockSignal=None):
"""
Implement 'lock' mechanism for Parameter Type
for handle in sigActivated_handles[ch]:
param.child(*handle[0]).sigActivated.connect(handle[1])
Modified from the source
"""
try:
if blockSignal is not None:
self.sigValueChanged.disconnect(blockSignal)
value = self._interpretValue(value)
if fn.eq(self.opts["value"], value):
return value
def _indicate_usage(param, control_method):
for item in param.child("i_set").items:
is_constant_current = control_method == "constant_current"
font = item.font(0)
font.setUnderline(is_constant_current)
font.setBold(is_constant_current)
item.setFont(0, font)
for item in param.child("target").items:
is_temperature_pid = control_method == "temperature_pid"
font = item.font(0)
font.setUnderline(is_temperature_pid)
font.setBold(is_temperature_pid)
item.setFont(0, font)
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)
param.child("output", "control_method").sigValueChanged.connect(_indicate_usage)
return self.opts["value"]
for item in param.child("output", "control_method").items:
font = item.font(0)
font.setBold(True)
item.setFont(0, font)
def change_params_title(self, channel, path, title):
self.params[channel].child(*path).setOpts(title=title)
@ -125,57 +86,59 @@ class CtrlPanel(QObject):
for settings in pid_settings:
channel = settings["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"]
)
self.params[channel].child("pid", "ki").setValue(
self.params[channel].child("pid", "ki").set_value_with_lock(
settings["parameters"]["ki"]
)
self.params[channel].child("pid", "kd").setValue(
self.params[channel].child("pid", "kd").set_value_with_lock(
settings["parameters"]["kd"]
)
self.params[channel].child(
"pid", "pid_output_clamping", "output_min"
).setValue(settings["parameters"]["output_min"] * 1000)
).set_value_with_lock(settings["parameters"]["output_min"])
self.params[channel].child(
"pid", "pid_output_clamping", "output_max"
).setValue(settings["parameters"]["output_max"] * 1000)
).set_value_with_lock(settings["parameters"]["output_max"])
self.params[channel].child(
"output", "control_method", "target"
).setValue(settings["target"])
).set_value_with_lock(settings["target"])
@pyqtSlot("QVariantList")
def update_report(self, report_data):
for settings in report_data:
channel = settings["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"
)
self.params[channel].child(
"output", "control_method", "i_set"
).setValue(settings["i_set"] * 1000)
).set_value_with_lock(settings["i_set"])
if settings["temperature"] is not None:
self.params[channel].child("temperature").setValue(
settings["temperature"]
)
self.params[channel].child(
"readings", "temperature"
).set_value_with_lock(settings["temperature"])
if settings["tec_i"] is not None:
self.params[channel].child("tec_i").setValue(
settings["tec_i"] * 1000
)
self.params[channel].child(
"readings", "tec_i"
).set_value_with_lock(settings["tec_i"])
@pyqtSlot("QVariantList")
def update_thermistor(self, sh_data):
for sh_param in sh_data:
channel = sh_param["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
)
self.params[channel].child("thermistor", "r0").setValue(
self.params[channel].child("thermistor", "r0").set_value_with_lock(
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"]
)
@ -186,15 +149,15 @@ class CtrlPanel(QObject):
for pwm_params in pwm_data:
channel = pwm_params["channel"]
with QSignalBlocker(self.params[channel]):
self.params[channel].child("output", "limits", "max_v").setValue(
pwm_params["max_v"]["value"]
)
self.params[channel].child("output", "limits", "max_i_pos").setValue(
pwm_params["max_i_pos"]["value"] * 1000
)
self.params[channel].child("output", "limits", "max_i_neg").setValue(
pwm_params["max_i_neg"]["value"] * 1000
)
self.params[channel].child(
"output", "limits", "max_v"
).set_value_with_lock(pwm_params["max_v"]["value"])
self.params[channel].child(
"output", "limits", "max_i_pos"
).set_value_with_lock(pwm_params["max_i_pos"]["value"])
self.params[channel].child(
"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":
if pwm_params[limit]["value"] == 0.0:
@ -206,6 +169,6 @@ class CtrlPanel(QObject):
for postfilter_params in postfilter_data:
channel = postfilter_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"]
)

View File

@ -1,21 +1,31 @@
{
"ctrl_panel": [
{
"name": "temperature",
"title": "Temperature",
"type": "float",
"format": "{value:.4f} °C",
"readonly": true,
"tip": "The measured temperature at the thermistor"
},
{
"name": "tec_i",
"title": "Current through TEC",
"type": "float",
"suffix": "mA",
"decimals": 6,
"readonly": true,
"tip": "The measured current through the TEC"
"name": "readings",
"title": "Readings",
"type": "group",
"tip": "Thermostat readings",
"children": [
{
"name": "temperature",
"title": "Temperature",
"type": "float",
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"readonly": true,
"tip": "The measured temperature at the thermistor"
},
{
"name": "tec_i",
"title": "Current through TEC",
"type": "float",
"siPrefix": true,
"suffix": "A",
"decimals": 6,
"readonly": true,
"tip": "The measured current through the TEC"
}
]
},
{
"name": "output",
@ -27,7 +37,7 @@
{
"name": "control_method",
"title": "Control Method",
"type": "mutex",
"type": "list",
"limits": {
"Constant Current": "constant_current",
"Temperature PID": "temperature_pid"
@ -47,14 +57,17 @@
"title": "Set Current",
"type": "float",
"value": 0,
"step": 100,
"step": 0.1,
"limits": [
-2000,
2000
-2,
2
],
"triggerOnShow": true,
"decimals": 6,
"suffix": "mA",
"pinSiPrefix": "m",
"suffix": "A",
"siPrefix": true,
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pwm",
"ch",
@ -73,7 +86,10 @@
-273,
300
],
"format": "{value:.4f} °C",
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
"ch",
@ -96,13 +112,17 @@
"title": "Max Cooling Current",
"type": "float",
"value": 0,
"step": 100,
"step": 0.1,
"decimals": 6,
"limits": [
0,
2000
2
],
"suffix": "mA",
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pwm",
"ch",
@ -116,13 +136,17 @@
"title": "Max Heating Current",
"type": "float",
"value": 0,
"step": 100,
"step": 0.1,
"decimals": 6,
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"limits": [
0,
2000
2
],
"suffix": "mA",
"compactHeight": false,
"param": [
"pwm",
"ch",
@ -137,12 +161,14 @@
"type": "float",
"value": 0,
"step": 0.1,
"decimals": 3,
"limits": [
0,
5
4
],
"siPrefix": true,
"suffix": "V",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pwm",
"ch",
@ -160,7 +186,7 @@
"title": "Thermistor Settings",
"expanded": true,
"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": [
{
"name": "t0",
@ -172,7 +198,10 @@
-100,
100
],
"format": "{value:.4f} °C",
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"s-h",
"ch",
@ -186,9 +215,13 @@
"title": "R₀",
"type": "float",
"value": 10000,
"step": 1,
"step": 100,
"min": 0,
"siPrefix": true,
"pinSiPrefix": "k",
"suffix": "Ω",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"s-h",
"ch",
@ -202,9 +235,11 @@
"title": "B",
"type": "float",
"value": 3950,
"step": 1,
"step": 10,
"suffix": "K",
"noUnitEditing": true,
"decimals": 4,
"compactHeight": false,
"param": [
"s-h",
"ch",
@ -212,10 +247,18 @@
],
"tip": "The Beta Parameter",
"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",
"title": "Postfilter Rate",
"title": "Rejection",
"type": "list",
"value": 16.67,
"param": [
@ -224,11 +267,11 @@
"rate"
],
"limits": {
"Off": null,
"16.67 Hz": 16.67,
"20 Hz": 20.0,
"21.25 Hz": 21.25,
"27 Hz": 27.0
"Off @ 10 Hz": null,
"47 dB @ 10.41 Hz": 27.0,
"62 dB @ 10 Hz": 21.25,
"86 dB @ 9.1 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
@ -247,7 +290,7 @@
"title": "Kp",
"type": "float",
"step": 0.1,
"suffix": "",
"compactHeight": false,
"param": [
"pid",
"ch",
@ -262,6 +305,8 @@
"type": "float",
"step": 0.1,
"suffix": "Hz",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
"ch",
@ -276,6 +321,8 @@
"type": "float",
"step": 0.1,
"suffix": "s",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
"ch",
@ -295,13 +342,17 @@
"name": "output_min",
"title": "Minimum",
"type": "float",
"step": 100,
"step": 0.1,
"limits": [
-2000,
2000
-2,
2
],
"decimals": 6,
"suffix": "mA",
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
"ch",
@ -314,13 +365,17 @@
"name": "output_max",
"title": "Maximum",
"type": "float",
"step": 100,
"step": 0.1,
"limits": [
-2000,
2000
-2,
2
],
"decimals": 6,
"suffix": "mA",
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"param": [
"pid",
"ch",
@ -333,7 +388,7 @@
},
{
"name": "pid_autotune",
"title": "PID Auto Tune",
"title": "PID Autotune",
"expanded": false,
"type": "group",
"tip": "Automatically tune PID parameters",
@ -344,7 +399,10 @@
"type": "float",
"value": 20,
"step": 0.1,
"format": "{value:.4f} °C",
"format": "{value:.4f} {suffix}",
"suffix": "°C",
"noUnitEditing": true,
"compactHeight": false,
"pid_autotune": [
"target_temp",
"ch"
@ -357,12 +415,16 @@
"type": "float",
"value": 0,
"decimals": 6,
"step": 100,
"step": 0.1,
"limits": [
-2000,
2000
0,
2
],
"suffix": "mA",
"siPrefix": true,
"pinSiPrefix": "m",
"suffix": "A",
"noUnitEditing": true,
"compactHeight": false,
"pid_autotune": [
"test_current",
"ch"
@ -375,8 +437,10 @@
"type": "float",
"value": 1.5,
"step": 0.1,
"prefix": "±",
"format": "{value:.4f} °C",
"format": "{value:.4f} {suffix}",
"suffix": "K",
"noUnitEditing": true,
"compactHeight": false,
"pid_autotune": [
"temp_swing",
"ch"
@ -389,7 +453,10 @@
"type": "float",
"value": 3.0,
"step": 0.1,
"format": "{value:.4f} s",
"format": "{value:.4f} {suffix}",
"noUnitEditing": true,
"suffix": "s",
"compactHeight": false,
"pid_autotune": [
"lookback",
"ch"

View File

@ -0,0 +1,185 @@
import re
from PyQt6.QtCore import QSignalBlocker
from PyQt6.QtGui import QValidator
from pyqtgraph import SpinBox
import pyqtgraph.functions as fn
from pyqtgraph.parametertree import registerParameterItemType
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):
"""
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.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"]
pinned_si_prefix = self.opts.get("pinSiPrefix")
suffix_edited = not strn.endswith(suffix)
pinned_si_prefix_edited = (
pinned_si_prefix is not None
and not strn.removesuffix(suffix).endswith(pinned_si_prefix)
)
if suffix_edited or pinned_si_prefix_edited:
ret = QValidator.State.Invalid
return ret, strn, pos
def _editor_cursor_position_changed(self, oldpos, newpos):
# Called on cursor position change
# Skips over the suffix and pinned SI prefix on cursor navigation if option
# noUnitEditing is enabled.
# Modified from the original Qt C++ source,
# QAbstractSpinBox::editorCursorPositionChanged.
# Their suffix is different than our suffix; there's no obvious way to set
# theirs here in the derived class since it is private.
if self.opts.get("noUnitEditing") is True:
edit = self.lineEdit()
if edit.hasSelectedText():
return # Allow for selecting units, for copy-and-paste
pinned_si_prefix = self.opts.get("pinSiPrefix") or ""
unit_len = len(pinned_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 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):
"""
In addition to pyqtgraph.SpinBox's formatting, incorporate the
'pinSiPrefix' mechanism, where SI prefixes could be fixed.
"""
# 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}
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

@ -82,7 +82,7 @@ class MainWindow(QtWidgets.QMainWindow):
[["load"], partial(self.thermostat.load_cfg, ch)],
[
["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)
@ -262,14 +262,11 @@ class MainWindow(QtWidgets.QMainWindow):
@asyncSlot(object, object)
async def send_command(self, param, changes):
"""Translates parameter tree changes into thermostat set_param calls"""
ch = param.channel
ch = param.value()
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
@ -283,10 +280,10 @@ class MainWindow(QtWidgets.QMainWindow):
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]
autotuner_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)
self.autotuners.set_params(autotuner_param, ch, data)
if inner_param.opts.get("activaters", None) is not None:
activater = inner_param.opts["activaters"][
@ -296,9 +293,13 @@ 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_auto_tune_request(self, ch=0):
async def pid_autotune_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)