2016-07-07 22:52:44 +03:00
|
|
|
# Code for coordinating events on the printer toolhead
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import math, logging, time
|
2016-09-15 19:20:49 +03:00
|
|
|
import cartesian, delta
|
2016-07-07 22:52:44 +03:00
|
|
|
|
2016-07-12 19:04:06 +03:00
|
|
|
EXTRUDE_DIFF_IGNORE = 1.02
|
|
|
|
|
2016-07-07 22:52:44 +03:00
|
|
|
# Common suffixes: _d is distance (in mm), _v is velocity (in
|
|
|
|
# mm/second), _t is time (in seconds), _r is ratio (scalar between
|
|
|
|
# 0.0 and 1.0)
|
|
|
|
|
2016-07-07 23:01:58 +03:00
|
|
|
# Class to track each move request
|
2016-07-07 22:52:44 +03:00
|
|
|
class Move:
|
2016-10-26 02:11:05 +03:00
|
|
|
def __init__(self, toolhead, start_pos, end_pos, speed, accel):
|
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)
|
2016-07-07 22:52:44 +03:00
|
|
|
self.accel = accel
|
2016-12-28 19:48:40 +03:00
|
|
|
self.do_calc_junction = 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)]
|
2016-09-30 21:47:45 +03:00
|
|
|
if axes_d[2]:
|
|
|
|
# Move with Z
|
|
|
|
move_d = math.sqrt(sum([d*d for d in axes_d[:3]]))
|
|
|
|
self.do_calc_junction = False
|
|
|
|
else:
|
|
|
|
move_d = math.sqrt(axes_d[0]**2 + axes_d[1]**2)
|
|
|
|
if not move_d:
|
|
|
|
# Extrude only move
|
|
|
|
move_d = abs(axes_d[3])
|
2016-10-26 02:11:05 +03:00
|
|
|
if not move_d:
|
|
|
|
# No move
|
|
|
|
self.move_d = 0.
|
|
|
|
return
|
2016-12-28 19:48:40 +03:00
|
|
|
self.do_calc_junction = self.is_kinematic_move = False
|
2016-09-30 21:47:45 +03:00
|
|
|
self.move_d = move_d
|
2016-07-12 19:04:06 +03:00
|
|
|
self.extrude_r = axes_d[3] / move_d
|
2016-07-11 20:21:27 +03:00
|
|
|
# Junction speeds are velocities squared. The junction_delta
|
|
|
|
# is the maximum amount of this squared-velocity that can
|
|
|
|
# change in this move.
|
2016-07-07 22:52:44 +03:00
|
|
|
self.junction_max = speed**2
|
|
|
|
self.junction_delta = 2.0 * move_d * accel
|
|
|
|
self.junction_start_max = 0.
|
2016-09-30 21:47:45 +03:00
|
|
|
def limit_speed(self, speed, accel):
|
|
|
|
self.junction_max = min(self.junction_max, speed**2)
|
|
|
|
self.accel = min(self.accel, accel)
|
|
|
|
self.junction_delta = 2.0 * self.move_d * self.accel
|
2016-07-07 22:52:44 +03:00
|
|
|
def calc_junction(self, prev_move):
|
2016-09-30 21:47:45 +03:00
|
|
|
if not self.do_calc_junction or not prev_move.do_calc_junction:
|
|
|
|
return
|
2016-07-12 19:04:06 +03:00
|
|
|
# Find max junction_start_velocity between two moves
|
|
|
|
if (self.extrude_r > prev_move.extrude_r * EXTRUDE_DIFF_IGNORE
|
|
|
|
or prev_move.extrude_r > self.extrude_r * EXTRUDE_DIFF_IGNORE):
|
|
|
|
# Extrude ratio between moves is too different
|
|
|
|
return
|
|
|
|
self.extrude_r = prev_move.extrude_r
|
|
|
|
# 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/
|
|
|
|
junction_cos_theta = -((self.axes_d[0] * prev_move.axes_d[0]
|
|
|
|
+ self.axes_d[1] * prev_move.axes_d[1])
|
|
|
|
/ (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)
|
|
|
|
self.junction_start_max = min(
|
2016-11-04 01:10:32 +03:00
|
|
|
R * self.accel, self.junction_max, prev_move.junction_max
|
|
|
|
, prev_move.junction_start_max + prev_move.junction_delta)
|
2016-11-04 22:08:41 +03:00
|
|
|
def process(self, junction_start, junction_cruise, junction_end
|
|
|
|
, cornering_min, cornering_max):
|
2016-07-11 20:21:27 +03:00
|
|
|
# Determine accel, cruise, and decel portions of the move distance
|
2016-07-07 22:52:44 +03:00
|
|
|
inv_junction_delta = 1. / self.junction_delta
|
|
|
|
accel_r = (junction_cruise-junction_start) * inv_junction_delta
|
|
|
|
decel_r = (junction_cruise-junction_end) * inv_junction_delta
|
|
|
|
cruise_r = 1. - accel_r - decel_r
|
|
|
|
self.accel_r, self.cruise_r, self.decel_r = accel_r, cruise_r, decel_r
|
2016-07-07 23:01:58 +03:00
|
|
|
# Determine move velocities
|
2016-07-07 22:52:44 +03:00
|
|
|
start_v = math.sqrt(junction_start)
|
|
|
|
cruise_v = math.sqrt(junction_cruise)
|
|
|
|
end_v = math.sqrt(junction_end)
|
|
|
|
self.start_v, self.cruise_v, self.end_v = start_v, cruise_v, end_v
|
2016-11-04 22:08:41 +03:00
|
|
|
self.corner_min = math.sqrt(cornering_min)
|
|
|
|
self.corner_max = math.sqrt(cornering_max)
|
2016-07-11 20:21:27 +03:00
|
|
|
# Determine time spent in each portion of move (time is the
|
|
|
|
# distance divided by average velocity)
|
|
|
|
accel_t = accel_r * self.move_d / ((start_v + cruise_v) * 0.5)
|
|
|
|
cruise_t = cruise_r * self.move_d / cruise_v
|
|
|
|
decel_t = decel_r * self.move_d / ((end_v + cruise_v) * 0.5)
|
2016-07-07 22:52:44 +03:00
|
|
|
self.accel_t, self.cruise_t, self.decel_t = accel_t, cruise_t, decel_t
|
|
|
|
# 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:
|
|
|
|
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)
|
2016-07-07 22:52:44 +03:00
|
|
|
self.toolhead.update_move_time(accel_t + cruise_t + decel_t)
|
|
|
|
|
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:
|
2016-07-07 23:08:00 +03:00
|
|
|
def __init__(self):
|
2016-07-07 23:01:58 +03:00
|
|
|
self.queue = []
|
|
|
|
self.junction_flush = 0.
|
2016-09-19 20:18:55 +03:00
|
|
|
def reset(self):
|
|
|
|
del self.queue[:]
|
2016-07-07 23:01:58 +03:00
|
|
|
def flush(self, lazy=False):
|
|
|
|
flush_count = len(self.queue)
|
2016-11-04 22:08:41 +03:00
|
|
|
move_info = [None] * flush_count
|
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.
|
2016-11-04 22:08:41 +03:00
|
|
|
next_junction_end = cornering_min = cornering_max = 0.
|
2016-11-04 01:10:32 +03:00
|
|
|
for i in range(flush_count-1, -1, -1):
|
2016-07-07 23:01:58 +03:00
|
|
|
move = self.queue[i]
|
2016-11-04 22:08:41 +03:00
|
|
|
reachable_start = next_junction_end + move.junction_delta
|
|
|
|
junction_start = min(move.junction_start_max, reachable_start)
|
|
|
|
junction_cruise = min((junction_start + reachable_start) * .5
|
|
|
|
, move.junction_max)
|
|
|
|
move_info[i] = (junction_start, junction_cruise, next_junction_end
|
|
|
|
, cornering_min, cornering_max)
|
|
|
|
if reachable_start > junction_start:
|
|
|
|
cornering_min = junction_start
|
|
|
|
if junction_start + move.junction_delta > next_junction_end:
|
|
|
|
cornering_max = junction_cruise
|
|
|
|
if lazy:
|
|
|
|
flush_count = i
|
|
|
|
lazy = False
|
|
|
|
next_junction_end = junction_start
|
|
|
|
if lazy:
|
|
|
|
flush_count = 0
|
2016-07-11 20:21:27 +03:00
|
|
|
# Generate step times for all moves ready to be flushed
|
2016-07-07 23:01:58 +03:00
|
|
|
for i in range(flush_count):
|
2016-11-04 22:08:41 +03:00
|
|
|
self.queue[i].process(*move_info[i])
|
2016-07-11 20:21:27 +03:00
|
|
|
# Remove processed moves from the queue
|
2016-07-07 23:01:58 +03:00
|
|
|
del self.queue[:flush_count]
|
|
|
|
if self.queue:
|
2016-11-04 22:08:41 +03:00
|
|
|
self.junction_flush = 2. * self.queue[-1].junction_max
|
2016-07-07 23:01:58 +03:00
|
|
|
def add_move(self, move):
|
|
|
|
self.queue.append(move)
|
|
|
|
if len(self.queue) == 1:
|
2016-11-04 22:08:41 +03:00
|
|
|
self.junction_flush = 2. * move.junction_max
|
2016-07-07 23:01:58 +03:00
|
|
|
return
|
2016-07-07 23:08:00 +03:00
|
|
|
move.calc_junction(self.queue[-2])
|
2016-07-07 23:01:58 +03:00
|
|
|
self.junction_flush -= move.junction_delta
|
|
|
|
if self.junction_flush <= 0.:
|
2016-07-11 20:21:27 +03:00
|
|
|
# There are enough queued moves to return to zero velocity
|
|
|
|
# from the first move's maximum possible velocity, so at
|
|
|
|
# least one move can be flushed.
|
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:
|
|
|
|
def __init__(self, printer, config):
|
|
|
|
self.printer = printer
|
|
|
|
self.reactor = printer.reactor
|
2016-07-10 19:23:35 +03:00
|
|
|
self.extruder = printer.objects.get('extruder')
|
2016-09-15 19:20:49 +03:00
|
|
|
kintypes = {'cartesian': cartesian.CartKinematics,
|
|
|
|
'delta': delta.DeltaKinematics}
|
2016-11-30 23:50:28 +03:00
|
|
|
self.kin = config.getchoice('kinematics', kintypes)(printer, config)
|
2016-12-01 23:29:26 +03:00
|
|
|
self.max_speed = config.getfloat('max_velocity')
|
|
|
|
self.max_accel = config.getfloat('max_accel')
|
2016-07-07 22:52:44 +03:00
|
|
|
self.junction_deviation = config.getfloat('junction_deviation', 0.02)
|
2016-07-07 23:08:00 +03:00
|
|
|
self.move_queue = MoveQueue()
|
2016-07-07 22:52:44 +03:00
|
|
|
self.commanded_pos = [0., 0., 0., 0.]
|
|
|
|
# Print time tracking
|
|
|
|
self.buffer_time_high = config.getfloat('buffer_time_high', 5.000)
|
|
|
|
self.buffer_time_low = config.getfloat('buffer_time_low', 0.150)
|
|
|
|
self.move_flush_time = config.getfloat('move_flush_time', 0.050)
|
|
|
|
self.motor_off_delay = config.getfloat('motor_off_time', 60.000)
|
|
|
|
self.print_time = 0.
|
2016-12-09 17:41:46 +03:00
|
|
|
self.need_check_stall = -1.
|
2016-07-07 22:52:44 +03:00
|
|
|
self.print_time_stall = 0
|
|
|
|
self.motor_off_time = self.reactor.NEVER
|
2016-12-09 17:41:46 +03:00
|
|
|
self.flush_timer = self.reactor.register_timer(self._flush_handler)
|
2016-07-07 22:52:44 +03:00
|
|
|
def build_config(self):
|
2016-12-01 23:29:26 +03:00
|
|
|
self.kin.set_max_jerk(0.005 * self.max_accel, self.max_accel) # XXX
|
2016-07-07 22:52:44 +03:00
|
|
|
self.kin.build_config()
|
|
|
|
# Print time tracking
|
|
|
|
def update_move_time(self, movetime):
|
|
|
|
self.print_time += movetime
|
|
|
|
flush_to_time = self.print_time - self.move_flush_time
|
|
|
|
self.printer.mcu.flush_moves(flush_to_time)
|
|
|
|
def get_next_move_time(self):
|
|
|
|
if not self.print_time:
|
|
|
|
self.print_time = self.buffer_time_low + STALL_TIME
|
|
|
|
curtime = time.time()
|
|
|
|
self.printer.mcu.set_print_start_time(curtime)
|
|
|
|
self.reactor.update_timer(self.flush_timer, self.reactor.NOW)
|
|
|
|
return self.print_time
|
|
|
|
def get_last_move_time(self):
|
|
|
|
self.move_queue.flush()
|
|
|
|
return self.get_next_move_time()
|
|
|
|
def reset_motor_off_time(self, eventtime):
|
|
|
|
self.motor_off_time = eventtime + self.motor_off_delay
|
|
|
|
def reset_print_time(self):
|
|
|
|
self.move_queue.flush()
|
|
|
|
self.printer.mcu.flush_moves(self.print_time)
|
|
|
|
self.print_time = 0.
|
2016-12-09 17:41:46 +03:00
|
|
|
self.need_check_stall = -1.
|
2016-07-07 22:52:44 +03:00
|
|
|
self.reset_motor_off_time(time.time())
|
|
|
|
self.reactor.update_timer(self.flush_timer, self.motor_off_time)
|
2016-12-09 17:41:46 +03:00
|
|
|
def _check_stall(self):
|
2016-07-07 22:52:44 +03:00
|
|
|
if not self.print_time:
|
|
|
|
# XXX - find better way to flush initial move_queue items
|
|
|
|
if self.move_queue.queue:
|
2016-12-09 17:41:46 +03:00
|
|
|
self.reactor.update_timer(self.flush_timer, time.time() + 0.100)
|
|
|
|
return
|
|
|
|
eventtime = time.time()
|
|
|
|
while 1:
|
|
|
|
buffer_time = self.printer.mcu.get_print_buffer_time(
|
|
|
|
eventtime, self.print_time)
|
|
|
|
stall_time = buffer_time - self.buffer_time_high
|
|
|
|
if stall_time <= 0.:
|
|
|
|
break
|
|
|
|
eventtime = self.reactor.pause(eventtime + stall_time)
|
|
|
|
if not self.print_time:
|
|
|
|
return
|
|
|
|
self.need_check_stall = self.print_time - stall_time + 0.100
|
|
|
|
def _flush_handler(self, eventtime):
|
2016-09-19 20:18:55 +03:00
|
|
|
try:
|
2016-07-07 22:52:44 +03:00
|
|
|
if not self.print_time:
|
2016-09-19 20:18:55 +03:00
|
|
|
self.move_queue.flush()
|
|
|
|
if not self.print_time:
|
|
|
|
if eventtime >= self.motor_off_time:
|
|
|
|
self.motor_off()
|
|
|
|
self.reset_print_time()
|
|
|
|
self.motor_off_time = self.reactor.NEVER
|
|
|
|
return self.motor_off_time
|
|
|
|
print_time = self.print_time
|
|
|
|
buffer_time = self.printer.mcu.get_print_buffer_time(
|
|
|
|
eventtime, print_time)
|
|
|
|
if buffer_time > self.buffer_time_low:
|
|
|
|
return eventtime + buffer_time - self.buffer_time_low
|
|
|
|
self.move_queue.flush()
|
|
|
|
if print_time != self.print_time:
|
|
|
|
self.print_time_stall += 1
|
|
|
|
self.dwell(self.buffer_time_low + STALL_TIME)
|
|
|
|
return self.reactor.NOW
|
|
|
|
self.reset_print_time()
|
|
|
|
return self.motor_off_time
|
|
|
|
except:
|
|
|
|
logging.exception("Exception in flush_handler")
|
|
|
|
self.force_shutdown()
|
2016-07-07 22:52:44 +03:00
|
|
|
def stats(self, eventtime):
|
|
|
|
buffer_time = 0.
|
|
|
|
if self.print_time:
|
|
|
|
buffer_time = self.printer.mcu.get_print_buffer_time(
|
|
|
|
eventtime, self.print_time)
|
|
|
|
return "print_time=%.3f buffer_time=%.3f print_time_stall=%d" % (
|
|
|
|
self.print_time, buffer_time, self.print_time_stall)
|
|
|
|
# Movement commands
|
|
|
|
def get_position(self):
|
|
|
|
return list(self.commanded_pos)
|
|
|
|
def set_position(self, newpos):
|
|
|
|
self.move_queue.flush()
|
|
|
|
self.commanded_pos[:] = newpos
|
|
|
|
self.kin.set_position(newpos)
|
2016-12-01 06:34:38 +03:00
|
|
|
def move(self, newpos, speed):
|
2016-09-30 21:47:45 +03:00
|
|
|
speed = min(speed, self.max_speed)
|
2016-10-26 02:11:05 +03:00
|
|
|
move = Move(self, self.commanded_pos, newpos, speed, self.max_accel)
|
|
|
|
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)
|
|
|
|
self.commanded_pos[:] = newpos
|
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()
|
2016-11-18 19:27:16 +03:00
|
|
|
def home(self, homing_state):
|
|
|
|
self.kin.home(homing_state)
|
2016-07-07 22:52:44 +03:00
|
|
|
def dwell(self, delay):
|
|
|
|
self.get_last_move_time()
|
|
|
|
self.update_move_time(delay)
|
2016-12-09 17:41:46 +03:00
|
|
|
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)
|
2016-07-10 19:23:35 +03:00
|
|
|
self.extruder.motor_off(last_move_time)
|
2016-07-07 22:52:44 +03:00
|
|
|
self.dwell(STALL_TIME)
|
|
|
|
logging.debug('; Max time of %f' % (last_move_time,))
|
2016-12-29 05:24:12 +03:00
|
|
|
def wait_moves(self):
|
|
|
|
self.move_queue.flush()
|
|
|
|
eventtime = time.time()
|
|
|
|
while self.print_time:
|
|
|
|
eventtime = self.reactor.pause(eventtime + 0.100)
|
2016-12-08 20:42:12 +03:00
|
|
|
def query_endstops(self):
|
|
|
|
last_move_time = self.get_last_move_time()
|
|
|
|
return self.kin.query_endstops(last_move_time)
|
2016-09-19 20:18:55 +03:00
|
|
|
def force_shutdown(self):
|
|
|
|
self.printer.mcu.force_shutdown()
|
|
|
|
self.move_queue.reset()
|
2016-12-29 06:28:29 +03:00
|
|
|
self.reset_print_time()
|