gcode: Support running arbitrary gcode on extruder change

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-05-19 21:05:46 -04:00
parent 72dc21fb1a
commit d093200966
3 changed files with 32 additions and 9 deletions

View File

@ -150,6 +150,18 @@ filament_diameter: 3.500
# otherwise cause retraction followed immediately by pressure # otherwise cause retraction followed immediately by pressure
# buildup. This setting only applies if pressure_advance is # buildup. This setting only applies if pressure_advance is
# non-zero. The default is 0.010 (10 milliseconds). # 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. # The remaining variables describe the extruder heater.
heater_pin: ar4 heater_pin: ar4

View File

@ -24,6 +24,8 @@ class PrinterExtruder:
self.max_e_dist = config.getfloat( self.max_e_dist = config.getfloat(
'max_extrude_only_distance', 50., minval=0.) 'max_extrude_only_distance', 50., minval=0.)
self.max_e_velocity = self.max_e_accel = None 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( self.pressure_advance = config.getfloat(
'pressure_advance', 0., minval=0.) 'pressure_advance', 0., minval=0.)
self.pressure_advance_lookahead_time = 0. self.pressure_advance_lookahead_time = 0.
@ -44,6 +46,10 @@ class PrinterExtruder:
return self.heater return self.heater
def set_active(self, print_time, is_active): def set_active(self, print_time, is_active):
return self.extrude_pos 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): def motor_off(self, move_time):
self.stepper.motor_enable(move_time, 0) self.stepper.motor_enable(move_time, 0)
self.need_motor_enable = True self.need_motor_enable = True

View File

@ -19,7 +19,7 @@ class GCodeParser:
self.fd_handle = None self.fd_handle = None
if not is_fileinput: if not is_fileinput:
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
self.input_commands = [""] self.partial_input = ""
self.bytes_read = 0 self.bytes_read = 0
self.input_log = collections.deque([], 50) self.input_log = collections.deque([], 50)
# Command handling # Command handling
@ -82,9 +82,9 @@ class GCodeParser:
logging.info("Read %f: %s" % (eventtime, repr(data))) logging.info("Read %f: %s" % (eventtime, repr(data)))
# Parse input into commands # Parse input into commands
args_r = re.compile('([a-zA-Z_]+|[a-zA-Z*])') args_r = re.compile('([a-zA-Z_]+|[a-zA-Z*])')
def process_commands(self, eventtime): def process_commands(self, commands, need_ack=True):
while len(self.input_commands) > 1: prev_need_ack = self.need_ack
line = self.input_commands.pop(0) for line in commands:
# Ignore comments and leading/trailing spaces # Ignore comments and leading/trailing spaces
line = origline = line.strip() line = origline = line.strip()
cpos = line.find(';') cpos = line.find(';')
@ -103,7 +103,7 @@ class GCodeParser:
continue continue
params['#command'] = cmd = parts[0].upper() + parts[1].strip() params['#command'] = cmd = parts[0].upper() + parts[1].strip()
# Invoke handler for command # Invoke handler for command
self.need_ack = True self.need_ack = need_ack
handler = self.gcode_handlers.get(cmd, self.cmd_default) handler = self.gcode_handlers.get(cmd, self.cmd_default)
try: try:
handler(params) handler(params)
@ -117,15 +117,16 @@ class GCodeParser:
self.printer.request_exit('exit_eof') self.printer.request_exit('exit_eof')
break break
self.ack() self.ack()
self.need_ack = prev_need_ack
def process_data(self, eventtime): def process_data(self, eventtime):
data = os.read(self.fd, 4096) data = os.read(self.fd, 4096)
self.input_log.append((eventtime, data)) self.input_log.append((eventtime, data))
self.bytes_read += len(data) self.bytes_read += len(data)
lines = data.split('\n') lines = data.split('\n')
lines[0] = self.input_commands.pop() + lines[0] lines[0] = self.partial_input + lines[0]
self.input_commands.extend(lines) self.partial_input = lines.pop()
if self.is_processing_data: if self.is_processing_data:
if len(lines) <= 1: if not lines:
return return
if not self.is_fileinput and lines[0].strip().upper() == 'M112': if not self.is_fileinput and lines[0].strip().upper() == 'M112':
self.cmd_M112({}) self.cmd_M112({})
@ -133,7 +134,7 @@ class GCodeParser:
self.fd_handle = None self.fd_handle = None
return return
self.is_processing_data = True self.is_processing_data = True
self.process_commands(eventtime) self.process_commands(lines)
self.is_processing_data = False self.is_processing_data = False
if self.fd_handle is None: if self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
@ -259,6 +260,8 @@ class GCodeParser:
e = extruders[index] e = extruders[index]
if self.extruder is e: if self.extruder is e:
return return
deactivate_gcode = self.extruder.get_activate_gcode(False)
self.process_commands(deactivate_gcode.split('\n'), need_ack=False)
try: try:
self.toolhead.set_extruder(e) self.toolhead.set_extruder(e)
except homing.EndstopError, e: except homing.EndstopError, e:
@ -266,6 +269,8 @@ class GCodeParser:
return return
self.extruder = e self.extruder = e
self.last_position = self.toolhead.get_position() 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 = [ all_handlers = [
'G1', 'G4', 'G20', 'G28', 'G90', 'G91', 'G92', 'G1', 'G4', 'G20', 'G28', 'G90', 'G91', 'G92',
'M82', 'M83', 'M18', 'M105', 'M104', 'M109', 'M112', 'M114', 'M115', 'M82', 'M83', 'M18', 'M105', 'M104', 'M109', 'M112', 'M114', 'M115',