From c06618193d3c58521e45a0c2241278268664030e Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 24 Nov 2019 19:43:50 -0500 Subject: [PATCH] extruder: Add an ACTIVATE_EXTRUDER command Add support for an extended g-code command to change the currently active extruder. Signed-off-by: Kevin O'Connor --- docs/G-Codes.md | 3 +++ klippy/gcode.py | 6 ++++++ klippy/kinematics/extruder.py | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/docs/G-Codes.md b/docs/G-Codes.md index 029c9ae0..a7d1adcd 100644 --- a/docs/G-Codes.md +++ b/docs/G-Codes.md @@ -153,6 +153,9 @@ The following standard commands are supported: - `SET_HEATER_TEMPERATURE HEATER= [TARGET=]`: Sets the target temperature for a heater. If a target temperature is not supplied, the target is 0. +- `ACTIVATE_EXTRUDER EXTRUDER=`: In a printer with + multiple extruders this command is used to change the active + extruder. - `SET_PRESSURE_ADVANCE [EXTRUDER=] [ADVANCE=] [SMOOTH_TIME=]`: Set pressure advance parameters. If EXTRUDER is not specified, it defaults to the active diff --git a/klippy/gcode.py b/klippy/gcode.py index 93e97ec7..9ac7ddda 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -17,6 +17,8 @@ class GCodeParser: printer.register_event_handler("klippy:shutdown", self._handle_shutdown) printer.register_event_handler("klippy:disconnect", self._handle_disconnect) + printer.register_event_handler("extruder:activate_extruder", + self._handle_activate_extruder) # Input handling self.reactor = printer.get_reactor() self.is_processing_data = False @@ -175,6 +177,10 @@ class GCodeParser: self.fd_handle = self.reactor.register_fd(self.fd, self._process_data) self._respond_state("Ready") + def _handle_activate_extruder(self): + self.reset_last_position() + self.extrude_factor = 1. + self.base_position[3] = self.last_position[3] def reset_last_position(self): self.last_position = self.position_with_transform() def _dump_debug(self): diff --git a/klippy/kinematics/extruder.py b/klippy/kinematics/extruder.py index b55e6f0e..78e8dc10 100644 --- a/klippy/kinematics/extruder.py +++ b/klippy/kinematics/extruder.py @@ -73,6 +73,9 @@ class PrinterExtruder: gcode.register_mux_command("SET_PRESSURE_ADVANCE", "EXTRUDER", self.name, self.cmd_SET_PRESSURE_ADVANCE, desc=self.cmd_SET_PRESSURE_ADVANCE_help) + gcode.register_mux_command("ACTIVATE_EXTRUDER", "EXTRUDER", + self.name, self.cmd_ACTIVATE_EXTRUDER, + desc=self.cmd_ACTIVATE_EXTRUDER_help) def update_move_time(self, flush_time): self.trapq_free_moves(self.trapq, flush_time) def _set_pressure_advance(self, pressure_advance, smooth_time): @@ -168,6 +171,16 @@ class PrinterExtruder: pressure_advance, smooth_time)) self.printer.set_rollover_info(self.name, "%s: %s" % (self.name, msg)) gcode.respond_info(msg, log=False) + cmd_ACTIVATE_EXTRUDER_help = "Change the active extruder" + def cmd_ACTIVATE_EXTRUDER(self, params): + gcode = self.printer.lookup_object('gcode') + toolhead = self.printer.lookup_object('toolhead') + if toolhead.get_extruder() is self: + gcode.respond_info("Extruder %s already active" % (self.name)) + return + gcode.respond_info("Activating extruder %s" % (self.name)) + toolhead.set_extruder(self, self.extrude_pos) + self.printer.send_event("extruder:activate_extruder") # Dummy extruder class used when a printer has no extruder at all class DummyExtruder: