kirdy/pykirdy/asyncio_exmaple.py

149 lines
5.1 KiB
Python
Raw Normal View History

2024-06-21 16:52:43 +08:00
from pprint import pp
2024-08-02 17:46:25 +08:00
from driver.kirdy import Kirdy, FilterConfig
2024-06-21 16:52:43 +08:00
import signal
2024-08-02 17:46:25 +08:00
import time
2024-06-21 16:52:43 +08:00
"""
Enter Device Firmware Upgrade(Dfu) mode
Please see README.md for flash instructions.
"""
2024-08-02 17:46:25 +08:00
def enter_dfu_mode(kirdy: Kirdy):
kirdy.device.dfu()
kirdy.end_session()
2024-06-21 16:52:43 +08:00
"""
Configure Kirdy to actively report status
Press Ctrl + C to exit active report mode
"""
2024-08-02 17:46:25 +08:00
def active_report(kirdy: Kirdy):
2024-06-21 16:52:43 +08:00
class SignalHandler:
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, signum, frame):
kirdy.stop_report_mode()
signal_handler = SignalHandler()
2024-08-02 17:46:25 +08:00
for data in kirdy.get_report_stream():
2024-06-21 16:52:43 +08:00
pp(data)
"""
Configure Kirdy network and board specific transconductance settings.
These configs are saved to flash immediately after command is processed.
"""
2024-08-02 17:46:25 +08:00
def device_cfg(kirdy: Kirdy):
2024-06-21 16:52:43 +08:00
# Kirdy rev0_3's gain and transconductance varies between boards to maximize the
# PD current range resolution.
2024-08-02 17:46:25 +08:00
kirdy.device.set_pd_mon_fin_gain(1.0)
kirdy.device.set_pd_mon_transconductance(1/1000.0)
2024-06-21 16:52:43 +08:00
# Network Settings will be updated on next reboot.
2024-08-02 17:46:25 +08:00
kirdy.device.set_ip_settings(
2024-06-21 16:52:43 +08:00
addr="192.168.1.128",
port=1337,
prefix_len=24,
gateway="192.168.1.1"
)
# Hard reset Kirdy.
2024-08-02 17:46:25 +08:00
kirdy.device.hard_reset()
2024-06-21 16:52:43 +08:00
"""
Control and config laser diode and thermostat parameters.
"""
2024-08-02 17:46:25 +08:00
def ld_thermostat_cfg(kirdy: Kirdy):
2024-06-21 16:52:43 +08:00
# Load the laser diode and thermostat settings from flash
2024-08-02 17:46:25 +08:00
kirdy.device.restore_settings_from_flash()
2024-06-21 16:52:43 +08:00
# Power off the laser diode and thermostat and clear alarm (if any)
2024-08-02 17:46:25 +08:00
kirdy.laser.set_power_on(False)
kirdy.laser.clear_alarm()
kirdy.thermostat.set_power_on(False)
kirdy.thermostat.clear_alarm()
2024-06-21 16:52:43 +08:00
# Set the laser diode terminals not to be shorted
2024-08-02 17:46:25 +08:00
kirdy.laser.set_ld_terms_short(False)
2024-06-21 16:52:43 +08:00
# Do not power up the laser & thermostat during initial startup
2024-08-02 17:46:25 +08:00
kirdy.laser.set_default_pwr_on(False)
kirdy.thermostat.set_default_pwr_on(False)
2024-06-21 16:52:43 +08:00
2024-08-02 17:46:25 +08:00
kirdy.laser.set_i(0)
2024-06-21 16:52:43 +08:00
# Configure the laser diode output power limit and photodiode parameters
# Exceeding the measured power limit triggers overpower protection alarm.
# The laser diode power will be turned off while the thermostat power remains unchanged.
2024-08-02 17:46:25 +08:00
kirdy.laser.set_ld_pwr_limit(0.0)
kirdy.laser.set_pd_mon_dark_current(0.0)
kirdy.laser.set_pd_mon_responsitivity(0.0)
2024-06-21 16:52:43 +08:00
# Configure the thermostat NTC thermistor parameters.
2024-08-02 17:46:25 +08:00
kirdy.thermostat.set_sh_r0(10.0 * 1000)
kirdy.thermostat.set_sh_t0(25)
kirdy.thermostat.set_sh_beta(3900)
2024-06-21 16:52:43 +08:00
# Configure the thermostat output limits.
# The actual output current is limited by the hardware limit set below.
2024-08-02 17:46:25 +08:00
kirdy.thermostat.set_tec_max_cooling_i(1.0)
kirdy.thermostat.set_tec_max_heating_i(1.0)
kirdy.thermostat.set_tec_max_v(4.0)
2024-06-21 16:52:43 +08:00
# Configure the thermostat temperature monitor limit.
# Exceeding the limit will trigger over temperature protection alarm.
# The laser diode and thermostat power will be turned off.
2024-08-02 17:46:25 +08:00
kirdy.thermostat.set_temp_mon_upper_limit(70)
kirdy.thermostat.set_temp_mon_lower_limit(0)
2024-06-21 16:52:43 +08:00
# Configure the thermostat PID related parameter.
# You can configure the PID parameter with the autotune tool.
# Here provides an example if it is configured manually.
2024-08-02 17:46:25 +08:00
kirdy.thermostat.set_temperature_setpoint(25)
kirdy.thermostat.set_pid_kp(0.15668282198105507)
kirdy.thermostat.set_pid_ki(0.002135962407793784)
kirdy.thermostat.set_pid_kd(0.829254515277143)
kirdy.thermostat.set_pid_output_max(1.0)
kirdy.thermostat.set_pid_output_min(-1.0)
2024-06-21 16:52:43 +08:00
# Configure the thermostat ADC Filter Setting / PID Update Rate / Report Rate.
# The ADC sampling rate determines the report and pid update rate.
# The chosen filter and sampling rate affects the noise of the readings.
2024-08-02 17:46:25 +08:00
# For details, please refer to the AD7172 da`tas`heet.
kirdy.thermostat.config_temp_adc_filter(FilterConfig.Sinc5Sinc1With50hz60HzRejection.f16sps)
2024-06-21 16:52:43 +08:00
# Configure thermostat to run in PID control mode
2024-08-02 17:46:25 +08:00
kirdy.thermostat.set_pid_control_mode()
2024-06-21 16:52:43 +08:00
# When control mode is switched from PID to constant current(CC) control mode,
# the thermostat keeps its instantaneous output current unchanged.
# Thermostat output current should only be set if it is in CC control mode
# or the value set will not be overwritten by PID output.
2024-08-02 17:46:25 +08:00
kirdy.thermostat.set_constant_current_control_mode()
kirdy.thermostat.set_tec_i_out(0.0)
2024-06-21 16:52:43 +08:00
# Save the above settings configured into the flash
2024-08-02 17:46:25 +08:00
kirdy.device.save_current_settings_to_flash()
2024-06-21 16:52:43 +08:00
# Power on the laser diode and thermostat
2024-08-02 17:46:25 +08:00
kirdy.laser.set_power_on(True)
kirdy.thermostat.set_power_on(True)
2024-06-21 16:52:43 +08:00
2024-08-02 17:46:25 +08:00
pp(kirdy.device.get_settings_summary())
pp(kirdy.device.get_status_report())
2024-06-21 16:52:43 +08:00
2024-08-02 17:46:25 +08:00
def main():
2024-06-21 16:52:43 +08:00
kirdy = Kirdy()
2024-08-02 17:46:25 +08:00
kirdy.start_session(host='192.168.1.128', port=1337)
2024-06-21 16:52:43 +08:00
2024-08-02 17:46:25 +08:00
while not(kirdy.connected()):
pass
ld_thermostat_cfg(kirdy)
# active_report(kirdy)
2024-06-21 16:52:43 +08:00
# await device_cfg(kirdy)
2024-08-02 17:46:25 +08:00
# enter_dfu_mode(kirdy)
2024-08-02 17:46:25 +08:00
kirdy.end_session(block=True)
if __name__ == "__main__":
2024-08-02 17:46:25 +08:00
main()