diff --git a/klippy/gcode.py b/klippy/gcode.py index 82e9a303..b71bb747 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -4,7 +4,7 @@ # # This file may be distributed under the terms of the GNU GPLv3 license. import os, re, logging, collections, shlex -import homing, kinematics.extruder +import homing # Parse and handle G-Code commands class GCodeParser: @@ -56,7 +56,7 @@ class GCodeParser: self.move_transform = self.move_with_transform = None self.position_with_transform = (lambda: [0., 0., 0., 0.]) self.need_ack = False - self.toolhead = self.fan = self.extruder = None + self.toolhead = self.fan = None self.heaters = None self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3} def register_command(self, cmd, func, when_not_ready=False, desc=None): @@ -171,10 +171,6 @@ class GCodeParser: if self.move_transform is None: self.move_with_transform = self.toolhead.move self.position_with_transform = self.toolhead.get_position - extruders = kinematics.extruder.get_printer_extruders(self.printer) - if extruders: - self.extruder = extruders[0] - self.toolhead.set_extruder(self.extruder) 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, @@ -410,8 +406,8 @@ class GCodeParser: extruder = self.printer.lookup_object(section, None) if extruder is not None: heater = extruder.get_heater() - elif self.extruder is not None: - heater = self.extruder.get_heater() + else: + heater = self.toolhead.get_extruder().get_heater() if heater is None: if temp > 0.: self.respond_error("Heater not configured") @@ -452,18 +448,25 @@ class GCodeParser: self.respond_info('Unknown command:"%s"' % (cmd,)) def cmd_Tn(self, params): # Select Tool - extruders = kinematics.extruder.get_printer_extruders(self.printer) - index = self.get_int('T', params, minval=0, maxval=len(extruders)-1) - e = extruders[index] - if self.extruder is e: + index = self.get_int('T', params, minval=0) + section = 'extruder' + if index: + section = 'extruder%d' % (index,) + new_extruder = self.printer.lookup_object(section, None) + if new_extruder is None: + raise self.error("Unknown extruder %d on Tn command" % (index,)) + old_extruder = self.toolhead.get_extruder() + if old_extruder is new_extruder: return - self.run_script_from_command(self.extruder.get_activate_gcode(False)) - self.toolhead.set_extruder(e) - self.extruder = e + self.run_script_from_command(old_extruder.get_activate_gcode(False)) + print_time = self.toolhead.get_last_move_time() + old_extruder.set_active(print_time, False) + extrude_pos = new_extruder.set_active(print_time, True) + self.toolhead.set_extruder(new_extruder, extrude_pos) self.reset_last_position() self.extrude_factor = 1. self.base_position[3] = self.last_position[3] - self.run_script_from_command(self.extruder.get_activate_gcode(True)) + self.run_script_from_command(new_extruder.get_activate_gcode(True)) def _cmd_mux(self, params): key, values = self.mux_commands[params['#command']] if None in values: diff --git a/klippy/kinematics/extruder.py b/klippy/kinematics/extruder.py index 2e7ae177..c2276d26 100644 --- a/klippy/kinematics/extruder.py +++ b/klippy/kinematics/extruder.py @@ -60,9 +60,10 @@ class PrinterExtruder: self.stepper.set_trapq(self.trapq) toolhead.register_step_generator(self.stepper.generate_steps) toolhead.register_step_generator(self._free_moves) - # Setup SET_PRESSURE_ADVANCE command + # Register commands gcode = self.printer.lookup_object('gcode') if self.name == 'extruder': + toolhead.set_extruder(self, self.extrude_pos) gcode.register_mux_command("SET_PRESSURE_ADVANCE", "EXTRUDER", None, self.cmd_default_SET_PRESSURE_ADVANCE, desc=self.cmd_SET_PRESSURE_ADVANCE_help) @@ -236,6 +237,8 @@ class DummyExtruder: return move.max_cruise_v2 def lookahead(self, moves, flush_count, lazy): return flush_count + def get_heater(self): + raise homing.CommandError("Extruder not configured") def add_printer_objects(config): printer = config.get_printer() @@ -247,15 +250,3 @@ def add_printer_objects(config): break pe = PrinterExtruder(config.getsection(section), i) printer.add_object(section, pe) - -def get_printer_extruders(printer): - out = [] - for i in range(99): - section = 'extruder' - if i: - section = 'extruder%d' % (i,) - extruder = printer.lookup_object(section, None) - if extruder is None: - break - out.append(extruder) - return out diff --git a/klippy/toolhead.py b/klippy/toolhead.py index 521475d2..a2651fea 100644 --- a/klippy/toolhead.py +++ b/klippy/toolhead.py @@ -410,10 +410,7 @@ class ToolHead: if not self.can_pause: break eventtime = self.reactor.pause(eventtime + 0.100) - def set_extruder(self, extruder): - last_move_time = self.get_last_move_time() - self.extruder.set_active(last_move_time, False) - extrude_pos = extruder.set_active(last_move_time, True) + def set_extruder(self, extruder, extrude_pos): self.extruder = extruder self.move_queue.set_extruder(extruder) self.commanded_pos[3] = extrude_pos