Try make the i_set parameter adjustable pid

"Auto" box
This commit is contained in:
atse 2024-07-12 17:19:51 +08:00
parent fda4cc9bfb
commit e1565e2dfc
3 changed files with 129 additions and 102 deletions

View File

@ -1,45 +1,81 @@
from PyQt6.QtCore import pyqtSignal, QObject, QSignalBlocker, pyqtSlot from PyQt6.QtCore import pyqtSignal, QObject, QSignalBlocker, pyqtSlot
from PyQt6.QtWidgets import QCheckBox
import pyqtgraph.parametertree.parameterTypes as pTypes import pyqtgraph.parametertree.parameterTypes as pTypes
from pyqtgraph.parametertree import ( from pyqtgraph.parametertree import (
Parameter, Parameter,
registerParameterType, registerParameterItemType,
) )
class MutexParameter(pTypes.ListParameter): class SimpleAutoParameter(pTypes.SimpleParameter):
"""
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. sigAutoChanged = pyqtSignal(object, object) ## self, auto
"""
def __init__(self, **opts): def __init__(self, **opts):
super().__init__(**opts) super().__init__(**opts)
if "auto" not in self.opts:
self.opts["auto"] = False
self.sigValueChanged.connect(self.show_chosen_child) self.sigAutoChanged.connect(self._emitAutoChanged)
self.sigValueChanged.emit(self, self.opts["value"])
def _get_param_from_value(self, value): def setAuto(self, auto, blockSignal=None):
if isinstance(self.opts["limits"], dict): try:
values_list = list(self.opts["limits"].values()) if blockSignal is not None:
else: self.sigAutochanged.disconnect(blockSignal)
values_list = self.opts["limits"]
return self.children()[values_list.index(value)] if self.opts["auto"] == auto:
return auto
self.opts["auto"] = auto
self.sigAutoChanged.emit(self, auto)
finally:
if blockSignal is not None:
self.sigAutoChanged.connect(blockSignal)
@pyqtSlot(object, object) return auto
def show_chosen_child(self, value):
for param in self.children():
param.hide()
child_to_show = self._get_param_from_value(value.value()) def _emitAutoChanged(self, param, data):
child_to_show.show() self.emitStateChanged("auto", data)
if child_to_show.opts.get("triggerOnShow", None):
child_to_show.sigValueChanged.emit(child_to_show, child_to_show.value())
registerParameterType("mutex", MutexParameter) class NumericWithAutoParameterItem(pTypes.NumericParameterItem):
"""
Subclasses `NumericParameterItem` to add an auto checkbox option
========================== =============================================================
**Registered Types:**
int_auto Displays a :class:`SpinBox <pyqtgraph.SpinBox>` in integer
mode, with a :class:`QCheckBox <QtWidgets.SpinBox>` QCheckBox beside it.
float_auto Displays a :class:`SpinBox <pyqtgraph.SpinBox>`, with a
:class:`QCheckBox <QtWidgets.SpinBox>` QCheckBox beside it.
========================== =============================================================
"""
def __init__(self, param, depth):
# Defer normal numeric handling to base class
t = param.opts["type"]
param.opts["type"] = t.removesuffix("_auto")
super().__init__(param, depth)
# Set up the auto checkbox and insert it to the right of the SpinBox
auto_lbl_txt = param.opts.get("auto_label", "Auto")
auto_tip = param.opts.get("auto_tip")
self.auto_checkbox = QCheckBox(auto_lbl_txt)
self.auto_checkbox.setChecked(param.opts.get("auto"))
self.auto_checkbox.setToolTip(auto_tip)
self.layoutWidget.layout().insertWidget(2, self.auto_checkbox)
# Connect it up
self.auto_checkbox.toggled.connect(self.param.setAuto)
param.sigAutoChanged.connect(self.auto_changed)
def auto_changed(self, param, auto):
self.auto_checkbox.setChecked(auto)
registerParameterItemType("int_auto", NumericWithAutoParameterItem, SimpleAutoParameter)
registerParameterItemType(
"float_auto", NumericWithAutoParameterItem, SimpleAutoParameter
)
def set_tree_label_tips(tree): def set_tree_label_tips(tree):
@ -125,6 +161,7 @@ class CtrlPanel(QObject):
for settings in pid_settings: for settings in pid_settings:
channel = settings["channel"] channel = settings["channel"]
with QSignalBlocker(self.params[channel]): with QSignalBlocker(self.params[channel]):
self.params[channel].child("pid", "target").setValue(settings["target"])
self.params[channel].child("pid", "kp").setValue( self.params[channel].child("pid", "kp").setValue(
settings["parameters"]["kp"] settings["parameters"]["kp"]
) )
@ -140,29 +177,25 @@ class CtrlPanel(QObject):
self.params[channel].child( self.params[channel].child(
"pid", "pid_output_clamping", "output_max" "pid", "pid_output_clamping", "output_max"
).setValue(settings["parameters"]["output_max"] * 1000) ).setValue(settings["parameters"]["output_max"] * 1000)
self.params[channel].child(
"output", "control_method", "target"
).setValue(settings["target"])
@pyqtSlot("QVariantList") @pyqtSlot("QVariantList")
def update_report(self, report_data): def update_report(self, report_data):
for settings in report_data: for settings in report_data:
channel = settings["channel"] channel = settings["channel"]
with QSignalBlocker(self.params[channel]): with QSignalBlocker(self.params[channel]):
self.params[channel].child("output", "control_method").setValue( self.params[channel].child("output", "i_set").setValue(
"temperature_pid" if settings["pid_engaged"] else "constant_current" settings["i_set"] * 1000
) )
self.params[channel].child( self.params[channel].child("output", "i_set").setAuto(
"output", "control_method", "i_set" settings["pid_engaged"]
).setValue(settings["i_set"] * 1000) )
if settings["temperature"] is not None: if settings["temperature"] is not None:
self.params[channel].child("temperature").setValue( self.params[channel].child("temperature").setValue(
settings["temperature"] settings["temperature"]
) )
if settings["tec_i"] is not None: if settings["tec_i"] is not None:
self.params[channel].child("tec_i").setValue( self.params[channel].child("tec_i").setValue(settings["tec_i"])
settings["tec_i"]
)
@pyqtSlot("QVariantList") @pyqtSlot("QVariantList")
def update_thermistor(self, sh_data): def update_thermistor(self, sh_data):

