From 3528d8a68f21a75d8d73e42a385c189594f31915 Mon Sep 17 00:00:00 2001 From: linuswck Date: Wed, 28 Feb 2024 13:33:51 +0800 Subject: [PATCH] Update Python Test Scripts --- eth_cmd_test.py | 64 +++++++++++++++++++++++++++++++++---------------- pid_autotune.py | 18 ++++++++------ 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/eth_cmd_test.py b/eth_cmd_test.py index 17c290c..a5ced94 100644 --- a/eth_cmd_test.py +++ b/eth_cmd_test.py @@ -20,7 +20,11 @@ dfu_cmd = { ld_cmd = { "laser_diode_cmd": "SetI", - "data_f64": 0.0, + "data_f32": 0.0, +} + +tec_clear_alarm = { + "thermostat_cmd": "ClearAlarm", } tec_power_down = { @@ -29,47 +33,47 @@ tec_power_down = { tec_set_sh_t0_cmd = { "thermostat_cmd": "SetShT0", - "data_f64": 25.0, + "data_f32": 25.0, } tec_set_sh_r0_cmd = { "thermostat_cmd": "SetShR0", - "data_f64": 10.0 * 1000, + "data_f32": 10.0 * 1000, } tec_set_sh_beta_cmd = { "thermostat_cmd": "SetShBeta", - "data_f64": 3900.0, + "data_f32": 3900.0, } tec_set_temperature_setpoint_cmd = { "thermostat_cmd": "SetTemperatureSetpoint", - "data_f64": 25.0, + "data_f32": 30.0, } tec_set_pid_kp_cmd = { "thermostat_cmd": "SetPidKp", - "data_f64": 0.10889684439011593 + "data_f32": 0.14398316474562461 } tec_set_pid_ki_cmd = { "thermostat_cmd": "SetPidKi", - "data_f64": 0.0038377132059211646 + "data_f32": 0.004117762308816449 } tec_set_pid_kd_cmd = { "thermostat_cmd": "SetPidKd", - "data_f64": 0.22294449514406328 + "data_f32": 0.36324599631515664 } tec_set_pid_out_min_cmd = { "thermostat_cmd": "SetPidOutMin", - "data_f64": -1.0, + "data_f32": -1.0, } tec_set_pid_out_max_cmd = { "thermostat_cmd": "SetPidOutMax", - "data_f64": 1.0, + "data_f32": 1.0, } tec_power_up = { @@ -80,8 +84,21 @@ tec_pid_engage = { "thermostat_cmd": "SetPidEngage", } -tec_get_tec_status = { - "thermostat_cmd": "GetTecStatus", +kirdy_get_status_report = { + "device_cmd": "GetStatusReport", +} + +kirdy_report_mode_cmd = { + "device_cmd": "SetActiveReportMode", + "data_bool": True, +} + +temp_adc_config = { + "thermostat_cmd": "ConfigTempAdcFilter", + "temp_adc_filter": { + "filter_type": "Sinc5Sinc1With50hz60HzRejection", + "sinc5sinc1postfilter": "F16SPS" + } } def send_cmd(input, socket): @@ -89,15 +106,20 @@ def send_cmd(input, socket): # Give some time for Kirdy to process the cmd time.sleep(0.5) -def read_cmd(input, socket): - socket.send(bytes(json.dumps(input), "UTF-8")) - data = socket.recv(1024).decode('utf8') - return json.loads(data) +def poll_status_report(socket): + while True: + data = socket.recv(1024).decode('utf8') + try: + json_data = json.loads(data) + break + except json.decoder.JSONDecodeError: + pass + return json_data s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def signal_handler(sig, frame): - ld_cmd["data_f64"] = 0.0 + ld_cmd["data_f32"] = 0.0 send_cmd(ld_cmd, s) send_cmd(tec_power_down, s) s.close() @@ -116,10 +138,12 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: send_cmd(tec_set_pid_kp_cmd, s) send_cmd(tec_set_pid_ki_cmd, s) send_cmd(tec_set_pid_kd_cmd, s) + send_cmd(tec_clear_alarm, s) + send_cmd(temp_adc_config, s) send_cmd(tec_pid_engage, s) send_cmd(tec_power_up, s) + send_cmd(kirdy_report_mode_cmd, s) while True: - tec_status = read_cmd(tec_get_tec_status, s) - print(f"Ts: {tec_status['ts']} | Temperature: {tec_status['temperature'] - 273.15}") - s.close() + status_report = poll_status_report(s) + print(f"Ts: {status_report['ts']} | Temperature: {status_report['tec']['temperature'] - 273.15}") diff --git a/pid_autotune.py b/pid_autotune.py index 20c2b34..ea33c7c 100644 --- a/pid_autotune.py +++ b/pid_autotune.py @@ -227,6 +227,10 @@ tec_power_down = { "thermostat_cmd": "PowerDown", } +kirdy_get_status_report = { + "device_cmd": "GetStatusReport", +} + tec_get_tec_status = { "thermostat_cmd": "GetTecStatus", } @@ -236,8 +240,7 @@ tec_pid_dis_engage = { } tec_set_i_out = { - "thermostat_cmd": "SetTecIOut", - "data_f64": 0.0, + "tec_set_i": 0.0, } # Kirdy IP and Port Number @@ -284,21 +287,22 @@ def main(): send_cmd(tec_power_up, s) while True: - tec_status = read_cmd(tec_get_tec_status, s) + status_report = read_cmd(kirdy_get_status_report, s) - temperature = tec_status["temperature"] - 273.15 - ts = tec_status['ts'] + temperature = status_report["tec"]["temperature"] - 273.15 + print(temperature) + ts = status_report['ts'] if (tuner.run(temperature, ts / 1000.0)): break tuner_out = tuner.output() - tec_set_i_out["data_f64"] = float(tuner_out * 1000.0) + tec_set_i_out["tec_set_i"] = float(tuner_out * 1000.0) send_cmd(tec_set_i_out, s) - tec_set_i_out["data_f64"] = 0.0 + tec_set_i_out["tec_set_i"] = 0.0 send_cmd(tec_power_down, s) s.close()