2020-09-29 08:52:46 +08:00
|
|
|
import socket
|
|
|
|
import json
|
2023-01-10 16:47:02 +08:00
|
|
|
import logging
|
2024-09-16 11:52:15 +08:00
|
|
|
import time
|
2020-09-29 08:52:46 +08:00
|
|
|
|
2020-12-17 05:14:21 +08:00
|
|
|
class CommandError(Exception):
|
|
|
|
pass
|
2020-10-01 05:13:11 +08:00
|
|
|
|
2020-09-29 08:52:46 +08:00
|
|
|
class Client:
|
|
|
|
def __init__(self, host="192.168.1.26", port=23, timeout=None):
|
|
|
|
self._socket = socket.create_connection((host, port), timeout)
|
|
|
|
self._lines = [""]
|
2023-01-10 16:47:02 +08:00
|
|
|
self._check_zero_limits()
|
|
|
|
|
2023-05-19 11:23:39 +08:00
|
|
|
def disconnect(self):
|
|
|
|
self._socket.shutdown(socket.SHUT_RDWR)
|
|
|
|
self._socket.close()
|
|
|
|
|
2023-01-10 16:47:02 +08:00
|
|
|
def _check_zero_limits(self):
|
2024-08-15 17:38:03 +08:00
|
|
|
output_report = self.get_output()
|
|
|
|
for output_channel in output_report:
|
2023-01-10 16:47:02 +08:00
|
|
|
for limit in ["max_i_neg", "max_i_pos", "max_v"]:
|
2024-08-15 17:38:03 +08:00
|
|
|
if output_channel[limit] == 0.0:
|
|
|
|
logging.warning("`{}` limit is set to zero on channel {}".format(limit, output_channel["channel"]))
|
2020-09-29 08:52:46 +08:00
|
|
|
|
|
|
|
def _read_line(self):
|
|
|
|
# read more lines
|
|
|
|
while len(self._lines) <= 1:
|
|
|
|
chunk = self._socket.recv(4096)
|
|
|
|
if not chunk:
|
|
|
|
return None
|
|
|
|
buf = self._lines[-1] + chunk.decode('utf-8', errors='ignore')
|
|
|
|
self._lines = buf.split("\n")
|
|
|
|
|
|
|
|
line = self._lines[0]
|
|
|
|
self._lines = self._lines[1:]
|
|
|
|
return line
|
2020-10-01 05:13:11 +08:00
|
|
|
|
2020-12-17 05:14:21 +08:00
|
|
|
def _command(self, *command):
|
|
|
|
self._socket.sendall((" ".join(command) + "\n").encode('utf-8'))
|
|
|
|
|
|
|
|
line = self._read_line()
|
|
|
|
response = json.loads(line)
|
2023-06-26 10:20:48 +08:00
|
|
|
logging.debug(f"{command}: {response}")
|
2020-12-17 05:14:21 +08:00
|
|
|
if "error" in response:
|
|
|
|
raise CommandError(response["error"])
|
|
|
|
return response
|
|
|
|
|
2020-10-01 05:13:11 +08:00
|
|
|
def _get_conf(self, topic):
|
2020-12-17 05:14:21 +08:00
|
|
|
result = [None, None]
|
|
|
|
for item in self._command(topic):
|
|
|
|
result[int(item["channel"])] = item
|
2020-10-01 05:13:11 +08:00
|
|
|
return result
|
|
|
|
|
2024-08-15 17:38:03 +08:00
|
|
|
def get_output(self):
|
|
|
|
"""Retrieve output limits for the TEC
|
2020-10-01 06:21:43 +08:00
|
|
|
|
|
|
|
Example::
|
|
|
|
[{'channel': 0,
|
|
|
|
'center': 'vref',
|
2024-08-16 12:38:51 +08:00
|
|
|
'i_set': -0.02002179650216762,
|
|
|
|
'max_i_neg': 2.0,
|
|
|
|
'max_v': 3.988,
|
|
|
|
'max_i_pos': 2.0,
|
2024-09-30 10:45:42 +08:00
|
|
|
'polarity': 'normal',
|
2020-10-01 06:21:43 +08:00
|
|
|
{'channel': 1,
|
|
|
|
'center': 'vref',
|
2024-08-16 12:38:51 +08:00
|
|
|
'i_set': -0.02002179650216762,
|
|
|
|
'max_i_neg': 2.0,
|
|
|
|
'max_v': 3.988,
|
|
|
|
'max_i_pos': 2.0}
|
2024-09-30 10:45:42 +08:00
|
|
|
'polarity': 'normal',
|
2020-10-01 06:21:43 +08:00
|
|
|
]
|
|
|
|
"""
|
2024-08-15 17:38:03 +08:00
|
|
|
return self._get_conf("output")
|
2020-10-01 05:13:11 +08:00
|
|
|
|
|
|
|
def get_pid(self):
|
2020-10-01 06:21:43 +08:00
|
|
|
"""Retrieve PID control state
|
|
|
|
|
|
|
|
Example::
|
|
|
|
[{'channel': 0,
|
|
|
|
'parameters': {
|
|
|
|
'kp': 10.0,
|
|
|
|
'ki': 0.02,
|
|
|
|
'kd': 0.0,
|
|
|
|
'output_min': 0.0,
|
2022-02-24 20:16:47 +08:00
|
|
|
'output_max': 3.0},
|
|
|
|
'target': 37.0},
|
2020-10-01 06:21:43 +08:00
|
|
|
{'channel': 1,
|
|
|
|
'parameters': {
|
|
|
|
'kp': 10.0,
|
|
|
|
'ki': 0.02,
|
|
|
|
'kd': 0.0,
|
|
|
|
'output_min': 0.0,
|
2022-02-24 20:16:47 +08:00
|
|
|
'output_max': 3.0},
|
|
|
|
'target': 36.5}]
|
2020-10-01 06:21:43 +08:00
|
|
|
"""
|
2020-10-01 05:13:11 +08:00
|
|
|
return self._get_conf("pid")
|
|
|
|
|
2023-08-25 17:34:07 +08:00
|
|
|
def get_b_parameter(self):
|
|
|
|
"""Retrieve B-Parameter equation parameters for resistance to temperature conversion
|
2020-10-01 06:21:43 +08:00
|
|
|
|
|
|
|
Example::
|
|
|
|
[{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 0},
|
|
|
|
{'params': {'b': 3800.0, 'r0': 10000.0, 't0': 298.15}, 'channel': 1}]
|
|
|
|
"""
|
2023-08-25 17:34:07 +08:00
|
|
|
return self._get_conf("b-p")
|
2020-10-01 05:13:11 +08:00
|
|
|
|
|
|
|
def get_postfilter(self):
|
2020-10-01 06:21:43 +08:00
|
|
|
"""Retrieve DAC postfilter configuration
|
|
|
|
|
|
|
|
Example::
|
|
|
|
[{'rate': None, 'channel': 0},
|
|
|
|
{'rate': 21.25, 'channel': 1}]
|
|
|
|
"""
|
2020-10-01 05:13:11 +08:00
|
|
|
return self._get_conf("postfilter")
|
|
|
|
|
2020-09-29 08:52:46 +08:00
|
|
|
def report_mode(self):
|
|
|
|
"""Start reporting measurement values
|
|
|
|
|
2020-10-01 06:21:43 +08:00
|
|
|
Example of yielded data::
|
|
|
|
{'channel': 0,
|
|
|
|
'time': 2302524,
|
|
|
|
'adc': 0.6199188965423515,
|
|
|
|
'sens': 6138.519310282602,
|
|
|
|
'temperature': 36.87032392655527,
|
|
|
|
'pid_engaged': True,
|
|
|
|
'i_set': 2.0635816680889123,
|
|
|
|
'vref': 1.494,
|
|
|
|
'dac_value': 2.527790834044456,
|
|
|
|
'dac_feedback': 2.523,
|
|
|
|
'i_tec': 2.331,
|
|
|
|
'tec_i': 2.0925,
|
|
|
|
'tec_u_meas': 2.5340000000000003,
|
|
|
|
'pid_output': 2.067581958092247}
|
2020-09-29 08:52:46 +08:00
|
|
|
"""
|
|
|
|
while True:
|
2024-09-16 11:52:15 +08:00
|
|
|
self._socket.sendall("report\n".encode('utf-8'))
|
2020-09-29 08:52:46 +08:00
|
|
|
line = self._read_line()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
try:
|
|
|
|
yield json.loads(line)
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
pass
|
2024-09-16 11:52:15 +08:00
|
|
|
time.sleep(0.05)
|
2020-09-29 08:52:46 +08:00
|
|
|
|
|
|
|
def set_param(self, topic, channel, field="", value=""):
|
2020-10-01 06:34:59 +08:00
|
|
|
"""Set configuration parameters
|
|
|
|
|
|
|
|
Examples::
|
2024-08-15 17:38:03 +08:00
|
|
|
tec.set_param("output", 0, "max_v", 2.0)
|
2020-10-01 06:34:59 +08:00
|
|
|
tec.set_param("pid", 1, "output_max", 2.5)
|
2023-08-25 17:34:07 +08:00
|
|
|
tec.set_param("b-p", 0, "t0", 20.0)
|
2020-10-01 06:34:59 +08:00
|
|
|
tec.set_param("center", 0, "vref")
|
|
|
|
tec.set_param("postfilter", 1, 21)
|
|
|
|
|
|
|
|
See the firmware's README.md for a full list.
|
|
|
|
"""
|
2020-09-29 08:52:46 +08:00
|
|
|
if type(value) is float:
|
|
|
|
value = "{:f}".format(value)
|
|
|
|
if type(value) is not str:
|
|
|
|
value = str(value)
|
|
|
|
self._command(topic, str(channel), field, value)
|
|
|
|
|
|
|
|
def power_up(self, channel, target):
|
2020-10-01 06:21:43 +08:00
|
|
|
"""Start closed-loop mode"""
|
2020-09-29 08:52:46 +08:00
|
|
|
self.set_param("pid", channel, "target", value=target)
|
2024-08-15 17:38:03 +08:00
|
|
|
self.set_param("output", channel, "pid")
|
2020-10-01 06:21:43 +08:00
|
|
|
|
|
|
|
def save_config(self):
|
|
|
|
"""Save current configuration to EEPROM"""
|
|
|
|
self._command("save")
|
|
|
|
|
|
|
|
def load_config(self):
|
|
|
|
"""Load current configuration from EEPROM"""
|
|
|
|
self._command("load")
|
2023-05-19 11:23:39 +08:00
|
|
|
|
|
|
|
def hw_rev(self):
|
|
|
|
"""Get Thermostat hardware revision"""
|
|
|
|
return self._command("hwrev")
|
|
|
|
|
|
|
|
def fan(self):
|
|
|
|
"""Get Thermostat current fan settings"""
|
|
|
|
return self._command("fan")
|