View File

@ -25,66 +25,33 @@
"type":"group", "type":"group",
"tip": "Settings of the output through to the load", "tip": "Settings of the output through to the load",
"children":[ "children":[
{ {
"name":"control_method", "name":"i_set",
"title":"Control Method", "title":"Set Current (mA)",
"type":"mutex", "type":"float_auto",
"limits":{ "value":0,
"Constant Current": "constant_current", "step":100,
"Temperature PID": "temperature_pid" "limits":[
}, -2000,
"activaters":[ 2000
null,
[
"pwm",
"$ch",
"pid"
]
], ],
"tip":"Select control method of output", "decimals":6,
"children":[ "compactHeight":false,
{ "param":[
"name":"i_set", "pwm",
"title":"Set Current (mA)", "$ch",
"type":"float", "i_set"
"value":0, ],
"step":100, "auto":false,
"limits":[ "auto_label":"PID",
-2000, "auto_tip":"Control by PID",
2000 "auto_param":[
], "pwm",
"triggerOnShow":true, "$ch",
"decimals":6, "pid"
"compactHeight":false, ],
"param":[ "tip": "",
"pwm", "lock":false
"$ch",
"i_set"
],
"tip": "Set current through output",
"lock":false
},
{
"name": "target",
"title":"Set Temperature (°C)",
"type":"float",
"value":25,
"step":0.1,
"limits":[
-273,
300
],
"format":"{value:.4f}",
"compactHeight":false,
"param":[
"pid",
"$ch",
"target"
],
"tip": "Set target temperature",
"lock":false
}
]
}, },
{ {
"name":"limits", "name":"limits",
@ -244,6 +211,26 @@
"type":"group", "type":"group",
"tip": "", "tip": "",
"children":[ "children":[
{
"name":"target",
"title":"Setpoint (°C)",
"type":"float",
"value":25,
"step":0.1,
"limits":[
-273,
300
],
"format":"{value:.4f}",
"compactHeight":false,
"param":[
"pid",
"ch",
"target"
],
"tip": "",
"lock":false
},
{ {
"name":"kp", "name":"kp",
"title":"Kp", "title":"Kp",

View File

@ -296,14 +296,21 @@ class MainWindow(QtWidgets.QMainWindow):
ch = inner_param.opts["pid_autotune"][1] ch = inner_param.opts["pid_autotune"][1]
self.autotuners.set_params(auto_tuner_param, ch, data) self.autotuners.set_params(auto_tuner_param, ch, data)
if inner_param.opts.get("activaters", None) is not None: if change == "auto":
activater = inner_param.opts["activaters"][ is_auto = data
inner_param.reverse[0].index(data) # ListParameter.reverse = list of codename values if is_auto:
] if "auto_param" in inner_param.opts:
if activater is not None: thermostat_param = inner_param.opts["auto_param"]
if activater[1] == "$ch": else:
activater[1] = ch thermostat_param = [*inner_param.opts["param"], inner_param.value() / 1000]
await self.client.set_param(*activater)
if thermostat_param[1] == "$ch":
thermostat_param[1] = ch
inner_param.setOpts(lock=True)
await self.client.set_param(*thermostat_param)
inner_param.setOpts(lock=False)
@asyncSlot() @asyncSlot()
async def pid_auto_tune_request(self, ch=0): async def pid_auto_tune_request(self, ch=0):