idle_timeout: Add printing/ready/idle tracking
Internally track the overall printer state. Generate events on state transitions. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
a5e55c2acc
commit
d25e02144c
|
@ -9,41 +9,85 @@ TURN_OFF_HEATERS
|
||||||
M84
|
M84
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
PIN_MIN_TIME = 0.100
|
||||||
|
READY_TIMEOUT = .500
|
||||||
|
|
||||||
class IdleTimeout:
|
class IdleTimeout:
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.printer = config.get_printer()
|
self.printer = config.get_printer()
|
||||||
self.toolhead = None
|
self.reactor = self.printer.get_reactor()
|
||||||
self.last_timeout = 0.
|
self.gcode = self.printer.lookup_object('gcode')
|
||||||
|
self.toolhead = self.timeout_timer = None
|
||||||
|
self.state = "Idle"
|
||||||
self.idle_timeout = config.getfloat('timeout', 600., above=0.)
|
self.idle_timeout = config.getfloat('timeout', 600., above=0.)
|
||||||
self.idle_gcode = config.get('gcode', DEFAULT_IDLE_GCODE).split('\n')
|
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')
|
||||||
reactor = self.printer.get_reactor()
|
self.timeout_timer = self.reactor.register_timer(
|
||||||
reactor.register_timer(self.timeout_handler, reactor.NOW)
|
self.timeout_handler)
|
||||||
def run_idle_script(self):
|
self.printer.register_event_handler("toolhead:sync_print_time",
|
||||||
gcode = self.printer.lookup_object('gcode')
|
self.handle_sync_print_time)
|
||||||
|
def transition_idle_state(self, eventtime):
|
||||||
|
self.state = "Printing"
|
||||||
try:
|
try:
|
||||||
res = gcode.process_batch(self.idle_gcode)
|
res = self.gcode.process_batch(self.idle_gcode)
|
||||||
except:
|
except:
|
||||||
logging.exception("idle timeout gcode execution")
|
logging.exception("idle timeout gcode execution")
|
||||||
return
|
return eventtime + 1.
|
||||||
if not res:
|
if not res:
|
||||||
# Raced with incoming g-code commands
|
# Raced with incoming g-code commands
|
||||||
return
|
|
||||||
self.last_timeout = self.toolhead.get_last_move_time()
|
|
||||||
def timeout_handler(self, eventtime):
|
|
||||||
info = self.toolhead.get_status(eventtime)
|
|
||||||
print_time = info['print_time']
|
|
||||||
status = info['status']
|
|
||||||
if print_time <= self.last_timeout or status == 'Printing':
|
|
||||||
return eventtime + self.idle_timeout
|
|
||||||
estimated_print_time = info['estimated_print_time']
|
|
||||||
elapsed_time = estimated_print_time - print_time
|
|
||||||
if elapsed_time < self.idle_timeout:
|
|
||||||
return eventtime + self.idle_timeout - elapsed_time
|
|
||||||
self.run_idle_script()
|
|
||||||
return eventtime + 1.
|
return eventtime + 1.
|
||||||
|
print_time = self.toolhead.get_last_move_time()
|
||||||
|
self.state = "Idle"
|
||||||
|
self.printer.send_event("idle_timeout:idle", print_time)
|
||||||
|
return self.reactor.NEVER
|
||||||
|
def check_idle_timeout(self, eventtime):
|
||||||
|
# Make sure toolhead class isn't busy
|
||||||
|
print_time, est_print_time, lookahead_empty = self.toolhead.check_busy(
|
||||||
|
eventtime)
|
||||||
|
idle_time = est_print_time - print_time
|
||||||
|
if not lookahead_empty or idle_time < 1.:
|
||||||
|
# Toolhead is busy
|
||||||
|
return eventtime + self.idle_timeout
|
||||||
|
if idle_time < self.idle_timeout:
|
||||||
|
# Wait for idle timeout
|
||||||
|
return eventtime + self.idle_timeout - idle_time
|
||||||
|
if not self.gcode.process_batch([]):
|
||||||
|
# Gcode class busy
|
||||||
|
return eventtime + 1.
|
||||||
|
# Idle timeout has elapsed
|
||||||
|
return self.transition_idle_state(eventtime)
|
||||||
|
def timeout_handler(self, eventtime):
|
||||||
|
if self.state == "Ready":
|
||||||
|
return self.check_idle_timeout(eventtime)
|
||||||
|
# Check if need to transition to "ready" state
|
||||||
|
print_time, est_print_time, lookahead_empty = self.toolhead.check_busy(
|
||||||
|
eventtime)
|
||||||
|
buffer_time = min(2., print_time - est_print_time)
|
||||||
|
if not lookahead_empty:
|
||||||
|
# Toolhead is busy
|
||||||
|
return eventtime + READY_TIMEOUT + max(0., buffer_time)
|
||||||
|
if buffer_time > -READY_TIMEOUT:
|
||||||
|
# Wait for ready timeout
|
||||||
|
return eventtime + READY_TIMEOUT + buffer_time
|
||||||
|
if not self.gcode.process_batch([]):
|
||||||
|
# Gcode class busy
|
||||||
|
return eventtime + READY_TIMEOUT
|
||||||
|
# Transition to "ready" state
|
||||||
|
self.state = "Ready"
|
||||||
|
self.printer.send_event("idle_timeout:ready",
|
||||||
|
est_print_time + PIN_MIN_TIME)
|
||||||
|
return eventtime + self.idle_timeout
|
||||||
|
def handle_sync_print_time(self, curtime, print_time, est_print_time):
|
||||||
|
if self.state == "Printing":
|
||||||
|
return
|
||||||
|
# Transition to "printing" state
|
||||||
|
self.state = "Printing"
|
||||||
|
check_time = READY_TIMEOUT + print_time - est_print_time
|
||||||
|
self.reactor.update_timer(self.timeout_timer, curtime + check_time)
|
||||||
|
self.printer.send_event("idle_timeout:printing",
|
||||||
|
est_print_time + PIN_MIN_TIME)
|
||||||
|
|
||||||
def load_config(config):
|
def load_config(config):
|
||||||
return IdleTimeout(config)
|
return IdleTimeout(config)
|
||||||
|
|
|
@ -265,10 +265,13 @@ class ToolHead:
|
||||||
for m in self.all_mcus:
|
for m in self.all_mcus:
|
||||||
m.flush_moves(flush_to_time)
|
m.flush_moves(flush_to_time)
|
||||||
def _calc_print_time(self):
|
def _calc_print_time(self):
|
||||||
est_print_time = self.mcu.estimated_print_time(self.reactor.monotonic())
|
curtime = self.reactor.monotonic()
|
||||||
|
est_print_time = self.mcu.estimated_print_time(curtime)
|
||||||
if est_print_time + self.buffer_time_start > self.print_time:
|
if est_print_time + self.buffer_time_start > self.print_time:
|
||||||
self.print_time = est_print_time + self.buffer_time_start
|
self.print_time = est_print_time + self.buffer_time_start
|
||||||
self.last_print_start_time = self.print_time
|
self.last_print_start_time = self.print_time
|
||||||
|
self.printer.send_event("toolhead:sync_print_time",
|
||||||
|
curtime, est_print_time, self.print_time)
|
||||||
def get_next_move_time(self):
|
def get_next_move_time(self):
|
||||||
if self.sync_print_time:
|
if self.sync_print_time:
|
||||||
self.sync_print_time = False
|
self.sync_print_time = False
|
||||||
|
@ -391,6 +394,10 @@ class ToolHead:
|
||||||
is_active = buffer_time > -60. or not self.sync_print_time
|
is_active = buffer_time > -60. or not self.sync_print_time
|
||||||
return is_active, "print_time=%.3f buffer_time=%.3f print_stall=%d" % (
|
return is_active, "print_time=%.3f buffer_time=%.3f print_stall=%d" % (
|
||||||
self.print_time, max(buffer_time, 0.), self.print_stall)
|
self.print_time, max(buffer_time, 0.), self.print_stall)
|
||||||
|
def check_busy(self, eventtime):
|
||||||
|
est_print_time = self.mcu.estimated_print_time(eventtime)
|
||||||
|
lookahead_empty = not self.move_queue.queue
|
||||||
|
return self.print_time, est_print_time, lookahead_empty
|
||||||
def get_status(self, eventtime):
|
def get_status(self, eventtime):
|
||||||
print_time = self.print_time
|
print_time = self.print_time
|
||||||
estimated_print_time = self.mcu.estimated_print_time(eventtime)
|
estimated_print_time = self.mcu.estimated_print_time(eventtime)
|
||||||
|
|
Loading…
Reference in New Issue