update eth_cmd_test code

This commit is contained in:
linuswck 2024-02-07 16:57:10 +08:00
parent 09b3765877
commit 89d415194d
1 changed files with 47 additions and 39 deletions

View File

@ -1,7 +1,6 @@
# Python Test Scripts for Controlling Kirdy # Python Test Scripts for Controlling Kirdy
# Kirdy is written to be controlled via a json object based on miniconf rust crate # Kirdy is written to be controlled via a json object based on miniconf rust crate
# Json Field: # Json Field:
# "rev": hw_rev
# "laser_diode_cmd / thermostat_cmd": Check cmd_handler.rs for the list of cmds # "laser_diode_cmd / thermostat_cmd": Check cmd_handler.rs for the list of cmds
# "data_f32": Optional f32 Data field depending on cmd # "data_f32": Optional f32 Data field depending on cmd
# "data_f64": Optional f64 Data field depending on cmd # "data_f64": Optional f64 Data field depending on cmd
@ -9,103 +8,112 @@
import socket import socket
import json import json
import time import time
import signal
# Kirdy IP and Port Number # Kirdy IP and Port Number
HOST = "192.168.1.132" HOST = "192.168.1.132"
PORT = 1337 PORT = 1337
ld_cmd = { ld_cmd = {
"rev": 3,
"laser_diode_cmd": "SetI", "laser_diode_cmd": "SetI",
"data_f64": 0.0, "data_f64": 0.0,
} }
tec_power_down = { tec_power_down = {
"rev": 3,
"thermostat_cmd": "PowerDown", "thermostat_cmd": "PowerDown",
} }
tec_set_sh_t0_cmd = { tec_set_sh_t0_cmd = {
"rev": 3,
"thermostat_cmd": "SetShT0", "thermostat_cmd": "SetShT0",
"data_f64": 25.0, "data_f64": 25.0,
} }
tec_set_sh_r0_cmd = { tec_set_sh_r0_cmd = {
"rev": 3,
"thermostat_cmd": "SetShR0", "thermostat_cmd": "SetShR0",
"data_f64": 10.0 * 1000, "data_f64": 10.0 * 1000,
} }
tec_set_sh_beta_cmd = { tec_set_sh_beta_cmd = {
"rev": 3,
"thermostat_cmd": "SetShBeta", "thermostat_cmd": "SetShBeta",
"data_f64": 3900.0, "data_f64": 3900.0,
} }
tec_set_temperature_setpoint_cmd = { tec_set_temperature_setpoint_cmd = {
"rev": 3,
"thermostat_cmd": "SetTemperatureSetpoint", "thermostat_cmd": "SetTemperatureSetpoint",
"data_f64": 45.0, "data_f64": 25.0,
} }
tec_set_pid_kp_cmd = { tec_set_pid_kp_cmd = {
"rev": 3,
"thermostat_cmd": "SetPidKp", "thermostat_cmd": "SetPidKp",
"data_f64": 1.0, "data_f64": 0.10889684439011593
} }
tec_set_pid_ki_cmd = { tec_set_pid_ki_cmd = {
"rev": 3,
"thermostat_cmd": "SetPidKi", "thermostat_cmd": "SetPidKi",
"data_f64": 0.01, "data_f64": 0.0038377132059211646
} }
tec_set_pid_kd_cmd = { tec_set_pid_kd_cmd = {
"rev": 3,
"thermostat_cmd": "SetPidKd", "thermostat_cmd": "SetPidKd",
"data_f64": 0.0, "data_f64": 0.22294449514406328
} }
tec_set_pid_out_min_cmd = { tec_set_pid_out_min_cmd = {
"rev": 3,
"thermostat_cmd": "SetPidOutMin", "thermostat_cmd": "SetPidOutMin",
"data_f64": -1.0, "data_f64": -1.0,
} }
tec_set_pid_out_max_cmd = { tec_set_pid_out_max_cmd = {
"rev": 3,
"thermostat_cmd": "SetPidOutMax", "thermostat_cmd": "SetPidOutMax",
"data_f64": 1.0, "data_f64": 1.0,
} }
tec_power_up = { tec_power_up = {
"rev": 3,
"thermostat_cmd": "PowerUp", "thermostat_cmd": "PowerUp",
} }
# Current version of cmd_handler cannot service multiple cmds in the same eth buffer tec_pid_engage = {
delay = 0.25 "thermostat_cmd": "SetPidEngage",
}
tec_get_tec_status = {
"thermostat_cmd": "GetTecStatus",
}
def send_cmd(input, socket):
socket.send(bytes(json.dumps(input), "UTF-8"))
# 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)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def signal_handler(sig, frame):
send_cmd(tec_power_down, s)
s.close()
exit()
signal.signal(signal.SIGINT, signal_handler)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT)) s.connect((HOST, PORT))
s.send(bytes(json.dumps(tec_power_down), "UTF-8")) send_cmd(ld_cmd, s)
time.sleep(delay) send_cmd(tec_power_down, s)
s.send(bytes(json.dumps(tec_set_sh_t0_cmd), "UTF-8")) send_cmd(tec_set_sh_t0_cmd, s)
time.sleep(delay) send_cmd(tec_set_sh_r0_cmd, s)
s.send(bytes(json.dumps(tec_set_sh_r0_cmd), "UTF-8")) send_cmd(tec_set_sh_beta_cmd, s)
time.sleep(delay) send_cmd(tec_set_temperature_setpoint_cmd, s)
s.send(bytes(json.dumps(tec_set_sh_beta_cmd), "UTF-8")) send_cmd(tec_set_pid_kp_cmd, s)
time.sleep(delay) send_cmd(tec_set_pid_ki_cmd, s)
s.send(bytes(json.dumps(tec_set_temperature_setpoint_cmd), "UTF-8")) send_cmd(tec_set_pid_kd_cmd, s)
time.sleep(delay) send_cmd(tec_pid_engage, s)
s.send(bytes(json.dumps(tec_set_pid_kp_cmd), "UTF-8")) send_cmd(tec_power_up, s)
time.sleep(delay)
s.send(bytes(json.dumps(tec_set_pid_ki_cmd), "UTF-8"))
time.sleep(delay)
s.send(bytes(json.dumps(tec_set_pid_kd_cmd), "UTF-8"))
time.sleep(delay)
s.send(bytes(json.dumps(tec_power_up), "UTF-8"))
print("press enter to force thermostat to stop")
input()
s.send(bytes(json.dumps(tec_power_down), "UTF-8"))
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()