2017-06-28 02:23:30 +02:00
|
|
|
#!/usr/bin/env python2
|
2016-05-25 17:37:40 +02:00
|
|
|
# Main code for host side printer firmware
|
|
|
|
#
|
2018-01-20 04:22:17 +01:00
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-09-05 02:49:47 +02:00
|
|
|
import sys, os, optparse, logging, time, threading, collections, importlib
|
2019-06-06 16:22:14 +02:00
|
|
|
import util, reactor, queuelogger, msgproto, homing
|
2018-09-05 02:49:47 +02:00
|
|
|
import gcode, configfile, pins, heater, mcu, toolhead
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2017-02-12 04:21:55 +01:00
|
|
|
message_ready = "Printer is ready"
|
|
|
|
|
2016-11-30 20:30:45 +01:00
|
|
|
message_startup = """
|
2018-03-13 04:12:39 +01:00
|
|
|
Printer is not ready
|
2016-11-30 20:30:45 +01:00
|
|
|
The klippy host software is attempting to connect. Please
|
|
|
|
retry in a few moments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
message_restart = """
|
2016-12-01 05:47:40 +01:00
|
|
|
Once the underlying issue is corrected, use the "RESTART"
|
|
|
|
command to reload the config and restart the host software.
|
2016-11-30 20:30:45 +01:00
|
|
|
Printer is halted
|
|
|
|
"""
|
|
|
|
|
2017-01-10 05:50:13 +01:00
|
|
|
message_protocol_error = """
|
|
|
|
This type of error is frequently caused by running an older
|
|
|
|
version of the firmware on the micro-controller (fix by
|
|
|
|
recompiling and flashing the firmware).
|
|
|
|
Once the underlying issue is corrected, use the "RESTART"
|
|
|
|
command to reload the config and restart the host software.
|
|
|
|
Protocol error connecting to printer
|
|
|
|
"""
|
|
|
|
|
2016-11-30 20:30:45 +01:00
|
|
|
message_mcu_connect_error = """
|
2017-03-09 04:26:10 +01:00
|
|
|
Once the underlying issue is corrected, use the
|
|
|
|
"FIRMWARE_RESTART" command to reset the firmware, reload the
|
|
|
|
config, and restart the host software.
|
2016-11-30 20:30:45 +01:00
|
|
|
Error configuring printer
|
|
|
|
"""
|
|
|
|
|
|
|
|
message_shutdown = """
|
2017-04-13 20:53:41 +02:00
|
|
|
Once the underlying issue is corrected, use the
|
|
|
|
"FIRMWARE_RESTART" command to reset the firmware, reload the
|
|
|
|
config, and restart the host software.
|
2016-11-30 20:30:45 +01:00
|
|
|
Printer is shutdown
|
|
|
|
"""
|
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
class Printer:
|
2018-09-05 02:49:47 +02:00
|
|
|
config_error = configfile.error
|
2019-06-06 16:22:14 +02:00
|
|
|
command_error = homing.CommandError
|
2017-08-21 23:19:43 +02:00
|
|
|
def __init__(self, input_fd, bglogger, start_args):
|
2017-05-01 19:44:06 +02:00
|
|
|
self.bglogger = bglogger
|
2017-08-21 23:19:43 +02:00
|
|
|
self.start_args = start_args
|
2016-05-25 17:37:40 +02:00
|
|
|
self.reactor = reactor.Reactor()
|
2018-09-02 18:43:40 +02:00
|
|
|
self.reactor.register_callback(self._connect)
|
2016-11-30 20:30:45 +01:00
|
|
|
self.state_message = message_startup
|
2017-10-12 21:15:14 +02:00
|
|
|
self.is_shutdown = False
|
2016-12-01 05:47:40 +01:00
|
|
|
self.run_result = None
|
2018-10-07 18:22:10 +02:00
|
|
|
self.event_handlers = {}
|
2019-01-08 15:15:40 +01:00
|
|
|
gc = gcode.GCodeParser(self, input_fd)
|
|
|
|
self.objects = collections.OrderedDict({'gcode': gc})
|
2017-08-21 23:19:43 +02:00
|
|
|
def get_start_args(self):
|
|
|
|
return self.start_args
|
2018-01-20 04:22:17 +01:00
|
|
|
def get_reactor(self):
|
|
|
|
return self.reactor
|
|
|
|
def get_state_message(self):
|
|
|
|
return self.state_message
|
2018-06-16 21:15:17 +02:00
|
|
|
def _set_state(self, msg):
|
2019-01-11 18:36:09 +01:00
|
|
|
if self.state_message in (message_ready, message_startup):
|
|
|
|
self.state_message = msg
|
2018-06-16 21:15:17 +02:00
|
|
|
if (msg != message_ready
|
|
|
|
and self.start_args.get('debuginput') is not None):
|
|
|
|
self.request_exit('error_exit')
|
2018-01-20 04:22:17 +01:00
|
|
|
def add_object(self, name, obj):
|
|
|
|
if obj in self.objects:
|
|
|
|
raise self.config_error(
|
|
|
|
"Printer object '%s' already created" % (name,))
|
|
|
|
self.objects[name] = obj
|
2018-09-05 02:49:47 +02:00
|
|
|
def lookup_object(self, name, default=configfile.sentinel):
|
2018-01-20 04:22:17 +01:00
|
|
|
if name in self.objects:
|
|
|
|
return self.objects[name]
|
2018-09-05 02:49:47 +02:00
|
|
|
if default is configfile.sentinel:
|
2018-01-20 04:22:17 +01:00
|
|
|
raise self.config_error("Unknown config object '%s'" % (name,))
|
|
|
|
return default
|
2018-09-02 18:51:37 +02:00
|
|
|
def lookup_objects(self, module=None):
|
|
|
|
if module is None:
|
|
|
|
return list(self.objects.items())
|
|
|
|
prefix = module + ' '
|
|
|
|
objs = [(n, self.objects[n])
|
|
|
|
for n in self.objects if n.startswith(prefix)]
|
|
|
|
if module in self.objects:
|
|
|
|
return [(module, self.objects[module])] + objs
|
2018-01-20 04:22:17 +01:00
|
|
|
return objs
|
2018-09-04 00:32:10 +02:00
|
|
|
def set_rollover_info(self, name, info, log=True):
|
|
|
|
if log:
|
|
|
|
logging.info(info)
|
2018-01-20 04:22:17 +01:00
|
|
|
if self.bglogger is not None:
|
|
|
|
self.bglogger.set_rollover_info(name, info)
|
2018-03-07 20:41:09 +01:00
|
|
|
def try_load_module(self, config, section):
|
2018-01-20 03:25:58 +01:00
|
|
|
if section in self.objects:
|
2018-06-14 18:00:55 +02:00
|
|
|
return self.objects[section]
|
2018-02-03 18:53:11 +01:00
|
|
|
module_parts = section.split()
|
|
|
|
module_name = module_parts[0]
|
2018-01-20 03:25:58 +01:00
|
|
|
py_name = os.path.join(os.path.dirname(__file__),
|
|
|
|
'extras', module_name + '.py')
|
2018-06-27 19:01:48 +02:00
|
|
|
py_dirname = os.path.join(os.path.dirname(__file__),
|
|
|
|
'extras', module_name, '__init__.py')
|
|
|
|
if not os.path.exists(py_name) and not os.path.exists(py_dirname):
|
2018-06-14 18:00:55 +02:00
|
|
|
return None
|
2018-01-20 03:25:58 +01:00
|
|
|
mod = importlib.import_module('extras.' + module_name)
|
2018-02-03 18:53:11 +01:00
|
|
|
init_func = 'load_config'
|
|
|
|
if len(module_parts) > 1:
|
|
|
|
init_func = 'load_config_prefix'
|
|
|
|
init_func = getattr(mod, init_func, None)
|
|
|
|
if init_func is not None:
|
|
|
|
self.objects[section] = init_func(config.getsection(section))
|
2018-06-14 18:00:55 +02:00
|
|
|
return self.objects[section]
|
2018-02-03 18:17:42 +01:00
|
|
|
def _read_config(self):
|
2018-09-05 02:49:47 +02:00
|
|
|
self.objects['configfile'] = pconfig = configfile.PrinterConfig(self)
|
|
|
|
config = pconfig.read_main_config()
|
2017-05-01 19:44:06 +02:00
|
|
|
if self.bglogger is not None:
|
2018-09-05 02:49:47 +02:00
|
|
|
pconfig.log_config(config)
|
2017-04-29 19:57:02 +02:00
|
|
|
# Create printer components
|
2018-04-03 23:01:10 +02:00
|
|
|
for m in [pins, heater, mcu]:
|
2018-07-13 04:26:32 +02:00
|
|
|
m.add_printer_objects(config)
|
2018-09-05 02:49:47 +02:00
|
|
|
for section_config in config.get_prefix_sections(''):
|
|
|
|
self.try_load_module(config, section_config.get_name())
|
2018-07-13 04:15:45 +02:00
|
|
|
for m in [toolhead]:
|
2018-07-13 04:26:32 +02:00
|
|
|
m.add_printer_objects(config)
|
2017-03-13 03:43:05 +01:00
|
|
|
# Validate that there are no undefined parameters in the config file
|
2018-09-05 02:49:47 +02:00
|
|
|
pconfig.check_unused_options(config)
|
2017-08-26 06:13:36 +02:00
|
|
|
def _connect(self, eventtime):
|
2016-11-30 20:30:45 +01:00
|
|
|
try:
|
2018-02-03 18:17:42 +01:00
|
|
|
self._read_config()
|
2019-01-08 17:09:55 +01:00
|
|
|
for cb in self.event_handlers.get("klippy:connect", []):
|
2018-01-20 04:49:27 +01:00
|
|
|
if self.state_message is not message_startup:
|
2018-10-18 18:49:26 +02:00
|
|
|
return
|
2019-01-08 17:09:55 +01:00
|
|
|
cb()
|
2017-09-06 20:51:47 +02:00
|
|
|
except (self.config_error, pins.error) as e:
|
2016-11-30 20:57:18 +01:00
|
|
|
logging.exception("Config error")
|
2018-06-16 21:15:17 +02:00
|
|
|
self._set_state("%s%s" % (str(e), message_restart))
|
2019-01-08 19:46:26 +01:00
|
|
|
return
|
2017-06-10 05:32:49 +02:00
|
|
|
except msgproto.error as e:
|
2017-01-10 05:50:13 +01:00
|
|
|
logging.exception("Protocol error")
|
2018-06-16 21:15:17 +02:00
|
|
|
self._set_state("%s%s" % (str(e), message_protocol_error))
|
2019-01-08 19:46:26 +01:00
|
|
|
return
|
2017-06-10 05:32:49 +02:00
|
|
|
except mcu.error as e:
|
2016-11-30 20:30:45 +01:00
|
|
|
logging.exception("MCU error during connect")
|
2018-06-16 21:15:17 +02:00
|
|
|
self._set_state("%s%s" % (str(e), message_mcu_connect_error))
|
2019-01-08 19:46:26 +01:00
|
|
|
return
|
2019-04-05 00:37:20 +02:00
|
|
|
except Exception as e:
|
2016-11-30 20:30:45 +01:00
|
|
|
logging.exception("Unhandled exception during connect")
|
2019-04-05 00:37:20 +02:00
|
|
|
self._set_state("Internal error during connect: %s\n%s" % (
|
|
|
|
str(e), message_restart,))
|
2019-01-08 19:46:26 +01:00
|
|
|
return
|
2019-01-08 16:55:18 +01:00
|
|
|
try:
|
|
|
|
self._set_state(message_ready)
|
|
|
|
for cb in self.event_handlers.get("klippy:ready", []):
|
|
|
|
if self.state_message is not message_ready:
|
|
|
|
return
|
|
|
|
cb()
|
2019-04-05 00:37:20 +02:00
|
|
|
except Exception as e:
|
2019-01-08 16:55:18 +01:00
|
|
|
logging.exception("Unhandled exception during ready callback")
|
2019-04-05 00:37:20 +02:00
|
|
|
self.invoke_shutdown("Internal error during ready callback: %s" % (
|
|
|
|
str(e),))
|
2016-11-28 19:14:56 +01:00
|
|
|
def run(self):
|
2017-02-06 19:31:34 +01:00
|
|
|
systime = time.time()
|
|
|
|
monotime = self.reactor.monotonic()
|
2017-09-27 17:43:14 +02:00
|
|
|
logging.info("Start printer at %s (%.1f %.1f)",
|
|
|
|
time.asctime(time.localtime(systime)), systime, monotime)
|
2018-09-13 01:11:20 +02:00
|
|
|
# Enter main reactor loop
|
|
|
|
try:
|
|
|
|
self.reactor.run()
|
|
|
|
except:
|
|
|
|
logging.exception("Unhandled exception during run")
|
|
|
|
return "error_exit"
|
|
|
|
# Check restart flags
|
|
|
|
run_result = self.run_result
|
|
|
|
try:
|
|
|
|
if run_result == 'firmware_restart':
|
|
|
|
for n, m in self.lookup_objects(module='mcu'):
|
|
|
|
m.microcontroller_restart()
|
2019-01-08 16:55:18 +01:00
|
|
|
self.send_event("klippy:disconnect")
|
2018-09-13 01:11:20 +02:00
|
|
|
except:
|
|
|
|
logging.exception("Unhandled exception during post run")
|
|
|
|
return run_result
|
2017-12-04 00:13:47 +01:00
|
|
|
def invoke_shutdown(self, msg):
|
2017-10-12 21:15:14 +02:00
|
|
|
if self.is_shutdown:
|
|
|
|
return
|
|
|
|
self.is_shutdown = True
|
2018-06-16 21:15:17 +02:00
|
|
|
self._set_state("%s%s" % (msg, message_shutdown))
|
2019-01-08 15:15:40 +01:00
|
|
|
for cb in self.event_handlers.get("klippy:shutdown", []):
|
|
|
|
try:
|
|
|
|
cb()
|
|
|
|
except:
|
|
|
|
logging.exception("Exception during shutdown handler")
|
2017-10-12 21:15:14 +02:00
|
|
|
def invoke_async_shutdown(self, msg):
|
2018-09-02 18:43:40 +02:00
|
|
|
self.reactor.register_async_callback(
|
|
|
|
(lambda e: self.invoke_shutdown(msg)))
|
2018-10-07 18:22:10 +02:00
|
|
|
def register_event_handler(self, event, callback):
|
|
|
|
self.event_handlers.setdefault(event, []).append(callback)
|
|
|
|
def send_event(self, event, *params):
|
|
|
|
return [cb(*params) for cb in self.event_handlers.get(event, [])]
|
2018-06-16 21:15:17 +02:00
|
|
|
def request_exit(self, result):
|
2017-03-09 04:26:10 +01:00
|
|
|
self.run_result = result
|
2016-12-01 05:47:40 +01:00
|
|
|
self.reactor.end()
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Startup
|
|
|
|
######################################################################
|
|
|
|
|
2017-08-14 17:46:35 +02:00
|
|
|
def arg_dictionary(option, opt_str, value, parser):
|
|
|
|
key, fname = "dictionary", value
|
|
|
|
if '=' in value:
|
|
|
|
mcu_name, fname = value.split('=', 1)
|
|
|
|
key = "dictionary_" + mcu_name
|
|
|
|
if parser.values.dictionary is None:
|
|
|
|
parser.values.dictionary = {}
|
|
|
|
parser.values.dictionary[key] = fname
|
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
def main():
|
|
|
|
usage = "%prog [options] <config file>"
|
|
|
|
opts = optparse.OptionParser(usage)
|
2017-08-21 23:19:43 +02:00
|
|
|
opts.add_option("-i", "--debuginput", dest="debuginput",
|
2016-05-25 17:37:40 +02:00
|
|
|
help="read commands from file instead of from tty port")
|
2019-02-27 19:18:43 +01:00
|
|
|
opts.add_option("-I", "--input-tty", dest="inputtty",
|
|
|
|
default='/tmp/printer',
|
2016-11-21 02:40:31 +01:00
|
|
|
help="input tty name (default is /tmp/printer)")
|
2016-05-25 17:37:40 +02:00
|
|
|
opts.add_option("-l", "--logfile", dest="logfile",
|
|
|
|
help="write log to file instead of stderr")
|
|
|
|
opts.add_option("-v", action="store_true", dest="verbose",
|
|
|
|
help="enable debug messages")
|
2017-08-21 23:19:43 +02:00
|
|
|
opts.add_option("-o", "--debugoutput", dest="debugoutput",
|
|
|
|
help="write output to file instead of to serial port")
|
2017-08-14 17:46:35 +02:00
|
|
|
opts.add_option("-d", "--dictionary", dest="dictionary", type="string",
|
|
|
|
action="callback", callback=arg_dictionary,
|
2016-05-25 17:37:40 +02:00
|
|
|
help="file to read for mcu protocol dictionary")
|
|
|
|
options, args = opts.parse_args()
|
|
|
|
if len(args) != 1:
|
|
|
|
opts.error("Incorrect number of arguments")
|
2017-08-21 23:19:43 +02:00
|
|
|
start_args = {'config_file': args[0], 'start_reason': 'startup'}
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2017-08-21 23:19:43 +02:00
|
|
|
input_fd = bglogger = None
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
debuglevel = logging.INFO
|
|
|
|
if options.verbose:
|
|
|
|
debuglevel = logging.DEBUG
|
2017-08-21 23:19:43 +02:00
|
|
|
if options.debuginput:
|
|
|
|
start_args['debuginput'] = options.debuginput
|
|
|
|
debuginput = open(options.debuginput, 'rb')
|
2016-11-21 02:40:31 +01:00
|
|
|
input_fd = debuginput.fileno()
|
|
|
|
else:
|
|
|
|
input_fd = util.create_pty(options.inputtty)
|
2017-08-21 23:19:43 +02:00
|
|
|
if options.debugoutput:
|
|
|
|
start_args['debugoutput'] = options.debugoutput
|
2017-08-14 17:46:35 +02:00
|
|
|
start_args.update(options.dictionary)
|
2016-05-25 17:37:40 +02:00
|
|
|
if options.logfile:
|
2016-11-12 02:22:39 +01:00
|
|
|
bglogger = queuelogger.setup_bg_logging(options.logfile, debuglevel)
|
2016-05-25 17:37:40 +02:00
|
|
|
else:
|
|
|
|
logging.basicConfig(level=debuglevel)
|
|
|
|
logging.info("Starting Klippy...")
|
2017-08-21 23:19:43 +02:00
|
|
|
start_args['software_version'] = util.get_git_version()
|
2017-05-01 19:44:06 +02:00
|
|
|
if bglogger is not None:
|
2018-04-03 18:13:06 +02:00
|
|
|
versions = "\n".join([
|
|
|
|
"Args: %s" % (sys.argv,),
|
|
|
|
"Git version: %s" % (repr(start_args['software_version']),),
|
|
|
|
"CPU: %s" % (util.get_cpu_info(),),
|
|
|
|
"Python: %s" % (repr(sys.version),)])
|
|
|
|
logging.info(versions)
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2017-08-21 23:19:43 +02:00
|
|
|
# Start Printer() class
|
2016-12-01 05:47:40 +01:00
|
|
|
while 1:
|
2018-04-03 18:13:06 +02:00
|
|
|
if bglogger is not None:
|
|
|
|
bglogger.clear_rollover_info()
|
|
|
|
bglogger.set_rollover_info('versions', versions)
|
2017-08-21 23:19:43 +02:00
|
|
|
printer = Printer(input_fd, bglogger, start_args)
|
2016-12-01 05:47:40 +01:00
|
|
|
res = printer.run()
|
2018-06-16 21:15:17 +02:00
|
|
|
if res in ['exit', 'error_exit']:
|
2017-08-21 23:19:43 +02:00
|
|
|
break
|
|
|
|
time.sleep(1.)
|
|
|
|
logging.info("Restarting printer")
|
|
|
|
start_args['start_reason'] = res
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2016-11-12 02:22:39 +01:00
|
|
|
if bglogger is not None:
|
|
|
|
bglogger.stop()
|
|
|
|
|
2018-06-16 21:15:17 +02:00
|
|
|
if res == 'error_exit':
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|