|
| 1 | +""" |
| 2 | +A file for the ThermalControl class |
| 3 | +""" |
| 4 | + |
| 5 | +import time |
| 6 | + |
| 7 | + |
| 8 | +class ThermalControl: |
| 9 | + """ |
| 10 | + The class for the ThermalControl |
| 11 | + """ |
| 12 | + |
| 13 | + FLAT_TYPE = 0 |
| 14 | + RAMP_TYPE = 1 |
| 15 | + SINE_TYPE = 2 |
| 16 | + |
| 17 | + def __init__(self, titrator): |
| 18 | + """ |
| 19 | + The constructor function for the ThermalControl class |
| 20 | + """ |
| 21 | + self.titrator = titrator |
| 22 | + self._heat = bool(True) |
| 23 | + self._base_thermal_target = 78 |
| 24 | + self._current_thermal_target = 67 |
| 25 | + self._thermal_function_type = ThermalControl.FLAT_TYPE |
| 26 | + self._ramp_time_start_seconds = 0 |
| 27 | + self._ramp_time_end_seconds = 0 |
| 28 | + self._ramp_initial_value = 0.0 |
| 29 | + |
| 30 | + def get_heat(self, default): |
| 31 | + """ |
| 32 | + Get the heat setting from EEPROM |
| 33 | + """ |
| 34 | + if self._heat is None: |
| 35 | + return default |
| 36 | + return self._heat |
| 37 | + |
| 38 | + def set_heat(self, value): |
| 39 | + """ |
| 40 | + Set the heat setting in EEPROM |
| 41 | + """ |
| 42 | + self._heat = value |
| 43 | + |
| 44 | + def get_base_thermal_target(self): |
| 45 | + """ |
| 46 | + Get the base thermal target |
| 47 | + """ |
| 48 | + return self._base_thermal_target |
| 49 | + |
| 50 | + def set_base_thermal_target(self, value): |
| 51 | + """ |
| 52 | + Set the base thermal target |
| 53 | + """ |
| 54 | + self._base_thermal_target = value |
| 55 | + |
| 56 | + def get_current_thermal_target(self): |
| 57 | + """ |
| 58 | + Get the current thermal target |
| 59 | + """ |
| 60 | + return self._current_thermal_target |
| 61 | + |
| 62 | + def set_current_thermal_target(self, value): |
| 63 | + """ |
| 64 | + Set the current thermal target |
| 65 | + """ |
| 66 | + self._current_thermal_target = value |
| 67 | + |
| 68 | + def get_thermal_function_type(self): |
| 69 | + """ |
| 70 | + Get the current thermal function type. |
| 71 | + """ |
| 72 | + return self._thermal_function_type |
| 73 | + |
| 74 | + def set_thermal_function_type(self, function_type): |
| 75 | + """ |
| 76 | + Set the current thermal function type. |
| 77 | + """ |
| 78 | + if function_type in ( |
| 79 | + ThermalControl.FLAT_TYPE, |
| 80 | + ThermalControl.RAMP_TYPE, |
| 81 | + ThermalControl.SINE_TYPE, |
| 82 | + ): |
| 83 | + self._thermal_function_type = function_type |
| 84 | + else: |
| 85 | + raise ValueError("Invalid thermal function type") |
| 86 | + |
| 87 | + def get_ramp_time_start(self): |
| 88 | + """ |
| 89 | + Get the ramp time start in seconds. |
| 90 | + """ |
| 91 | + return ( |
| 92 | + self._ramp_time_start_seconds |
| 93 | + if self._thermal_function_type != ThermalControl.FLAT_TYPE |
| 94 | + else 0 |
| 95 | + ) |
| 96 | + |
| 97 | + def get_ramp_time_end(self): |
| 98 | + """ |
| 99 | + Get the ramp time end in seconds. |
| 100 | + """ |
| 101 | + return ( |
| 102 | + self._ramp_time_end_seconds |
| 103 | + if self._thermal_function_type != ThermalControl.FLAT_TYPE |
| 104 | + else 0 |
| 105 | + ) |
| 106 | + |
| 107 | + def set_ramp_duration_hours(self, new_ph_ramp_duration): |
| 108 | + """ |
| 109 | + Set the ramp duration in hours. If the duration is greater than 0, configure ramp parameters; |
| 110 | + otherwise, set the function type to FLAT_TYPE. |
| 111 | + """ |
| 112 | + if new_ph_ramp_duration > 0: |
| 113 | + current_ramp_time = ( |
| 114 | + self._ramp_time_end_seconds - self._ramp_time_start_seconds |
| 115 | + ) |
| 116 | + current_ramp_time_str = f"{current_ramp_time:.3f}" |
| 117 | + new_ramp_duration_str = f"{new_ph_ramp_duration:.3f}" |
| 118 | + print( |
| 119 | + f"Change ramp time from {current_ramp_time_str} to {new_ramp_duration_str}" |
| 120 | + ) |
| 121 | + |
| 122 | + self._ramp_time_start_seconds = int(time.monotonic()) |
| 123 | + self._ramp_time_end_seconds = self._ramp_time_start_seconds + int( |
| 124 | + new_ph_ramp_duration * 3600 |
| 125 | + ) |
| 126 | + |
| 127 | + self._ramp_initial_value = self.titrator.thermal_probe.get_running_average() |
| 128 | + self._thermal_function_type = ThermalControl.RAMP_TYPE |
| 129 | + else: |
| 130 | + self._ramp_time_end_seconds = 0 |
| 131 | + self._thermal_function_type = ThermalControl.FLAT_TYPE |
| 132 | + print("Set ramp time to 0") |
0 commit comments