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
|
|
|
|
#
|
2017-08-21 17:25:26 +02:00
|
|
|
# Copyright (C) 2016,2017 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.
|
|
|
|
import sys, optparse, ConfigParser, logging, time, threading
|
2017-08-21 17:25:26 +02:00
|
|
|
import util, reactor, queuelogger, msgproto, gcode
|
2017-09-06 20:51:47 +02:00
|
|
|
import pins, mcu, chipmisc, toolhead, extruder, heater, fan
|
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 = """
|
|
|
|
The klippy host software is attempting to connect. Please
|
|
|
|
retry in a few moments.
|
|
|
|
Printer is not ready
|
|
|
|
"""
|
|
|
|
|
|
|
|
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 ConfigWrapper:
|
2016-11-30 21:05:26 +01:00
|
|
|
error = ConfigParser.Error
|
2016-11-30 21:39:36 +01:00
|
|
|
class sentinel:
|
|
|
|
pass
|
2016-05-25 17:37:40 +02:00
|
|
|
def __init__(self, printer, section):
|
|
|
|
self.printer = printer
|
|
|
|
self.section = section
|
2017-04-11 17:37:09 +02:00
|
|
|
def get_wrapper(self, parser, option, default
|
|
|
|
, minval=None, maxval=None, above=None, below=None):
|
2016-11-30 21:39:36 +01:00
|
|
|
if (default is not self.sentinel
|
|
|
|
and not self.printer.fileconfig.has_option(self.section, option)):
|
2016-05-25 17:37:40 +02:00
|
|
|
return default
|
2016-11-30 22:07:17 +01:00
|
|
|
self.printer.all_config_options[
|
|
|
|
(self.section.lower(), option.lower())] = 1
|
2016-11-30 21:39:36 +01:00
|
|
|
try:
|
2017-04-11 17:37:09 +02:00
|
|
|
v = parser(self.section, option)
|
2017-06-10 05:32:49 +02:00
|
|
|
except self.error as e:
|
2016-11-30 21:39:36 +01:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
raise self.error("Unable to parse option '%s' in section '%s'" % (
|
|
|
|
option, self.section))
|
2017-04-11 17:37:09 +02:00
|
|
|
if minval is not None and v < minval:
|
|
|
|
raise self.error(
|
|
|
|
"Option '%s' in section '%s' must have minimum of %s" % (
|
|
|
|
option, self.section, minval))
|
|
|
|
if maxval is not None and v > maxval:
|
|
|
|
raise self.error(
|
|
|
|
"Option '%s' in section '%s' must have maximum of %s" % (
|
|
|
|
option, self.section, maxval))
|
|
|
|
if above is not None and v <= above:
|
|
|
|
raise self.error(
|
|
|
|
"Option '%s' in section '%s' must be above %s" % (
|
|
|
|
option, self.section, above))
|
|
|
|
if below is not None and v >= below:
|
|
|
|
raise self.error(
|
|
|
|
"Option '%s' in section '%s' must be below %s" % (
|
|
|
|
option, self.section, below))
|
|
|
|
return v
|
2016-11-30 21:39:36 +01:00
|
|
|
def get(self, option, default=sentinel):
|
|
|
|
return self.get_wrapper(self.printer.fileconfig.get, option, default)
|
2017-04-11 17:37:09 +02:00
|
|
|
def getint(self, option, default=sentinel, minval=None, maxval=None):
|
|
|
|
return self.get_wrapper(
|
|
|
|
self.printer.fileconfig.getint, option, default, minval, maxval)
|
|
|
|
def getfloat(self, option, default=sentinel
|
|
|
|
, minval=None, maxval=None, above=None, below=None):
|
2016-11-30 21:39:36 +01:00
|
|
|
return self.get_wrapper(
|
2017-04-11 17:37:09 +02:00
|
|
|
self.printer.fileconfig.getfloat, option, default
|
|
|
|
, minval, maxval, above, below)
|
2016-11-30 21:39:36 +01:00
|
|
|
def getboolean(self, option, default=sentinel):
|
|
|
|
return self.get_wrapper(
|
|
|
|
self.printer.fileconfig.getboolean, option, default)
|
2016-11-30 21:50:28 +01:00
|
|
|
def getchoice(self, option, choices, default=sentinel):
|
|
|
|
c = self.get(option, default)
|
|
|
|
if c not in choices:
|
|
|
|
raise self.error(
|
|
|
|
"Option '%s' in section '%s' is not a valid choice" % (
|
|
|
|
option, self.section))
|
|
|
|
return choices[c]
|
2016-05-25 17:37:40 +02:00
|
|
|
def getsection(self, section):
|
|
|
|
return ConfigWrapper(self.printer, section)
|
2017-04-29 19:57:02 +02:00
|
|
|
def has_section(self, section):
|
|
|
|
return self.printer.fileconfig.has_section(section)
|
2017-07-04 18:24:11 +02:00
|
|
|
def get_prefix_sections(self, prefix):
|
|
|
|
return [self.getsection(s) for s in self.printer.fileconfig.sections()
|
|
|
|
if s.startswith(prefix)]
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2016-12-24 16:17:12 +01:00
|
|
|
class ConfigLogger():
|
2017-05-01 19:44:06 +02:00
|
|
|
def __init__(self, cfg, bglogger):
|
|
|
|
self.lines = ["===== Config file ====="]
|
2016-12-24 16:17:12 +01:00
|
|
|
cfg.write(self)
|
2017-05-01 19:44:06 +02:00
|
|
|
self.lines.append("=======================")
|
|
|
|
data = "\n".join(self.lines)
|
|
|
|
logging.info(data)
|
|
|
|
bglogger.set_rollover_info("config", data)
|
2016-12-24 16:17:12 +01:00
|
|
|
def write(self, data):
|
2017-05-01 19:44:06 +02:00
|
|
|
self.lines.append(data.strip())
|
2016-12-24 16:17:12 +01:00
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
class Printer:
|
2017-09-06 20:51:47 +02:00
|
|
|
config_error = ConfigParser.Error
|
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
|
2017-05-01 19:44:06 +02:00
|
|
|
if bglogger is not None:
|
|
|
|
bglogger.set_rollover_info("config", None)
|
2016-05-25 17:37:40 +02:00
|
|
|
self.reactor = reactor.Reactor()
|
2017-03-13 03:02:32 +01:00
|
|
|
self.objects = {}
|
2017-08-21 23:19:43 +02:00
|
|
|
self.gcode = gcode.GCodeParser(self, input_fd)
|
2017-08-26 06:13:36 +02:00
|
|
|
self.stats_timer = self.reactor.register_timer(self._stats)
|
2016-11-28 19:14:56 +01:00
|
|
|
self.connect_timer = self.reactor.register_timer(
|
2017-08-26 06:13:36 +02:00
|
|
|
self._connect, self.reactor.NOW)
|
2016-11-30 22:07:17 +01:00
|
|
|
self.all_config_options = {}
|
2016-12-01 01:05:25 +01:00
|
|
|
self.need_dump_debug = False
|
2016-11-30 20:30:45 +01:00
|
|
|
self.state_message = message_startup
|
2016-12-01 05:47:40 +01:00
|
|
|
self.run_result = None
|
2016-11-22 18:14:25 +01:00
|
|
|
self.fileconfig = None
|
|
|
|
self.mcu = None
|
2017-08-21 23:19:43 +02:00
|
|
|
def get_start_args(self):
|
|
|
|
return self.start_args
|
2017-08-26 06:13:36 +02:00
|
|
|
def _stats(self, eventtime, force_output=False):
|
2016-12-01 01:05:25 +01:00
|
|
|
if self.need_dump_debug:
|
|
|
|
# Call dump_debug here so it is executed in the main thread
|
|
|
|
self.gcode.dump_debug()
|
|
|
|
self.need_dump_debug = False
|
2017-04-14 16:29:39 +02:00
|
|
|
toolhead = self.objects.get('toolhead')
|
|
|
|
if toolhead is None or self.mcu is None:
|
2017-06-15 15:37:57 +02:00
|
|
|
return eventtime + 1.
|
2017-04-14 16:29:39 +02:00
|
|
|
is_active, thstats = toolhead.stats(eventtime)
|
|
|
|
if not is_active and not force_output:
|
2017-06-15 15:37:57 +02:00
|
|
|
return eventtime + 1.
|
2016-11-22 18:14:25 +01:00
|
|
|
out = []
|
|
|
|
out.append(self.gcode.stats(eventtime))
|
2017-04-14 16:29:39 +02:00
|
|
|
out.append(thstats)
|
|
|
|
out.append(self.mcu.stats(eventtime))
|
2017-01-10 18:29:52 +01:00
|
|
|
logging.info("Stats %.1f: %s" % (eventtime, ' '.join(out)))
|
2016-11-22 18:14:25 +01:00
|
|
|
return eventtime + 1.
|
2017-04-29 19:57:02 +02:00
|
|
|
def add_object(self, name, obj):
|
|
|
|
self.objects[name] = obj
|
2017-08-26 06:13:36 +02:00
|
|
|
def _load_config(self):
|
2016-11-22 18:14:25 +01:00
|
|
|
self.fileconfig = ConfigParser.RawConfigParser()
|
2017-08-21 23:19:43 +02:00
|
|
|
config_file = self.start_args['config_file']
|
|
|
|
res = self.fileconfig.read(config_file)
|
2016-11-30 20:57:18 +01:00
|
|
|
if not res:
|
2017-09-06 20:51:47 +02:00
|
|
|
raise self.config_error("Unable to open config file %s" % (
|
2017-08-21 23:19:43 +02:00
|
|
|
config_file,))
|
2017-05-01 19:44:06 +02:00
|
|
|
if self.bglogger is not None:
|
|
|
|
ConfigLogger(self.fileconfig, self.bglogger)
|
2017-04-29 19:57:02 +02:00
|
|
|
# Create printer components
|
|
|
|
config = ConfigWrapper(self, 'printer')
|
2017-09-06 20:51:47 +02:00
|
|
|
for m in [pins, mcu, chipmisc, toolhead, extruder, heater, fan]:
|
2017-04-29 19:57:02 +02:00
|
|
|
m.add_printer_objects(self, config)
|
2017-08-21 17:25:26 +02:00
|
|
|
self.mcu = self.objects['mcu']
|
2017-03-13 03:43:05 +01:00
|
|
|
# Validate that there are no undefined parameters in the config file
|
2017-06-06 18:35:13 +02:00
|
|
|
valid_sections = { s: 1 for s, o in self.all_config_options }
|
2016-11-30 22:07:17 +01:00
|
|
|
for section in self.fileconfig.sections():
|
|
|
|
section = section.lower()
|
|
|
|
if section not in valid_sections:
|
2017-09-06 20:51:47 +02:00
|
|
|
raise self.config_error("Unknown config file section '%s'" % (
|
2016-11-30 22:07:17 +01:00
|
|
|
section,))
|
|
|
|
for option in self.fileconfig.options(section):
|
|
|
|
option = option.lower()
|
|
|
|
if (section, option) not in self.all_config_options:
|
2017-09-06 20:51:47 +02:00
|
|
|
raise self.config_error(
|
2016-11-30 22:07:17 +01:00
|
|
|
"Unknown option '%s' in section '%s'" % (
|
|
|
|
option, section))
|
2017-08-26 06:13:36 +02:00
|
|
|
def _connect(self, eventtime):
|
2017-09-19 23:12:04 +02:00
|
|
|
self.reactor.unregister_timer(self.connect_timer)
|
2016-11-30 20:30:45 +01:00
|
|
|
try:
|
2017-08-26 06:13:36 +02:00
|
|
|
self._load_config()
|
2016-11-30 20:30:45 +01:00
|
|
|
self.mcu.connect()
|
|
|
|
self.gcode.set_printer_ready(True)
|
2017-02-12 04:21:55 +01:00
|
|
|
self.state_message = message_ready
|
2017-09-19 23:12:04 +02:00
|
|
|
if self.start_args.get('debugoutput') is None:
|
|
|
|
self.reactor.update_timer(self.stats_timer, self.reactor.NOW)
|
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")
|
|
|
|
self.state_message = "%s%s" % (str(e), message_restart)
|
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")
|
|
|
|
self.state_message = "%s%s" % (str(e), message_protocol_error)
|
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")
|
|
|
|
self.state_message = "%s%s" % (str(e), message_mcu_connect_error)
|
|
|
|
except:
|
|
|
|
logging.exception("Unhandled exception during connect")
|
|
|
|
self.state_message = "Internal error during connect.%s" % (
|
2017-08-26 19:20:16 +02:00
|
|
|
message_restart,)
|
2016-11-28 19:14:56 +01:00
|
|
|
return self.reactor.NEVER
|
|
|
|
def run(self):
|
2017-02-06 19:31:34 +01:00
|
|
|
systime = time.time()
|
|
|
|
monotime = self.reactor.monotonic()
|
|
|
|
logging.info("Start printer at %s (%.1f %.1f)" % (
|
|
|
|
time.asctime(time.localtime(systime)), systime, monotime))
|
2017-08-26 19:20:16 +02:00
|
|
|
# Enter main reactor loop
|
2016-11-30 20:30:45 +01:00
|
|
|
try:
|
|
|
|
self.reactor.run()
|
|
|
|
except:
|
|
|
|
logging.exception("Unhandled exception during run")
|
2017-08-26 19:20:16 +02:00
|
|
|
return "exit"
|
|
|
|
# Check restart flags
|
|
|
|
run_result = self.run_result
|
|
|
|
try:
|
|
|
|
self._stats(self.reactor.monotonic(), force_output=True)
|
|
|
|
if self.mcu is not None:
|
|
|
|
if run_result == 'firmware_restart':
|
|
|
|
self.mcu.microcontroller_restart()
|
|
|
|
self.mcu.disconnect()
|
|
|
|
except:
|
|
|
|
logging.exception("Unhandled exception during post run")
|
|
|
|
return run_result
|
2016-11-30 20:30:45 +01:00
|
|
|
def get_state_message(self):
|
|
|
|
return self.state_message
|
|
|
|
def note_shutdown(self, msg):
|
2017-02-12 04:21:55 +01:00
|
|
|
if self.state_message == message_ready:
|
2016-12-01 01:05:25 +01:00
|
|
|
self.need_dump_debug = True
|
2017-09-06 03:15:24 +02:00
|
|
|
self.state_message = "%s%s" % (msg, message_shutdown)
|
2016-11-23 01:38:51 +01:00
|
|
|
self.gcode.set_printer_ready(False)
|
2016-12-01 01:31:46 +01:00
|
|
|
def note_mcu_error(self, msg):
|
|
|
|
self.state_message = "%s%s" % (msg, message_restart)
|
|
|
|
self.gcode.set_printer_ready(False)
|
2016-12-01 05:47:40 +01:00
|
|
|
self.gcode.motor_heater_off()
|
2017-03-09 04:26:10 +01:00
|
|
|
def request_exit(self, result="exit"):
|
|
|
|
self.run_result = result
|
2016-12-01 05:47:40 +01:00
|
|
|
self.reactor.end()
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Startup
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
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")
|
2016-11-21 02:40:31 +01:00
|
|
|
opts.add_option("-I", "--input-tty", dest="inputtty", default='/tmp/printer',
|
|
|
|
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")
|
|
|
|
opts.add_option("-d", "--dictionary", dest="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
|
|
|
|
start_args['dictionary'] = 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:
|
|
|
|
lines = ["Args: %s" % (sys.argv,),
|
2017-08-21 23:19:43 +02:00
|
|
|
"Git version: %s" % (repr(start_args['software_version']),),
|
2017-05-01 19:44:06 +02:00
|
|
|
"CPU: %s" % (util.get_cpu_info(),),
|
|
|
|
"Python: %s" % (repr(sys.version),)]
|
|
|
|
lines = "\n".join(lines)
|
|
|
|
logging.info(lines)
|
|
|
|
bglogger.set_rollover_info('versions', lines)
|
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:
|
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()
|
2017-08-26 19:20:16 +02:00
|
|
|
if res == '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()
|
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|