From d093200966912ca043b31b95681678a24fdc08b6 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 19 May 2017 21:05:46 -0400 Subject: [PATCH] gcode: Support running arbitrary gcode on extruder change Signed-off-by: Kevin O'Connor --- config/example.cfg | 12 ++++++++++++ klippy/extruder.py | 6 ++++++ klippy/gcode.py | 23 ++++++++++++++--------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/config/example.cfg b/config/example.cfg index d6b9c0ce..2bba823e 100644 --- a/config/example.cfg +++ b/config/example.cfg @@ -150,6 +150,18 @@ filament_diameter: 3.500 # otherwise cause retraction followed immediately by pressure # buildup. This setting only applies if pressure_advance is # non-zero. The default is 0.010 (10 milliseconds). +#deactivate_gcode: +# A list of G-Code commands (one per line) to execute on a G-Code +# tool change command (eg, "T1") that deactivates this extruder and +# activates some other extruder. It only makes sense to define this +# section on multi-extruder printers. The default is to not run any +# special G-Code commands on deactivation. +#activate_gcode: +# A list of G-Code commands (one per line) to execute on a G-Code +# tool change command (eg, "T0") that activates this extruder. It +# only makes sense to define this section on multi-extruder +# printers. The default is to not run any special G-Code commands on +# activation. # # The remaining variables describe the extruder heater. heater_pin: ar4 diff --git a/klippy/extruder.py b/klippy/extruder.py index 6ce4fdfd..ce8f3366 100644 --- a/klippy/extruder.py +++ b/klippy/extruder.py @@ -24,6 +24,8 @@ class PrinterExtruder: self.max_e_dist = config.getfloat( 'max_extrude_only_distance', 50., minval=0.) self.max_e_velocity = self.max_e_accel = None + self.activate_gcode = config.get('activate_gcode', '') + self.deactivate_gcode = config.get('deactivate_gcode', '') self.pressure_advance = config.getfloat( 'pressure_advance', 0., minval=0.) self.pressure_advance_lookahead_time = 0. @@ -44,6 +46,10 @@ class PrinterExtruder: return self.heater def set_active(self, print_time, is_active): return self.extrude_pos + def get_activate_gcode(self, is_active): + if is_active: + return self.activate_gcode + return self.deactivate_gcode def motor_off(self, move_time): self.stepper.motor_enable(move_time, 0) self.need_motor_enable = True diff --git a/klippy/gcode.py b/klippy/gcode.py index 99eae3f2..6709e26f 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -19,7 +19,7 @@ class GCodeParser: self.fd_handle = None if not is_fileinput: self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) - self.input_commands = [""] + self.partial_input = "" self.bytes_read = 0 self.input_log = collections.deque([], 50) # Command handling @@ -82,9 +82,9 @@ class GCodeParser: logging.info("Read %f: %s" % (eventtime, repr(data))) # Parse input into commands args_r = re.compile('([a-zA-Z_]+|[a-zA-Z*])') - def process_commands(self, eventtime): - while len(self.input_commands) > 1: - line = self.input_commands.pop(0) + def process_commands(self, commands, need_ack=True): + prev_need_ack = self.need_ack + for line in commands: # Ignore comments and leading/trailing spaces line = origline = line.strip() cpos = line.find(';') @@ -103,7 +103,7 @@ class GCodeParser: continue params['#command'] = cmd = parts[0].upper() + parts[1].strip() # Invoke handler for command - self.need_ack = True + self.need_ack = need_ack handler = self.gcode_handlers.get(cmd, self.cmd_default) try: handler(params) @@ -117,15 +117,16 @@ class GCodeParser: self.printer.request_exit('exit_eof') break self.ack() + self.need_ack = prev_need_ack def process_data(self, eventtime): data = os.read(self.fd, 4096) self.input_log.append((eventtime, data)) self.bytes_read += len(data) lines = data.split('\n') - lines[0] = self.input_commands.pop() + lines[0] - self.input_commands.extend(lines) + lines[0] = self.partial_input + lines[0] + self.partial_input = lines.pop() if self.is_processing_data: - if len(lines) <= 1: + if not lines: return if not self.is_fileinput and lines[0].strip().upper() == 'M112': self.cmd_M112({}) @@ -133,7 +134,7 @@ class GCodeParser: self.fd_handle = None return self.is_processing_data = True - self.process_commands(eventtime) + self.process_commands(lines) self.is_processing_data = False if self.fd_handle is None: self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) @@ -259,6 +260,8 @@ class GCodeParser: e = extruders[index] if self.extruder is e: return + deactivate_gcode = self.extruder.get_activate_gcode(False) + self.process_commands(deactivate_gcode.split('\n'), need_ack=False) try: self.toolhead.set_extruder(e) except homing.EndstopError, e: @@ -266,6 +269,8 @@ class GCodeParser: return self.extruder = e self.last_position = self.toolhead.get_position() + activate_gcode = self.extruder.get_activate_gcode(True) + self.process_commands(activate_gcode.split('\n'), need_ack=False) all_handlers = [ 'G1', 'G4', 'G20', 'G28', 'G90', 'G91', 'G92', 'M82', 'M83', 'M18', 'M105', 'M104', 'M109', 'M112', 'M114', 'M115',