gcode: process_batch() should execute commands atomically

Update the process_batch() method so that it will not interleave
commands read from the input fd with the batched commands.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-10-18 11:51:35 -04:00
parent 37b9a2442f
commit a5e55c2acc
3 changed files with 26 additions and 23 deletions

View File

@ -15,7 +15,7 @@ class IdleTimeout:
self.toolhead = None self.toolhead = None
self.last_timeout = 0. self.last_timeout = 0.
self.idle_timeout = config.getfloat('timeout', 600., above=0.) self.idle_timeout = config.getfloat('timeout', 600., above=0.)
self.idle_script = config.get('gcode', DEFAULT_IDLE_GCODE) self.idle_gcode = config.get('gcode', DEFAULT_IDLE_GCODE).split('\n')
def printer_state(self, state): def printer_state(self, state):
if state == 'ready': if state == 'ready':
self.toolhead = self.printer.lookup_object('toolhead') self.toolhead = self.printer.lookup_object('toolhead')
@ -23,14 +23,14 @@ class IdleTimeout:
reactor.register_timer(self.timeout_handler, reactor.NOW) reactor.register_timer(self.timeout_handler, reactor.NOW)
def run_idle_script(self): def run_idle_script(self):
gcode = self.printer.lookup_object('gcode') gcode = self.printer.lookup_object('gcode')
for line in self.idle_script.split('\n'): try:
try: res = gcode.process_batch(self.idle_gcode)
res = gcode.process_batch(line) except:
except: logging.exception("idle timeout gcode execution")
break return
if not res: if not res:
# Raced with incoming g-code commands # Raced with incoming g-code commands
return return
self.last_timeout = self.toolhead.get_last_move_time() self.last_timeout = self.toolhead.get_last_move_time()
def timeout_handler(self, eventtime): def timeout_handler(self, eventtime):
info = self.toolhead.get_status(eventtime) info = self.toolhead.get_status(eventtime)

View File

@ -164,7 +164,7 @@ class VirtualSD:
continue continue
# Dispatch command # Dispatch command
try: try:
res = self.gcode.process_batch(lines[-1]) res = self.gcode.process_batch([lines[-1]])
if not res: if not res:
self.reactor.pause(self.reactor.monotonic() + 0.100) self.reactor.pause(self.reactor.monotonic() + 0.100)
continue continue

View File

@ -247,16 +247,20 @@ class GCodeParser:
pending_commands = self.pending_commands pending_commands = self.pending_commands
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)
def process_batch(self, command): def process_batch(self, commands):
if self.is_processing_data: if self.is_processing_data:
return False return False
self.is_processing_data = True self.is_processing_data = True
try: try:
self.process_commands([command], need_ack=False) self.process_commands(commands, need_ack=False)
finally: except error as e:
if self.pending_commands: if self.pending_commands:
self.process_pending() self.process_pending()
self.is_processing_data = False self.is_processing_data = False
raise
if self.pending_commands:
self.process_pending()
self.is_processing_data = False
return True return True
def run_script_from_command(self, script): def run_script_from_command(self, script):
prev_need_ack = self.need_ack prev_need_ack = self.need_ack
@ -265,16 +269,15 @@ class GCodeParser:
finally: finally:
self.need_ack = prev_need_ack self.need_ack = prev_need_ack
def run_script(self, script): def run_script(self, script):
curtime = self.reactor.monotonic() commands = script.split('\n')
for line in script.split('\n'): curtime = None
while 1: while 1:
try: res = self.process_batch(commands)
res = self.process_batch(line) if res:
except: break
break if curtime is None:
if res: curtime = self.reactor.monotonic()
break curtime = self.reactor.pause(curtime + 0.100)
curtime = self.reactor.pause(curtime + 0.100)
# Response handling # Response handling
def ack(self, msg=None): def ack(self, msg=None):
if not self.need_ack or self.is_fileinput: if not self.need_ack or self.is_fileinput: