From 3a0b834f3a8929208d4ea6116f206a55fbc186ee Mon Sep 17 00:00:00 2001 From: Ben Lye Date: Thu, 5 Mar 2026 10:42:50 +0000 Subject: [PATCH] hall_filament_width_sensor: Sync runout and width sensor states Keeps the width sensor state in sync with the associated runout sensor and normalizes the output when the width sensor is disabled. When the width sensor was disabled the associated filament sensor remained enabled but was no longer updated so would be 'stuck' in the last state it was in before the width sensor was disabled. The toggle switch and readings in the web interface could also be out of sync with the actual sensor and filament states, misleading the user. Now, when the width sensor is disabled: - The filament sensor is disabled - The reported filament state is 'present' - The reported filament diameter is the nominal diameter When the width sensor is enabled: - The filament sensor is enabled - The reported filament state is the derived state - The reported filament diameter is the measured value Finally, `min_diameter` is now used to determine the runout state rather than a fixed value of `0.5`. Signed-off-by: Ben Lye ben@lye.co.nz --- docs/Status_Reference.md | 3 ++- klippy/extras/hall_filament_width_sensor.py | 22 ++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/docs/Status_Reference.md b/docs/Status_Reference.md index 913bf10a8..19e36ca42 100644 --- a/docs/Status_Reference.md +++ b/docs/Status_Reference.md @@ -249,7 +249,8 @@ object: [filament_switch_sensor](Status_Reference.md#filament_switch_sensor) - `is_active`: Returns True if the sensor is currently active. - `flow_compensation_enabled`: Returns True if flow compensation is enabled. -- `Diameter`: The last reading from the sensor in mm. +- `Diameter`: Returns the last width reading in mm if the sensor is active or + the nominal filament diameter if it is not. - `Raw`: The last raw ADC reading from the sensor. ## heater diff --git a/klippy/extras/hall_filament_width_sensor.py b/klippy/extras/hall_filament_width_sensor.py index 2d362f576..1be3d1b16 100644 --- a/klippy/extras/hall_filament_width_sensor.py +++ b/klippy/extras/hall_filament_width_sensor.py @@ -94,6 +94,7 @@ class HallFilamentWidthSensor: desc=self.cmd_DISABLE_FILAMENT_WIDTH_LOG_help) self.runout_helper = filament_switch_sensor.RunoutHelper(config) + self.runout_helper.sensor_enabled = self.is_active # Initialization def handle_ready(self): # Load printer objects @@ -112,12 +113,15 @@ class HallFilamentWidthSensor: # read sensor value read_time, read_value = samples[-1] self.lastFilamentWidthReading2 = round(read_value * 10000) - # calculate diameter - diameter_new = round((self.dia2 - self.dia1)/ - (self.rawdia2-self.rawdia1)* - ((self.lastFilamentWidthReading+self.lastFilamentWidthReading2) - -self.rawdia1)+self.dia1,2) - self.diameter=(5.0 * self.diameter + diameter_new)/6 + if self.is_active: + # calculate diameter + diameter_new = round((self.dia2 - self.dia1)/ + (self.rawdia2-self.rawdia1)* + ((self.lastFilamentWidthReading+self.lastFilamentWidthReading2) + -self.rawdia1)+self.dia1,2) + self.diameter=(5.0 * self.diameter + diameter_new)/6 + else: + self.diameter = self.nominal_filament_dia def update_filament_array(self, last_epos): # Fill array @@ -150,7 +154,7 @@ class HallFilamentWidthSensor: self.runout_helper.note_filament_present(eventtime, self.runout_dia_min <= self.diameter <= self.runout_dia_max) # Does filament exists - if self.diameter > 0.5: + if self.diameter > self.runout_dia_min: if len(self.filament_array) > 0: # Get first position in filament array pending_position = self.filament_array[0][0] @@ -224,6 +228,7 @@ class HallFilamentWidthSensor: was_active = self.is_active self.is_active = True + self.runout_helper.sensor_enabled = True response = ("Filament width sensor: ON" + "\nFlow compensation: " + ("ON" if self.enable_flow_compensation else "OFF")) @@ -239,11 +244,14 @@ class HallFilamentWidthSensor: def cmd_M406(self, gcmd): response = "Filament width sensor: OFF" self.is_active = False + self.runout_helper.sensor_enabled = False # Stop extrude factor update timer self.reactor.update_timer(self.extrude_factor_update_timer, self.reactor.NEVER) # Clear filament array self.filament_array = [] + # Tell the filament sensor that there is filament present + self.runout_helper.note_filament_present(self.reactor.NOW, True) # Set extrude multiplier to 100% self.gcode.run_script_from_command("M221 S100") gcmd.respond_info(response)