Skip to content

Commit

Permalink
adc_temperature: Enhance "ADC out of range" error reports
Browse files Browse the repository at this point in the history
Try to report which ADC is reporting out of range.

Signed-off-by: Kevin O'Connor <[email protected]>
  • Loading branch information
KevinOConnor committed Jun 21, 2024
1 parent d897220 commit 2d73211
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions klippy/extras/adc_temperature.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Obtain temperature using linear interpolation of ADC values
#
# Copyright (C) 2016-2018 Kevin O'Connor <[email protected]>
# Copyright (C) 2016-2024 Kevin O'Connor <[email protected]>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging, bisect
Expand All @@ -22,8 +22,8 @@ def __init__(self, config, adc_convert):
ppins = config.get_printer().lookup_object('pins')
self.mcu_adc = ppins.setup_pin('adc', config.get('sensor_pin'))
self.mcu_adc.setup_adc_callback(REPORT_TIME, self.adc_callback)
query_adc = config.get_printer().load_object(config, 'query_adc')
query_adc.register_adc(config.get_name(), self.mcu_adc)
self.diag_helper = HelperTemperatureDiagnostics(
config, self.mcu_adc, adc_convert.calc_temp)
def setup_callback(self, temperature_callback):
self.temperature_callback = temperature_callback
def get_report_time_delta(self):
Expand All @@ -33,9 +33,43 @@ def adc_callback(self, read_time, read_value):
self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)
def setup_minmax(self, min_temp, max_temp):
arange = [self.adc_convert.calc_adc(t) for t in [min_temp, max_temp]]
min_adc, max_adc = sorted(arange)
self.mcu_adc.setup_adc_sample(SAMPLE_TIME, SAMPLE_COUNT,
minval=min(arange), maxval=max(arange),
minval=min_adc, maxval=max_adc,
range_check_count=RANGE_CHECK_COUNT)
self.diag_helper.setup_diag_minmax(min_temp, max_temp, min_adc, max_adc)

# Tool to register with query_adc and report extra info on ADC range errors
class HelperTemperatureDiagnostics:
def __init__(self, config, mcu_adc, calc_temp_cb):
self.printer = config.get_printer()
self.name = config.get_name()
self.mcu_adc = mcu_adc
self.calc_temp_cb = calc_temp_cb
self.min_temp = self.max_temp = self.min_adc = self.max_adc = None
query_adc = self.printer.load_object(config, 'query_adc')
query_adc.register_adc(self.name, self.mcu_adc)
error_mcu = self.printer.load_object(config, 'error_mcu')
error_mcu.add_clarify("ADC out of range", self._clarify_adc_range)
def setup_diag_minmax(self, min_temp, max_temp, min_adc, max_adc):
self.min_temp, self.max_temp = min_temp, max_temp
self.min_adc, self.max_adc = min_adc, max_adc
def _clarify_adc_range(self, msg, details):
if self.min_temp is None:
return None
last_value, last_read_time = self.mcu_adc.get_last_value()
if not last_read_time:
return None
if last_value >= self.min_adc and last_value <= self.max_adc:
return None
tempstr = "?"
try:
last_temp = self.calc_temp_cb(last_value)
tempstr = "%.3f" % (last_temp,)
except e:
logging.exception("Error in calc_temp callback")
return ("Sensor '%s' temperature %s not in range %.3f:%.3f"
% (self.name, tempstr, self.min_temp, self.max_temp))


######################################################################
Expand Down

1 comment on commit 2d73211

@Tom-Neverwinter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming we have to rebuild Firmware after this change?

Please sign in to comment.