2017-09-15 19:37:00 +02:00
|
|
|
# Interface to Klipper micro-controller code
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
2020-02-19 22:46:06 +01:00
|
|
|
# Copyright (C) 2016-2020 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.
|
2017-04-13 18:09:37 +02:00
|
|
|
import sys, os, zlib, logging, math
|
2017-09-19 03:41:00 +02:00
|
|
|
import serialhdl, pins, chelper, clocksync
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2016-11-30 00:27:48 +01:00
|
|
|
class error(Exception):
|
|
|
|
pass
|
2016-11-18 19:03:40 +01:00
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
class MCU_endstop:
|
|
|
|
RETRY_QUERY = 1.000
|
2017-08-21 17:25:26 +02:00
|
|
|
def __init__(self, mcu, pin_params):
|
2016-05-25 17:37:40 +02:00
|
|
|
self._mcu = mcu
|
2017-03-05 21:30:04 +01:00
|
|
|
self._steppers = []
|
2017-08-21 17:25:26 +02:00
|
|
|
self._pin = pin_params['pin']
|
|
|
|
self._pullup = pin_params['pullup']
|
|
|
|
self._invert = pin_params['invert']
|
2019-06-26 22:29:07 +02:00
|
|
|
self._reactor = mcu.get_printer().get_reactor()
|
2020-02-19 22:46:06 +01:00
|
|
|
self._oid = self._home_cmd = self._requery_cmd = self._query_cmd = None
|
2018-09-03 17:48:22 +02:00
|
|
|
self._mcu.register_config_callback(self._build_config)
|
2019-06-26 22:29:07 +02:00
|
|
|
self._min_query_time = self._last_sent_time = 0.
|
2019-06-26 03:25:01 +02:00
|
|
|
self._next_query_print_time = self._end_home_time = 0.
|
|
|
|
self._trigger_completion = self._home_completion = None
|
2017-08-29 23:20:26 +02:00
|
|
|
def get_mcu(self):
|
|
|
|
return self._mcu
|
2017-03-05 21:30:04 +01:00
|
|
|
def add_stepper(self, stepper):
|
2017-12-05 23:51:54 +01:00
|
|
|
if stepper.get_mcu() is not self._mcu:
|
|
|
|
raise pins.error("Endstop and stepper must be on the same mcu")
|
2018-08-26 17:06:20 +02:00
|
|
|
if stepper in self._steppers:
|
|
|
|
return
|
2017-03-05 21:30:04 +01:00
|
|
|
self._steppers.append(stepper)
|
2017-12-06 03:51:44 +01:00
|
|
|
def get_steppers(self):
|
|
|
|
return list(self._steppers)
|
2018-09-03 17:48:22 +02:00
|
|
|
def _build_config(self):
|
2017-08-24 15:34:31 +02:00
|
|
|
self._oid = self._mcu.create_oid()
|
2017-03-13 03:23:10 +01:00
|
|
|
self._mcu.add_config_cmd(
|
2019-07-12 14:09:30 +02:00
|
|
|
"config_endstop oid=%d pin=%s pull_up=%d stepper_count=%d" % (
|
2017-03-05 21:30:04 +01:00
|
|
|
self._oid, self._pin, self._pullup, len(self._steppers)))
|
2017-12-05 18:13:09 +01:00
|
|
|
self._mcu.add_config_cmd(
|
2019-07-12 14:09:30 +02:00
|
|
|
"endstop_home oid=%d clock=0 sample_ticks=0 sample_count=0"
|
2020-07-19 18:26:31 +02:00
|
|
|
" rest_ticks=0 pin_value=0" % (self._oid,), on_restart=True)
|
2017-08-22 03:42:00 +02:00
|
|
|
for i, s in enumerate(self._steppers):
|
|
|
|
self._mcu.add_config_cmd(
|
2019-07-12 14:09:30 +02:00
|
|
|
"endstop_set_stepper oid=%d pos=%d stepper_oid=%d" % (
|
2017-08-22 03:42:00 +02:00
|
|
|
self._oid, i, s.get_oid()), is_init=True)
|
2018-02-27 20:16:16 +01:00
|
|
|
cmd_queue = self._mcu.alloc_command_queue()
|
2017-03-13 02:30:18 +01:00
|
|
|
self._home_cmd = self._mcu.lookup_command(
|
2019-07-12 14:09:30 +02:00
|
|
|
"endstop_home oid=%c clock=%u sample_ticks=%u sample_count=%c"
|
2018-02-27 20:16:16 +01:00
|
|
|
" rest_ticks=%u pin_value=%c", cq=cmd_queue)
|
2020-02-19 22:46:06 +01:00
|
|
|
self._requery_cmd = self._mcu.lookup_command(
|
2019-07-12 14:09:30 +02:00
|
|
|
"endstop_query_state oid=%c", cq=cmd_queue)
|
2020-02-19 22:46:06 +01:00
|
|
|
self._query_cmd = self._mcu.lookup_query_command(
|
|
|
|
"endstop_query_state oid=%c",
|
|
|
|
"endstop_state oid=%c homing=%c pin_value=%c", oid=self._oid,
|
|
|
|
cq=cmd_queue)
|
2019-01-05 02:20:02 +01:00
|
|
|
def home_start(self, print_time, sample_time, sample_count, rest_time,
|
2020-02-19 17:38:11 +01:00
|
|
|
triggered=True):
|
2017-09-12 18:47:40 +02:00
|
|
|
clock = self._mcu.print_time_to_clock(print_time)
|
2020-03-06 22:33:54 +01:00
|
|
|
rest_ticks = self._mcu.print_time_to_clock(print_time+rest_time) - clock
|
2019-01-10 18:27:56 +01:00
|
|
|
self._next_query_print_time = print_time + self.RETRY_QUERY
|
2019-06-26 22:29:07 +02:00
|
|
|
self._min_query_time = self._reactor.monotonic()
|
|
|
|
self._last_sent_time = 0.
|
2019-06-26 03:25:01 +02:00
|
|
|
self._home_end_time = self._reactor.NEVER
|
|
|
|
self._trigger_completion = self._reactor.completion()
|
2019-07-12 14:09:30 +02:00
|
|
|
self._mcu.register_response(self._handle_endstop_state,
|
|
|
|
"endstop_state", self._oid)
|
2018-02-27 20:16:16 +01:00
|
|
|
self._home_cmd.send(
|
|
|
|
[self._oid, clock, self._mcu.seconds_to_clock(sample_time),
|
2019-01-05 02:20:02 +01:00
|
|
|
sample_count, rest_ticks, triggered ^ self._invert],
|
|
|
|
reqclock=clock)
|
2019-06-26 03:25:01 +02:00
|
|
|
self._home_completion = self._reactor.register_callback(
|
|
|
|
self._home_retry)
|
2020-02-19 17:38:11 +01:00
|
|
|
return self._trigger_completion
|
2019-07-12 14:09:30 +02:00
|
|
|
def _handle_endstop_state(self, params):
|
|
|
|
logging.debug("endstop_state %s", params)
|
2019-06-26 22:29:07 +02:00
|
|
|
if params['#sent_time'] >= self._min_query_time:
|
|
|
|
if params['homing']:
|
|
|
|
self._last_sent_time = params['#sent_time']
|
|
|
|
else:
|
|
|
|
self._min_query_time = self._reactor.NEVER
|
2020-02-19 17:38:11 +01:00
|
|
|
self._reactor.async_complete(self._trigger_completion, True)
|
2019-06-26 03:25:01 +02:00
|
|
|
def _home_retry(self, eventtime):
|
2019-06-26 22:29:07 +02:00
|
|
|
if self._mcu.is_fileoutput():
|
2019-06-26 03:25:01 +02:00
|
|
|
return True
|
2019-06-26 22:29:07 +02:00
|
|
|
while 1:
|
2020-02-19 17:38:11 +01:00
|
|
|
did_trigger = self._trigger_completion.wait(eventtime + 0.100)
|
|
|
|
if did_trigger is not None:
|
2019-06-26 22:29:07 +02:00
|
|
|
# Homing completed successfully
|
2019-06-26 03:25:01 +02:00
|
|
|
return True
|
2019-06-26 22:29:07 +02:00
|
|
|
# Check for timeout
|
|
|
|
last = self._mcu.estimated_print_time(self._last_sent_time)
|
2019-06-26 03:25:01 +02:00
|
|
|
if last > self._home_end_time or self._mcu.is_shutdown():
|
|
|
|
return False
|
2019-06-26 22:29:07 +02:00
|
|
|
# Check for resend
|
2019-06-26 03:25:01 +02:00
|
|
|
eventtime = self._reactor.monotonic()
|
|
|
|
est_print_time = self._mcu.estimated_print_time(eventtime)
|
2019-06-26 22:29:07 +02:00
|
|
|
if est_print_time >= self._next_query_print_time:
|
|
|
|
self._next_query_print_time = est_print_time + self.RETRY_QUERY
|
2020-02-19 22:46:06 +01:00
|
|
|
self._requery_cmd.send([self._oid])
|
2019-06-26 03:25:01 +02:00
|
|
|
def home_wait(self, home_end_time):
|
|
|
|
self._home_end_time = home_end_time
|
|
|
|
did_trigger = self._home_completion.wait()
|
2019-07-12 14:09:30 +02:00
|
|
|
self._mcu.register_response(None, "endstop_state", self._oid)
|
2019-06-26 03:25:01 +02:00
|
|
|
self._home_cmd.send([self._oid, 0, 0, 0, 0, 0])
|
|
|
|
for s in self._steppers:
|
|
|
|
s.note_homing_end(did_trigger=did_trigger)
|
2020-02-19 17:38:11 +01:00
|
|
|
if not self._trigger_completion.test():
|
|
|
|
self._trigger_completion.complete(False)
|
2020-02-19 18:07:30 +01:00
|
|
|
return did_trigger
|
2017-09-12 18:47:40 +02:00
|
|
|
def query_endstop(self, print_time):
|
2019-06-26 17:04:37 +02:00
|
|
|
clock = self._mcu.print_time_to_clock(print_time)
|
|
|
|
if self._mcu.is_fileoutput():
|
|
|
|
return 0
|
2020-02-19 22:46:06 +01:00
|
|
|
params = self._query_cmd.send([self._oid], minclock=clock)
|
2019-06-26 17:04:37 +02:00
|
|
|
return params['pin_value'] ^ self._invert
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
class MCU_digital_out:
|
2017-08-21 17:25:26 +02:00
|
|
|
def __init__(self, mcu, pin_params):
|
2016-05-25 17:37:40 +02:00
|
|
|
self._mcu = mcu
|
2017-08-24 15:34:31 +02:00
|
|
|
self._oid = None
|
2018-09-03 17:48:22 +02:00
|
|
|
self._mcu.register_config_callback(self._build_config)
|
2017-08-21 17:25:26 +02:00
|
|
|
self._pin = pin_params['pin']
|
2017-12-18 19:07:41 +01:00
|
|
|
self._invert = pin_params['invert']
|
|
|
|
self._start_value = self._shutdown_value = self._invert
|
2018-01-09 01:51:35 +01:00
|
|
|
self._is_static = False
|
2017-08-21 17:25:26 +02:00
|
|
|
self._max_duration = 2.
|
2016-05-25 17:37:40 +02:00
|
|
|
self._last_clock = 0
|
2017-03-13 02:30:18 +01:00
|
|
|
self._set_cmd = None
|
2017-08-29 23:20:26 +02:00
|
|
|
def get_mcu(self):
|
|
|
|
return self._mcu
|
2017-08-21 17:25:26 +02:00
|
|
|
def setup_max_duration(self, max_duration):
|
|
|
|
self._max_duration = max_duration
|
2018-01-09 01:51:35 +01:00
|
|
|
def setup_start_value(self, start_value, shutdown_value, is_static=False):
|
|
|
|
if is_static and start_value != shutdown_value:
|
|
|
|
raise pins.error("Static pin can not have shutdown value")
|
2017-12-18 19:07:41 +01:00
|
|
|
self._start_value = (not not start_value) ^ self._invert
|
|
|
|
self._shutdown_value = (not not shutdown_value) ^ self._invert
|
2018-01-09 01:51:35 +01:00
|
|
|
self._is_static = is_static
|
2018-09-03 17:48:22 +02:00
|
|
|
def _build_config(self):
|
2018-01-09 01:51:35 +01:00
|
|
|
if self._is_static:
|
2020-07-16 21:33:48 +02:00
|
|
|
self._mcu.add_config_cmd("set_digital_out pin=%s value=%d"
|
|
|
|
% (self._pin, self._start_value))
|
2017-08-24 15:34:31 +02:00
|
|
|
return
|
|
|
|
self._oid = self._mcu.create_oid()
|
2017-08-21 17:25:26 +02:00
|
|
|
self._mcu.add_config_cmd(
|
2017-10-12 04:50:29 +02:00
|
|
|
"config_digital_out oid=%d pin=%s value=%d default_value=%d"
|
2017-08-26 05:21:55 +02:00
|
|
|
" max_duration=%d" % (
|
2018-01-09 01:51:35 +01:00
|
|
|
self._oid, self._pin, self._start_value, self._shutdown_value,
|
2017-09-12 18:47:40 +02:00
|
|
|
self._mcu.seconds_to_clock(self._max_duration)))
|
2020-07-16 21:33:48 +02:00
|
|
|
self._mcu.add_config_cmd("update_digital_out oid=%d value=%d"
|
|
|
|
% (self._oid, self._start_value),
|
|
|
|
on_restart=True)
|
2018-02-27 20:16:16 +01:00
|
|
|
cmd_queue = self._mcu.alloc_command_queue()
|
2017-03-13 02:30:18 +01:00
|
|
|
self._set_cmd = self._mcu.lookup_command(
|
2018-02-27 20:16:16 +01:00
|
|
|
"schedule_digital_out oid=%c clock=%u value=%c", cq=cmd_queue)
|
2017-09-12 18:47:40 +02:00
|
|
|
def set_digital(self, print_time, value):
|
|
|
|
clock = self._mcu.print_time_to_clock(print_time)
|
2018-02-27 20:16:16 +01:00
|
|
|
self._set_cmd.send([self._oid, clock, (not not value) ^ self._invert],
|
|
|
|
minclock=self._last_clock, reqclock=clock)
|
2016-05-25 17:37:40 +02:00
|
|
|
self._last_clock = clock
|
2020-09-27 02:52:42 +02:00
|
|
|
def set_pwm(self, print_time, value, cycle_time=None):
|
2017-09-12 18:47:40 +02:00
|
|
|
self.set_digital(print_time, value >= 0.5)
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
class MCU_pwm:
|
2017-08-21 17:25:26 +02:00
|
|
|
def __init__(self, mcu, pin_params):
|
2016-05-25 17:37:40 +02:00
|
|
|
self._mcu = mcu
|
2018-01-29 18:54:06 +01:00
|
|
|
self._hardware_pwm = False
|
2017-08-21 17:25:26 +02:00
|
|
|
self._cycle_time = 0.100
|
|
|
|
self._max_duration = 2.
|
2017-08-24 15:34:31 +02:00
|
|
|
self._oid = None
|
2018-09-03 17:48:22 +02:00
|
|
|
self._mcu.register_config_callback(self._build_config)
|
2017-08-21 17:25:26 +02:00
|
|
|
self._pin = pin_params['pin']
|
|
|
|
self._invert = pin_params['invert']
|
2017-12-18 19:07:41 +01:00
|
|
|
self._start_value = self._shutdown_value = float(self._invert)
|
2018-01-09 01:51:35 +01:00
|
|
|
self._is_static = False
|
2016-05-25 17:37:40 +02:00
|
|
|
self._last_clock = 0
|
2017-05-07 23:33:45 +02:00
|
|
|
self._pwm_max = 0.
|
2017-03-13 02:30:18 +01:00
|
|
|
self._set_cmd = None
|
2017-08-29 23:20:26 +02:00
|
|
|
def get_mcu(self):
|
|
|
|
return self._mcu
|
2017-08-21 17:25:26 +02:00
|
|
|
def setup_max_duration(self, max_duration):
|
|
|
|
self._max_duration = max_duration
|
2018-01-29 18:54:06 +01:00
|
|
|
def setup_cycle_time(self, cycle_time, hardware_pwm=False):
|
2017-08-21 17:25:26 +02:00
|
|
|
self._cycle_time = cycle_time
|
2018-01-29 18:54:06 +01:00
|
|
|
self._hardware_pwm = hardware_pwm
|
2018-01-09 01:51:35 +01:00
|
|
|
def setup_start_value(self, start_value, shutdown_value, is_static=False):
|
|
|
|
if is_static and start_value != shutdown_value:
|
|
|
|
raise pins.error("Static pin can not have shutdown value")
|
2017-10-12 04:50:29 +02:00
|
|
|
if self._invert:
|
2017-12-18 19:07:41 +01:00
|
|
|
start_value = 1. - start_value
|
|
|
|
shutdown_value = 1. - shutdown_value
|
|
|
|
self._start_value = max(0., min(1., start_value))
|
|
|
|
self._shutdown_value = max(0., min(1., shutdown_value))
|
2018-01-09 01:51:35 +01:00
|
|
|
self._is_static = is_static
|
2018-09-03 17:48:22 +02:00
|
|
|
def _build_config(self):
|
2018-02-27 20:16:16 +01:00
|
|
|
cmd_queue = self._mcu.alloc_command_queue()
|
2018-01-29 18:54:06 +01:00
|
|
|
cycle_ticks = self._mcu.seconds_to_clock(self._cycle_time)
|
|
|
|
if self._hardware_pwm:
|
2017-09-19 17:13:44 +02:00
|
|
|
self._pwm_max = self._mcu.get_constant_float("PWM_MAX")
|
2018-01-09 01:51:35 +01:00
|
|
|
if self._is_static:
|
2017-08-24 15:34:31 +02:00
|
|
|
self._mcu.add_config_cmd(
|
2020-07-16 21:33:48 +02:00
|
|
|
"set_pwm_out pin=%s cycle_ticks=%d value=%d"
|
|
|
|
% (self._pin, cycle_ticks,
|
|
|
|
self._start_value * self._pwm_max))
|
2017-08-24 15:34:31 +02:00
|
|
|
return
|
|
|
|
self._oid = self._mcu.create_oid()
|
2017-08-21 17:25:26 +02:00
|
|
|
self._mcu.add_config_cmd(
|
2017-10-12 04:50:29 +02:00
|
|
|
"config_pwm_out oid=%d pin=%s cycle_ticks=%d value=%d"
|
2020-07-16 21:33:48 +02:00
|
|
|
" default_value=%d max_duration=%d"
|
|
|
|
% (self._oid, self._pin, cycle_ticks,
|
|
|
|
self._start_value * self._pwm_max,
|
|
|
|
self._shutdown_value * self._pwm_max,
|
|
|
|
self._mcu.seconds_to_clock(self._max_duration)))
|
2020-09-09 19:16:02 +02:00
|
|
|
curtime = self._mcu.get_printer().get_reactor().monotonic()
|
|
|
|
printtime = self._mcu.estimated_print_time(curtime)
|
|
|
|
self._last_clock = self._mcu.print_time_to_clock(printtime + 0.100)
|
|
|
|
svalue = int(self._start_value * self._pwm_max + 0.5)
|
|
|
|
self._mcu.add_config_cmd("schedule_pwm_out oid=%d clock=%d value=%d"
|
|
|
|
% (self._oid, self._last_clock, svalue),
|
|
|
|
on_restart=True)
|
2017-03-13 02:30:18 +01:00
|
|
|
self._set_cmd = self._mcu.lookup_command(
|
2020-07-16 21:33:48 +02:00
|
|
|
"schedule_pwm_out oid=%c clock=%u value=%hu", cq=cmd_queue)
|
|
|
|
return
|
|
|
|
# Software PWM
|
|
|
|
if self._shutdown_value not in [0., 1.]:
|
|
|
|
raise pins.error("shutdown value must be 0.0 or 1.0 on soft pwm")
|
|
|
|
self._pwm_max = float(cycle_ticks)
|
|
|
|
if self._is_static:
|
|
|
|
self._mcu.add_config_cmd("set_digital_out pin=%s value=%d"
|
|
|
|
% (self._pin, self._start_value >= 0.5))
|
|
|
|
return
|
|
|
|
self._oid = self._mcu.create_oid()
|
|
|
|
self._mcu.add_config_cmd(
|
2020-09-07 12:31:35 +02:00
|
|
|
"config_soft_pwm_out oid=%d pin=%s value=%d"
|
2020-07-16 21:33:48 +02:00
|
|
|
" default_value=%d max_duration=%d"
|
2020-09-07 12:31:35 +02:00
|
|
|
% (self._oid, self._pin, self._start_value >= 1.0,
|
|
|
|
self._shutdown_value >= 0.5,
|
2020-07-16 21:33:48 +02:00
|
|
|
self._mcu.seconds_to_clock(self._max_duration)))
|
|
|
|
curtime = self._mcu.get_printer().get_reactor().monotonic()
|
|
|
|
printtime = self._mcu.estimated_print_time(curtime)
|
|
|
|
self._last_clock = self._mcu.print_time_to_clock(printtime + 0.100)
|
|
|
|
svalue = int(self._start_value * self._pwm_max + 0.5)
|
|
|
|
self._mcu.add_config_cmd(
|
2020-09-07 12:31:35 +02:00
|
|
|
"schedule_soft_pwm_out oid=%d clock=%d on_ticks=%d off_ticks=%d"
|
|
|
|
% (self._oid, self._last_clock, svalue, cycle_ticks - svalue),
|
|
|
|
is_init=True)
|
2020-07-16 21:33:48 +02:00
|
|
|
self._set_cmd = self._mcu.lookup_command(
|
2020-09-07 12:31:35 +02:00
|
|
|
"schedule_soft_pwm_out oid=%c clock=%u on_ticks=%u off_ticks=%u",
|
2020-07-16 21:33:48 +02:00
|
|
|
cq=cmd_queue)
|
2020-09-27 02:52:42 +02:00
|
|
|
def set_pwm(self, print_time, value, cycle_time=None):
|
2017-09-12 18:47:40 +02:00
|
|
|
clock = self._mcu.print_time_to_clock(print_time)
|
2020-09-28 00:09:53 +02:00
|
|
|
minclock = self._last_clock
|
|
|
|
self._last_clock = clock
|
2017-03-09 06:44:25 +01:00
|
|
|
if self._invert:
|
|
|
|
value = 1. - value
|
2020-09-07 12:31:35 +02:00
|
|
|
if self._hardware_pwm:
|
2020-09-28 00:09:53 +02:00
|
|
|
v = int(max(0., min(1., value)) * self._pwm_max + 0.5)
|
|
|
|
self._set_cmd.send([self._oid, clock, v],
|
|
|
|
minclock=minclock, reqclock=clock)
|
|
|
|
return
|
|
|
|
# Soft pwm update
|
|
|
|
if cycle_time is None:
|
|
|
|
cycle_time = self._cycle_time
|
|
|
|
cycle_ticks = self._mcu.seconds_to_clock(cycle_time)
|
|
|
|
on_ticks = int(max(0., min(1., value)) * float(cycle_ticks) + 0.5)
|
|
|
|
self._set_cmd.send([self._oid, clock, on_ticks, cycle_ticks - on_ticks],
|
|
|
|
minclock=minclock, reqclock=clock)
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
class MCU_adc:
|
2017-08-21 17:25:26 +02:00
|
|
|
def __init__(self, mcu, pin_params):
|
2016-05-25 17:37:40 +02:00
|
|
|
self._mcu = mcu
|
2017-08-21 17:25:26 +02:00
|
|
|
self._pin = pin_params['pin']
|
2017-03-13 02:30:18 +01:00
|
|
|
self._min_sample = self._max_sample = 0.
|
|
|
|
self._sample_time = self._report_time = 0.
|
2018-07-02 19:47:22 +02:00
|
|
|
self._sample_count = self._range_check_count = 0
|
2016-05-25 17:37:40 +02:00
|
|
|
self._report_clock = 0
|
2019-11-08 01:23:20 +01:00
|
|
|
self._last_state = (0., 0.)
|
2017-08-24 15:34:31 +02:00
|
|
|
self._oid = self._callback = None
|
2018-09-03 17:48:22 +02:00
|
|
|
self._mcu.register_config_callback(self._build_config)
|
2016-08-24 21:16:02 +02:00
|
|
|
self._inv_max_adc = 0.
|
2017-08-29 23:20:26 +02:00
|
|
|
def get_mcu(self):
|
|
|
|
return self._mcu
|
2018-07-02 19:47:22 +02:00
|
|
|
def setup_minmax(self, sample_time, sample_count,
|
|
|
|
minval=0., maxval=1., range_check_count=0):
|
2017-08-21 17:25:26 +02:00
|
|
|
self._sample_time = sample_time
|
|
|
|
self._sample_count = sample_count
|
|
|
|
self._min_sample = minval
|
|
|
|
self._max_sample = maxval
|
2018-07-02 19:47:22 +02:00
|
|
|
self._range_check_count = range_check_count
|
2017-08-21 17:25:26 +02:00
|
|
|
def setup_adc_callback(self, report_time, callback):
|
|
|
|
self._report_time = report_time
|
|
|
|
self._callback = callback
|
2019-11-08 01:23:20 +01:00
|
|
|
def get_last_value(self):
|
|
|
|
return self._last_state
|
2018-09-03 17:48:22 +02:00
|
|
|
def _build_config(self):
|
2017-08-22 03:42:00 +02:00
|
|
|
if not self._sample_count:
|
|
|
|
return
|
2017-08-24 15:34:31 +02:00
|
|
|
self._oid = self._mcu.create_oid()
|
2017-08-21 17:25:26 +02:00
|
|
|
self._mcu.add_config_cmd("config_analog_in oid=%d pin=%s" % (
|
|
|
|
self._oid, self._pin))
|
2017-09-19 04:53:12 +02:00
|
|
|
clock = self._mcu.get_query_slot(self._oid)
|
2017-09-12 18:47:40 +02:00
|
|
|
sample_ticks = self._mcu.seconds_to_clock(self._sample_time)
|
2017-09-19 17:13:44 +02:00
|
|
|
mcu_adc_max = self._mcu.get_constant_float("ADC_MAX")
|
2017-03-13 02:30:18 +01:00
|
|
|
max_adc = self._sample_count * mcu_adc_max
|
|
|
|
self._inv_max_adc = 1.0 / max_adc
|
2017-09-12 18:47:40 +02:00
|
|
|
self._report_clock = self._mcu.seconds_to_clock(self._report_time)
|
2017-09-06 00:19:36 +02:00
|
|
|
min_sample = max(0, min(0xffff, int(self._min_sample * max_adc)))
|
|
|
|
max_sample = max(0, min(0xffff, int(
|
|
|
|
math.ceil(self._max_sample * max_adc))))
|
2017-08-22 03:42:00 +02:00
|
|
|
self._mcu.add_config_cmd(
|
|
|
|
"query_analog_in oid=%d clock=%d sample_ticks=%d sample_count=%d"
|
2018-07-02 19:47:22 +02:00
|
|
|
" rest_ticks=%d min_value=%d max_value=%d range_check_count=%d" % (
|
2017-08-22 03:42:00 +02:00
|
|
|
self._oid, clock, sample_ticks, self._sample_count,
|
2018-07-02 19:47:22 +02:00
|
|
|
self._report_clock, min_sample, max_sample,
|
|
|
|
self._range_check_count), is_init=True)
|
2019-06-20 23:36:46 +02:00
|
|
|
self._mcu.register_response(self._handle_analog_in_state,
|
|
|
|
"analog_in_state", self._oid)
|
2016-05-25 17:37:40 +02:00
|
|
|
def _handle_analog_in_state(self, params):
|
2016-08-24 21:16:02 +02:00
|
|
|
last_value = params['value'] * self._inv_max_adc
|
2017-09-15 18:28:55 +02:00
|
|
|
next_clock = self._mcu.clock32_to_clock64(params['next_clock'])
|
2017-09-12 18:47:40 +02:00
|
|
|
last_read_clock = next_clock - self._report_clock
|
|
|
|
last_read_time = self._mcu.clock_to_print_time(last_read_clock)
|
2019-11-08 01:23:20 +01:00
|
|
|
self._last_state = (last_value, last_read_time)
|
2016-05-25 17:37:40 +02:00
|
|
|
if self._callback is not None:
|
2016-08-24 21:16:02 +02:00
|
|
|
self._callback(last_read_time, last_value)
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2020-02-16 21:46:04 +01:00
|
|
|
# Class to retry sending of a query command until a given response is received
|
|
|
|
class RetryAsyncCommand:
|
|
|
|
TIMEOUT_TIME = 5.0
|
|
|
|
RETRY_TIME = 0.500
|
2020-02-19 22:46:06 +01:00
|
|
|
def __init__(self, serial, name, oid=None):
|
2020-02-16 21:46:04 +01:00
|
|
|
self.serial = serial
|
|
|
|
self.name = name
|
|
|
|
self.oid = oid
|
2020-02-19 22:46:06 +01:00
|
|
|
self.reactor = serial.get_reactor()
|
2020-02-16 21:46:04 +01:00
|
|
|
self.completion = self.reactor.completion()
|
|
|
|
self.min_query_time = self.reactor.monotonic()
|
|
|
|
self.serial.register_response(self.handle_callback, name, oid)
|
|
|
|
def handle_callback(self, params):
|
|
|
|
if params['#sent_time'] >= self.min_query_time:
|
|
|
|
self.min_query_time = self.reactor.NEVER
|
|
|
|
self.reactor.async_complete(self.completion, params)
|
2020-02-19 22:46:06 +01:00
|
|
|
def get_response(self, cmd, cmd_queue, minclock=0):
|
|
|
|
self.serial.raw_send_wait_ack(cmd, minclock, minclock, cmd_queue)
|
|
|
|
first_query_time = query_time = self.reactor.monotonic()
|
2020-02-16 21:46:04 +01:00
|
|
|
while 1:
|
|
|
|
params = self.completion.wait(query_time + self.RETRY_TIME)
|
|
|
|
if params is not None:
|
|
|
|
self.serial.register_response(None, self.name, self.oid)
|
|
|
|
return params
|
|
|
|
query_time = self.reactor.monotonic()
|
|
|
|
if query_time > first_query_time + self.TIMEOUT_TIME:
|
|
|
|
self.serial.register_response(None, self.name, self.oid)
|
|
|
|
raise error("Timeout on wait for '%s' response" % (self.name,))
|
2020-02-19 22:46:06 +01:00
|
|
|
self.serial.raw_send(cmd, minclock, minclock, cmd_queue)
|
2020-02-16 21:46:04 +01:00
|
|
|
|
2020-02-19 22:46:06 +01:00
|
|
|
# Wrapper around query commands
|
|
|
|
class CommandQueryWrapper:
|
|
|
|
def __init__(self, serial, msgformat, respformat, oid=None,
|
2020-06-12 03:39:27 +02:00
|
|
|
cmd_queue=None, is_async=False):
|
2019-06-22 01:35:34 +02:00
|
|
|
self._serial = serial
|
2020-02-19 22:46:06 +01:00
|
|
|
self._cmd = serial.get_msgparser().lookup_command(msgformat)
|
|
|
|
serial.get_msgparser().lookup_command(respformat)
|
|
|
|
self._response = respformat.split()[0]
|
|
|
|
self._oid = oid
|
|
|
|
self._xmit_helper = serialhdl.SerialRetryCommand
|
2020-06-12 03:39:27 +02:00
|
|
|
if is_async:
|
2020-02-19 22:46:06 +01:00
|
|
|
self._xmit_helper = RetryAsyncCommand
|
|
|
|
if cmd_queue is None:
|
|
|
|
cmd_queue = serial.get_default_command_queue()
|
2019-06-22 01:35:34 +02:00
|
|
|
self._cmd_queue = cmd_queue
|
2020-02-19 22:46:06 +01:00
|
|
|
def send(self, data=(), minclock=0):
|
2019-06-22 01:35:34 +02:00
|
|
|
cmd = self._cmd.encode(data)
|
2020-02-19 22:46:06 +01:00
|
|
|
xh = self._xmit_helper(self._serial, self._response, self._oid)
|
2019-06-22 01:35:34 +02:00
|
|
|
try:
|
2020-02-19 22:46:06 +01:00
|
|
|
return xh.get_response(cmd, self._cmd_queue, minclock=minclock)
|
2019-06-22 01:35:34 +02:00
|
|
|
except serialhdl.error as e:
|
|
|
|
raise error(str(e))
|
2020-02-19 22:46:06 +01:00
|
|
|
|
|
|
|
# Wrapper around command sending
|
|
|
|
class CommandWrapper:
|
|
|
|
def __init__(self, serial, msgformat, cmd_queue=None):
|
|
|
|
self._serial = serial
|
|
|
|
self._cmd = serial.get_msgparser().lookup_command(msgformat)
|
|
|
|
if cmd_queue is None:
|
|
|
|
cmd_queue = serial.get_default_command_queue()
|
|
|
|
self._cmd_queue = cmd_queue
|
|
|
|
def send(self, data=(), minclock=0, reqclock=0):
|
2020-02-16 21:46:04 +01:00
|
|
|
cmd = self._cmd.encode(data)
|
2020-02-19 22:46:06 +01:00
|
|
|
self._serial.raw_send(cmd, minclock, reqclock, self._cmd_queue)
|
2019-06-22 01:35:34 +02:00
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
class MCU:
|
2016-12-08 18:42:12 +01:00
|
|
|
error = error
|
2018-07-13 04:26:32 +02:00
|
|
|
def __init__(self, config, clocksync):
|
|
|
|
self._printer = config.get_printer()
|
2017-09-19 03:41:00 +02:00
|
|
|
self._clocksync = clocksync
|
2018-07-13 04:26:32 +02:00
|
|
|
self._reactor = self._printer.get_reactor()
|
2018-01-20 04:22:17 +01:00
|
|
|
self._name = config.get_name()
|
2017-08-14 17:46:35 +02:00
|
|
|
if self._name.startswith('mcu '):
|
|
|
|
self._name = self._name[4:]
|
2019-01-08 17:09:55 +01:00
|
|
|
self._printer.register_event_handler("klippy:connect", self._connect)
|
2019-11-05 05:00:00 +01:00
|
|
|
self._printer.register_event_handler("klippy:mcu_identify",
|
|
|
|
self._mcu_identify)
|
2019-01-08 15:15:40 +01:00
|
|
|
self._printer.register_event_handler("klippy:shutdown", self._shutdown)
|
2019-01-08 16:58:35 +01:00
|
|
|
self._printer.register_event_handler("klippy:disconnect",
|
|
|
|
self._disconnect)
|
2016-05-25 17:37:40 +02:00
|
|
|
# Serial port
|
2017-03-09 04:26:10 +01:00
|
|
|
self._serialport = config.get('serial', '/dev/ttyS0')
|
2020-04-01 23:52:55 +02:00
|
|
|
serial_rts = True
|
|
|
|
if config.get('restart_method', None) == "cheetah":
|
|
|
|
# Special case: Cheetah boards require RTS to be deasserted, else
|
|
|
|
# a reset will trigger the built-in bootloader.
|
|
|
|
serial_rts = False
|
2017-09-19 18:10:06 +02:00
|
|
|
baud = 0
|
2017-07-20 06:02:43 +02:00
|
|
|
if not (self._serialport.startswith("/dev/rpmsg_")
|
|
|
|
or self._serialport.startswith("/tmp/klipper_host_")):
|
2017-05-08 16:29:59 +02:00
|
|
|
baud = config.getint('baud', 250000, minval=2400)
|
2017-09-19 17:13:44 +02:00
|
|
|
self._serial = serialhdl.SerialReader(
|
2020-04-01 23:52:55 +02:00
|
|
|
self._reactor, self._serialport, baud, serial_rts)
|
2017-09-19 18:10:06 +02:00
|
|
|
# Restarts
|
2017-09-04 04:02:48 +02:00
|
|
|
self._restart_method = 'command'
|
|
|
|
if baud:
|
2020-04-01 23:52:55 +02:00
|
|
|
rmethods = {m: m for m in [None, 'arduino', 'cheetah', 'command',
|
|
|
|
'rpi_usb']}
|
2017-09-04 04:02:48 +02:00
|
|
|
self._restart_method = config.getchoice(
|
2018-01-29 16:16:42 +01:00
|
|
|
'restart_method', rmethods, None)
|
2017-09-19 18:10:06 +02:00
|
|
|
self._reset_cmd = self._config_reset_cmd = None
|
|
|
|
self._emergency_stop_cmd = None
|
2018-01-08 16:55:37 +01:00
|
|
|
self._is_shutdown = self._is_timeout = False
|
2017-09-19 18:10:06 +02:00
|
|
|
self._shutdown_msg = ""
|
|
|
|
# Config building
|
2018-07-13 04:26:32 +02:00
|
|
|
self._printer.lookup_object('pins').register_chip(self._name, self)
|
2017-08-24 00:55:04 +02:00
|
|
|
self._oid_count = 0
|
2018-09-03 17:48:22 +02:00
|
|
|
self._config_callbacks = []
|
2016-05-25 17:37:40 +02:00
|
|
|
self._config_cmds = []
|
2020-07-19 18:26:31 +02:00
|
|
|
self._restart_cmds = []
|
|
|
|
self._init_cmds = []
|
2017-03-13 04:05:01 +01:00
|
|
|
self._pin_map = config.get('pin_map', None)
|
2017-09-12 18:24:44 +02:00
|
|
|
self._mcu_freq = 0.
|
2016-05-25 17:37:40 +02:00
|
|
|
# Move command queuing
|
2017-04-04 16:13:02 +02:00
|
|
|
ffi_main, self._ffi_lib = chelper.get_ffi()
|
2020-09-17 05:41:27 +02:00
|
|
|
self._max_stepper_error = config.getfloat('max_stepper_error', 0.000025,
|
|
|
|
minval=0.)
|
2017-08-24 00:59:25 +02:00
|
|
|
self._stepqueues = []
|
2016-05-25 17:37:40 +02:00
|
|
|
self._steppersync = None
|
|
|
|
# Stats
|
2017-01-10 05:08:23 +01:00
|
|
|
self._stats_sumsq_base = 0.
|
2016-05-25 17:37:40 +02:00
|
|
|
self._mcu_tick_avg = 0.
|
|
|
|
self._mcu_tick_stddev = 0.
|
2017-07-13 04:16:16 +02:00
|
|
|
self._mcu_tick_awake = 0.
|
2017-09-19 18:10:06 +02:00
|
|
|
# Serial callbacks
|
2018-07-12 18:19:04 +02:00
|
|
|
def _handle_mcu_stats(self, params):
|
2016-05-25 17:37:40 +02:00
|
|
|
count = params['count']
|
|
|
|
tick_sum = params['sum']
|
2016-08-25 18:09:37 +02:00
|
|
|
c = 1.0 / (count * self._mcu_freq)
|
2016-05-25 17:37:40 +02:00
|
|
|
self._mcu_tick_avg = tick_sum * c
|
2017-01-10 05:08:23 +01:00
|
|
|
tick_sumsq = params['sumsq'] * self._stats_sumsq_base
|
2019-08-30 19:57:07 +02:00
|
|
|
diff = count*tick_sumsq - tick_sum**2
|
|
|
|
self._mcu_tick_stddev = c * math.sqrt(max(0., diff))
|
2017-07-13 04:16:16 +02:00
|
|
|
self._mcu_tick_awake = tick_sum / self._mcu_freq
|
2018-07-12 18:19:04 +02:00
|
|
|
def _handle_shutdown(self, params):
|
2017-09-19 17:13:44 +02:00
|
|
|
if self._is_shutdown:
|
2016-05-25 17:37:40 +02:00
|
|
|
return
|
2017-09-19 17:13:44 +02:00
|
|
|
self._is_shutdown = True
|
2019-03-05 18:42:35 +01:00
|
|
|
self._shutdown_msg = msg = params['static_string_id']
|
2017-09-27 19:13:38 +02:00
|
|
|
logging.info("MCU '%s' %s: %s\n%s\n%s", self._name, params['#name'],
|
|
|
|
self._shutdown_msg, self._clocksync.dump_debug(),
|
|
|
|
self._serial.dump_debug())
|
2017-08-14 17:46:35 +02:00
|
|
|
prefix = "MCU '%s' shutdown: " % (self._name,)
|
2017-09-06 03:15:24 +02:00
|
|
|
if params['#name'] == 'is_shutdown':
|
2017-08-14 17:46:35 +02:00
|
|
|
prefix = "Previous MCU '%s' shutdown: " % (self._name,)
|
2017-10-12 21:15:14 +02:00
|
|
|
self._printer.invoke_async_shutdown(prefix + msg + error_help(msg))
|
2020-03-22 16:46:09 +01:00
|
|
|
def _handle_starting(self, params):
|
|
|
|
if not self._is_shutdown:
|
|
|
|
self._printer.invoke_async_shutdown("MCU '%s' spontaneous restart"
|
|
|
|
% (self._name,))
|
2016-05-25 17:37:40 +02:00
|
|
|
# Connection phase
|
2017-04-13 18:09:37 +02:00
|
|
|
def _check_restart(self, reason):
|
2017-08-21 23:19:43 +02:00
|
|
|
start_reason = self._printer.get_start_args().get("start_reason")
|
|
|
|
if start_reason == 'firmware_restart':
|
2017-04-13 18:09:37 +02:00
|
|
|
return
|
2017-08-14 17:46:35 +02:00
|
|
|
logging.info("Attempting automated MCU '%s' restart: %s",
|
|
|
|
self._name, reason)
|
2017-04-13 18:09:37 +02:00
|
|
|
self._printer.request_exit('firmware_restart')
|
2018-01-20 04:22:17 +01:00
|
|
|
self._reactor.pause(self._reactor.monotonic() + 2.000)
|
2017-08-14 17:46:35 +02:00
|
|
|
raise error("Attempt MCU '%s' restart failed" % (self._name,))
|
2017-08-21 23:19:43 +02:00
|
|
|
def _connect_file(self, pace=False):
|
|
|
|
# In a debugging mode. Open debug output file and read data dictionary
|
2017-08-14 17:46:35 +02:00
|
|
|
start_args = self._printer.get_start_args()
|
|
|
|
if self._name == 'mcu':
|
|
|
|
out_fname = start_args.get('debugoutput')
|
|
|
|
dict_fname = start_args.get('dictionary')
|
|
|
|
else:
|
|
|
|
out_fname = start_args.get('debugoutput') + "-" + self._name
|
|
|
|
dict_fname = start_args.get('dictionary_' + self._name)
|
2017-08-21 23:19:43 +02:00
|
|
|
outfile = open(out_fname, 'wb')
|
|
|
|
dfile = open(dict_fname, 'rb')
|
|
|
|
dict_data = dfile.read()
|
|
|
|
dfile.close()
|
2017-09-19 17:13:44 +02:00
|
|
|
self._serial.connect_file(outfile, dict_data)
|
|
|
|
self._clocksync.connect_file(self._serial, pace)
|
2017-08-21 23:19:43 +02:00
|
|
|
# Handle pacing
|
2016-05-25 17:37:40 +02:00
|
|
|
if not pace:
|
2017-09-12 18:24:44 +02:00
|
|
|
def dummy_estimated_print_time(eventtime):
|
|
|
|
return 0.
|
|
|
|
self.estimated_print_time = dummy_estimated_print_time
|
2018-07-12 18:54:12 +02:00
|
|
|
def _send_config(self, prev_crc):
|
2016-05-25 17:37:40 +02:00
|
|
|
# Build config commands
|
2018-09-03 17:48:22 +02:00
|
|
|
for cb in self._config_callbacks:
|
|
|
|
cb()
|
2020-07-16 21:38:33 +02:00
|
|
|
self._config_cmds.insert(0, "allocate_oids count=%d"
|
|
|
|
% (self._oid_count,))
|
2016-05-25 17:37:40 +02:00
|
|
|
# Resolve pin names
|
2019-06-21 04:08:38 +02:00
|
|
|
mcu_type = self._serial.get_msgparser().get_constant('MCU')
|
2019-04-01 02:30:14 +02:00
|
|
|
ppins = self._printer.lookup_object('pins')
|
2019-08-09 17:42:50 +02:00
|
|
|
pin_resolver = ppins.get_pin_resolver(self._name)
|
2018-01-11 00:34:42 +01:00
|
|
|
if self._pin_map is not None:
|
2019-08-09 17:42:50 +02:00
|
|
|
pin_resolver.add_pin_mapping(mcu_type, self._pin_map)
|
2020-07-19 18:26:31 +02:00
|
|
|
for cmdlist in (self._config_cmds, self._restart_cmds, self._init_cmds):
|
|
|
|
for i, cmd in enumerate(cmdlist):
|
|
|
|
cmdlist[i] = pin_resolver.update_command(cmd)
|
2016-05-25 17:37:40 +02:00
|
|
|
# Calculate config CRC
|
2018-07-12 18:54:12 +02:00
|
|
|
config_crc = zlib.crc32('\n'.join(self._config_cmds)) & 0xffffffff
|
|
|
|
self.add_config_cmd("finalize_config crc=%d" % (config_crc,))
|
2020-03-22 16:46:09 +01:00
|
|
|
if prev_crc is not None and config_crc != prev_crc:
|
|
|
|
self._check_restart("CRC mismatch")
|
|
|
|
raise error("MCU '%s' CRC does not match config" % (self._name,))
|
2018-07-12 18:54:12 +02:00
|
|
|
# Transmit config messages (if needed)
|
2020-03-22 16:46:09 +01:00
|
|
|
self.register_response(self._handle_starting, 'starting')
|
2018-07-12 18:54:12 +02:00
|
|
|
if prev_crc is None:
|
2018-07-26 16:54:31 +02:00
|
|
|
logging.info("Sending MCU '%s' printer configuration...",
|
|
|
|
self._name)
|
2018-07-12 18:54:12 +02:00
|
|
|
for c in self._config_cmds:
|
|
|
|
self._serial.send(c)
|
2020-07-19 18:26:31 +02:00
|
|
|
else:
|
|
|
|
for c in self._restart_cmds:
|
|
|
|
self._serial.send(c)
|
2018-07-12 18:54:12 +02:00
|
|
|
# Transmit init messages
|
|
|
|
for c in self._init_cmds:
|
|
|
|
self._serial.send(c)
|
|
|
|
def _send_get_config(self):
|
2020-02-19 22:46:06 +01:00
|
|
|
get_config_cmd = self.lookup_query_command(
|
|
|
|
"get_config",
|
|
|
|
"config is_config=%c crc=%u move_count=%hu is_shutdown=%c")
|
2017-08-21 23:19:43 +02:00
|
|
|
if self.is_fileoutput():
|
2018-07-12 18:54:12 +02:00
|
|
|
return { 'is_config': 0, 'move_count': 500, 'crc': 0 }
|
2020-02-19 22:46:06 +01:00
|
|
|
config_params = get_config_cmd.send()
|
2018-07-12 18:54:12 +02:00
|
|
|
if self._is_shutdown:
|
|
|
|
raise error("MCU '%s' error during config: %s" % (
|
|
|
|
self._name, self._shutdown_msg))
|
|
|
|
if config_params['is_shutdown']:
|
|
|
|
raise error("Can not update MCU '%s' config as it is shutdown" % (
|
|
|
|
self._name,))
|
|
|
|
return config_params
|
2019-11-05 05:00:00 +01:00
|
|
|
def _log_info(self):
|
|
|
|
msgparser = self._serial.get_msgparser()
|
|
|
|
log_info = [
|
|
|
|
"Loaded MCU '%s' %d commands (%s / %s)" % (
|
|
|
|
self._name, len(msgparser.messages_by_id),
|
|
|
|
msgparser.version, msgparser.build_versions),
|
|
|
|
"MCU '%s' config: %s" % (self._name, " ".join(
|
|
|
|
["%s=%s" % (k, v) for k, v in self.get_constants().items()]))]
|
|
|
|
return "\n".join(log_info)
|
|
|
|
def _connect(self):
|
2018-07-12 18:54:12 +02:00
|
|
|
config_params = self._send_get_config()
|
2016-11-28 20:52:22 +01:00
|
|
|
if not config_params['is_config']:
|
2017-04-13 18:09:37 +02:00
|
|
|
if self._restart_method == 'rpi_usb':
|
|
|
|
# Only configure mcu after usb power reset
|
|
|
|
self._check_restart("full reset before config")
|
2018-07-12 18:54:12 +02:00
|
|
|
# Not configured - send config and issue get_config again
|
|
|
|
self._send_config(None)
|
|
|
|
config_params = self._send_get_config()
|
|
|
|
if not config_params['is_config'] and not self.is_fileoutput():
|
|
|
|
raise error("Unable to configure MCU '%s'" % (self._name,))
|
2017-08-21 23:19:43 +02:00
|
|
|
else:
|
|
|
|
start_reason = self._printer.get_start_args().get("start_reason")
|
|
|
|
if start_reason == 'firmware_restart':
|
2020-09-17 05:41:27 +02:00
|
|
|
raise error("Failed automated reset of MCU '%s'"
|
|
|
|
% (self._name,))
|
2018-07-12 18:54:12 +02:00
|
|
|
# Already configured - send init commands
|
|
|
|
self._send_config(config_params['crc'])
|
|
|
|
# Setup steppersync with the move_count returned by get_config
|
2019-11-05 05:00:00 +01:00
|
|
|
move_count = config_params['move_count']
|
2020-09-17 05:41:27 +02:00
|
|
|
ffi_main, ffi_lib = chelper.get_ffi()
|
|
|
|
self._steppersync = ffi_main.gc(
|
|
|
|
ffi_lib.steppersync_alloc(self._serial.serialqueue,
|
|
|
|
self._stepqueues, len(self._stepqueues),
|
|
|
|
move_count),
|
|
|
|
ffi_lib.steppersync_free)
|
|
|
|
ffi_lib.steppersync_set_time(self._steppersync, 0., self._mcu_freq)
|
2019-11-05 05:00:00 +01:00
|
|
|
# Log config information
|
|
|
|
move_msg = "Configured MCU '%s' (%d moves)" % (self._name, move_count)
|
|
|
|
logging.info(move_msg)
|
|
|
|
log_info = self._log_info() + "\n" + move_msg
|
|
|
|
self._printer.set_rollover_info(self._name, log_info, log=False)
|
|
|
|
def _mcu_identify(self):
|
2017-09-19 18:10:06 +02:00
|
|
|
if self.is_fileoutput():
|
|
|
|
self._connect_file()
|
|
|
|
else:
|
|
|
|
if (self._restart_method == 'rpi_usb'
|
|
|
|
and not os.path.exists(self._serialport)):
|
|
|
|
# Try toggling usb power
|
|
|
|
self._check_restart("enable power")
|
2019-06-22 00:54:56 +02:00
|
|
|
try:
|
|
|
|
self._serial.connect()
|
|
|
|
self._clocksync.connect(self._serial)
|
|
|
|
except serialhdl.error as e:
|
|
|
|
raise error(str(e))
|
2019-11-05 05:00:00 +01:00
|
|
|
logging.info(self._log_info())
|
2019-04-01 02:30:14 +02:00
|
|
|
ppins = self._printer.lookup_object('pins')
|
2019-11-05 05:00:00 +01:00
|
|
|
pin_resolver = ppins.get_pin_resolver(self._name)
|
2019-05-21 15:40:13 +02:00
|
|
|
for cname, value in self.get_constants().items():
|
|
|
|
if cname.startswith("RESERVE_PINS_"):
|
2019-04-01 02:30:14 +02:00
|
|
|
for pin in value.split(','):
|
2019-08-09 17:42:50 +02:00
|
|
|
pin_resolver.reserve_pin(pin, cname[13:])
|
2017-09-19 18:10:06 +02:00
|
|
|
self._mcu_freq = self.get_constant_float('CLOCK_FREQ')
|
|
|
|
self._stats_sumsq_base = self.get_constant_float('STATS_SUMSQ_BASE')
|
|
|
|
self._emergency_stop_cmd = self.lookup_command("emergency_stop")
|
|
|
|
self._reset_cmd = self.try_lookup_command("reset")
|
|
|
|
self._config_reset_cmd = self.try_lookup_command("config_reset")
|
2019-11-13 04:12:25 +01:00
|
|
|
ext_only = self._reset_cmd is None and self._config_reset_cmd is None
|
|
|
|
mbaud = self._serial.get_msgparser().get_constant('SERIAL_BAUD', None)
|
|
|
|
if self._restart_method is None and mbaud is None and not ext_only:
|
2018-01-29 16:16:42 +01:00
|
|
|
self._restart_method = 'command'
|
2019-06-20 23:36:46 +02:00
|
|
|
self.register_response(self._handle_shutdown, 'shutdown')
|
|
|
|
self.register_response(self._handle_shutdown, 'is_shutdown')
|
|
|
|
self.register_response(self._handle_mcu_stats, 'stats')
|
2016-05-25 17:37:40 +02:00
|
|
|
# Config creation helpers
|
2018-07-26 15:44:45 +02:00
|
|
|
def setup_pin(self, pin_type, pin_params):
|
2019-11-12 19:55:50 +01:00
|
|
|
pcs = {'endstop': MCU_endstop,
|
2017-08-21 17:25:26 +02:00
|
|
|
'digital_out': MCU_digital_out, 'pwm': MCU_pwm, 'adc': MCU_adc}
|
|
|
|
if pin_type not in pcs:
|
|
|
|
raise pins.error("pin type %s not supported on mcu" % (pin_type,))
|
2018-09-03 17:48:22 +02:00
|
|
|
return pcs[pin_type](self, pin_params)
|
2017-08-24 00:55:04 +02:00
|
|
|
def create_oid(self):
|
|
|
|
self._oid_count += 1
|
|
|
|
return self._oid_count - 1
|
2018-09-03 17:48:22 +02:00
|
|
|
def register_config_callback(self, cb):
|
|
|
|
self._config_callbacks.append(cb)
|
2020-07-19 18:26:31 +02:00
|
|
|
def add_config_cmd(self, cmd, is_init=False, on_restart=False):
|
2017-08-22 03:42:00 +02:00
|
|
|
if is_init:
|
|
|
|
self._init_cmds.append(cmd)
|
2020-07-19 18:26:31 +02:00
|
|
|
elif on_restart:
|
|
|
|
self._restart_cmds.append(cmd)
|
2017-08-22 03:42:00 +02:00
|
|
|
else:
|
|
|
|
self._config_cmds.append(cmd)
|
2017-09-19 18:10:06 +02:00
|
|
|
def get_query_slot(self, oid):
|
|
|
|
slot = self.seconds_to_clock(oid * .01)
|
2019-06-26 22:29:07 +02:00
|
|
|
t = int(self.estimated_print_time(self._reactor.monotonic()) + 1.5)
|
2017-09-19 18:10:06 +02:00
|
|
|
return self.print_time_to_clock(t) + slot
|
2017-08-24 00:59:25 +02:00
|
|
|
def register_stepqueue(self, stepqueue):
|
|
|
|
self._stepqueues.append(stepqueue)
|
2017-09-19 18:10:06 +02:00
|
|
|
def seconds_to_clock(self, time):
|
|
|
|
return int(time * self._mcu_freq)
|
|
|
|
def get_max_stepper_error(self):
|
|
|
|
return self._max_stepper_error
|
|
|
|
# Wrapper functions
|
2019-04-01 02:30:14 +02:00
|
|
|
def get_printer(self):
|
|
|
|
return self._printer
|
|
|
|
def get_name(self):
|
|
|
|
return self._name
|
2019-06-20 23:36:46 +02:00
|
|
|
def register_response(self, cb, msg, oid=None):
|
|
|
|
self._serial.register_response(cb, msg, oid)
|
2016-05-25 17:37:40 +02:00
|
|
|
def alloc_command_queue(self):
|
2017-09-19 17:13:44 +02:00
|
|
|
return self._serial.alloc_command_queue()
|
2018-02-27 20:16:16 +01:00
|
|
|
def lookup_command(self, msgformat, cq=None):
|
2020-02-19 22:46:06 +01:00
|
|
|
return CommandWrapper(self._serial, msgformat, cq)
|
|
|
|
def lookup_query_command(self, msgformat, respformat, oid=None,
|
2020-06-12 03:39:27 +02:00
|
|
|
cq=None, is_async=False):
|
2020-02-19 22:46:06 +01:00
|
|
|
return CommandQueryWrapper(self._serial, msgformat, respformat, oid,
|
2020-06-12 03:39:27 +02:00
|
|
|
cq, is_async)
|
2017-09-04 04:02:48 +02:00
|
|
|
def try_lookup_command(self, msgformat):
|
|
|
|
try:
|
2018-02-27 20:16:16 +01:00
|
|
|
return self.lookup_command(msgformat)
|
2019-06-21 04:08:38 +02:00
|
|
|
except self._serial.get_msgparser().error as e:
|
2017-09-04 04:02:48 +02:00
|
|
|
return None
|
2018-02-27 20:16:16 +01:00
|
|
|
def lookup_command_id(self, msgformat):
|
2019-06-21 04:08:38 +02:00
|
|
|
return self._serial.get_msgparser().lookup_command(msgformat).msgid
|
2019-04-01 02:30:14 +02:00
|
|
|
def get_enumerations(self):
|
2019-06-21 04:08:38 +02:00
|
|
|
return self._serial.get_msgparser().get_enumerations()
|
2019-04-01 02:30:14 +02:00
|
|
|
def get_constants(self):
|
2019-06-21 04:08:38 +02:00
|
|
|
return self._serial.get_msgparser().get_constants()
|
2017-09-19 17:13:44 +02:00
|
|
|
def get_constant_float(self, name):
|
2019-06-21 04:08:38 +02:00
|
|
|
return self._serial.get_msgparser().get_constant_float(name)
|
2017-09-12 18:24:44 +02:00
|
|
|
def print_time_to_clock(self, print_time):
|
2017-09-15 18:28:55 +02:00
|
|
|
return self._clocksync.print_time_to_clock(print_time)
|
2017-09-12 18:24:44 +02:00
|
|
|
def clock_to_print_time(self, clock):
|
2017-09-15 18:28:55 +02:00
|
|
|
return self._clocksync.clock_to_print_time(clock)
|
2017-09-12 18:24:44 +02:00
|
|
|
def estimated_print_time(self, eventtime):
|
2017-09-15 18:28:55 +02:00
|
|
|
return self._clocksync.estimated_print_time(eventtime)
|
|
|
|
def clock32_to_clock64(self, clock32):
|
|
|
|
return self._clocksync.clock32_to_clock64(clock32)
|
2017-09-20 22:02:43 +02:00
|
|
|
# Restarts
|
2018-01-20 04:49:27 +01:00
|
|
|
def _disconnect(self):
|
|
|
|
self._serial.disconnect()
|
2020-09-17 05:41:27 +02:00
|
|
|
self._steppersync = None
|
2018-01-20 04:49:27 +01:00
|
|
|
def _shutdown(self, force=False):
|
2018-07-26 16:54:31 +02:00
|
|
|
if (self._emergency_stop_cmd is None
|
|
|
|
or (self._is_shutdown and not force)):
|
2018-01-20 04:49:27 +01:00
|
|
|
return
|
2018-02-27 20:16:16 +01:00
|
|
|
self._emergency_stop_cmd.send()
|
2017-09-20 22:02:43 +02:00
|
|
|
def _restart_arduino(self):
|
|
|
|
logging.info("Attempting MCU '%s' reset", self._name)
|
2018-01-20 04:49:27 +01:00
|
|
|
self._disconnect()
|
2018-01-20 04:22:17 +01:00
|
|
|
serialhdl.arduino_reset(self._serialport, self._reactor)
|
2020-04-01 23:52:55 +02:00
|
|
|
def _restart_cheetah(self):
|
|
|
|
logging.info("Attempting MCU '%s' Cheetah-style reset", self._name)
|
|
|
|
self._disconnect()
|
|
|
|
serialhdl.cheetah_reset(self._serialport, self._reactor)
|
2017-09-20 22:02:43 +02:00
|
|
|
def _restart_via_command(self):
|
|
|
|
if ((self._reset_cmd is None and self._config_reset_cmd is None)
|
2018-06-01 00:18:23 +02:00
|
|
|
or not self._clocksync.is_active()):
|
2018-07-26 16:54:31 +02:00
|
|
|
logging.info("Unable to issue reset command on MCU '%s'",
|
|
|
|
self._name)
|
2017-09-20 22:02:43 +02:00
|
|
|
return
|
|
|
|
if self._reset_cmd is None:
|
|
|
|
# Attempt reset via config_reset command
|
|
|
|
logging.info("Attempting MCU '%s' config_reset command", self._name)
|
|
|
|
self._is_shutdown = True
|
2018-01-20 04:49:27 +01:00
|
|
|
self._shutdown(force=True)
|
2018-01-20 04:22:17 +01:00
|
|
|
self._reactor.pause(self._reactor.monotonic() + 0.015)
|
2018-02-27 20:16:16 +01:00
|
|
|
self._config_reset_cmd.send()
|
2017-09-20 22:02:43 +02:00
|
|
|
else:
|
|
|
|
# Attempt reset via reset command
|
|
|
|
logging.info("Attempting MCU '%s' reset command", self._name)
|
2018-02-27 20:16:16 +01:00
|
|
|
self._reset_cmd.send()
|
2018-01-20 04:22:17 +01:00
|
|
|
self._reactor.pause(self._reactor.monotonic() + 0.015)
|
2018-01-20 04:49:27 +01:00
|
|
|
self._disconnect()
|
2017-09-20 22:02:43 +02:00
|
|
|
def _restart_rpi_usb(self):
|
|
|
|
logging.info("Attempting MCU '%s' reset via rpi usb power", self._name)
|
2018-01-20 04:49:27 +01:00
|
|
|
self._disconnect()
|
2017-09-20 22:02:43 +02:00
|
|
|
chelper.run_hub_ctrl(0)
|
2018-01-20 04:22:17 +01:00
|
|
|
self._reactor.pause(self._reactor.monotonic() + 2.)
|
2017-09-20 22:02:43 +02:00
|
|
|
chelper.run_hub_ctrl(1)
|
|
|
|
def microcontroller_restart(self):
|
|
|
|
if self._restart_method == 'rpi_usb':
|
|
|
|
self._restart_rpi_usb()
|
|
|
|
elif self._restart_method == 'command':
|
|
|
|
self._restart_via_command()
|
2020-04-01 23:52:55 +02:00
|
|
|
elif self._restart_method == 'cheetah':
|
|
|
|
self._restart_cheetah()
|
2017-09-20 22:02:43 +02:00
|
|
|
else:
|
|
|
|
self._restart_arduino()
|
2017-09-19 18:10:06 +02:00
|
|
|
# Misc external commands
|
|
|
|
def is_fileoutput(self):
|
|
|
|
return self._printer.get_start_args().get('debugoutput') is not None
|
|
|
|
def is_shutdown(self):
|
|
|
|
return self._is_shutdown
|
2016-05-25 17:37:40 +02:00
|
|
|
def flush_moves(self, print_time):
|
2016-12-29 04:17:49 +01:00
|
|
|
if self._steppersync is None:
|
|
|
|
return
|
2017-09-12 18:24:44 +02:00
|
|
|
clock = self.print_time_to_clock(print_time)
|
2017-08-14 17:46:35 +02:00
|
|
|
if clock < 0:
|
|
|
|
return
|
2017-04-04 16:13:02 +02:00
|
|
|
ret = self._ffi_lib.steppersync_flush(self._steppersync, clock)
|
2017-02-06 17:37:03 +01:00
|
|
|
if ret:
|
2020-09-17 05:41:27 +02:00
|
|
|
raise error("Internal error in MCU '%s' stepcompress"
|
|
|
|
% (self._name,))
|
2017-09-19 18:10:06 +02:00
|
|
|
def check_active(self, print_time, eventtime):
|
2017-08-14 17:46:35 +02:00
|
|
|
if self._steppersync is None:
|
|
|
|
return
|
|
|
|
offset, freq = self._clocksync.calibrate_clock(print_time, eventtime)
|
|
|
|
self._ffi_lib.steppersync_set_time(self._steppersync, offset, freq)
|
2018-06-01 00:18:23 +02:00
|
|
|
if (self._clocksync.is_active() or self.is_fileoutput()
|
2018-01-08 16:55:37 +01:00
|
|
|
or self._is_timeout):
|
2017-09-19 18:10:06 +02:00
|
|
|
return
|
2018-01-08 16:55:37 +01:00
|
|
|
self._is_timeout = True
|
2017-08-14 17:46:35 +02:00
|
|
|
logging.info("Timeout with MCU '%s' (eventtime=%f)",
|
|
|
|
self._name, eventtime)
|
2017-10-12 21:15:14 +02:00
|
|
|
self._printer.invoke_shutdown("Lost communication with MCU '%s'" % (
|
2017-08-14 17:46:35 +02:00
|
|
|
self._name,))
|
2017-09-19 18:10:06 +02:00
|
|
|
def stats(self, eventtime):
|
2017-08-14 17:46:35 +02:00
|
|
|
msg = "%s: mcu_awake=%.03f mcu_task_avg=%.06f mcu_task_stddev=%.06f" % (
|
|
|
|
self._name, self._mcu_tick_awake, self._mcu_tick_avg,
|
|
|
|
self._mcu_tick_stddev)
|
2018-02-05 19:52:05 +01:00
|
|
|
return False, ' '.join([msg, self._serial.stats(eventtime),
|
|
|
|
self._clocksync.stats(eventtime)])
|
2017-08-21 17:25:26 +02:00
|
|
|
|
2017-09-06 03:15:24 +02:00
|
|
|
Common_MCU_errors = {
|
|
|
|
("Timer too close", "No next step", "Missed scheduling of next "): """
|
|
|
|
This is generally indicative of an intermittent
|
2017-09-06 05:07:45 +02:00
|
|
|
communication failure between micro-controller and host.""",
|
2017-09-06 03:15:24 +02:00
|
|
|
("ADC out of range",): """
|
|
|
|
This generally occurs when a heater temperature exceeds
|
2017-09-06 05:07:45 +02:00
|
|
|
its configured min_temp or max_temp.""",
|
2017-09-06 03:15:24 +02:00
|
|
|
("Rescheduled timer in the past", "Stepper too far in past"): """
|
|
|
|
This generally occurs when the micro-controller has been
|
|
|
|
requested to step at a rate higher than it is capable of
|
|
|
|
obtaining.""",
|
|
|
|
("Command request",): """
|
|
|
|
This generally occurs in response to an M112 G-Code command
|
|
|
|
or in response to an internal error in the host software.""",
|
|
|
|
}
|
|
|
|
|
|
|
|
def error_help(msg):
|
|
|
|
for prefixes, help_msg in Common_MCU_errors.items():
|
|
|
|
for prefix in prefixes:
|
|
|
|
if msg.startswith(prefix):
|
|
|
|
return help_msg
|
|
|
|
return ""
|
|
|
|
|
2018-07-13 04:26:32 +02:00
|
|
|
def add_printer_objects(config):
|
|
|
|
printer = config.get_printer()
|
2018-01-20 04:22:17 +01:00
|
|
|
reactor = printer.get_reactor()
|
|
|
|
mainsync = clocksync.ClockSync(reactor)
|
2018-07-13 04:26:32 +02:00
|
|
|
printer.add_object('mcu', MCU(config.getsection('mcu'), mainsync))
|
2017-08-14 17:46:35 +02:00
|
|
|
for s in config.get_prefix_sections('mcu '):
|
|
|
|
printer.add_object(s.section, MCU(
|
2018-07-13 04:26:32 +02:00
|
|
|
s, clocksync.SecondarySync(reactor, mainsync)))
|
2017-08-14 17:46:35 +02:00
|
|
|
|
|
|
|
def get_printer_mcu(printer, name):
|
2018-01-20 04:22:17 +01:00
|
|
|
if name == 'mcu':
|
|
|
|
return printer.lookup_object(name)
|
|
|
|
return printer.lookup_object('mcu ' + name)
|