heaters: Make heater.py an "extras" module

The heater logic is an independent module that does not need to be
treated as part of the "core" klipper code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-04-25 13:27:41 -04:00
parent d858498a53
commit 17123889f6
16 changed files with 50 additions and 45 deletions

View File

@ -273,7 +273,7 @@ PT1000 = [
def load_config(config): def load_config(config):
# Register default sensors # Register default sensors
pheater = config.get_printer().lookup_object("heater") pheaters = config.get_printer().try_load_module(config, "heaters")
for sensor_type, params in [("AD595", AD595), for sensor_type, params in [("AD595", AD595),
("AD8494", AD8494), ("AD8494", AD8494),
("AD8495", AD8495), ("AD8495", AD8495),
@ -282,17 +282,17 @@ def load_config(config):
("PT100 INA826", PT100)]: ("PT100 INA826", PT100)]:
func = (lambda config, params=params: func = (lambda config, params=params:
PrinterADCtoTemperature(config, LinearVoltage(config, params))) PrinterADCtoTemperature(config, LinearVoltage(config, params)))
pheater.add_sensor_factory(sensor_type, func) pheaters.add_sensor_factory(sensor_type, func)
for sensor_type, params in [("PT1000", PT1000)]: for sensor_type, params in [("PT1000", PT1000)]:
func = (lambda config, params=params: func = (lambda config, params=params:
PrinterADCtoTemperature(config, PrinterADCtoTemperature(config,
LinearResistance(config, params))) LinearResistance(config, params)))
pheater.add_sensor_factory(sensor_type, func) pheaters.add_sensor_factory(sensor_type, func)
def load_config_prefix(config): def load_config_prefix(config):
if config.get("resistance1", None) is None: if config.get("resistance1", None) is None:
custom_sensor = CustomLinearVoltage(config) custom_sensor = CustomLinearVoltage(config)
else: else:
custom_sensor = CustomLinearResistance(config) custom_sensor = CustomLinearResistance(config)
pheater = config.get_printer().lookup_object("heater") pheaters = config.get_printer().try_load_module(config, "heaters")
pheater.add_sensor_factory(custom_sensor.name, custom_sensor.create) pheaters.add_sensor_factory(custom_sensor.name, custom_sensor.create)

View File

@ -198,5 +198,5 @@ class BME280:
def load_config(config): def load_config(config):
# Register sensor # Register sensor
pheater = config.get_printer().lookup_object("heater") pheaters = config.get_printer().try_load_module(config, "heaters")
pheater.add_sensor_factory("BME280", BME280) pheaters.add_sensor_factory("BME280", BME280)

View File

@ -14,6 +14,7 @@ class ControllerFan:
self.stepper_names = [] self.stepper_names = []
self.stepper_enable = self.printer.try_load_module(config, self.stepper_enable = self.printer.try_load_module(config,
'stepper_enable') 'stepper_enable')
self.printer.try_load_module(config, 'heaters')
self.heaters = [] self.heaters = []
self.fan = fan.PrinterFan(config) self.fan = fan.PrinterFan(config)
self.mcu = self.fan.mcu_fan.get_mcu() self.mcu = self.fan.mcu_fan.get_mcu()
@ -27,8 +28,8 @@ class ControllerFan:
self.heater_name = config.get("heater", "extruder") self.heater_name = config.get("heater", "extruder")
self.last_on = self.idle_timeout self.last_on = self.idle_timeout
def handle_ready(self): def handle_ready(self):
pheater = self.printer.lookup_object('heater') pheaters = self.printer.lookup_object('heaters')
self.heaters = [pheater.lookup_heater(n.strip()) self.heaters = [pheaters.lookup_heater(n.strip())
for n in self.heater_name.split(',')] for n in self.heater_name.split(',')]
kin = self.printer.lookup_object('toolhead').get_kinematics() kin = self.printer.lookup_object('toolhead').get_kinematics()
self.stepper_names = [s.get_name() for s in kin.get_steppers()] self.stepper_names = [s.get_name() for s in kin.get_steppers()]

View File

@ -7,8 +7,8 @@
class PrinterHeaterBed: class PrinterHeaterBed:
def __init__(self, config): def __init__(self, config):
self.printer = config.get_printer() self.printer = config.get_printer()
pheater = self.printer.lookup_object('heater') pheaters = self.printer.try_load_module(config, 'heaters')
self.heater = pheater.setup_heater(config, 'B') self.heater = pheaters.setup_heater(config, 'B')
self.get_status = self.heater.get_status self.get_status = self.heater.get_status
self.stats = self.heater.stats self.stats = self.heater.stats
# Register commands # Register commands
@ -21,8 +21,8 @@ class PrinterHeaterBed:
temp = gcode.get_float('S', params, 0.) temp = gcode.get_float('S', params, 0.)
self.heater.set_temp(temp) self.heater.set_temp(temp)
if wait and temp: if wait and temp:
pheater = self.printer.lookup_object('heater') pheaters = self.printer.lookup_object('heaters')
pheater.wait_for_temperature(self.heater) pheaters.wait_for_temperature(self.heater)
def cmd_M190(self, params): def cmd_M190(self, params):
# Set Bed Temperature and Wait # Set Bed Temperature and Wait
self.cmd_M140(params, wait=True) self.cmd_M140(params, wait=True)

View File

@ -10,6 +10,7 @@ PIN_MIN_TIME = 0.100
class PrinterHeaterFan: class PrinterHeaterFan:
def __init__(self, config): def __init__(self, config):
self.printer = config.get_printer() self.printer = config.get_printer()
self.printer.try_load_module(config, 'heaters')
self.printer.register_event_handler("klippy:ready", self.handle_ready) self.printer.register_event_handler("klippy:ready", self.handle_ready)
self.heater_name = config.get("heater", "extruder") self.heater_name = config.get("heater", "extruder")
self.heater_temp = config.getfloat("heater_temp", 50.0) self.heater_temp = config.getfloat("heater_temp", 50.0)
@ -18,8 +19,8 @@ class PrinterHeaterFan:
self.mcu = self.fan.mcu_fan.get_mcu() self.mcu = self.fan.mcu_fan.get_mcu()
self.fan_speed = config.getfloat("fan_speed", 1., minval=0., maxval=1.) self.fan_speed = config.getfloat("fan_speed", 1., minval=0., maxval=1.)
def handle_ready(self): def handle_ready(self):
pheater = self.printer.lookup_object('heater') pheaters = self.printer.lookup_object('heaters')
self.heaters = [pheater.lookup_heater(n.strip()) self.heaters = [pheaters.lookup_heater(n.strip())
for n in self.heater_name.split(',')] for n in self.heater_name.split(',')]
reactor = self.printer.get_reactor() reactor = self.printer.get_reactor()
reactor.register_timer(self.callback, reactor.NOW) reactor.register_timer(self.callback, reactor.NOW)

View File

@ -5,4 +5,5 @@
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
def load_config_prefix(config): def load_config_prefix(config):
return config.get_printer().lookup_object('heater').setup_heater(config) pheaters = config.get_printer().try_load_module(config, 'heaters')
return pheaters.setup_heater(config)

View File

@ -1,6 +1,6 @@
# Printer heater support # Tracking of PWM controlled heaters and their temperature control
# #
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import logging, threading import logging, threading
@ -323,5 +323,5 @@ class PrinterHeaters:
gcode.respond_raw(self._get_temp(eventtime)) gcode.respond_raw(self._get_temp(eventtime))
eventtime = reactor.pause(eventtime + 1.) eventtime = reactor.pause(eventtime + 1.)
def add_printer_objects(config): def load_config(config):
config.get_printer().add_object('heater', PrinterHeaters(config)) return PrinterHeaters(config)

View File

@ -19,12 +19,12 @@ class HomingHeaters:
self.disable_heaters = [] self.disable_heaters = []
self.steppers_needing_quiet = config.get("steppers", "") self.steppers_needing_quiet = config.get("steppers", "")
self.flaky_steppers = [] self.flaky_steppers = []
self.pheater = self.printer.lookup_object('heater') self.pheaters = self.printer.try_load_module(config, 'heaters')
self.target_save = {} self.target_save = {}
def handle_connect(self): def handle_connect(self):
# heaters to disable # heaters to disable
all_heaters = self.pheater.get_all_heaters() all_heaters = self.pheaters.get_all_heaters()
self.disable_heaters = [n.strip() self.disable_heaters = [n.strip()
for n in self.heaters_to_disable.split(',')] for n in self.heaters_to_disable.split(',')]
if self.disable_heaters == [""]: if self.disable_heaters == [""]:
@ -56,14 +56,14 @@ class HomingHeaters:
if not self.check_eligible(endstops): if not self.check_eligible(endstops):
return return
for heater_name in self.disable_heaters: for heater_name in self.disable_heaters:
heater = self.pheater.lookup_heater(heater_name) heater = self.pheaters.lookup_heater(heater_name)
self.target_save[heater_name] = heater.get_temp(0)[1] self.target_save[heater_name] = heater.get_temp(0)[1]
heater.set_temp(0.) heater.set_temp(0.)
def handle_homing_move_end(self, endstops): def handle_homing_move_end(self, endstops):
if not self.check_eligible(endstops): if not self.check_eligible(endstops):
return return
for heater_name in self.disable_heaters: for heater_name in self.disable_heaters:
heater = self.pheater.lookup_heater(heater_name) heater = self.pheaters.lookup_heater(heater_name)
heater.set_temp(self.target_save[heater_name]) heater.set_temp(self.target_save[heater_name])
def load_config(config): def load_config(config):

View File

@ -18,9 +18,9 @@ class PIDCalibrate:
heater_name = self.gcode.get_str('HEATER', params) heater_name = self.gcode.get_str('HEATER', params)
target = self.gcode.get_float('TARGET', params) target = self.gcode.get_float('TARGET', params)
write_file = self.gcode.get_int('WRITE_FILE', params, 0) write_file = self.gcode.get_int('WRITE_FILE', params, 0)
pheater = self.printer.lookup_object('heater') pheaters = self.printer.lookup_object('heaters')
try: try:
heater = pheater.lookup_heater(heater_name) heater = pheaters.lookup_heater(heater_name)
except self.printer.config_error as e: except self.printer.config_error as e:
raise self.gcode.error(str(e)) raise self.gcode.error(str(e))
self.printer.lookup_object('toolhead').get_last_move_time() self.printer.lookup_object('toolhead').get_last_move_time()
@ -31,7 +31,7 @@ class PIDCalibrate:
except self.printer.command_error as e: except self.printer.command_error as e:
heater.set_control(old_control) heater.set_control(old_control)
raise raise
pheater.wait_for_temperature(heater) pheaters.wait_for_temperature(heater)
heater.set_control(old_control) heater.set_control(old_control)
if write_file: if write_file:
calibrate.write_file('/tmp/heattest.txt') calibrate.write_file('/tmp/heattest.txt')

View File

@ -336,6 +336,6 @@ Sensors = {
def load_config(config): def load_config(config):
# Register sensors # Register sensors
pheater = config.get_printer().lookup_object("heater") pheaters = config.get_printer().try_load_module(config, "heaters")
for name, klass in Sensors.items(): for name, klass in Sensors.items():
pheater.add_sensor_factory(name, klass) pheaters.add_sensor_factory(name, klass)

View File

@ -18,10 +18,11 @@ class TemperatureFan:
self.gcode = self.printer.lookup_object('gcode') self.gcode = self.printer.lookup_object('gcode')
self.min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELSIUS) self.min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELSIUS)
self.max_temp = config.getfloat('max_temp', above=self.min_temp) self.max_temp = config.getfloat('max_temp', above=self.min_temp)
self.sensor = self.printer.lookup_object('heater').setup_sensor(config) pheaters = self.printer.try_load_module(config, 'heaters')
self.sensor = pheaters.setup_sensor(config)
self.sensor.setup_minmax(self.min_temp, self.max_temp) self.sensor.setup_minmax(self.min_temp, self.max_temp)
self.sensor.setup_callback(self.temperature_callback) self.sensor.setup_callback(self.temperature_callback)
self.printer.lookup_object('heater').register_sensor(config, self) pheaters.register_sensor(config, self)
self.speed_delay = self.sensor.get_report_time_delta() self.speed_delay = self.sensor.get_report_time_delta()
self.max_speed = config.getfloat('max_speed', 1., above=0., maxval=1.) self.max_speed = config.getfloat('max_speed', 1., above=0., maxval=1.)
self.min_speed = config.getfloat('min_speed', 0.3, minval=0., maxval=1.) self.min_speed = config.getfloat('min_speed', 0.3, minval=0., maxval=1.)

