2016-07-07 22:52:44 +03:00
|
|
|
# Code for coordinating events on the printer toolhead
|
|
|
|
#
|
2018-01-20 06:22:17 +03:00
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
2016-07-07 22:52:44 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-07-13 05:15:45 +03:00
|
|
|
import math, logging, importlib
|
2018-07-13 17:52:40 +03:00
|
|
|
import mcu, homing, chelper, kinematics.extruder
|
2016-07-12 19:04:06 +03:00
|
|
|
|
2016-07-07 22:52:44 +03:00
|
|
|
# Common suffixes: _d is distance (in mm), _v is velocity (in
|
2017-01-14 21:35:27 +03:00
|
|
|
# mm/second), _v2 is velocity squared (mm^2/s^2), _t is time (in
|
|
|
|
# seconds), _r is ratio (scalar between 0.0 and 1.0)
|
2016-07-07 22:52:44 +03:00
|
|
|
|
2016-07-07 23:01:58 +03:00
|
|
|
# Class to track each move request
|
2016-07-07 22:52:44 +03:00
|
|
|
class Move:
|
2017-01-14 20:41:49 +03:00
|
|
|
def __init__(self, toolhead, start_pos, end_pos, speed):
|
2016-07-07 22:52:44 +03:00
|
|
|
self.toolhead = toolhead
|
2016-10-26 02:11:05 +03:00
|
|
|
self.start_pos = tuple(start_pos)
|
|
|
|
self.end_pos = tuple(end_pos)
|
2017-01-14 20:41:49 +03:00
|
|
|
self.accel = toolhead.max_accel
|
2019-01-04 20:42:55 +03:00
|
|
|
velocity = min(speed, toolhead.max_velocity)
|
2018-07-13 17:52:40 +03:00
|
|
|
self.cmove = toolhead.cmove
|
2017-01-31 19:04:49 +03:00
|
|
|
self.is_kinematic_move = True
|
2016-10-26 02:11:05 +03:00
|
|
|
self.axes_d = axes_d = [end_pos[i] - start_pos[i] for i in (0, 1, 2, 3)]
|
2017-01-31 19:04:49 +03:00
|
|
|
self.move_d = move_d = math.sqrt(sum([d*d for d in axes_d[:3]]))
|
2018-09-11 20:23:20 +03:00
|
|
|
if move_d < .000000001:
|
2017-01-31 19:04:49 +03:00
|
|
|
# Extrude only move
|
2018-09-11 20:23:20 +03:00
|
|
|
self.end_pos = (start_pos[0], start_pos[1], start_pos[2],
|
|
|
|
end_pos[3])
|
|
|
|
axes_d[0] = axes_d[1] = axes_d[2] = 0.
|
2017-01-31 19:04:49 +03:00
|
|
|
self.move_d = move_d = abs(axes_d[3])
|
2019-01-04 20:42:55 +03:00
|
|
|
self.accel = 99999999.9
|
|
|
|
velocity = speed
|
2017-01-31 19:04:49 +03:00
|
|
|
self.is_kinematic_move = False
|
2019-01-04 20:42:55 +03:00
|
|
|
self.min_move_t = move_d / velocity
|
2017-01-14 21:35:27 +03:00
|
|
|
# Junction speeds are tracked in velocity squared. The
|
|
|
|
# delta_v2 is the maximum amount of this squared-velocity that
|
|
|
|
# can change in this move.
|
|
|
|
self.max_start_v2 = 0.
|
2019-01-04 20:42:55 +03:00
|
|
|
self.max_cruise_v2 = velocity**2
|
2017-01-14 21:35:27 +03:00
|
|
|
self.delta_v2 = 2.0 * move_d * self.accel
|
2017-01-31 23:29:16 +03:00
|
|
|
self.max_smoothed_v2 = 0.
|
|
|
|
self.smooth_delta_v2 = 2.0 * move_d * toolhead.max_accel_to_decel
|
2016-09-30 21:47:45 +03:00
|
|
|
def limit_speed(self, speed, accel):
|
2017-02-11 20:29:19 +03:00
|
|
|
speed2 = speed**2
|
|
|
|
if speed2 < self.max_cruise_v2:
|
|
|
|
self.max_cruise_v2 = speed2
|
|
|
|
self.min_move_t = self.move_d / speed
|
2017-01-31 19:04:49 +03:00
|
|
|
self.accel = min(self.accel, accel)
|
|
|
|
self.delta_v2 = 2.0 * self.move_d * self.accel
|
2017-01-31 23:29:16 +03:00
|
|
|
self.smooth_delta_v2 = min(self.smooth_delta_v2, self.delta_v2)
|
2016-07-07 22:52:44 +03:00
|
|
|
def calc_junction(self, prev_move):
|
2017-09-03 21:10:21 +03:00
|
|
|
if not self.is_kinematic_move or not prev_move.is_kinematic_move:
|
2016-09-30 21:47:45 +03:00
|
|
|
return
|
2017-01-28 06:06:54 +03:00
|
|
|
# Allow extruder to calculate its maximum junction
|
|
|
|
extruder_v2 = self.toolhead.extruder.calc_junction(prev_move, self)
|
2016-07-12 19:04:06 +03:00
|
|
|
# Find max velocity using approximated centripetal velocity as
|
|
|
|
# described at:
|
2016-07-07 22:52:44 +03:00
|
|
|
# https://onehossshay.wordpress.com/2011/09/24/improving_grbl_cornering_algorithm/
|
2017-09-03 21:10:21 +03:00
|
|
|
axes_d = self.axes_d
|
|
|
|
prev_axes_d = prev_move.axes_d
|
2017-01-31 19:04:49 +03:00
|
|
|
junction_cos_theta = -((axes_d[0] * prev_axes_d[0]
|
2017-09-03 21:10:21 +03:00
|
|
|
+ axes_d[1] * prev_axes_d[1]
|
|
|
|
+ axes_d[2] * prev_axes_d[2])
|
2016-07-07 22:52:44 +03:00
|
|
|
/ (self.move_d * prev_move.move_d))
|
|
|
|
if junction_cos_theta > 0.999999:
|
|
|
|
return
|
|
|
|
junction_cos_theta = max(junction_cos_theta, -0.999999)
|
2016-07-12 19:04:06 +03:00
|
|
|
sin_theta_d2 = math.sqrt(0.5*(1.0-junction_cos_theta))
|
2016-07-07 22:52:44 +03:00
|
|
|
R = self.toolhead.junction_deviation * sin_theta_d2 / (1. - sin_theta_d2)
|
2018-03-26 01:41:15 +03:00
|
|
|
tan_theta_d2 = sin_theta_d2 / math.sqrt(0.5*(1.0+junction_cos_theta))
|
|
|
|
move_centripetal_v2 = .5 * self.move_d * tan_theta_d2 * self.accel
|
|
|
|
prev_move_centripetal_v2 = (.5 * prev_move.move_d * tan_theta_d2
|
|
|
|
* prev_move.accel)
|
2017-01-14 21:35:27 +03:00
|
|
|
self.max_start_v2 = min(
|
2018-03-26 01:41:15 +03:00
|
|
|
R * self.accel, R * prev_move.accel,
|
|
|
|
move_centripetal_v2, prev_move_centripetal_v2,
|
|
|
|
extruder_v2, self.max_cruise_v2, prev_move.max_cruise_v2,
|
|
|
|
prev_move.max_start_v2 + prev_move.delta_v2)
|
2017-01-31 23:29:16 +03:00
|
|
|
self.max_smoothed_v2 = min(
|
|
|
|
self.max_start_v2
|
|
|
|
, prev_move.max_smoothed_v2 + prev_move.smooth_delta_v2)
|
2017-01-28 06:06:54 +03:00
|
|
|
def set_junction(self, start_v2, cruise_v2, end_v2):
|
2016-07-11 20:21:27 +03:00
|
|
|
# Determine accel, cruise, and decel portions of the move distance
|
2017-01-14 21:35:27 +03:00
|
|
|
inv_delta_v2 = 1. / self.delta_v2
|
|
|
|
self.accel_r = accel_r = (cruise_v2 - start_v2) * inv_delta_v2
|
|
|
|
self.decel_r = decel_r = (cruise_v2 - end_v2) * inv_delta_v2
|
|
|
|
self.cruise_r = cruise_r = 1. - accel_r - decel_r
|
2016-07-07 23:01:58 +03:00
|
|
|
# Determine move velocities
|
2017-01-14 21:35:27 +03:00
|
|
|
self.start_v = start_v = math.sqrt(start_v2)
|
|
|
|
self.cruise_v = cruise_v = math.sqrt(cruise_v2)
|
|
|
|
self.end_v = end_v = math.sqrt(end_v2)
|
2016-07-11 20:21:27 +03:00
|
|
|
# Determine time spent in each portion of move (time is the
|
|
|
|
# distance divided by average velocity)
|
2017-01-28 06:06:54 +03:00
|
|
|
self.accel_t = accel_r * self.move_d / ((start_v + cruise_v) * 0.5)
|
|
|
|
self.cruise_t = cruise_r * self.move_d / cruise_v
|
|
|
|
self.decel_t = decel_r * self.move_d / ((end_v + cruise_v) * 0.5)
|
|
|
|
def move(self):
|
2016-07-07 22:52:44 +03:00
|
|
|
# Generate step times for the move
|
|
|
|
next_move_time = self.toolhead.get_next_move_time()
|
2016-12-28 19:48:40 +03:00
|
|
|
if self.is_kinematic_move:
|
2018-07-13 17:52:40 +03:00
|
|
|
self.toolhead.move_fill(
|
|
|
|
self.cmove, next_move_time,
|
|
|
|
self.accel_t, self.cruise_t, self.decel_t,
|
|
|
|
self.start_pos[0], self.start_pos[1], self.start_pos[2],
|
|
|
|
self.axes_d[0], self.axes_d[1], self.axes_d[2],
|
|
|
|
self.start_v, self.cruise_v, self.accel)
|
2016-12-28 19:48:40 +03:00
|
|
|
self.toolhead.kin.move(next_move_time, self)
|
2016-07-10 19:23:35 +03:00
|
|
|
if self.axes_d[3]:
|
|
|
|
self.toolhead.extruder.move(next_move_time, self)
|
2017-01-28 06:06:54 +03:00
|
|
|
self.toolhead.update_move_time(
|
|
|
|
self.accel_t + self.cruise_t + self.decel_t)
|
2016-07-07 22:52:44 +03:00
|
|
|
|
2017-02-11 20:29:19 +03:00
|
|
|
LOOKAHEAD_FLUSH_TIME = 0.250
|
|
|
|
|
2016-07-07 23:01:58 +03:00
|
|
|
# Class to track a list of pending move requests and to facilitate
|
|
|
|
# "look-ahead" across moves to reduce acceleration between moves.
|
|
|
|
class MoveQueue:
|
2017-04-29 21:56:39 +03:00
|
|
|
def __init__(self):
|
|
|
|
self.extruder_lookahead = None
|
2016-07-07 23:01:58 +03:00
|
|
|
self.queue = []
|
2017-01-31 21:42:52 +03:00
|
|
|
self.leftover = 0
|
2017-02-11 20:29:19 +03:00
|
|
|
self.junction_flush = LOOKAHEAD_FLUSH_TIME
|
2016-09-19 20:18:55 +03:00
|
|
|
def reset(self):
|
|
|
|
del self.queue[:]
|
2017-01-31 21:42:52 +03:00
|
|
|
self.leftover = 0
|
2017-02-11 20:29:19 +03:00
|
|
|
self.junction_flush = LOOKAHEAD_FLUSH_TIME
|
2017-02-11 22:43:36 +03:00
|
|
|
def set_flush_time(self, flush_time):
|
|
|
|
self.junction_flush = flush_time
|
2017-04-29 21:56:39 +03:00
|
|
|
def set_extruder(self, extruder):
|
|
|
|
self.extruder_lookahead = extruder.lookahead
|
2016-07-07 23:01:58 +03:00
|
|
|
def flush(self, lazy=False):
|
2017-02-11 20:29:19 +03:00
|
|
|
self.junction_flush = LOOKAHEAD_FLUSH_TIME
|
2017-01-28 06:06:54 +03:00
|
|
|
update_flush_count = lazy
|
|
|
|
queue = self.queue
|
|
|
|
flush_count = len(queue)
|
2016-07-11 20:21:27 +03:00
|
|
|
# Traverse queue from last to first move and determine maximum
|
|
|
|
# junction speed assuming the robot comes to a complete stop
|
|
|
|
# after the last move.
|
2017-01-31 23:29:16 +03:00
|
|
|
delayed = []
|
|
|
|
next_end_v2 = next_smoothed_v2 = peak_cruise_v2 = 0.
|
2017-01-31 21:42:52 +03:00
|
|
|
for i in range(flush_count-1, self.leftover-1, -1):
|
2017-01-28 06:06:54 +03:00
|
|
|
move = queue[i]
|
2017-01-14 21:35:27 +03:00
|
|
|
reachable_start_v2 = next_end_v2 + move.delta_v2
|
|
|
|
start_v2 = min(move.max_start_v2, reachable_start_v2)
|
2017-01-31 23:29:16 +03:00
|
|
|
reachable_smoothed_v2 = next_smoothed_v2 + move.smooth_delta_v2
|
|
|
|
smoothed_v2 = min(move.max_smoothed_v2, reachable_smoothed_v2)
|
|
|
|
if smoothed_v2 < reachable_smoothed_v2:
|
|
|
|
# It's possible for this move to accelerate
|
|
|
|
if (smoothed_v2 + move.smooth_delta_v2 > next_smoothed_v2
|
2017-02-20 20:37:43 +03:00
|
|
|
or delayed):
|
|
|
|
# This move can decelerate or this is a full accel
|
|
|
|
# move after a full decel move
|
2017-01-31 23:29:16 +03:00
|
|
|
if update_flush_count and peak_cruise_v2:
|
|
|
|
flush_count = i
|
|
|
|
update_flush_count = False
|
|
|
|
peak_cruise_v2 = min(move.max_cruise_v2, (
|
|
|
|
smoothed_v2 + reachable_smoothed_v2) * .5)
|
|
|
|
if delayed:
|
|
|
|
# Propagate peak_cruise_v2 to any delayed moves
|
2017-02-20 20:37:43 +03:00
|
|
|
if not update_flush_count and i < flush_count:
|
|
|
|
for m, ms_v2, me_v2 in delayed:
|
|
|
|
mc_v2 = min(peak_cruise_v2, ms_v2)
|
|
|
|
m.set_junction(min(ms_v2, mc_v2), mc_v2
|
|
|
|
, min(me_v2, mc_v2))
|
2017-01-31 23:29:16 +03:00
|
|
|
del delayed[:]
|
|
|
|
if not update_flush_count and i < flush_count:
|
2017-02-20 20:37:43 +03:00
|
|
|
cruise_v2 = min((start_v2 + reachable_start_v2) * .5
|
|
|
|
, move.max_cruise_v2, peak_cruise_v2)
|
2017-01-31 23:29:16 +03:00
|
|
|
move.set_junction(min(start_v2, cruise_v2), cruise_v2
|
|
|
|
, min(next_end_v2, cruise_v2))
|
2017-02-20 20:37:43 +03:00
|
|
|
else:
|
2017-01-31 23:29:16 +03:00
|
|
|
# Delay calculating this move until peak_cruise_v2 is known
|
|
|
|
delayed.append((move, start_v2, next_end_v2))
|
2017-01-14 21:35:27 +03:00
|
|
|
next_end_v2 = start_v2
|
2017-01-31 23:29:16 +03:00
|
|
|
next_smoothed_v2 = smoothed_v2
|
2017-01-28 06:06:54 +03:00
|
|
|
if update_flush_count:
|
2017-02-11 20:29:19 +03:00
|
|
|
return
|
2017-01-28 06:06:54 +03:00
|
|
|
# Allow extruder to do its lookahead
|
2017-01-31 21:42:52 +03:00
|
|
|
move_count = self.extruder_lookahead(queue, flush_count, lazy)
|
2016-07-11 20:21:27 +03:00
|
|
|
# Generate step times for all moves ready to be flushed
|
2017-01-31 21:42:52 +03:00
|
|
|
for move in queue[:move_count]:
|
2017-01-28 06:06:54 +03:00
|
|
|
move.move()
|
2016-07-11 20:21:27 +03:00
|
|
|
# Remove processed moves from the queue
|
2017-01-31 21:42:52 +03:00
|
|
|
self.leftover = flush_count - move_count
|
|
|
|
del queue[:move_count]
|
2016-07-07 23:01:58 +03:00
|
|
|
def add_move(self, move):
|
|
|
|
self.queue.append(move)
|
|
|
|
if len(self.queue) == 1:
|
|
|
|
return
|
2016-07-07 23:08:00 +03:00
|
|
|
move.calc_junction(self.queue[-2])
|
2017-02-11 20:29:19 +03:00
|
|
|
self.junction_flush -= move.min_move_t
|
2016-07-07 23:01:58 +03:00
|
|
|
if self.junction_flush <= 0.:
|
2018-09-25 22:44:45 +03:00
|
|
|
# Enough moves have been queued to reach the target flush time.
|
2016-07-07 23:01:58 +03:00
|
|
|
self.flush(lazy=True)
|
|
|
|
|
2016-07-07 22:52:44 +03:00
|
|
|
STALL_TIME = 0.100
|
|
|
|
|
2016-07-07 23:01:58 +03:00
|
|
|
# Main code to track events (and their timing) on the printer toolhead
|
2016-07-07 22:52:44 +03:00
|
|
|
class ToolHead:
|
2018-06-21 20:47:39 +03:00
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.reactor = self.printer.get_reactor()
|
2018-09-02 19:51:37 +03:00
|
|
|
self.all_mcus = [
|
|
|
|
m for n, m in self.printer.lookup_objects(module='mcu')]
|
2017-08-14 18:46:35 +03:00
|
|
|
self.mcu = self.all_mcus[0]
|
2018-07-08 21:08:21 +03:00
|
|
|
self.move_queue = MoveQueue()
|
|
|
|
self.commanded_pos = [0., 0., 0., 0.]
|
|
|
|
# Velocity and acceleration control
|
2017-09-03 22:17:02 +03:00
|
|
|
self.max_velocity = config.getfloat('max_velocity', above=0.)
|
2017-04-11 18:37:09 +03:00
|
|
|
self.max_accel = config.getfloat('max_accel', above=0.)
|
2018-04-21 05:13:29 +03:00
|
|
|
self.requested_accel_to_decel = config.getfloat(
|
|
|
|
'max_accel_to_decel', self.max_accel * 0.5, above=0.)
|
2018-10-02 05:02:33 +03:00
|
|
|
self.max_accel_to_decel = self.requested_accel_to_decel
|
2018-07-08 21:08:21 +03:00
|
|
|
self.square_corner_velocity = config.getfloat(
|
|
|
|
'square_corner_velocity', 5., minval=0.)
|
2018-04-21 05:13:29 +03:00
|
|
|
self.config_max_velocity = self.max_velocity
|
|
|
|
self.config_max_accel = self.max_accel
|
2018-07-08 21:08:21 +03:00
|
|
|
self.config_square_corner_velocity = self.square_corner_velocity
|
|
|
|
self.junction_deviation = 0.
|
|
|
|
self._calc_junction_deviation()
|
2016-07-07 22:52:44 +03:00
|
|
|
# Print time tracking
|
2017-04-11 18:37:09 +03:00
|
|
|
self.buffer_time_low = config.getfloat(
|
|
|
|
'buffer_time_low', 1.000, above=0.)
|
|
|
|
self.buffer_time_high = config.getfloat(
|
|
|
|
'buffer_time_high', 2.000, above=self.buffer_time_low)
|
|
|
|
self.buffer_time_start = config.getfloat(
|
|
|
|
'buffer_time_start', 0.250, above=0.)
|
|
|
|
self.move_flush_time = config.getfloat(
|
|
|
|
'move_flush_time', 0.050, above=0.)
|
2016-07-07 22:52:44 +03:00
|
|
|
self.print_time = 0.
|
2018-02-21 04:50:06 +03:00
|
|
|
self.last_print_start_time = 0.
|
2016-12-09 17:41:46 +03:00
|
|
|
self.need_check_stall = -1.
|
2017-02-11 22:43:36 +03:00
|
|
|
self.print_stall = 0
|
2017-09-12 19:24:44 +03:00
|
|
|
self.sync_print_time = True
|
2018-01-13 00:24:47 +03:00
|
|
|
self.idle_flush_print_time = 0.
|
2016-12-09 17:41:46 +03:00
|
|
|
self.flush_timer = self.reactor.register_timer(self._flush_handler)
|
2017-02-11 22:43:36 +03:00
|
|
|
self.move_queue.set_flush_time(self.buffer_time_high)
|
2018-07-13 02:07:54 +03:00
|
|
|
self.printer.try_load_module(config, "idle_timeout")
|
2018-09-02 20:07:43 +03:00
|
|
|
self.printer.try_load_module(config, "statistics")
|
2018-07-13 17:52:40 +03:00
|
|
|
# Setup iterative solver
|
|
|
|
ffi_main, ffi_lib = chelper.get_ffi()
|
|
|
|
self.cmove = ffi_main.gc(ffi_lib.move_alloc(), ffi_lib.free)
|
|
|
|
self.move_fill = ffi_lib.move_fill
|
2017-09-03 22:17:02 +03:00
|
|
|
# Create kinematics class
|
2018-07-13 05:15:45 +03:00
|
|
|
self.extruder = kinematics.extruder.DummyExtruder()
|
2017-09-03 22:17:02 +03:00
|
|
|
self.move_queue.set_extruder(self.extruder)
|
2018-07-13 05:15:45 +03:00
|
|
|
kin_name = config.get('kinematics')
|
|
|
|
try:
|
|
|
|
mod = importlib.import_module('kinematics.' + kin_name)
|
|
|
|
self.kin = mod.load_kinematics(self, config)
|
2018-08-30 17:14:32 +03:00
|
|
|
except config.error as e:
|
|
|
|
raise
|
|
|
|
except self.printer.lookup_object('pins').error as e:
|
|
|
|
raise
|
2018-07-13 05:15:45 +03:00
|
|
|
except:
|
|
|
|
msg = "Error loading kinematics '%s'" % (kin_name,)
|
|
|
|
logging.exception(msg)
|
|
|
|
raise config.error(msg)
|
2018-04-21 05:13:29 +03:00
|
|
|
# SET_VELOCITY_LIMIT command
|
2018-06-21 20:47:39 +03:00
|
|
|
gcode = self.printer.lookup_object('gcode')
|
2018-04-21 05:13:29 +03:00
|
|
|
gcode.register_command('SET_VELOCITY_LIMIT', self.cmd_SET_VELOCITY_LIMIT,
|
|
|
|
desc=self.cmd_SET_VELOCITY_LIMIT_help)
|
2018-06-15 19:00:33 +03:00
|
|
|
gcode.register_command('M204', self.cmd_M204)
|
2016-07-07 22:52:44 +03:00
|
|
|
# Print time tracking
|
|
|
|
def update_move_time(self, movetime):
|
|
|
|
self.print_time += movetime
|
|
|
|
flush_to_time = self.print_time - self.move_flush_time
|
2017-08-14 18:46:35 +03:00
|
|
|
for m in self.all_mcus:
|
|
|
|
m.flush_moves(flush_to_time)
|
2018-10-16 01:24:15 +03:00
|
|
|
def _calc_print_time(self):
|
2018-10-18 19:04:50 +03:00
|
|
|
curtime = self.reactor.monotonic()
|
|
|
|
est_print_time = self.mcu.estimated_print_time(curtime)
|
2018-02-21 04:50:06 +03:00
|
|
|
if est_print_time + self.buffer_time_start > self.print_time:
|
|
|
|
self.print_time = est_print_time + self.buffer_time_start
|
|
|
|
self.last_print_start_time = self.print_time
|
2018-10-18 19:04:50 +03:00
|
|
|
self.printer.send_event("toolhead:sync_print_time",
|
|
|
|
curtime, est_print_time, self.print_time)
|
2018-10-16 01:24:15 +03:00
|
|
|
def get_next_move_time(self):
|
|
|
|
if self.sync_print_time:
|
|
|
|
self.sync_print_time = False
|
|
|
|
self.reactor.update_timer(self.flush_timer, self.reactor.NOW)
|
|
|
|
self._calc_print_time()
|
2016-07-07 22:52:44 +03:00
|
|
|
return self.print_time
|
2017-09-12 19:24:44 +03:00
|
|
|
def _flush_lookahead(self, must_sync=False):
|
|
|
|
sync_print_time = self.sync_print_time
|
2016-07-07 22:52:44 +03:00
|
|
|
self.move_queue.flush()
|
2018-01-13 00:24:47 +03:00
|
|
|
self.idle_flush_print_time = 0.
|
2017-09-12 19:24:44 +03:00
|
|
|
if sync_print_time or must_sync:
|
|
|
|
self.sync_print_time = True
|
2017-02-11 22:43:36 +03:00
|
|
|
self.move_queue.set_flush_time(self.buffer_time_high)
|
2017-09-12 19:24:44 +03:00
|
|
|
self.need_check_stall = -1.
|
|
|
|
self.reactor.update_timer(self.flush_timer, self.reactor.NEVER)
|
2017-08-14 18:46:35 +03:00
|
|
|
for m in self.all_mcus:
|
|
|
|
m.flush_moves(self.print_time)
|
2017-02-11 22:43:36 +03:00
|
|
|
def get_last_move_time(self):
|
|
|
|
self._flush_lookahead()
|
2018-10-16 01:24:15 +03:00
|
|
|
if self.sync_print_time:
|
|
|
|
self._calc_print_time()
|
|
|
|
return self.print_time
|
2017-09-28 02:21:26 +03:00
|
|
|
def reset_print_time(self, min_print_time=0.):
|
2017-09-12 19:24:44 +03:00
|
|
|
self._flush_lookahead(must_sync=True)
|
2018-10-16 01:24:15 +03:00
|
|
|
est_print_time = self.mcu.estimated_print_time(self.reactor.monotonic())
|
|
|
|
self.print_time = max(min_print_time, est_print_time)
|
2016-12-09 17:41:46 +03:00
|
|
|
def _check_stall(self):
|
2017-02-06 22:07:55 +03:00
|
|
|
eventtime = self.reactor.monotonic()
|
2017-09-12 19:24:44 +03:00
|
|
|
if self.sync_print_time:
|
2017-02-06 22:07:55 +03:00
|
|
|
# Building initial queue - make sure to flush on idle input
|
2018-01-13 00:24:47 +03:00
|
|
|
if self.idle_flush_print_time:
|
|
|
|
est_print_time = self.mcu.estimated_print_time(eventtime)
|
|
|
|
if est_print_time < self.idle_flush_print_time:
|
|
|
|
self.print_stall += 1
|
|
|
|
self.idle_flush_print_time = 0.
|
2017-02-06 22:07:55 +03:00
|
|
|
self.reactor.update_timer(self.flush_timer, eventtime + 0.100)
|
2016-12-09 17:41:46 +03:00
|
|
|
return
|
2017-02-06 22:07:55 +03:00
|
|
|
# Check if there are lots of queued moves and stall if so
|
2016-12-09 17:41:46 +03:00
|
|
|
while 1:
|
2017-09-12 19:24:44 +03:00
|
|
|
est_print_time = self.mcu.estimated_print_time(eventtime)
|
|
|
|
buffer_time = self.print_time - est_print_time
|
2016-12-09 17:41:46 +03:00
|
|
|
stall_time = buffer_time - self.buffer_time_high
|
|
|
|
if stall_time <= 0.:
|
|
|
|
break
|
2017-09-12 19:24:44 +03:00
|
|
|
if self.mcu.is_fileoutput():
|
|
|
|
self.need_check_stall = self.reactor.NEVER
|
2016-12-09 17:41:46 +03:00
|
|
|
return
|
2017-09-12 19:24:44 +03:00
|
|
|
eventtime = self.reactor.pause(eventtime + min(1., stall_time))
|
|
|
|
self.need_check_stall = est_print_time + self.buffer_time_high + 0.100
|
2016-12-09 17:41:46 +03:00
|
|
|
def _flush_handler(self, eventtime):
|
2016-09-19 20:18:55 +03:00
|
|
|
try:
|
|
|
|
print_time = self.print_time
|
2017-09-12 19:24:44 +03:00
|
|
|
buffer_time = print_time - self.mcu.estimated_print_time(eventtime)
|
2016-09-19 20:18:55 +03:00
|
|
|
if buffer_time > self.buffer_time_low:
|
2017-02-06 22:07:55 +03:00
|
|
|
# Running normally - reschedule check
|
2016-09-19 20:18:55 +03:00
|
|
|
return eventtime + buffer_time - self.buffer_time_low
|
2017-02-06 22:07:55 +03:00
|
|
|
# Under ran low buffer mark - flush lookahead queue
|
2017-09-12 19:24:44 +03:00
|
|
|
self._flush_lookahead(must_sync=True)
|
2016-09-19 20:18:55 +03:00
|
|
|
if print_time != self.print_time:
|
2018-01-13 00:24:47 +03:00
|
|
|
self.idle_flush_print_time = self.print_time
|
2016-09-19 20:18:55 +03:00
|
|
|
except:
|
|
|
|
logging.exception("Exception in flush_handler")
|
2017-10-12 22:15:14 +03:00
|
|
|
self.printer.invoke_shutdown("Exception in flush_handler")
|
2017-02-06 22:07:55 +03:00
|
|
|
return self.reactor.NEVER
|
2016-07-07 22:52:44 +03:00
|
|
|
# Movement commands
|
|
|
|
def get_position(self):
|
|
|
|
return list(self.commanded_pos)
|
2018-01-17 02:58:41 +03:00
|
|
|
def set_position(self, newpos, homing_axes=()):
|
2017-02-11 22:43:36 +03:00
|
|
|
self._flush_lookahead()
|
2016-07-07 22:52:44 +03:00
|
|
|
self.commanded_pos[:] = newpos
|
2018-01-17 02:58:41 +03:00
|
|
|
self.kin.set_position(newpos, homing_axes)
|
2016-12-01 06:34:38 +03:00
|
|
|
def move(self, newpos, speed):
|
2017-01-14 20:41:49 +03:00
|
|
|
move = Move(self, self.commanded_pos, newpos, speed)
|
2016-10-26 02:11:05 +03:00
|
|
|
if not move.move_d:
|
|
|
|
return
|
2016-12-28 19:48:40 +03:00
|
|
|
if move.is_kinematic_move:
|
|
|
|
self.kin.check_move(move)
|
2016-10-26 02:11:05 +03:00
|
|
|
if move.axes_d[3]:
|
2016-09-30 21:47:45 +03:00
|
|
|
self.extruder.check_move(move)
|
2018-09-11 20:45:33 +03:00
|
|
|
self.commanded_pos[:] = move.end_pos
|
2016-07-07 22:52:44 +03:00
|
|
|
self.move_queue.add_move(move)
|
2016-12-09 17:41:46 +03:00
|
|
|
if self.print_time > self.need_check_stall:
|
|
|
|
self._check_stall()
|
2017-11-30 03:53:16 +03:00
|
|
|
def dwell(self, delay, check_stall=True):
|
2016-07-07 22:52:44 +03:00
|
|
|
self.get_last_move_time()
|
|
|
|
self.update_move_time(delay)
|
2017-11-30 03:53:16 +03:00
|
|
|
if check_stall:
|
|
|
|
self._check_stall()
|
2016-07-07 22:52:44 +03:00
|
|
|
def motor_off(self):
|
|
|
|
self.dwell(STALL_TIME)
|
|
|
|
last_move_time = self.get_last_move_time()
|
|
|
|
self.kin.motor_off(last_move_time)
|
2018-07-13 05:15:45 +03:00
|
|
|
for ext in kinematics.extruder.get_printer_extruders(self.printer):
|
2018-06-24 03:41:43 +03:00
|
|
|
ext.motor_off(last_move_time)
|
2016-07-07 22:52:44 +03:00
|
|
|
self.dwell(STALL_TIME)
|
2017-09-27 18:43:14 +03:00
|
|
|
logging.debug('; Max time of %f', last_move_time)
|
2016-12-29 05:24:12 +03:00
|
|
|
def wait_moves(self):
|
2017-02-11 22:43:36 +03:00
|
|
|
self._flush_lookahead()
|
2017-09-13 19:06:08 +03:00
|
|
|
if self.mcu.is_fileoutput():
|
2017-08-22 00:19:43 +03:00
|
|
|
return
|
2017-02-06 21:31:34 +03:00
|
|
|
eventtime = self.reactor.monotonic()
|
2017-09-12 19:24:44 +03:00
|
|
|
while (not self.sync_print_time
|
|
|
|
or self.print_time >= self.mcu.estimated_print_time(eventtime)):
|
2016-12-29 05:24:12 +03:00
|
|
|
eventtime = self.reactor.pause(eventtime + 0.100)
|
2017-04-29 21:56:39 +03:00
|
|
|
def set_extruder(self, extruder):
|
|
|
|
last_move_time = self.get_last_move_time()
|
|
|
|
self.extruder.set_active(last_move_time, False)
|
|
|
|
extrude_pos = extruder.set_active(last_move_time, True)
|
|
|
|
self.extruder = extruder
|
|
|
|
self.move_queue.set_extruder(extruder)
|
|
|
|
self.commanded_pos[3] = extrude_pos
|
2018-05-20 19:52:19 +03:00
|
|
|
def get_extruder(self):
|
|
|
|
return self.extruder
|
2017-02-06 22:07:55 +03:00
|
|
|
# Misc commands
|
2018-02-05 21:52:05 +03:00
|
|
|
def stats(self, eventtime):
|
2017-08-14 18:46:35 +03:00
|
|
|
for m in self.all_mcus:
|
|
|
|
m.check_active(self.print_time, eventtime)
|
2018-02-05 21:52:05 +03:00
|
|
|
buffer_time = self.print_time - self.mcu.estimated_print_time(eventtime)
|
|
|
|
is_active = buffer_time > -60. or not self.sync_print_time
|
|
|
|
return is_active, "print_time=%.3f buffer_time=%.3f print_stall=%d" % (
|
|
|
|
self.print_time, max(buffer_time, 0.), self.print_stall)
|
2018-10-18 19:04:50 +03:00
|
|
|
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
|
2018-02-21 04:50:06 +03:00
|
|
|
def get_status(self, eventtime):
|
2018-07-13 02:07:54 +03:00
|
|
|
print_time = self.print_time
|
|
|
|
estimated_print_time = self.mcu.estimated_print_time(eventtime)
|
|
|
|
last_print_start_time = self.last_print_start_time
|
|
|
|
buffer_time = print_time - estimated_print_time
|
2018-02-21 04:50:06 +03:00
|
|
|
if buffer_time > -1. or not self.sync_print_time:
|
|
|
|
status = "Printing"
|
|
|
|
else:
|
2018-07-13 02:07:54 +03:00
|
|
|
status = "Ready"
|
|
|
|
return { 'status': status, 'print_time': print_time,
|
|
|
|
'estimated_print_time': estimated_print_time,
|
|
|
|
'printing_time': print_time - last_print_start_time }
|
2018-01-20 06:49:27 +03:00
|
|
|
def printer_state(self, state):
|
|
|
|
if state == 'shutdown':
|
|
|
|
try:
|
|
|
|
self.move_queue.reset()
|
|
|
|
self.reset_print_time()
|
|
|
|
except:
|
|
|
|
logging.exception("Exception in toolhead shutdown")
|
2017-12-06 17:59:00 +03:00
|
|
|
def get_kinematics(self):
|
|
|
|
return self.kin
|
2017-09-03 22:17:02 +03:00
|
|
|
def get_max_velocity(self):
|
|
|
|
return self.max_velocity, self.max_accel
|
2017-10-12 07:48:01 +03:00
|
|
|
def get_max_axis_halt(self):
|
2017-09-03 22:17:02 +03:00
|
|
|
# Determine the maximum velocity a cartesian axis could halt
|
|
|
|
# at due to the junction_deviation setting. The 8.0 was
|
|
|
|
# determined experimentally.
|
2017-10-12 07:48:01 +03:00
|
|
|
return min(self.max_velocity,
|
|
|
|
math.sqrt(8. * self.junction_deviation * self.max_accel))
|
2018-07-08 21:08:21 +03:00
|
|
|
def _calc_junction_deviation(self):
|
|
|
|
scv2 = self.square_corner_velocity**2
|
|
|
|
self.junction_deviation = scv2 * (math.sqrt(2.) - 1.) / self.max_accel
|
2018-10-02 05:02:33 +03:00
|
|
|
self.max_accel_to_decel = min(self.requested_accel_to_decel,
|
|
|
|
self.max_accel)
|
2018-04-21 05:13:29 +03:00
|
|
|
cmd_SET_VELOCITY_LIMIT_help = "Set printer velocity limits"
|
|
|
|
def cmd_SET_VELOCITY_LIMIT(self, params):
|
|
|
|
print_time = self.get_last_move_time()
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
2019-01-04 20:35:42 +03:00
|
|
|
max_velocity = gcode.get_float('VELOCITY', params, self.max_velocity,
|
|
|
|
above=0.)
|
|
|
|
max_accel = gcode.get_float('ACCEL', params, self.max_accel, above=0.)
|
2018-07-08 21:08:21 +03:00
|
|
|
square_corner_velocity = gcode.get_float(
|
|
|
|
'SQUARE_CORNER_VELOCITY', params, self.square_corner_velocity,
|
2019-01-04 20:35:42 +03:00
|
|
|
minval=0.)
|
2018-04-21 05:13:29 +03:00
|
|
|
self.requested_accel_to_decel = gcode.get_float(
|
|
|
|
'ACCEL_TO_DECEL', params, self.requested_accel_to_decel, above=0.)
|
2019-01-04 20:35:42 +03:00
|
|
|
self.max_velocity = min(max_velocity, self.config_max_velocity)
|
|
|
|
self.max_accel = min(max_accel, self.config_max_accel)
|
|
|
|
self.square_corner_velocity = min(square_corner_velocity,
|
|
|
|
self.config_square_corner_velocity)
|
2018-07-08 21:08:21 +03:00
|
|
|
self._calc_junction_deviation()
|
2018-04-21 05:13:29 +03:00
|
|
|
msg = ("max_velocity: %.6f\n"
|
|
|
|
"max_accel: %.6f\n"
|
|
|
|
"max_accel_to_decel: %.6f\n"
|
2018-07-08 21:08:21 +03:00
|
|
|
"square_corner_velocity: %.6f"% (
|
2018-04-21 05:13:29 +03:00
|
|
|
max_velocity, max_accel, self.requested_accel_to_decel,
|
2018-07-08 21:08:21 +03:00
|
|
|
square_corner_velocity))
|
2018-04-21 05:13:29 +03:00
|
|
|
self.printer.set_rollover_info("toolhead", "toolhead: %s" % (msg,))
|
|
|
|
gcode.respond_info(msg)
|
2018-06-15 19:00:33 +03:00
|
|
|
def cmd_M204(self, params):
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
2018-09-02 19:22:43 +03:00
|
|
|
if 'P' in params and 'T' in params and 'S' not in params:
|
|
|
|
# Use minimum of P and T for accel
|
|
|
|
accel = min(gcode.get_float('P', params, above=0.),
|
|
|
|
gcode.get_float('T', params, above=0.))
|
|
|
|
else:
|
|
|
|
# Use S for accel
|
|
|
|
accel = gcode.get_float('S', params, above=0.)
|
2018-06-15 19:00:33 +03:00
|
|
|
self.max_accel = min(accel, self.config_max_accel)
|
2018-07-08 21:08:21 +03:00
|
|
|
self._calc_junction_deviation()
|
2017-04-29 20:57:02 +03:00
|
|
|
|
2018-07-13 05:26:32 +03:00
|
|
|
def add_printer_objects(config):
|
|
|
|
config.get_printer().add_object('toolhead', ToolHead(config))
|
2018-07-13 05:15:45 +03:00
|
|
|
kinematics.extruder.add_printer_objects(config)
|