1
0
Fork 0

Update example code

This commit is contained in:
linuswck 2024-09-03 10:36:49 +08:00
parent f2ad06ecae
commit 2fe2ef531b
1 changed files with 83 additions and 95 deletions

View File

@ -2,147 +2,135 @@ from pprint import pp
from driver.kirdy import Kirdy, FilterConfig
import signal
import time
import asyncio
async def enter_dfu_mode(kirdy: Kirdy):
"""
Enter Device Firmware Upgrade(Dfu) mode
Please see README.md for flash instructions.
Please refer to README.md for firmware update instructions.
"""
def enter_dfu_mode(kirdy: Kirdy):
kirdy.device.dfu()
kirdy.end_session()
await kirdy.device.dfu()
async def active_report(kirdy: Kirdy):
"""
Configure Kirdy to actively report status
Configure Kirdy to actively report status to connected socket
Press Ctrl + C to exit active report mode
"""
def active_report(kirdy: Kirdy):
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()
for data in kirdy.get_report_stream():
async for data in kirdy.report_mode():
pp(data)
async def device_cfg(kirdy: Kirdy):
"""
Configure Kirdy network and board specific transconductance settings.
Configure Kirdy's network and board specific transconductance settings.
These configs are saved to flash immediately after command is processed.
"""
def device_cfg(kirdy: Kirdy):
# Kirdy rev0_3's gain and transconductance varies between boards to maximize the
# PD current range resolution.
kirdy.device.set_pd_mon_fin_gain(1.0)
kirdy.device.set_pd_mon_transconductance(1/1000.0)
await kirdy.device.set_pd_mon_fin_gain(1.0)
await kirdy.device.set_pd_mon_transconductance(1/1000.0)
# Network Settings will be updated on next reboot.
kirdy.device.set_ip_settings(
await kirdy.device.set_ip_settings(
addr="192.168.1.128",
port=1337,
prefix_len=24,
gateway="192.168.1.1"
)
# Hard reset Kirdy.
kirdy.device.hard_reset()
await kirdy.device.hard_reset()
async def ld_thermostat_cfg(kirdy: Kirdy):
"""
Control and config laser diode and thermostat parameters.
"""
def ld_thermostat_cfg(kirdy: Kirdy):
# Load the laser diode and thermostat settings from flash
kirdy.device.restore_settings_from_flash()
# Load the laser diode and thermostat settings from flash memory.
await kirdy.device.restore_settings_from_flash()
# Power off the laser diode and thermostat and clear alarm (if any)
kirdy.laser.set_power_on(False)
kirdy.laser.clear_alarm()
kirdy.thermostat.set_power_on(False)
kirdy.thermostat.clear_alarm()
# Power off the laser diode & thermostat and clear any asserted alarm
await kirdy.laser.set_power_on(False)
await kirdy.laser.clear_alarm()
await kirdy.thermostat.set_power_on(False)
await kirdy.thermostat.clear_alarm()
# Set the laser diode terminals not to be shorted
kirdy.laser.set_ld_terms_short(False)
await kirdy.laser.set_ld_terms_short(False)
# Do not power up the laser & thermostat during initial startup
kirdy.laser.set_default_pwr_on(False)
kirdy.thermostat.set_default_pwr_on(False)
await kirdy.laser.set_default_pwr_on(False)
await kirdy.thermostat.set_default_pwr_on(False)
kirdy.laser.set_i(0)
await kirdy.laser.set_i(0)
# 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.
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)
# Exceeding the power limit triggers overpower protection alarm.
# The laser diode power will be cut off upon alarm assertion while the thermostat power remains unchanged.
await kirdy.laser.set_ld_pwr_limit(0.0)
await kirdy.laser.set_pd_mon_dark_current(0.0)
await kirdy.laser.set_pd_mon_responsitivity(0.0)
# Configure the thermostat NTC thermistor parameters.
kirdy.thermostat.set_sh_r0(10.0 * 1000)
kirdy.thermostat.set_sh_t0(25)
kirdy.thermostat.set_sh_beta(3900)
await kirdy.thermostat.set_sh_r0(10.0 * 1000)
await kirdy.thermostat.set_sh_t0(25)
await kirdy.thermostat.set_sh_beta(3900)
# Configure the thermostat output limits.
# The actual output current is limited by the hardware limit set below.
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)
# Configure the thermostat TEC settings.
# The actual output current is limited by value set below.
await kirdy.thermostat.set_tec_max_cooling_i(1.0)
await kirdy.thermostat.set_tec_max_heating_i(1.0)
await kirdy.thermostat.set_tec_max_v(4.0)
# 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.
kirdy.thermostat.set_temp_mon_upper_limit(70)
kirdy.thermostat.set_temp_mon_lower_limit(0)
# Configure the thermostat temperature monitor limits.
# Exceeding the temperature limits trigger over temperature protection alarm.
# Both laser diode and thermostat power will be cut off upon alarm assertion.
await kirdy.thermostat.set_temp_mon_upper_limit(70)
await kirdy.thermostat.set_temp_mon_lower_limit(0)
# 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.
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)
# Configure the thermostat PID parameters.
# You can configure the PID parameter by the included autotune script.
await kirdy.thermostat.set_temperature_setpoint(25)
await kirdy.thermostat.set_pid_kp(0.15668282198105507)
await kirdy.thermostat.set_pid_ki(0.002135962407793784)
await kirdy.thermostat.set_pid_kd(0.829254515277143)
await kirdy.thermostat.set_pid_output_max(1.0)
await kirdy.thermostat.set_pid_output_min(-1.0)
# 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.
# For details, please refer to the AD7172 da`tas`heet.
kirdy.thermostat.config_temp_adc_filter(FilterConfig.Sinc5Sinc1With50hz60HzRejection.f16sps)
# For details, please refer to the AD7172 datasheet.
await kirdy.thermostat.config_temp_adc_filter(FilterConfig.Sinc5Sinc1With50hz60HzRejection.f16sps)
# Configure thermostat to run in PID control mode
kirdy.thermostat.set_pid_control_mode()
await kirdy.thermostat.set_pid_control_mode()
# When control mode is switched from PID to constant current(CC) control mode,
# the thermostat keeps its instantaneous output current unchanged.
# 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.
kirdy.thermostat.set_constant_current_control_mode()
kirdy.thermostat.set_tec_i_out(0.0)
# or the value set will be overwritten by PID output.
await kirdy.thermostat.set_constant_current_control_mode()
await kirdy.thermostat.set_tec_i_out(0.0)
# Save the above settings configured into the flash
kirdy.device.save_current_settings_to_flash()
# Save the current settings to flash memory
await kirdy.device.save_current_settings_to_flash()
# Power on the laser diode and thermostat
kirdy.laser.set_power_on(True)
kirdy.thermostat.set_power_on(True)
await kirdy.laser.set_power_on(True)
await kirdy.thermostat.set_power_on(True)
pp(kirdy.device.get_settings_summary())
pp(kirdy.device.get_status_report())
pp(await kirdy.device.get_settings_summary())
pp(await kirdy.device.get_status_report())
def main():
async def main():
kirdy = Kirdy()
kirdy.start_session(host='192.168.1.128', port=1337)
await kirdy.wait_until_connected()
while not(kirdy.connected()):
pass
ld_thermostat_cfg(kirdy)
# active_report(kirdy)
await ld_thermostat_cfg(kirdy)
# await active_report(kirdy)
# await device_cfg(kirdy)
# enter_dfu_mode(kirdy)
# await enter_dfu_mode(kirdy)
kirdy.end_session(block=True)
await kirdy.end_session()
if __name__ == "__main__":
main()
asyncio.run(main())