From 55a75838671401e748fc31e1c6702f16338e6458 Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 3 Jul 2024 13:40:50 +0800 Subject: [PATCH 01/21] Format JSON --- pytec/pytec/gui/view/param_tree.json | 666 +++++++++++++-------------- 1 file changed, 333 insertions(+), 333 deletions(-) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 28ce704..bb98215 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -1,335 +1,335 @@ { - "ctrl_panel":[ - { - "name":"Temperature", - "type":"float", - "format":"{value:.4f} °C", - "readonly":true - }, - { - "name":"Current through TEC", - "type":"float", - "suffix":"mA", - "decimals":6, - "readonly":true - }, - { - "name":"Output Config", - "expanded":true, - "type":"group", - "children":[ - { - "name":"Control Method", - "type":"mutex", - "limits":[ - "Constant Current", - "Temperature PID" - ], - "thermostat:set_param":{ - "topic":"pwm", - "field":"pid" - }, - "children":[ - { - "name":"Set Current", - "type":"float", - "value":0, - "step":100, - "limits":[ - -2000, - 2000 - ], - "triggerOnShow":true, - "decimals":6, - "suffix":"mA", - "thermostat:set_param":{ - "topic":"pwm", - "field":"i_set" - }, - "lock":false - }, - { - "name":"Set Temperature", - "type":"float", - "value":25, - "step":0.1, - "limits":[ - -273, - 300 - ], - "format":"{value:.4f} °C", - "thermostat:set_param":{ - "topic":"pid", - "field":"target" - }, - "lock":false - } - ] - }, - { - "name":"Limits", - "expanded":true, - "type":"group", - "children":[ - { - "name":"Max Cooling Current", - "type":"float", - "value":0, - "step":100, - "decimals":6, - "limits":[ - 0, - 2000 - ], - "suffix":"mA", - "thermostat:set_param":{ - "topic":"pwm", - "field":"max_i_pos" - }, - "lock":false - }, - { - "name":"Max Heating Current", - "type":"float", - "value":0, - "step":100, - "decimals":6, - "limits":[ - 0, - 2000 - ], - "suffix":"mA", - "thermostat:set_param":{ - "topic":"pwm", - "field":"max_i_neg" - }, - "lock":false - }, - { - "name":"Max Voltage Difference", - "type":"float", - "value":0, - "step":0.1, - "limits":[ - 0, - 5 - ], - "siPrefix":true, - "suffix":"V", - "thermostat:set_param":{ - "topic":"pwm", - "field":"max_v" - }, - "lock":false - } - ] - } - ] - }, - { - "name":"Thermistor Config", - "expanded":true, - "type":"group", - "children":[ - { - "name":"T₀", - "type":"float", - "value":25, - "step":0.1, - "limits":[ - -100, - 100 - ], - "format":"{value:.4f} °C", - "thermostat:set_param":{ - "topic":"s-h", - "field":"t0" - }, - "lock":false - }, - { - "name":"R₀", - "type":"float", - "value":10000, - "step":1, - "siPrefix":true, - "suffix":"Ω", - "thermostat:set_param":{ - "topic":"s-h", - "field":"r0" - }, - "lock":false - }, - { - "name":"B", - "type":"float", - "value":3950, - "step":1, - "suffix":"K", - "decimals":4, - "thermostat:set_param":{ - "topic":"s-h", - "field":"b" - }, - "lock":false - }, - { - "name":"Postfilter Rate", - "type":"list", - "value":16.67, - "thermostat:set_param":{ - "topic":"postfilter", - "field":"rate" - }, - "limits":{ - "Off":null, - "16.67 Hz":16.67, - "20 Hz":20.0, - "21.25 Hz":21.25, - "27 Hz":27.0 - }, - "lock":false - } - ] - }, - { - "name":"PID Config", - "expanded":true, - "type":"group", - "children":[ - { - "name":"Kp", - "type":"float", - "step":0.1, - "suffix":"", - "thermostat:set_param":{ - "topic":"pid", - "field":"kp" - }, - "lock":false - }, - { - "name":"Ki", - "type":"float", - "step":0.1, - "suffix":"Hz", - "thermostat:set_param":{ - "topic":"pid", - "field":"ki" - }, - "lock":false - }, - { - "name":"Kd", - "type":"float", - "step":0.1, - "suffix":"s", - "thermostat:set_param":{ - "topic":"pid", - "field":"kd" - }, - "lock":false - }, - { - "name":"PID Output Clamping", - "expanded":true, - "type":"group", - "children":[ - { - "name":"Minimum", - "type":"float", - "step":100, - "limits":[ - -2000, - 2000 - ], - "decimals":6, - "suffix":"mA", - "thermostat:set_param":{ - "topic":"pid", - "field":"output_min" - }, - "lock":false - }, - { - "name":"Maximum", - "type":"float", - "step":100, - "limits":[ - -2000, - 2000 - ], - "decimals":6, - "suffix":"mA", - "thermostat:set_param":{ - "topic":"pid", - "field":"output_max" - }, - "lock":false - } - ] - }, - { - "name":"PID Auto Tune", - "expanded":false, - "type":"group", - "children":[ - { - "name":"Target Temperature", - "type":"float", - "value":20, - "step":0.1, - "format":"{value:.4f} °C", - "pid_autotune":"target_temp" - }, - { - "name":"Test Current", - "type":"float", - "value":0, - "decimals":6, - "step":100, - "limits":[ - -2000, - 2000 - ], - "suffix":"mA", - "pid_autotune":"test_current" - }, - { - "name":"Temperature Swing", - "type":"float", - "value":1.5, - "step":0.1, - "prefix":"±", - "format":"{value:.4f} °C", - "pid_autotune":"temp_swing" - }, - { - "name":"Lookback", - "type":"float", - "value":3.0, - "step":0.1, - "format":"{value:.4f} s", - "pid_autotune":"lookback" + "ctrl_panel": [ + { + "name": "Temperature", + "type": "float", + "format": "{value:.4f} °C", + "readonly": true + }, + { + "name": "Current through TEC", + "type": "float", + "suffix": "mA", + "decimals": 6, + "readonly": true + }, + { + "name": "Output Config", + "expanded": true, + "type": "group", + "children": [ + { + "name": "Control Method", + "type": "mutex", + "limits": [ + "Constant Current", + "Temperature PID" + ], + "thermostat:set_param": { + "topic": "pwm", + "field": "pid" + }, + "children": [ + { + "name": "Set Current", + "type": "float", + "value": 0, + "step": 100, + "limits": [ + -2000, + 2000 + ], + "triggerOnShow": true, + "decimals": 6, + "suffix": "mA", + "thermostat:set_param": { + "topic": "pwm", + "field": "i_set" + }, + "lock": false }, - { - "name":"Run", - "type":"action", - "tip":"Run" - } - ] - } - ] - }, - { - "name":"Save to flash", - "type":"action", - "tip":"Save config to thermostat, applies on reset" - }, - { - "name":"Load from flash", - "type":"action", - "tip":"Load config from flash" - } - ] - } \ No newline at end of file + { + "name": "Set Temperature", + "type": "float", + "value": 25, + "step": 0.1, + "limits": [ + -273, + 300 + ], + "format": "{value:.4f} °C", + "thermostat:set_param": { + "topic": "pid", + "field": "target" + }, + "lock": false + } + ] + }, + { + "name": "Limits", + "expanded": true, + "type": "group", + "children": [ + { + "name": "Max Cooling Current", + "type": "float", + "value": 0, + "step": 100, + "decimals": 6, + "limits": [ + 0, + 2000 + ], + "suffix": "mA", + "thermostat:set_param": { + "topic": "pwm", + "field": "max_i_pos" + }, + "lock": false + }, + { + "name": "Max Heating Current", + "type": "float", + "value": 0, + "step": 100, + "decimals": 6, + "limits": [ + 0, + 2000 + ], + "suffix": "mA", + "thermostat:set_param": { + "topic": "pwm", + "field": "max_i_neg" + }, + "lock": false + }, + { + "name": "Max Voltage Difference", + "type": "float", + "value": 0, + "step": 0.1, + "limits": [ + 0, + 5 + ], + "siPrefix": true, + "suffix": "V", + "thermostat:set_param": { + "topic": "pwm", + "field": "max_v" + }, + "lock": false + } + ] + } + ] + }, + { + "name": "Thermistor Config", + "expanded": true, + "type": "group", + "children": [ + { + "name": "T₀", + "type": "float", + "value": 25, + "step": 0.1, + "limits": [ + -100, + 100 + ], + "format": "{value:.4f} °C", + "thermostat:set_param": { + "topic": "s-h", + "field": "t0" + }, + "lock": false + }, + { + "name": "R₀", + "type": "float", + "value": 10000, + "step": 1, + "siPrefix": true, + "suffix": "Ω", + "thermostat:set_param": { + "topic": "s-h", + "field": "r0" + }, + "lock": false + }, + { + "name": "B", + "type": "float", + "value": 3950, + "step": 1, + "suffix": "K", + "decimals": 4, + "thermostat:set_param": { + "topic": "s-h", + "field": "b" + }, + "lock": false + }, + { + "name": "Postfilter Rate", + "type": "list", + "value": 16.67, + "thermostat:set_param": { + "topic": "postfilter", + "field": "rate" + }, + "limits": { + "Off": null, + "16.67 Hz": 16.67, + "20 Hz": 20.0, + "21.25 Hz": 21.25, + "27 Hz": 27.0 + }, + "lock": false + } + ] + }, + { + "name": "PID Config", + "expanded": true, + "type": "group", + "children": [ + { + "name": "Kp", + "type": "float", + "step": 0.1, + "suffix": "", + "thermostat:set_param": { + "topic": "pid", + "field": "kp" + }, + "lock": false + }, + { + "name": "Ki", + "type": "float", + "step": 0.1, + "suffix": "Hz", + "thermostat:set_param": { + "topic": "pid", + "field": "ki" + }, + "lock": false + }, + { + "name": "Kd", + "type": "float", + "step": 0.1, + "suffix": "s", + "thermostat:set_param": { + "topic": "pid", + "field": "kd" + }, + "lock": false + }, + { + "name": "PID Output Clamping", + "expanded": true, + "type": "group", + "children": [ + { + "name": "Minimum", + "type": "float", + "step": 100, + "limits": [ + -2000, + 2000 + ], + "decimals": 6, + "suffix": "mA", + "thermostat:set_param": { + "topic": "pid", + "field": "output_min" + }, + "lock": false + }, + { + "name": "Maximum", + "type": "float", + "step": 100, + "limits": [ + -2000, + 2000 + ], + "decimals": 6, + "suffix": "mA", + "thermostat:set_param": { + "topic": "pid", + "field": "output_max" + }, + "lock": false + } + ] + }, + { + "name": "PID Auto Tune", + "expanded": false, + "type": "group", + "children": [ + { + "name": "Target Temperature", + "type": "float", + "value": 20, + "step": 0.1, + "format": "{value:.4f} °C", + "pid_autotune": "target_temp" + }, + { + "name": "Test Current", + "type": "float", + "value": 0, + "decimals": 6, + "step": 100, + "limits": [ + -2000, + 2000 + ], + "suffix": "mA", + "pid_autotune": "test_current" + }, + { + "name": "Temperature Swing", + "type": "float", + "value": 1.5, + "step": 0.1, + "prefix": "±", + "format": "{value:.4f} °C", + "pid_autotune": "temp_swing" + }, + { + "name": "Lookback", + "type": "float", + "value": 3.0, + "step": 0.1, + "format": "{value:.4f} s", + "pid_autotune": "lookback" + }, + { + "name": "Run", + "type": "action", + "tip": "Run" + } + ] + } + ] + }, + { + "name": "Save to flash", + "type": "action", + "tip": "Save config to thermostat, applies on reset" + }, + { + "name": "Load from flash", + "type": "action", + "tip": "Load config from flash" + } + ] +} \ No newline at end of file From 9af4ffd125de2b32fb0b6487177c743fafa7ae2f Mon Sep 17 00:00:00 2001 From: atse Date: Mon, 24 Jun 2024 12:04:34 +0800 Subject: [PATCH 02/21] ctrl_panel: Config -> Settings --- pytec/pytec/gui/model/thermostat.py | 4 ++-- pytec/pytec/gui/view/ctrl_panel.py | 32 +++++++++++++++------------- pytec/pytec/gui/view/param_tree.json | 11 +++++----- pytec/tec_qt.py | 8 +++---- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/pytec/pytec/gui/model/thermostat.py b/pytec/pytec/gui/model/thermostat.py index 75615a2..58fbac7 100644 --- a/pytec/pytec/gui/model/thermostat.py +++ b/pytec/pytec/gui/model/thermostat.py @@ -117,14 +117,14 @@ class Thermostat(QObject, metaclass=PropertyMeta): async def save_cfg(self, ch): await self._client.save_config(ch) self.info_box_trigger.emit( - "Config saved", f"Channel {ch} Config has been saved from flash." + "Settings saved", f"Channel {ch} Settings has been saved to flash." ) @asyncSlot() async def load_cfg(self, ch): await self._client.load_config(ch) self.info_box_trigger.emit( - "Config loaded", f"Channel {ch} Config has been loaded from flash." + "Settings loaded", f"Channel {ch} Settings has been loaded from flash." ) async def dfu(self): diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index 0c8d9d2..4dc9346 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -116,23 +116,23 @@ class CtrlPanel(QObject): for settings in pid_settings: channel = settings["channel"] with QSignalBlocker(self.params[channel]): - self.params[channel].child("PID Config", "Kp").setValue( + self.params[channel].child("PID Settings", "Kp").setValue( settings["parameters"]["kp"] ) - self.params[channel].child("PID Config", "Ki").setValue( + self.params[channel].child("PID Settings", "Ki").setValue( settings["parameters"]["ki"] ) - self.params[channel].child("PID Config", "Kd").setValue( + self.params[channel].child("PID Settings", "Kd").setValue( settings["parameters"]["kd"] ) self.params[channel].child( - "PID Config", "PID Output Clamping", "Minimum" + "PID Settings", "PID Output Clamping", "Minimum" ).setValue(settings["parameters"]["output_min"] * 1000) self.params[channel].child( - "PID Config", "PID Output Clamping", "Maximum" + "PID Settings", "PID Output Clamping", "Maximum" ).setValue(settings["parameters"]["output_max"] * 1000) self.params[channel].child( - "Output Config", "Control Method", "Set Temperature" + "Output Settings", "Control Method", "Set Temperature" ).setValue(settings["target"]) @pyqtSlot("QVariantList") @@ -140,11 +140,13 @@ class CtrlPanel(QObject): for settings in report_data: channel = settings["channel"] with QSignalBlocker(self.params[channel]): - self.params[channel].child("Output Config", "Control Method").setValue( + self.params[channel].child( + "Output Settings", "Control Method" + ).setValue( "Temperature PID" if settings["pid_engaged"] else "Constant Current" ) self.params[channel].child( - "Output Config", "Control Method", "Set Current" + "Output Settings", "Control Method", "Set Current" ).setValue(settings["i_set"] * 1000) if settings["temperature"] is not None: self.params[channel].child("Temperature").setValue( @@ -160,13 +162,13 @@ class CtrlPanel(QObject): for sh_param in sh_data: channel = sh_param["channel"] with QSignalBlocker(self.params[channel]): - self.params[channel].child("Thermistor Config", "T₀").setValue( + self.params[channel].child("Thermistor Settings", "T₀").setValue( sh_param["params"]["t0"] - 273.15 ) - self.params[channel].child("Thermistor Config", "R₀").setValue( + self.params[channel].child("Thermistor Settings", "R₀").setValue( sh_param["params"]["r0"] ) - self.params[channel].child("Thermistor Config", "B").setValue( + self.params[channel].child("Thermistor Settings", "B").setValue( sh_param["params"]["b"] ) @@ -178,13 +180,13 @@ class CtrlPanel(QObject): channel = pwm_params["channel"] with QSignalBlocker(self.params[channel]): self.params[channel].child( - "Output Config", "Limits", "Max Voltage Difference" + "Output Settings", "Limits", "Max Voltage Difference" ).setValue(pwm_params["max_v"]["value"]) self.params[channel].child( - "Output Config", "Limits", "Max Cooling Current" + "Output Settings", "Limits", "Max Cooling Current" ).setValue(pwm_params["max_i_pos"]["value"] * 1000) self.params[channel].child( - "Output Config", "Limits", "Max Heating Current" + "Output Settings", "Limits", "Max Heating Current" ).setValue(pwm_params["max_i_neg"]["value"] * 1000) for limit in "max_i_pos", "max_i_neg", "max_v": @@ -198,5 +200,5 @@ class CtrlPanel(QObject): channel = postfilter_params["channel"] with QSignalBlocker(self.params[channel]): self.params[channel].child( - "Thermistor Config", "Postfilter Rate" + "Thermistor Settings", "Postfilter Rate" ).setValue(postfilter_params["rate"]) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index bb98215..a7bc544 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -14,7 +14,7 @@ "readonly": true }, { - "name": "Output Config", + "name": "Output Settings", "expanded": true, "type": "group", "children": [ @@ -127,9 +127,10 @@ ] }, { - "name": "Thermistor Config", + "name": "Thermistor Settings", "expanded": true, "type": "group", + "tip": "Settings of the connected Thermistor", "children": [ { "name": "T₀", @@ -193,7 +194,7 @@ ] }, { - "name": "PID Config", + "name": "PID Settings", "expanded": true, "type": "group", "children": [ @@ -324,12 +325,12 @@ { "name": "Save to flash", "type": "action", - "tip": "Save config to thermostat, applies on reset" + "tip": "Save settings to thermostat, applies on reset" }, { "name": "Load from flash", "type": "action", - "tip": "Load config from flash" + "tip": "Load settings from flash" } ] } \ No newline at end of file diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index c760d88..7dc9b75 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -81,7 +81,7 @@ class MainWindow(QtWidgets.QMainWindow): [["Save to flash"], partial(self.thermostat.save_cfg, ch)], [["Load from flash"], partial(self.thermostat.load_cfg, ch)], [ - ["PID Config", "PID Auto Tune", "Run"], + ["PID Settings", "PID Auto Tune", "Run"], partial(self.pid_auto_tune_request, ch), ], ] @@ -312,18 +312,18 @@ class MainWindow(QtWidgets.QMainWindow): match self.autotuners.get_state(ch): case PIDAutotuneState.STATE_OFF: self.ctrl_panel_view.change_params_title( - ch, ("PID Config", "PID Auto Tune", "Run"), "Run" + ch, ("PID Settings", "PID Auto Tune", "Run"), "Run" ) 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, ("PID Settings", "PID Auto Tune", "Run"), "Stop" ) 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.", + f"Channel {ch} PID Settings has been loaded to Thermostat. Regulating temperature.", ) self.info_box.show() From 6a38f9b5a66a3ac6984e699f83d464e4fcede7bc Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 11 Jul 2024 10:25:52 +0800 Subject: [PATCH 03/21] ctrl_panel: Refer to Parameters by concise `name`s For displayed string representations, use the `title` key, or for `ListParameter`s, use the dictionary mapping method instead. --- pytec/pytec/gui/view/ctrl_panel.py | 54 ++++++++------- pytec/pytec/gui/view/param_tree.json | 98 ++++++++++++++++++---------- pytec/tec_qt.py | 16 ++--- 3 files changed, 98 insertions(+), 70 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index 4dc9346..a728de1 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -116,23 +116,23 @@ class CtrlPanel(QObject): for settings in pid_settings: channel = settings["channel"] with QSignalBlocker(self.params[channel]): - self.params[channel].child("PID Settings", "Kp").setValue( + self.params[channel].child("pid", "kp").setValue( settings["parameters"]["kp"] ) - self.params[channel].child("PID Settings", "Ki").setValue( + self.params[channel].child("pid", "ki").setValue( settings["parameters"]["ki"] ) - self.params[channel].child("PID Settings", "Kd").setValue( + self.params[channel].child("pid", "kd").setValue( settings["parameters"]["kd"] ) self.params[channel].child( - "PID Settings", "PID Output Clamping", "Minimum" + "pid", "pid_output_clamping", "output_min" ).setValue(settings["parameters"]["output_min"] * 1000) self.params[channel].child( - "PID Settings", "PID Output Clamping", "Maximum" + "pid", "pid_output_clamping", "output_max" ).setValue(settings["parameters"]["output_max"] * 1000) self.params[channel].child( - "Output Settings", "Control Method", "Set Temperature" + "output", "control_method", "target" ).setValue(settings["target"]) @pyqtSlot("QVariantList") @@ -140,20 +140,18 @@ class CtrlPanel(QObject): for settings in report_data: channel = settings["channel"] with QSignalBlocker(self.params[channel]): - self.params[channel].child( - "Output Settings", "Control Method" - ).setValue( - "Temperature PID" if settings["pid_engaged"] else "Constant Current" + self.params[channel].child("output", "control_method").setValue( + "temperature_pid" if settings["pid_engaged"] else "constant_current" ) self.params[channel].child( - "Output Settings", "Control Method", "Set Current" + "output", "control_method", "i_set" ).setValue(settings["i_set"] * 1000) if settings["temperature"] is not None: - self.params[channel].child("Temperature").setValue( + self.params[channel].child("temperature").setValue( settings["temperature"] ) if settings["tec_i"] is not None: - self.params[channel].child("Current through TEC").setValue( + self.params[channel].child("tec_i").setValue( settings["tec_i"] * 1000 ) @@ -162,13 +160,13 @@ class CtrlPanel(QObject): for sh_param in sh_data: channel = sh_param["channel"] with QSignalBlocker(self.params[channel]): - self.params[channel].child("Thermistor Settings", "T₀").setValue( + self.params[channel].child("thermistor", "t0").setValue( sh_param["params"]["t0"] - 273.15 ) - self.params[channel].child("Thermistor Settings", "R₀").setValue( + self.params[channel].child("thermistor", "r0").setValue( sh_param["params"]["r0"] ) - self.params[channel].child("Thermistor Settings", "B").setValue( + self.params[channel].child("thermistor", "b").setValue( sh_param["params"]["b"] ) @@ -179,15 +177,15 @@ class CtrlPanel(QObject): for pwm_params in pwm_data: channel = pwm_params["channel"] with QSignalBlocker(self.params[channel]): - self.params[channel].child( - "Output Settings", "Limits", "Max Voltage Difference" - ).setValue(pwm_params["max_v"]["value"]) - self.params[channel].child( - "Output Settings", "Limits", "Max Cooling Current" - ).setValue(pwm_params["max_i_pos"]["value"] * 1000) - self.params[channel].child( - "Output Settings", "Limits", "Max Heating Current" - ).setValue(pwm_params["max_i_neg"]["value"] * 1000) + 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 + ) for limit in "max_i_pos", "max_i_neg", "max_v": if pwm_params[limit]["value"] == 0.0: @@ -199,6 +197,6 @@ class CtrlPanel(QObject): for postfilter_params in postfilter_data: channel = postfilter_params["channel"] with QSignalBlocker(self.params[channel]): - self.params[channel].child( - "Thermistor Settings", "Postfilter Rate" - ).setValue(postfilter_params["rate"]) + self.params[channel].child("thermistor", "rate").setValue( + postfilter_params["rate"] + ) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index a7bc544..ddd4931 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -1,37 +1,42 @@ { "ctrl_panel": [ { - "name": "Temperature", + "name": "temperature", + "title": "Temperature", "type": "float", "format": "{value:.4f} °C", "readonly": true }, { - "name": "Current through TEC", + "name": "tec_i", + "title": "Current through TEC", "type": "float", "suffix": "mA", "decimals": 6, "readonly": true }, { - "name": "Output Settings", + "name": "output", + "title": "Output Settings", "expanded": true, "type": "group", "children": [ { - "name": "Control Method", + "name": "control_method", + "title": "Control Method", "type": "mutex", - "limits": [ - "Constant Current", - "Temperature PID" - ], + "limits": { + "Constant Current": "constant_current", + "Temperature PID": "temperature_pid" + }, "thermostat:set_param": { "topic": "pwm", "field": "pid" }, "children": [ { - "name": "Set Current", + "name": "i_set", + "title": "Set Current", "type": "float", "value": 0, "step": 100, @@ -49,7 +54,8 @@ "lock": false }, { - "name": "Set Temperature", + "name": "target", + "title": "Set Temperature", "type": "float", "value": 25, "step": 0.1, @@ -67,12 +73,14 @@ ] }, { - "name": "Limits", + "name": "limits", + "title": "Limits", "expanded": true, "type": "group", "children": [ { - "name": "Max Cooling Current", + "name": "max_i_pos", + "title": "Max Cooling Current", "type": "float", "value": 0, "step": 100, @@ -89,7 +97,8 @@ "lock": false }, { - "name": "Max Heating Current", + "name": "max_i_neg", + "title": "Max Heating Current", "type": "float", "value": 0, "step": 100, @@ -106,7 +115,8 @@ "lock": false }, { - "name": "Max Voltage Difference", + "name": "max_v", + "title": "Max Voltage Difference", "type": "float", "value": 0, "step": 0.1, @@ -127,13 +137,15 @@ ] }, { - "name": "Thermistor Settings", + "name": "thermistor", + "title": "Thermistor Settings", "expanded": true, "type": "group", "tip": "Settings of the connected Thermistor", "children": [ { - "name": "T₀", + "name": "t0", + "title": "T₀", "type": "float", "value": 25, "step": 0.1, @@ -149,7 +161,8 @@ "lock": false }, { - "name": "R₀", + "name": "r0", + "title": "R₀", "type": "float", "value": 10000, "step": 1, @@ -162,7 +175,8 @@ "lock": false }, { - "name": "B", + "name": "b", + "title": "B", "type": "float", "value": 3950, "step": 1, @@ -175,7 +189,8 @@ "lock": false }, { - "name": "Postfilter Rate", + "name": "rate", + "title": "Postfilter Rate", "type": "list", "value": 16.67, "thermostat:set_param": { @@ -194,12 +209,14 @@ ] }, { - "name": "PID Settings", + "name": "pid", + "title": "PID Settings", "expanded": true, "type": "group", "children": [ { - "name": "Kp", + "name": "kp", + "title": "Kp", "type": "float", "step": 0.1, "suffix": "", @@ -210,7 +227,8 @@ "lock": false }, { - "name": "Ki", + "name": "ki", + "title": "Ki", "type": "float", "step": 0.1, "suffix": "Hz", @@ -221,7 +239,8 @@ "lock": false }, { - "name": "Kd", + "name": "kd", + "title": "Kd", "type": "float", "step": 0.1, "suffix": "s", @@ -232,12 +251,14 @@ "lock": false }, { - "name": "PID Output Clamping", + "name": "pid_output_clamping", + "title": "PID Output Clamping", "expanded": true, "type": "group", "children": [ { - "name": "Minimum", + "name": "output_min", + "title": "Minimum", "type": "float", "step": 100, "limits": [ @@ -253,7 +274,8 @@ "lock": false }, { - "name": "Maximum", + "name": "output_max", + "title": "Maximum", "type": "float", "step": 100, "limits": [ @@ -271,12 +293,14 @@ ] }, { - "name": "PID Auto Tune", + "name": "pid_autotune", + "title": "PID Auto Tune", "expanded": false, "type": "group", "children": [ { - "name": "Target Temperature", + "name": "target_temp", + "title": "Target Temperature", "type": "float", "value": 20, "step": 0.1, @@ -284,7 +308,8 @@ "pid_autotune": "target_temp" }, { - "name": "Test Current", + "name": "test_current", + "title": "Test Current", "type": "float", "value": 0, "decimals": 6, @@ -297,7 +322,8 @@ "pid_autotune": "test_current" }, { - "name": "Temperature Swing", + "name": "temp_swing", + "title": "Temperature Swing", "type": "float", "value": 1.5, "step": 0.1, @@ -306,7 +332,8 @@ "pid_autotune": "temp_swing" }, { - "name": "Lookback", + "name": "lookback", + "title": "Lookback", "type": "float", "value": 3.0, "step": 0.1, @@ -314,7 +341,8 @@ "pid_autotune": "lookback" }, { - "name": "Run", + "name": "run_pid", + "title": "Run", "type": "action", "tip": "Run" } @@ -323,12 +351,14 @@ ] }, { - "name": "Save to flash", + "name": "save", + "title": "Save to flash", "type": "action", "tip": "Save settings to thermostat, applies on reset" }, { - "name": "Load from flash", + "name": "load", + "title": "Load from flash", "type": "action", "tip": "Load settings from flash" } diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index 7dc9b75..e9bb2fc 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -78,10 +78,10 @@ class MainWindow(QtWidgets.QMainWindow): param_tree_sigActivated_handles = [ [ - [["Save to flash"], partial(self.thermostat.save_cfg, ch)], - [["Load from flash"], partial(self.thermostat.load_cfg, ch)], + [["save"], partial(self.thermostat.save_cfg, ch)], + [["load"], partial(self.thermostat.load_cfg, ch)], [ - ["PID Settings", "PID Auto Tune", "Run"], + ["pid", "pid_autotune", "run_pid"], partial(self.pid_auto_tune_request, ch), ], ] @@ -275,13 +275,13 @@ class MainWindow(QtWidgets.QMainWindow): # Handle thermostat command irregularities match inner_param.name(), new_value: - case "Postfilter Rate", None: + case "rate", None: thermostat_param = thermostat_param.copy() thermostat_param["field"] = "off" new_value = "" - case "Control Method", "Constant Current": + case "control_method", "constant_current": return - case "Control Method", "Temperature PID": + case "control_method", "temperature_pid": new_value = "" inner_param.setOpts(lock=True) @@ -312,11 +312,11 @@ class MainWindow(QtWidgets.QMainWindow): match self.autotuners.get_state(ch): case PIDAutotuneState.STATE_OFF: self.ctrl_panel_view.change_params_title( - ch, ("PID Settings", "PID Auto Tune", "Run"), "Run" + ch, ("pid", "pid_autotune", "run_pid"), "Run" ) case PIDAutotuneState.STATE_READY | PIDAutotuneState.STATE_RELAY_STEP_UP | PIDAutotuneState.STATE_RELAY_STEP_DOWN: self.ctrl_panel_view.change_params_title( - ch, ("PID Settings", "PID Auto Tune", "Run"), "Stop" + ch, ("pid", "pid_autotune", "run_pid"), "Stop" ) ch_tuning.append(ch) From dafde57e231ab2c217a0afdc125b3381af7a1e78 Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 20 Jun 2024 16:13:27 +0800 Subject: [PATCH 04/21] ctrl_panel: Add and improve tooltips and titles For users' better understanding of what the parameters do --- pytec/pytec/gui/view/ctrl_panel.py | 9 ++++++ pytec/pytec/gui/view/param_tree.json | 48 +++++++++++++++++++++------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index a728de1..560a122 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -42,6 +42,13 @@ class MutexParameter(pTypes.ListParameter): registerParameterType("mutex", MutexParameter) +def set_tree_label_tips(tree): + for item in tree.listAllItems(): + p = item.param + if "tip" in p.opts: + item.setToolTip(0, p.opts["tip"]) + + class CtrlPanel(QObject): set_zero_limits_warning_sig = pyqtSignal(list) @@ -79,6 +86,8 @@ class CtrlPanel(QObject): self.params[i].setValue = self._setValue self.params[i].sigTreeStateChanged.connect(sigTreeStateChanged_handle) + set_tree_label_tips(tree) + for handle in sigActivated_handles[i]: self.params[i].child(*handle[0]).sigActivated.connect(handle[1]) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index ddd4931..572caeb 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -5,7 +5,8 @@ "title": "Temperature", "type": "float", "format": "{value:.4f} °C", - "readonly": true + "readonly": true, + "tip": "The measured temperature at the thermistor" }, { "name": "tec_i", @@ -13,13 +14,15 @@ "type": "float", "suffix": "mA", "decimals": 6, - "readonly": true + "readonly": true, + "tip": "The measured current through the TEC" }, { "name": "output", "title": "Output Settings", "expanded": true, "type": "group", + "tip": "Settings of the output to the TEC", "children": [ { "name": "control_method", @@ -33,6 +36,7 @@ "topic": "pwm", "field": "pid" }, + "tip": "Select control method of output", "children": [ { "name": "i_set", @@ -51,11 +55,12 @@ "topic": "pwm", "field": "i_set" }, + "tip": "The set current through TEC", "lock": false }, { "name": "target", - "title": "Set Temperature", + "title": "Setpoint", "type": "float", "value": 25, "step": 0.1, @@ -68,6 +73,7 @@ "topic": "pid", "field": "target" }, + "tip": "The temperature setpoint of the TEC", "lock": false } ] @@ -77,6 +83,7 @@ "title": "Limits", "expanded": true, "type": "group", + "tip": "The limits of output, with the polarity at the front panel as reference", "children": [ { "name": "max_i_pos", @@ -94,6 +101,7 @@ "topic": "pwm", "field": "max_i_pos" }, + "tip": "The maximum cooling (+ve) current through the output pins", "lock": false }, { @@ -112,11 +120,12 @@ "topic": "pwm", "field": "max_i_neg" }, + "tip": "The maximum heating (-ve) current through the output pins", "lock": false }, { "name": "max_v", - "title": "Max Voltage Difference", + "title": "Max Absolute Voltage", "type": "float", "value": 0, "step": 0.1, @@ -130,6 +139,7 @@ "topic": "pwm", "field": "max_v" }, + "tip": "The maximum voltage (in both directions) across the output pins", "lock": false } ] @@ -141,7 +151,7 @@ "title": "Thermistor Settings", "expanded": true, "type": "group", - "tip": "Settings of the connected Thermistor", + "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", "children": [ { "name": "t0", @@ -158,6 +168,7 @@ "topic": "s-h", "field": "t0" }, + "tip": "The base temperature", "lock": false }, { @@ -172,6 +183,7 @@ "topic": "s-h", "field": "r0" }, + "tip": "The resistance of the thermistor at base temperature T₀", "lock": false }, { @@ -186,6 +198,7 @@ "topic": "s-h", "field": "b" }, + "tip": "The Beta Parameter", "lock": false }, { @@ -204,6 +217,7 @@ "21.25 Hz": 21.25, "27 Hz": 27.0 }, + "tip": "Adjust the output data rate of the enhanced 50 Hz & 60 Hz rejection filter\n(Helps avoid mains interference)", "lock": false } ] @@ -213,6 +227,7 @@ "title": "PID Settings", "expanded": true, "type": "group", + "tip": "Settings of PID parameters and clamping", "children": [ { "name": "kp", @@ -224,6 +239,7 @@ "topic": "pid", "field": "kp" }, + "tip": "Proportional gain", "lock": false }, { @@ -236,6 +252,7 @@ "topic": "pid", "field": "ki" }, + "tip": "Integral gain", "lock": false }, { @@ -248,6 +265,7 @@ "topic": "pid", "field": "kd" }, + "tip": "Differential gain", "lock": false }, { @@ -255,6 +273,7 @@ "title": "PID Output Clamping", "expanded": true, "type": "group", + "tip": "Clamps PID outputs to specified range\nCould be different than output limits", "children": [ { "name": "output_min", @@ -271,6 +290,7 @@ "topic": "pid", "field": "output_min" }, + "tip": "Minimum PID output", "lock": false }, { @@ -288,6 +308,7 @@ "topic": "pid", "field": "output_max" }, + "tip": "Maximum PID output", "lock": false } ] @@ -297,6 +318,7 @@ "title": "PID Auto Tune", "expanded": false, "type": "group", + "tip": "Automatically tune PID parameters", "children": [ { "name": "target_temp", @@ -305,7 +327,8 @@ "value": 20, "step": 0.1, "format": "{value:.4f} °C", - "pid_autotune": "target_temp" + "pid_autotune": "target_temp", + "tip": "The target temperature to autotune for" }, { "name": "test_current", @@ -319,7 +342,8 @@ 2000 ], "suffix": "mA", - "pid_autotune": "test_current" + "pid_autotune": "test_current", + "tip": "The testing current when autotuning" }, { "name": "temp_swing", @@ -329,7 +353,8 @@ "step": 0.1, "prefix": "±", "format": "{value:.4f} °C", - "pid_autotune": "temp_swing" + "pid_autotune": "temp_swing", + "tip": "The temperature swing around the target" }, { "name": "lookback", @@ -338,13 +363,14 @@ "value": 3.0, "step": 0.1, "format": "{value:.4f} s", - "pid_autotune": "lookback" + "pid_autotune": "lookback", + "tip": "Amount of time referenced for tuning" }, { "name": "run_pid", "title": "Run", "type": "action", - "tip": "Run" + "tip": "Run PID Autotune with above settings" } ] } @@ -360,7 +386,7 @@ "name": "load", "title": "Load from flash", "type": "action", - "tip": "Load settings from flash" + "tip": "Load settings from thermostat" } ] } \ No newline at end of file From 2b9bc020cec5c5b503551f0e85e43c2f428c6bd2 Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 18 Jul 2024 18:06:40 +0800 Subject: [PATCH 05/21] ctrl_panel: Improve postfilter description --- pytec/pytec/gui/view/param_tree.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 572caeb..274f50e 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -203,7 +203,7 @@ }, { "name": "rate", - "title": "Postfilter Rate", + "title": "50/60 Hz Rejection Filter", "type": "list", "value": 16.67, "thermostat:set_param": { @@ -211,11 +211,11 @@ "field": "rate" }, "limits": { - "Off": null, - "16.67 Hz": 16.67, - "20 Hz": 20.0, - "21.25 Hz": 21.25, - "27 Hz": 27.0 + "16.67 SPS": 16.67, + "20 SPS": 20.0, + "21.25 SPS": 21.25, + "27 SPS": 27.0, + "Off": null }, "tip": "Adjust the output data rate of the enhanced 50 Hz & 60 Hz rejection filter\n(Helps avoid mains interference)", "lock": false From 2df4c03c2d720a9bc79d1abc95a1184e0075a873 Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 28 Jun 2024 11:20:37 +0800 Subject: [PATCH 06/21] 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. --- pytec/pytec/gui/view/ctrl_panel.py | 4 +--- pytec/pytec/gui/view/param_tree.json | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index 560a122..b68c4fd 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -160,9 +160,7 @@ class CtrlPanel(QObject): settings["temperature"] ) if settings["tec_i"] is not None: - self.params[channel].child("tec_i").setValue( - settings["tec_i"] * 1000 - ) + self.params[channel].child("tec_i").setValue(settings["tec_i"]) @pyqtSlot("QVariantList") def update_thermistor(self, sh_data): diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 274f50e..8c69ec3 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -12,7 +12,8 @@ "name": "tec_i", "title": "Current through TEC", "type": "float", - "suffix": "mA", + "siPrefix": true, + "suffix": "A", "decimals": 6, "readonly": true, "tip": "The measured current through the TEC" From 6df37e31aaa78929e29ae93b4618fa96e76e1280 Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 31 Jul 2024 16:02:12 +0800 Subject: [PATCH 07/21] 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". --- pytec/pytec/gui/model/pid_autotuner.py | 2 +- pytec/pytec/gui/view/ctrl_panel.py | 11 +-- pytec/pytec/gui/view/param_tree.json | 56 +++++++++------ pytec/pytec/gui/view/pin_si_prefix.py | 95 ++++++++++++++++++++++++++ pytec/tec_qt.py | 3 - 5 files changed, 136 insertions(+), 31 deletions(-) create mode 100644 pytec/pytec/gui/view/pin_si_prefix.py diff --git a/pytec/pytec/gui/model/pid_autotuner.py b/pytec/pytec/gui/model/pid_autotuner.py index 2a0e08f..435d56b 100644 --- a/pytec/pytec/gui/model/pid_autotuner.py +++ b/pytec/pytec/gui/model/pid_autotuner.py @@ -28,7 +28,7 @@ class PIDAutoTuner(QObject): def load_params_and_set_ready(self, ch): self.autotuners[ch].setParam( self.target_temp[ch], - self.test_current[ch] / 1000, + self.test_current[ch], self.temp_swing[ch], 1 / self.sampling_interval[ch], self.lookback[ch], diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index b68c4fd..c907591 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -4,6 +4,7 @@ from pyqtgraph.parametertree import ( Parameter, registerParameterType, ) +import pytec.gui.view.pin_si_prefix class MutexParameter(pTypes.ListParameter): @@ -136,10 +137,10 @@ class CtrlPanel(QObject): ) self.params[channel].child( "pid", "pid_output_clamping", "output_min" - ).setValue(settings["parameters"]["output_min"] * 1000) + ).setValue(settings["parameters"]["output_min"]) self.params[channel].child( "pid", "pid_output_clamping", "output_max" - ).setValue(settings["parameters"]["output_max"] * 1000) + ).setValue(settings["parameters"]["output_max"]) self.params[channel].child( "output", "control_method", "target" ).setValue(settings["target"]) @@ -154,7 +155,7 @@ class CtrlPanel(QObject): ) self.params[channel].child( "output", "control_method", "i_set" - ).setValue(settings["i_set"] * 1000) + ).setValue(settings["i_set"]) if settings["temperature"] is not None: self.params[channel].child("temperature").setValue( settings["temperature"] @@ -188,10 +189,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"] * 1000 + pwm_params["max_i_pos"]["value"] ) self.params[channel].child("output", "limits", "max_i_neg").setValue( - pwm_params["max_i_neg"]["value"] * 1000 + pwm_params["max_i_neg"]["value"] ) for limit in "max_i_pos", "max_i_neg", "max_v": diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 8c69ec3..ee85edd 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -44,14 +44,16 @@ "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, "thermostat:set_param": { "topic": "pwm", "field": "i_set" @@ -91,13 +93,15 @@ "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", "thermostat:set_param": { "topic": "pwm", "field": "max_i_pos" @@ -110,13 +114,15 @@ "title": "Max Heating Current", "type": "float", "value": 0, - "step": 100, + "step": 0.1, "decimals": 6, + "siPrefix": true, + "pinSiPrefix": "m", + "suffix": "A", "limits": [ 0, - 2000 + 2 ], - "suffix": "mA", "thermostat:set_param": { "topic": "pwm", "field": "max_i_neg" @@ -280,13 +286,15 @@ "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", "thermostat:set_param": { "topic": "pid", "field": "output_min" @@ -298,13 +306,15 @@ "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", "thermostat:set_param": { "topic": "pid", "field": "output_max" @@ -337,12 +347,14 @@ "type": "float", "value": 0, "decimals": 6, - "step": 100, + "step": 0.1, "limits": [ - -2000, - 2000 + -2, + 2 ], - "suffix": "mA", + "siPrefix": true, + "pinSiPrefix": "m", + "suffix": "A", "pid_autotune": "test_current", "tip": "The testing current when autotuning" }, diff --git a/pytec/pytec/gui/view/pin_si_prefix.py b/pytec/pytec/gui/view/pin_si_prefix.py new file mode 100644 index 0000000..1b0ad3d --- /dev/null +++ b/pytec/pytec/gui/view/pin_si_prefix.py @@ -0,0 +1,95 @@ +from pyqtgraph import SpinBox +import pyqtgraph.functions as fn +from pyqtgraph.parametertree import registerParameterItemType +from pyqtgraph.parametertree.parameterTypes import SimpleParameter, NumericParameterItem + + +class PinSIPrefixSpinBox(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. + """ + + def setOpts(self, **opts): + if "pinSiPrefix" in opts: + self.opts["pinSiPrefix"] = opts.pop("pinSiPrefix") + + super().setOpts(**opts) + + 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 PinSIPrefixNumericParameterItem(NumericParameterItem): + """ + Subclasses PyQtGraph's `NumericParameterItem` and uses + PinSIPrefixSpinBox 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, + } + 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 = PinSIPrefixSpinBox() + w.setOpts(**defs) + w.sigChanged = w.sigValueChanged + w.sigChanging = w.sigValueChanging + return w + + +registerParameterItemType( + "float", PinSIPrefixNumericParameterItem, SimpleParameter, override=True +) +registerParameterItemType( + "int", PinSIPrefixNumericParameterItem, SimpleParameter, override=True +) diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index e9bb2fc..f96e06a 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -268,9 +268,6 @@ class MainWindow(QtWidgets.QMainWindow): if change == "value": new_value = data if "thermostat:set_param" in inner_param.opts: - if inner_param.opts.get("suffix", None) == "mA": - new_value /= 1000 # Given in mA - thermostat_param = inner_param.opts["thermostat:set_param"] # Handle thermostat command irregularities From 4bda62ab41ed8f0afd79d62e27446ca31950bf72 Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 26 Jul 2024 17:07:51 +0800 Subject: [PATCH 08/21] 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. --- pytec/pytec/gui/view/ctrl_panel.py | 2 +- .../{pin_si_prefix.py => lockable_unit.py} | 77 +++++++++++++++++-- pytec/pytec/gui/view/param_tree.json | 35 +++++++-- 3 files changed, 100 insertions(+), 14 deletions(-) rename pytec/pytec/gui/view/{pin_si_prefix.py => lockable_unit.py} (52%) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index c907591..c98af87 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -4,7 +4,7 @@ from pyqtgraph.parametertree import ( Parameter, registerParameterType, ) -import pytec.gui.view.pin_si_prefix +import pytec.gui.view.lockable_unit class MutexParameter(pTypes.ListParameter): diff --git a/pytec/pytec/gui/view/pin_si_prefix.py b/pytec/pytec/gui/view/lockable_unit.py similarity index 52% rename from pytec/pytec/gui/view/pin_si_prefix.py rename to pytec/pytec/gui/view/lockable_unit.py index 1b0ad3d..78e519f 100644 --- a/pytec/pytec/gui/view/pin_si_prefix.py +++ b/pytec/pytec/gui/view/lockable_unit.py @@ -1,21 +1,84 @@ +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 -class PinSIPrefixSpinBox(SpinBox): +class LockableUnitSpinBox(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 suffix and pinned siPrefix + of the SpinBox text 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) @@ -57,10 +120,10 @@ class PinSIPrefixSpinBox(SpinBox): return self.opts['format'].format(**parts) -class PinSIPrefixNumericParameterItem(NumericParameterItem): +class UnitfulNumericParameterItem(NumericParameterItem): """ Subclasses PyQtGraph's `NumericParameterItem` and uses - PinSIPrefixSpinBox for editing. + UnitfulSpinBox for editing. """ def makeWidget(self): @@ -70,7 +133,7 @@ class PinSIPrefixNumericParameterItem(NumericParameterItem): 'value': 0, 'min': None, 'max': None, 'step': 1.0, 'dec': False, 'siPrefix': False, 'suffix': '', 'decimals': 3, - 'pinSiPrefix': None, + 'pinSiPrefix': None, 'noUnitEditing': False, } if t == 'int': defs['int'] = True @@ -80,7 +143,7 @@ class PinSIPrefixNumericParameterItem(NumericParameterItem): defs[k] = opts[k] if 'limits' in opts: defs['min'], defs['max'] = opts['limits'] - w = PinSIPrefixSpinBox() + w = LockableUnitSpinBox() w.setOpts(**defs) w.sigChanged = w.sigValueChanged w.sigChanging = w.sigValueChanging @@ -88,8 +151,8 @@ class PinSIPrefixNumericParameterItem(NumericParameterItem): registerParameterItemType( - "float", PinSIPrefixNumericParameterItem, SimpleParameter, override=True + "float", UnitfulNumericParameterItem, SimpleParameter, override=True ) registerParameterItemType( - "int", PinSIPrefixNumericParameterItem, SimpleParameter, override=True + "int", UnitfulNumericParameterItem, SimpleParameter, override=True ) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index ee85edd..c9c91e5 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -4,7 +4,8 @@ "name": "temperature", "title": "Temperature", "type": "float", - "format": "{value:.4f} °C", + "format": "{value:.4f} {suffix}", + "suffix": "°C", "readonly": true, "tip": "The measured temperature at the thermistor" }, @@ -54,6 +55,7 @@ "pinSiPrefix": "m", "suffix": "A", "siPrefix": true, + "noUnitEditing": true, "thermostat:set_param": { "topic": "pwm", "field": "i_set" @@ -71,7 +73,9 @@ -273, 300 ], - "format": "{value:.4f} °C", + "format": "{value:.4f} {suffix}", + "suffix": "°C", + "noUnitEditing": true, "thermostat:set_param": { "topic": "pid", "field": "target" @@ -102,6 +106,7 @@ "siPrefix": true, "pinSiPrefix": "m", "suffix": "A", + "noUnitEditing": true, "thermostat:set_param": { "topic": "pwm", "field": "max_i_pos" @@ -119,6 +124,7 @@ "siPrefix": true, "pinSiPrefix": "m", "suffix": "A", + "noUnitEditing": true, "limits": [ 0, 2 @@ -142,6 +148,7 @@ ], "siPrefix": true, "suffix": "V", + "noUnitEditing": true, "thermostat:set_param": { "topic": "pwm", "field": "max_v" @@ -170,7 +177,9 @@ -100, 100 ], - "format": "{value:.4f} °C", + "format": "{value:.4f} {suffix}", + "suffix": "°C", + "noUnitEditing": true, "thermostat:set_param": { "topic": "s-h", "field": "t0" @@ -185,7 +194,9 @@ "value": 10000, "step": 1, "siPrefix": true, + "pinSiPrefix": "k", "suffix": "Ω", + "noUnitEditing": true, "thermostat:set_param": { "topic": "s-h", "field": "r0" @@ -200,6 +211,7 @@ "value": 3950, "step": 1, "suffix": "K", + "noUnitEditing": true, "decimals": 4, "thermostat:set_param": { "topic": "s-h", @@ -255,6 +267,7 @@ "type": "float", "step": 0.1, "suffix": "Hz", + "noUnitEditing": true, "thermostat:set_param": { "topic": "pid", "field": "ki" @@ -268,6 +281,7 @@ "type": "float", "step": 0.1, "suffix": "s", + "noUnitEditing": true, "thermostat:set_param": { "topic": "pid", "field": "kd" @@ -295,6 +309,7 @@ "siPrefix": true, "pinSiPrefix": "m", "suffix": "A", + "noUnitEditing": true, "thermostat:set_param": { "topic": "pid", "field": "output_min" @@ -315,6 +330,7 @@ "siPrefix": true, "pinSiPrefix": "m", "suffix": "A", + "noUnitEditing": true, "thermostat:set_param": { "topic": "pid", "field": "output_max" @@ -337,7 +353,9 @@ "type": "float", "value": 20, "step": 0.1, - "format": "{value:.4f} °C", + "format": "{value:.4f} {suffix}", + "suffix": "°C", + "noUnitEditing": true, "pid_autotune": "target_temp", "tip": "The target temperature to autotune for" }, @@ -355,6 +373,7 @@ "siPrefix": true, "pinSiPrefix": "m", "suffix": "A", + "noUnitEditing": true, "pid_autotune": "test_current", "tip": "The testing current when autotuning" }, @@ -365,7 +384,9 @@ "value": 1.5, "step": 0.1, "prefix": "±", - "format": "{value:.4f} °C", + "format": "{value:.4f} {suffix}", + "suffix": "°C", + "noUnitEditing": true, "pid_autotune": "temp_swing", "tip": "The temperature swing around the target" }, @@ -375,7 +396,9 @@ "type": "float", "value": 3.0, "step": 0.1, - "format": "{value:.4f} s", + "format": "{value:.4f} {suffix}", + "noUnitEditing": true, + "suffix": "s", "pid_autotune": "lookback", "tip": "Amount of time referenced for tuning" }, From ac10859f708523c4338b9b8d5c6cf3d93fe2e873 Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 28 Jun 2024 11:21:21 +0800 Subject: [PATCH 09/21] 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. --- pytec/pytec/gui/view/param_tree.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index c9c91e5..2de4695 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -56,6 +56,7 @@ "suffix": "A", "siPrefix": true, "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "pwm", "field": "i_set" @@ -76,6 +77,7 @@ "format": "{value:.4f} {suffix}", "suffix": "°C", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "pid", "field": "target" @@ -107,6 +109,7 @@ "pinSiPrefix": "m", "suffix": "A", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "pwm", "field": "max_i_pos" @@ -129,6 +132,7 @@ 0, 2 ], + "compactHeight": false, "thermostat:set_param": { "topic": "pwm", "field": "max_i_neg" @@ -149,6 +153,7 @@ "siPrefix": true, "suffix": "V", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "pwm", "field": "max_v" @@ -180,6 +185,7 @@ "format": "{value:.4f} {suffix}", "suffix": "°C", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "s-h", "field": "t0" @@ -197,6 +203,7 @@ "pinSiPrefix": "k", "suffix": "Ω", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "s-h", "field": "r0" @@ -213,6 +220,7 @@ "suffix": "K", "noUnitEditing": true, "decimals": 4, + "compactHeight": false, "thermostat:set_param": { "topic": "s-h", "field": "b" @@ -254,6 +262,7 @@ "type": "float", "step": 0.1, "suffix": "", + "compactHeight": false, "thermostat:set_param": { "topic": "pid", "field": "kp" @@ -268,6 +277,7 @@ "step": 0.1, "suffix": "Hz", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "pid", "field": "ki" @@ -282,6 +292,7 @@ "step": 0.1, "suffix": "s", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "pid", "field": "kd" @@ -310,6 +321,7 @@ "pinSiPrefix": "m", "suffix": "A", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "pid", "field": "output_min" @@ -331,6 +343,7 @@ "pinSiPrefix": "m", "suffix": "A", "noUnitEditing": true, + "compactHeight": false, "thermostat:set_param": { "topic": "pid", "field": "output_max" @@ -356,6 +369,7 @@ "format": "{value:.4f} {suffix}", "suffix": "°C", "noUnitEditing": true, + "compactHeight": false, "pid_autotune": "target_temp", "tip": "The target temperature to autotune for" }, @@ -374,6 +388,7 @@ "pinSiPrefix": "m", "suffix": "A", "noUnitEditing": true, + "compactHeight": false, "pid_autotune": "test_current", "tip": "The testing current when autotuning" }, @@ -387,6 +402,7 @@ "format": "{value:.4f} {suffix}", "suffix": "°C", "noUnitEditing": true, + "compactHeight": false, "pid_autotune": "temp_swing", "tip": "The temperature swing around the target" }, @@ -399,6 +415,7 @@ "format": "{value:.4f} {suffix}", "noUnitEditing": true, "suffix": "s", + "compactHeight": false, "pid_autotune": "lookback", "tip": "Amount of time referenced for tuning" }, From 85c8c23f2c96f31c5f22e73094959135adb703d6 Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 28 Jun 2024 11:44:37 +0800 Subject: [PATCH 10/21] ctrl_panel: PID Auto Tune -> PID Autotune --- pytec/pytec/gui/view/param_tree.json | 2 +- pytec/tec_qt.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 2de4695..fc6134b 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -355,7 +355,7 @@ }, { "name": "pid_autotune", - "title": "PID Auto Tune", + "title": "PID Autotune", "expanded": false, "type": "group", "tip": "Automatically tune PID parameters", diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index f96e06a..bf8f996 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -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) @@ -292,7 +292,7 @@ class MainWindow(QtWidgets.QMainWindow): self.autotuners.set_params(auto_tuner_param, ch, new_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) From 873f16a675fda42bf1e95ccfc02a94b3cfe2d17d Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 18 Jul 2024 15:50:02 +0800 Subject: [PATCH 11/21] 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 --- pytec/pytec/gui/view/ctrl_panel.py | 21 +++++++++------------ pytec/tec_qt.py | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index c98af87..c42330a 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -66,31 +66,28 @@ 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)] - 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): + 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]) def _setValue(self, value, blockSignal=None): """ diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index bf8f996..d197ac5 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -262,7 +262,7 @@ 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": From fde1e6b3e440f5609a869e94fbe8565898d98ba2 Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 17 Jul 2024 17:29:34 +0800 Subject: [PATCH 12/21] 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 --- pytec/pytec/gui/view/param_tree.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index fc6134b..3a85d83 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -148,7 +148,7 @@ "step": 0.1, "limits": [ 0, - 5 + 4 ], "siPrefix": true, "suffix": "V", @@ -199,6 +199,7 @@ "type": "float", "value": 10000, "step": 1, + "min": 0, "siPrefix": true, "pinSiPrefix": "k", "suffix": "Ω", @@ -381,7 +382,7 @@ "decimals": 6, "step": 0.1, "limits": [ - -2, + 0, 2 ], "siPrefix": true, From 9926c004949e298cf69eab82b293e289355145a3 Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 18 Jul 2024 13:38:46 +0800 Subject: [PATCH 13/21] 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. --- pytec/pytec/gui/view/ctrl_panel.py | 55 ++++++++++------------------ pytec/pytec/gui/view/param_tree.json | 3 +- pytec/tec_qt.py | 4 +- 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index c42330a..211bfef 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -7,42 +7,6 @@ from pyqtgraph.parametertree import ( import pytec.gui.view.lockable_unit -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): for item in tree.listAllItems(): p = item.param @@ -89,6 +53,25 @@ class CtrlPanel(QObject): for handle in sigActivated_handles[ch]: param.child(*handle[0]).sigActivated.connect(handle[1]) + def _indicate_usage(param, control_method="constant_current"): + 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) + + param.child("output", "control_method").sigValueChanged.connect( + _indicate_usage + ) + _indicate_usage(param.child("output", "control_method")) + def _setValue(self, value, blockSignal=None): """ Implement 'lock' mechanism for Parameter Type diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 3a85d83..8e4ab28 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -29,7 +29,7 @@ { "name": "control_method", "title": "Control Method", - "type": "mutex", + "type": "list", "limits": { "Constant Current": "constant_current", "Temperature PID": "temperature_pid" @@ -50,7 +50,6 @@ -2, 2 ], - "triggerOnShow": true, "decimals": 6, "pinSiPrefix": "m", "suffix": "A", diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index d197ac5..35e7828 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -277,7 +277,9 @@ class MainWindow(QtWidgets.QMainWindow): thermostat_param["field"] = "off" new_value = "" case "control_method", "constant_current": - return + thermostat_param = thermostat_param.copy() + thermostat_param["field"] = "i_set" + new_value = inner_param.child("i_set").value() case "control_method", "temperature_pid": new_value = "" From 2f9b95f04beb031e9e4a6be3ab579d6eff8fc6c0 Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 7 Aug 2024 18:30:59 +0800 Subject: [PATCH 14/21] ctrl_panel: Bold "Control Method" text --- pytec/pytec/gui/view/ctrl_panel.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index 211bfef..858d8de 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -72,6 +72,11 @@ class CtrlPanel(QObject): ) _indicate_usage(param.child("output", "control_method")) + for item in param.child("output", "control_method").items: + font = item.font(0) + font.setBold(True) + item.setFont(0, font) + def _setValue(self, value, blockSignal=None): """ Implement 'lock' mechanism for Parameter Type From 3ae89760abeb8a684f14d9585b733017c600b1db Mon Sep 17 00:00:00 2001 From: atse Date: Mon, 22 Jul 2024 15:38:27 +0800 Subject: [PATCH 15/21] 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. --- pytec/pytec/gui/view/param_tree.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 8e4ab28..42e4e5a 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -145,11 +145,11 @@ "type": "float", "value": 0, "step": 0.1, + "decimals": 3, "limits": [ 0, 4 ], - "siPrefix": true, "suffix": "V", "noUnitEditing": true, "compactHeight": false, From eddfc40e0743a50daedfccc1b7090d9735408f8f Mon Sep 17 00:00:00 2001 From: atse Date: Mon, 22 Jul 2024 16:00:17 +0800 Subject: [PATCH 16/21] ctrl_panel: Put plotted values into readings group For more intuitiveness to first-time users --- pytec/pytec/gui/view/ctrl_panel.py | 6 ++-- pytec/pytec/gui/view/param_tree.json | 42 +++++++++++++++++----------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index 858d8de..d29f7d2 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -142,11 +142,13 @@ class CtrlPanel(QObject): "output", "control_method", "i_set" ).setValue(settings["i_set"]) if settings["temperature"] is not None: - self.params[channel].child("temperature").setValue( + self.params[channel].child("readings", "temperature").setValue( settings["temperature"] ) if settings["tec_i"] is not None: - self.params[channel].child("tec_i").setValue(settings["tec_i"]) + self.params[channel].child("readings", "tec_i").setValue( + settings["tec_i"] + ) @pyqtSlot("QVariantList") def update_thermistor(self, sh_data): diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 42e4e5a..7a5ffdd 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -1,23 +1,31 @@ { "ctrl_panel": [ { - "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": "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", From 3fe343435dbb2b7ad01658e74a89f7f38d2cf54d Mon Sep 17 00:00:00 2001 From: atse Date: Tue, 30 Jul 2024 10:45:12 +0800 Subject: [PATCH 17/21] ctrl_panel: More appropriate steps and fixes --- pytec/pytec/gui/view/param_tree.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index 7a5ffdd..c3deabc 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -205,7 +205,7 @@ "title": "R₀", "type": "float", "value": 10000, - "step": 1, + "step": 100, "min": 0, "siPrefix": true, "pinSiPrefix": "k", @@ -224,7 +224,7 @@ "title": "B", "type": "float", "value": 3950, - "step": 1, + "step": 10, "suffix": "K", "noUnitEditing": true, "decimals": 4, @@ -269,7 +269,6 @@ "title": "Kp", "type": "float", "step": 0.1, - "suffix": "", "compactHeight": false, "thermostat:set_param": { "topic": "pid", @@ -406,9 +405,8 @@ "type": "float", "value": 1.5, "step": 0.1, - "prefix": "±", "format": "{value:.4f} {suffix}", - "suffix": "°C", + "suffix": "K", "noUnitEditing": true, "compactHeight": false, "pid_autotune": "temp_swing", From aed0c484dd49f30b25d2d47a51d868e83ab7e1fc Mon Sep 17 00:00:00 2001 From: atse Date: Tue, 30 Jul 2024 13:31:42 +0800 Subject: [PATCH 18/21] =?UTF-8?q?ctrl=5Fpanel:=20Fix=20editing=20fields=20?= =?UTF-8?q?with=20unit=20"=C2=B0C"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- pytec/pytec/gui/view/lockable_unit.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pytec/pytec/gui/view/lockable_unit.py b/pytec/pytec/gui/view/lockable_unit.py index 78e519f..431c38f 100644 --- a/pytec/pytec/gui/view/lockable_unit.py +++ b/pytec/pytec/gui/view/lockable_unit.py @@ -1,3 +1,5 @@ +import re + from PyQt6.QtCore import QSignalBlocker from PyQt6.QtGui import QValidator @@ -7,6 +9,13 @@ 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[+-]?((((\d+(\.\d*)?)|(\d*\.\d+))([eE][+-]?\d+)?)|((?i:nan)|(inf))))\s*" + + r"((?P[u" + fn.SI_PREFIXES + r"]?)(?P.*))?$" +) + + class LockableUnitSpinBox(SpinBox): """ Extension of PyQtGraph's SpinBox widget. From e1bd960900ec8fd34992e8c3cd9618ddbe6e1372 Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 2 Aug 2024 13:01:30 +0800 Subject: [PATCH 19/21] ctrl_panel: Use new locking mechanism from Kirdy --- pytec/pytec/gui/view/ctrl_panel.py | 88 ++++++++++++------------------ 1 file changed, 34 insertions(+), 54 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index d29f7d2..1367f28 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -30,6 +30,11 @@ class CtrlPanel(QObject): self.trees_ui = 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 = [ Parameter.create( name=f"Thermostat Channel {ch} Parameters", @@ -47,7 +52,6 @@ class CtrlPanel(QObject): set_tree_label_tips(tree) for ch, param in enumerate(self.params): - self.params[ch].setValue = self._setValue param.sigTreeStateChanged.connect(sigTreeStateChanged_handle) for handle in sigActivated_handles[ch]: @@ -77,32 +81,6 @@ class CtrlPanel(QObject): font.setBold(True) item.setFont(0, font) - def _setValue(self, value, blockSignal=None): - """ - Implement 'lock' mechanism for Parameter Type - - 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 - - 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): self.params[channel].child(*path).setOpts(title=title) @@ -111,57 +89,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"]) + ).set_value_with_lock(settings["parameters"]["output_min"]) self.params[channel].child( "pid", "pid_output_clamping", "output_max" - ).setValue(settings["parameters"]["output_max"]) + ).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"]) + ).set_value_with_lock(settings["i_set"]) if settings["temperature"] is not None: - self.params[channel].child("readings", "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("readings", "tec_i").setValue( - settings["tec_i"] - ) + 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"] ) @@ -172,15 +152,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"] - ) - self.params[channel].child("output", "limits", "max_i_neg").setValue( - pwm_params["max_i_neg"]["value"] - ) + 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: @@ -192,6 +172,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("thermistor", "rate").set_value_with_lock( postfilter_params["rate"] ) From ed6cee43945b1b370ee90655047ebd4b5d95348c Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 1 Aug 2024 15:49:45 +0800 Subject: [PATCH 20/21] ctrl_panel: Move postfilter into its own group --- pytec/pytec/gui/view/ctrl_panel.py | 2 +- pytec/pytec/gui/view/param_tree.json | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pytec/pytec/gui/view/ctrl_panel.py b/pytec/pytec/gui/view/ctrl_panel.py index 1367f28..c702a7b 100644 --- a/pytec/pytec/gui/view/ctrl_panel.py +++ b/pytec/pytec/gui/view/ctrl_panel.py @@ -172,6 +172,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").set_value_with_lock( + self.params[channel].child("postfilter", "rate").set_value_with_lock( postfilter_params["rate"] ) diff --git a/pytec/pytec/gui/view/param_tree.json b/pytec/pytec/gui/view/param_tree.json index c3deabc..766180d 100644 --- a/pytec/pytec/gui/view/param_tree.json +++ b/pytec/pytec/gui/view/param_tree.json @@ -177,7 +177,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", @@ -235,7 +235,15 @@ }, "tip": "The Beta Parameter", "lock": false - }, + } + ] + }, + { + "name": "postfilter", + "title": "ADC Settings", + "type": "group", + "tip": "Settings of the ADC on the SENS input", + "children": [ { "name": "rate", "title": "50/60 Hz Rejection Filter", From c9aa0eaab80dd817b067e4ad3c23d36aa0f80d4f Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 26 Jul 2024 17:07:14 +0800 Subject: [PATCH 21/21] 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. --- pytec/pytec/gui/view/lockable_unit.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pytec/pytec/gui/view/lockable_unit.py b/pytec/pytec/gui/view/lockable_unit.py index 431c38f..9f7e16b 100644 --- a/pytec/pytec/gui/view/lockable_unit.py +++ b/pytec/pytec/gui/view/lockable_unit.py @@ -91,6 +91,24 @@ class LockableUnitSpinBox(SpinBox): 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