View File

@ -9,14 +9,15 @@ KELVIN_TO_CELSIUS = -273.15
class PrinterSensorGeneric: class PrinterSensorGeneric:
def __init__(self, config): def __init__(self, config):
self.printer = config.get_printer() self.printer = config.get_printer()
self.sensor = self.printer.lookup_object('heater').setup_sensor(config) pheaters = self.printer.try_load_module(config, 'heaters')
self.sensor = pheaters.setup_sensor(config)
self.min_temp = config.getfloat('min_temp', KELVIN_TO_CELSIUS, self.min_temp = config.getfloat('min_temp', KELVIN_TO_CELSIUS,
minval=KELVIN_TO_CELSIUS) minval=KELVIN_TO_CELSIUS)
self.max_temp = config.getfloat('max_temp', 99999999.9, self.max_temp = config.getfloat('max_temp', 99999999.9,
above=self.min_temp) above=self.min_temp)
self.sensor.setup_minmax(self.min_temp, self.max_temp) self.sensor.setup_minmax(self.min_temp, self.max_temp)
self.sensor.setup_callback(self.temperature_callback) self.sensor.setup_callback(self.temperature_callback)
self.printer.lookup_object('heater').register_sensor(config, self) pheaters.register_sensor(config, self)
self.last_temp = 0. self.last_temp = 0.
def temperature_callback(self, read_time, temp): def temperature_callback(self, read_time, temp):
self.last_temp = temp self.last_temp = temp

