From f6c9150349d3ffb364a990132a16afa64c890b2e Mon Sep 17 00:00:00 2001 From: john-- Date: Wed, 2 Jan 2019 17:44:02 -0500 Subject: [PATCH] heater: take a gcode_id when setting up a heater (#1028) Allow an arbitrary gcode_id to be supplied during heater setup. This allows future extras to register additional IDs beyond B, and T#. Signed-off-by: John Jardine --- klippy/extras/heater_bed.py | 2 +- klippy/gcode.py | 35 ++++++++++++++++------------------- klippy/heater.py | 16 +++++++++++++--- klippy/kinematics/extruder.py | 9 +++++---- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/klippy/extras/heater_bed.py b/klippy/extras/heater_bed.py index 94040b8c..5e0fd821 100644 --- a/klippy/extras/heater_bed.py +++ b/klippy/extras/heater_bed.py @@ -5,4 +5,4 @@ # This file may be distributed under the terms of the GNU GPLv3 license. def load_config(config): - return config.get_printer().lookup_object('heater').setup_heater(config) + return config.get_printer().lookup_object('heater').setup_heater(config, 'B') diff --git a/klippy/gcode.py b/klippy/gcode.py index dcc88199..2897b664 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -52,7 +52,7 @@ class GCodeParser: # G-Code state self.need_ack = False self.toolhead = self.fan = self.extruder = None - self.heaters = [] + self.heater = None self.speed = 25.0 self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3} def register_command(self, cmd, func, when_not_ready=False, desc=None): @@ -130,6 +130,7 @@ class GCodeParser: self.is_printer_ready = True self.gcode_handlers = self.ready_gcode_handlers # Lookup printer components + self.heater = self.printer.lookup_object('heater') self.toolhead = self.printer.lookup_object('toolhead') if self.move_transform is None: self.move_with_transform = self.toolhead.move @@ -138,8 +139,6 @@ class GCodeParser: if extruders: self.extruder = extruders[0] self.toolhead.set_extruder(self.extruder) - self.heaters = [ e.get_heater() for e in extruders ] - self.heaters.append(self.printer.lookup_object('heater_bed', None)) self.fan = self.printer.lookup_object('fan', None) if self.is_fileinput and self.fd_handle is None: self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) @@ -367,13 +366,11 @@ class GCodeParser: def get_temp(self, eventtime): # Tn:XXX /YYY B:XXX /YYY out = [] - for i, heater in enumerate(self.heaters): - if heater is not None: - cur, target = heater.get_temp(eventtime) - name = "B" - if i < len(self.heaters) - 1: - name = "T%d" % (i,) - out.append("%s:%.1f /%.1f" % (name, cur, target)) + if self.heater is not None: + for heater in self.heater.get_all_heaters(): + if heater is not None: + cur, target = heater.get_temp(eventtime) + out.append("%s:%.1f /%.1f" % (heater.gcode_id, cur, target)) if not out: return "T:0" return " ".join(out) @@ -389,13 +386,12 @@ class GCodeParser: temp = self.get_float('S', params, 0.) heater = None if is_bed: - heater = self.heaters[-1] + heater = self.heater.get_heater_by_gcode_id('B') elif 'T' in params: - index = self.get_int( - 'T', params, minval=0, maxval=len(self.heaters)-2) - heater = self.heaters[index] - elif self.extruder is not None: - heater = self.extruder.get_heater() + index = self.get_int('T', params, minval=0) + heater = self.heater.get_heater_by_gcode_id('T%d' % (index,)) + else: + heater = self.heater.get_heater_by_gcode_id('T0') if heater is None: if temp > 0.: self.respond_error("Heater not configured") @@ -672,9 +668,10 @@ class GCodeParser: if self.is_printer_ready: self.toolhead.motor_off() print_time = self.toolhead.get_last_move_time() - for heater in self.heaters: - if heater is not None: - heater.set_temp(print_time, 0.) + if self.heater is not None: + for heater in self.heater.get_all_heaters(): + if heater is not None: + heater.set_temp(print_time, 0.) if self.fan is not None: self.fan.set_speed(print_time, 0.) self.toolhead.dwell(0.500) diff --git a/klippy/heater.py b/klippy/heater.py index 4a5873da..a3915b67 100644 --- a/klippy/heater.py +++ b/klippy/heater.py @@ -20,9 +20,10 @@ class error(Exception): class Heater: error = error - def __init__(self, config, sensor): + def __init__(self, config, sensor, gcode_id): printer = config.get_printer() self.name = config.get_name() + self.gcode_id = gcode_id # Setup sensor self.sensor = sensor self.min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELCIUS) @@ -220,13 +221,14 @@ class PrinterHeaters: self.printer = config.get_printer() self.sensors = {} self.heaters = {} + self.heaters_gcode_id = {} # Register TURN_OFF_HEATERS command gcode = self.printer.lookup_object('gcode') gcode.register_command("TURN_OFF_HEATERS", self.cmd_TURN_OFF_HEATERS, desc=self.cmd_TURN_OFF_HEATERS_help) def add_sensor(self, sensor_type, sensor_factory): self.sensors[sensor_type] = sensor_factory - def setup_heater(self, config): + def setup_heater(self, config, gcode_id): heater_name = config.get_name() if heater_name == 'extruder': heater_name = 'extruder0' @@ -235,7 +237,8 @@ class PrinterHeaters: # Setup sensor sensor = self.setup_sensor(config) # Create heater - self.heaters[heater_name] = heater = Heater(config, sensor) + self.heaters[heater_name] = heater = Heater(config, sensor, gcode_id) + self.heaters_gcode_id[heater.gcode_id] = heater_name return heater def lookup_heater(self, heater_name): if heater_name == 'extruder': @@ -258,6 +261,13 @@ class PrinterHeaters: print_time = self.printer.lookup_object('toolhead').get_last_move_time() for heater in self.heaters.values(): heater.set_temp(print_time, 0.) + def get_all_heaters(self): + return self.heaters.values() + def get_heater_by_gcode_id(self, gcode_id): + if gcode_id in self.heaters_gcode_id: + heater_name = self.heaters_gcode_id[gcode_id] + return self.heaters[heater_name] + return None def add_printer_objects(config): config.get_printer().add_object('heater', PrinterHeaters(config)) diff --git a/klippy/kinematics/extruder.py b/klippy/kinematics/extruder.py index f3821d05..ec51b4df 100644 --- a/klippy/kinematics/extruder.py +++ b/klippy/kinematics/extruder.py @@ -9,13 +9,14 @@ import stepper, homing, chelper EXTRUDE_DIFF_IGNORE = 1.02 class PrinterExtruder: - def __init__(self, config): + def __init__(self, config, extruder_num): self.printer = config.get_printer() self.name = config.get_name() shared_heater = config.get('shared_heater', None) pheater = self.printer.lookup_object('heater') + gcode_id = 'T%d' % (extruder_num,) if shared_heater is None: - self.heater = pheater.setup_heater(config) + self.heater = pheater.setup_heater(config, gcode_id) else: self.heater = pheater.lookup_heater(shared_heater) self.stepper = stepper.PrinterStepper(config) @@ -237,11 +238,11 @@ def add_printer_objects(config): section = 'extruder%d' % (i,) if not config.has_section(section): if not i and config.has_section('extruder'): - pe = PrinterExtruder(config.getsection('extruder')) + pe = PrinterExtruder(config.getsection('extruder'), 0) printer.add_object('extruder0', pe) continue break - printer.add_object(section, PrinterExtruder(config.getsection(section))) + printer.add_object(section, PrinterExtruder(config.getsection(section), i)) def get_printer_extruders(printer): out = []