Skip to content

Commit

Permalink
heaters: Disable heater if it appears main thread has stopped updating
Browse files Browse the repository at this point in the history
Update the heating code to periodically check that the main thread is
operating properly.  This is a mitigation for some rare cases where
the main thread may lockup while the background heater updating code
continues to run.  The goal is to detect these rare failures and
disable future heater updates.

Signed-off-by: Kevin O'Connor <[email protected]>
  • Loading branch information
KevinOConnor committed Jan 3, 2025
1 parent 80d185c commit 6b2f915
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions klippy/extras/heaters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
MAX_HEAT_TIME = 5.0
AMBIENT_TEMP = 25.
PID_PARAM_BASE = 255.
MAX_MAINTHREAD_TIME = 5.0

class Heater:
def __init__(self, config, sensor):
Expand All @@ -37,7 +38,7 @@ def __init__(self, config, sensor):
self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.)
self.smooth_time = config.getfloat('smooth_time', 1., above=0.)
self.inv_smooth_time = 1. / self.smooth_time
self.is_shutdown = False
self.verify_mainthread_time = -999.
self.lock = threading.Lock()
self.last_temp = self.smoothed_temp = self.target_temp = 0.
self.last_temp_time = 0.
Expand Down Expand Up @@ -66,7 +67,7 @@ def __init__(self, config, sensor):
self.printer.register_event_handler("klippy:shutdown",
self._handle_shutdown)
def set_pwm(self, read_time, value):
if self.target_temp <= 0. or self.is_shutdown:
if self.target_temp <= 0. or read_time > self.verify_mainthread_time:
value = 0.
if ((read_time < self.next_pwm_time or not self.last_pwm_value)
and abs(value - self.last_pwm_value) < 0.05):
Expand All @@ -91,7 +92,7 @@ def temperature_callback(self, read_time, temp):
self.can_extrude = (self.smoothed_temp >= self.min_extrude_temp)
#logging.debug("temp: %.3f %f = %f", read_time, temp)
def _handle_shutdown(self):
self.is_shutdown = True
self.verify_mainthread_time = -999.
# External commands
def get_name(self):
return self.name
Expand Down Expand Up @@ -129,6 +130,9 @@ def alter_target(self, target_temp):
target_temp = max(self.min_temp, min(self.max_temp, target_temp))
self.target_temp = target_temp
def stats(self, eventtime):
est_print_time = self.mcu_pwm.get_mcu().estimated_print_time(eventtime)
if not self.printer.is_shutdown():
self.verify_mainthread_time = est_print_time + MAX_MAINTHREAD_TIME
with self.lock:
target_temp = self.target_temp
last_temp = self.last_temp
Expand Down

0 comments on commit 6b2f915

Please sign in to comment.