View File

@ -116,12 +116,12 @@ Sensors = {
def load_config(config): def load_config(config):
# Register default thermistor types # Register default thermistor types
pheater = config.get_printer().lookup_object("heater") pheaters = config.get_printer().try_load_module(config, "heaters")
for sensor_type, params in Sensors.items(): for sensor_type, params in Sensors.items():
func = (lambda config, params=params: PrinterThermistor(config, params)) func = (lambda config, params=params: PrinterThermistor(config, params))
pheater.add_sensor_factory(sensor_type, func) pheaters.add_sensor_factory(sensor_type, func)
def load_config_prefix(config): def load_config_prefix(config):
thermistor = CustomThermistor(config) thermistor = CustomThermistor(config)
pheater = config.get_printer().lookup_object("heater") pheaters = config.get_printer().try_load_module(config, "heaters")
pheater.add_sensor_factory(thermistor.name, thermistor.create) pheaters.add_sensor_factory(thermistor.name, thermistor.create)

View File

@ -35,8 +35,8 @@ class HeaterCheck:
if self.printer.get_start_args().get('debugoutput') is not None: if self.printer.get_start_args().get('debugoutput') is not None:
# Disable verify_heater if outputting to a debug file # Disable verify_heater if outputting to a debug file
return return
pheater = self.printer.lookup_object('heater') pheaters = self.printer.lookup_object('heaters')
self.heater = pheater.lookup_heater(self.heater_name) self.heater = pheaters.lookup_heater(self.heater_name)
logging.info("Starting heater checks for %s", self.heater_name) logging.info("Starting heater checks for %s", self.heater_name)
reactor = self.printer.get_reactor() reactor = self.printer.get_reactor()
self.check_timer = reactor.register_timer(self.check_event, reactor.NOW) self.check_timer = reactor.register_timer(self.check_event, reactor.NOW)

View File

@ -11,12 +11,12 @@ class PrinterExtruder:
self.printer = config.get_printer() self.printer = config.get_printer()
self.name = config.get_name() self.name = config.get_name()
shared_heater = config.get('shared_heater', None) shared_heater = config.get('shared_heater', None)
pheater = self.printer.lookup_object('heater') pheaters = self.printer.try_load_module(config, 'heaters')
gcode_id = 'T%d' % (extruder_num,) gcode_id = 'T%d' % (extruder_num,)
if shared_heater is None: if shared_heater is None:
self.heater = pheater.setup_heater(config, gcode_id) self.heater = pheaters.setup_heater(config, gcode_id)
else: else:
self.heater = pheater.lookup_heater(shared_heater) self.heater = pheaters.lookup_heater(shared_heater)
self.stepper = stepper.PrinterStepper(config) self.stepper = stepper.PrinterStepper(config)
self.nozzle_diameter = config.getfloat('nozzle_diameter', above=0.) self.nozzle_diameter = config.getfloat('nozzle_diameter', above=0.)
filament_diameter = config.getfloat( filament_diameter = config.getfloat(
@ -167,7 +167,7 @@ class PrinterExtruder:
heater = extruder.get_heater() heater = extruder.get_heater()
heater.set_temp(temp) heater.set_temp(temp)
if wait and temp: if wait and temp:
self.printer.lookup_object('heater').wait_for_temperature(heater) self.printer.lookup_object('heaters').wait_for_temperature(heater)
def cmd_M109(self, params): def cmd_M109(self, params):
# Set Extruder Temperature and Wait # Set Extruder Temperature and Wait
self.cmd_M104(params, wait=True) self.cmd_M104(params, wait=True)

View File

@ -6,7 +6,7 @@
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import sys, os, optparse, logging, time, threading, collections, importlib import sys, os, optparse, logging, time, threading, collections, importlib
import util, reactor, queuelogger, msgproto, homing import util, reactor, queuelogger, msgproto, homing
import gcode, configfile, pins, heater, mcu, toolhead import gcode, configfile, pins, mcu, toolhead
message_ready = "Printer is ready" message_ready = "Printer is ready"
@ -123,7 +123,7 @@ class Printer:
if self.bglogger is not None: if self.bglogger is not None:
pconfig.log_config(config) pconfig.log_config(config)
# Create printer components # Create printer components
for m in [pins, heater, mcu]: for m in [pins, mcu]:
m.add_printer_objects(config) m.add_printer_objects(config)
for section_config in config.get_prefix_sections(''): for section_config in config.get_prefix_sections(''):
self.try_load_module(config, section_config.get_name()) self.try_load_module(config, section_config.get_name())