From 8c592ee3665ff06591db9b70c5e6557ff88f3df2 Mon Sep 17 00:00:00 2001 From: Ben Lye Date: Wed, 4 Mar 2026 11:14:54 +0000 Subject: [PATCH] hall_filament_width_sensor: Add config validation to avoid possible div-by-zero exceptions Adds two checks to prevent config settings which could lead to div-by-zero errors in specific circumstances: - `raw_dia1` and `raw_dia2` must be different to prevent a possible exception in `adc2_callback` - `max_difference` must be less than `default_nominal_filament_diameter` to prevent a possible exception in `extrude_factor_update_event` Doc is also updated. Signed-off-by: Ben Lye ben@lye.co.nz --- docs/Config_Reference.md | 7 ++++--- klippy/extras/hall_filament_width_sensor.py | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index f0ca2dce2..79f05f121 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -5064,15 +5064,16 @@ adc2: # 1.50 for cal_dia1 and 2.00 for cal_dia2. #raw_dia1: 9500 #raw_dia2: 10500 -# The raw calibration values for the sensors. The default is 9500 -# for raw_dia1 and 10500 for raw_dia2. +# The raw calibration values for the sensors. Both values must not be +# the same. The default is 9500 for raw_dia1 and 10500 for raw_dia2. #default_nominal_filament_diameter: 1.75 # The nominal filament diameter. This parameter must be provided. #max_difference: 0.200 # Maximum allowed filament diameter difference in millimeters (mm). # If difference between nominal filament diameter and sensor output # is more than +- max_difference, extrusion multiplier is set back -# to %100. The default is 0.200. +# to %100. Must be less than default_nominal_filament_diameter. +# The default is 0.200. #measurement_delay: 70 # The distance from sensor to the melting chamber/hot-end in # millimeters (mm). The filament between the sensor and the hot-end diff --git a/klippy/extras/hall_filament_width_sensor.py b/klippy/extras/hall_filament_width_sensor.py index 68180a002..b11b9d94e 100644 --- a/klippy/extras/hall_filament_width_sensor.py +++ b/klippy/extras/hall_filament_width_sensor.py @@ -35,6 +35,14 @@ class HallFilamentWidthSensor: self.is_log =config.getboolean('logging', False) self.enable_flow_compensation = config.getboolean( 'enable_flow_compensation', True) + if self.rawdia1 == self.rawdia2: + raise config.error( + "hall_filament_width_sensor: raw_dia1 and raw_dia2 must be" + " different") + if self.min_diameter <= 0: + raise config.error( + "hall_filament_width_sensor: max_difference must be less than" + " default_nominal_filament_diameter") # Use the current diameter instead of nominal while the first # measurement isn't in place self.use_current_dia_while_delay = config.getboolean(