2016-07-10 19:23:35 +03:00
|
|
|
# Code for handling printer nozzle extruders
|
|
|
|
#
|
2018-01-20 06:22:17 +03:00
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
2016-07-10 19:23:35 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2017-01-03 03:22:32 +03:00
|
|
|
import math, logging
|
2018-06-13 23:53:25 +03:00
|
|
|
import stepper, homing, chelper
|
2016-07-10 19:23:35 +03:00
|
|
|
|
2017-01-28 06:06:54 +03:00
|
|
|
EXTRUDE_DIFF_IGNORE = 1.02
|
|
|
|
|
2016-07-10 19:23:35 +03:00
|
|
|
class PrinterExtruder:
|
2018-06-21 20:47:39 +03:00
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
2018-04-03 18:42:43 +03:00
|
|
|
self.name = config.get_name()
|
2017-09-03 23:51:08 +03:00
|
|
|
shared_heater = config.get('shared_heater', None)
|
2018-06-21 20:47:39 +03:00
|
|
|
pheater = self.printer.lookup_object('heater')
|
2017-09-03 23:51:08 +03:00
|
|
|
if shared_heater is None:
|
2018-04-04 00:01:10 +03:00
|
|
|
self.heater = pheater.setup_heater(config)
|
2017-09-03 23:51:08 +03:00
|
|
|
else:
|
2018-04-04 00:01:10 +03:00
|
|
|
self.heater = pheater.lookup_heater(shared_heater)
|
2018-06-21 20:47:39 +03:00
|
|
|
self.stepper = stepper.PrinterStepper(config)
|
2017-04-11 18:37:09 +03:00
|
|
|
self.nozzle_diameter = config.getfloat('nozzle_diameter', above=0.)
|
|
|
|
filament_diameter = config.getfloat(
|
|
|
|
'filament_diameter', minval=self.nozzle_diameter)
|
2017-09-06 04:55:44 +03:00
|
|
|
self.filament_area = math.pi * (filament_diameter * .5)**2
|
2017-01-03 03:22:32 +03:00
|
|
|
max_cross_section = config.getfloat(
|
2017-04-11 18:37:09 +03:00
|
|
|
'max_extrude_cross_section', 4. * self.nozzle_diameter**2
|
|
|
|
, above=0.)
|
2017-09-06 04:55:44 +03:00
|
|
|
self.max_extrude_ratio = max_cross_section / self.filament_area
|
2018-06-06 03:48:55 +03:00
|
|
|
logging.info("Extruder max_extrude_ratio=%.6f", self.max_extrude_ratio)
|
2018-06-21 20:47:39 +03:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
2017-09-03 22:17:02 +03:00
|
|
|
max_velocity, max_accel = toolhead.get_max_velocity()
|
2018-01-17 05:40:53 +03:00
|
|
|
self.max_e_velocity = config.getfloat(
|
2017-09-03 22:17:02 +03:00
|
|
|
'max_extrude_only_velocity', max_velocity * self.max_extrude_ratio
|
|
|
|
, above=0.)
|
2018-01-17 05:40:53 +03:00
|
|
|
self.max_e_accel = config.getfloat(
|
2017-09-03 22:17:02 +03:00
|
|
|
'max_extrude_only_accel', max_accel * self.max_extrude_ratio
|
|
|
|
, above=0.)
|
|
|
|
self.stepper.set_max_jerk(9999999.9, 9999999.9)
|
2017-04-11 18:37:09 +03:00
|
|
|
self.max_e_dist = config.getfloat(
|
|
|
|
'max_extrude_only_distance', 50., minval=0.)
|
2017-05-20 04:05:46 +03:00
|
|
|
self.activate_gcode = config.get('activate_gcode', '')
|
|
|
|
self.deactivate_gcode = config.get('deactivate_gcode', '')
|
2017-04-11 18:37:09 +03:00
|
|
|
self.pressure_advance = config.getfloat(
|
|
|
|
'pressure_advance', 0., minval=0.)
|
2018-04-01 22:26:25 +03:00
|
|
|
self.pressure_advance_lookahead_time = config.getfloat(
|
|
|
|
'pressure_advance_lookahead_time', 0.010, minval=0.)
|
2016-11-14 21:40:35 +03:00
|
|
|
self.need_motor_enable = True
|
2016-07-17 02:58:41 +03:00
|
|
|
self.extrude_pos = 0.
|
2018-06-13 23:53:25 +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.extruder_move_fill = ffi_lib.extruder_move_fill
|
|
|
|
sk = ffi_main.gc(ffi_lib.extruder_stepper_alloc(), ffi_lib.free)
|
|
|
|
self.stepper.setup_itersolve(sk)
|
|
|
|
# Setup SET_PRESSURE_ADVANCE command
|
2018-05-20 19:52:19 +03:00
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
if self.name in ('extruder', 'extruder0'):
|
|
|
|
gcode.register_mux_command("SET_PRESSURE_ADVANCE", "EXTRUDER", None,
|
|
|
|
self.cmd_default_SET_PRESSURE_ADVANCE,
|
|
|
|
desc=self.cmd_SET_PRESSURE_ADVANCE_help)
|
|
|
|
gcode.register_mux_command("SET_PRESSURE_ADVANCE", "EXTRUDER", self.name,
|
|
|
|
self.cmd_SET_PRESSURE_ADVANCE,
|
|
|
|
desc=self.cmd_SET_PRESSURE_ADVANCE_help)
|
2017-04-29 21:56:39 +03:00
|
|
|
def get_heater(self):
|
|
|
|
return self.heater
|
|
|
|
def set_active(self, print_time, is_active):
|
|
|
|
return self.extrude_pos
|
2017-05-20 04:05:46 +03:00
|
|
|
def get_activate_gcode(self, is_active):
|
|
|
|
if is_active:
|
|
|
|
return self.activate_gcode
|
|
|
|
return self.deactivate_gcode
|
2018-01-29 07:37:56 +03:00
|
|
|
def stats(self, eventtime):
|
|
|
|
return self.heater.stats(eventtime)
|
2017-09-12 19:47:40 +03:00
|
|
|
def motor_off(self, print_time):
|
|
|
|
self.stepper.motor_enable(print_time, 0)
|
2016-11-14 21:40:35 +03:00
|
|
|
self.need_motor_enable = True
|
2016-09-30 21:47:45 +03:00
|
|
|
def check_move(self, move):
|
2017-01-28 06:06:54 +03:00
|
|
|
move.extrude_r = move.axes_d[3] / move.move_d
|
2017-01-28 06:32:12 +03:00
|
|
|
move.extrude_max_corner_v = 0.
|
2016-10-01 02:04:24 +03:00
|
|
|
if not self.heater.can_extrude:
|
2017-09-06 04:55:44 +03:00
|
|
|
raise homing.EndstopError(
|
|
|
|
"Extrude below minimum temp\n"
|
|
|
|
"See the 'min_extrude_temp' config option for details")
|
2017-05-02 15:57:38 +03:00
|
|
|
if not move.is_kinematic_move or move.extrude_r < 0.:
|
|
|
|
# Extrude only move (or retraction move) - limit accel and velocity
|
2017-05-02 14:50:58 +03:00
|
|
|
if abs(move.axes_d[3]) > self.max_e_dist:
|
2017-09-06 04:55:44 +03:00
|
|
|
raise homing.EndstopError(
|
|
|
|
"Extrude only move too long (%.3fmm vs %.3fmm)\n"
|
|
|
|
"See the 'max_extrude_only_distance' config"
|
|
|
|
" option for details" % (move.axes_d[3], self.max_e_dist))
|
2017-05-02 15:57:38 +03:00
|
|
|
inv_extrude_r = 1. / abs(move.extrude_r)
|
|
|
|
move.limit_speed(self.max_e_velocity * inv_extrude_r
|
|
|
|
, self.max_e_accel * inv_extrude_r)
|
2018-01-08 20:23:44 +03:00
|
|
|
elif move.extrude_r > self.max_extrude_ratio:
|
|
|
|
if move.axes_d[3] <= self.nozzle_diameter * self.max_extrude_ratio:
|
|
|
|
# Permit extrusion if amount extruded is tiny
|
|
|
|
move.extrude_r = self.max_extrude_ratio
|
|
|
|
return
|
2017-09-06 04:55:44 +03:00
|
|
|
area = move.axes_d[3] * self.filament_area / move.move_d
|
|
|
|
logging.debug("Overextrude: %s vs %s (area=%.3f dist=%.3f)",
|
|
|
|
move.extrude_r, self.max_extrude_ratio,
|
|
|
|
area, move.move_d)
|
|
|
|
raise homing.EndstopError(
|
|
|
|
"Move exceeds maximum extrusion (%.3fmm^2 vs %.3fmm^2)\n"
|
|
|
|
"See the 'max_extrude_cross_section' config option for details"
|
|
|
|
% (area, self.max_extrude_ratio * self.filament_area))
|
2017-01-28 06:06:54 +03:00
|
|
|
def calc_junction(self, prev_move, move):
|
2017-03-20 21:39:06 +03:00
|
|
|
extrude = move.axes_d[3]
|
|
|
|
prev_extrude = prev_move.axes_d[3]
|
|
|
|
if extrude or prev_extrude:
|
|
|
|
if not extrude or not prev_extrude:
|
|
|
|
# Extrude move to non-extrude move - disable lookahead
|
|
|
|
return 0.
|
|
|
|
if ((move.extrude_r > prev_move.extrude_r * EXTRUDE_DIFF_IGNORE
|
|
|
|
or prev_move.extrude_r > move.extrude_r * EXTRUDE_DIFF_IGNORE)
|
|
|
|
and abs(move.move_d * prev_move.extrude_r - extrude) >= .001):
|
2017-01-28 06:06:54 +03:00
|
|
|
# Extrude ratio between moves is too different
|
|
|
|
return 0.
|
|
|
|
move.extrude_r = prev_move.extrude_r
|
|
|
|
return move.max_cruise_v2
|
2017-01-28 06:32:12 +03:00
|
|
|
def lookahead(self, moves, flush_count, lazy):
|
2017-02-22 18:09:25 +03:00
|
|
|
lookahead_t = self.pressure_advance_lookahead_time
|
2018-04-01 22:26:25 +03:00
|
|
|
if not self.pressure_advance or not lookahead_t:
|
2017-01-28 06:32:12 +03:00
|
|
|
return flush_count
|
|
|
|
# Calculate max_corner_v - the speed the head will accelerate
|
|
|
|
# to after cornering.
|
|
|
|
for i in range(flush_count):
|
|
|
|
move = moves[i]
|
|
|
|
if not move.decel_t:
|
|
|
|
continue
|
|
|
|
cruise_v = move.cruise_v
|
|
|
|
max_corner_v = 0.
|
2017-02-22 18:09:25 +03:00
|
|
|
sum_t = lookahead_t
|
2017-01-28 06:32:12 +03:00
|
|
|
for j in range(i+1, flush_count):
|
|
|
|
fmove = moves[j]
|
|
|
|
if not fmove.max_start_v2:
|
|
|
|
break
|
2017-02-22 18:09:25 +03:00
|
|
|
if fmove.cruise_v > max_corner_v:
|
2017-03-03 22:13:35 +03:00
|
|
|
if (not max_corner_v
|
|
|
|
and not fmove.accel_t and not fmove.cruise_t):
|
|
|
|
# Start timing after any full decel moves
|
|
|
|
continue
|
2017-02-22 18:09:25 +03:00
|
|
|
if sum_t >= fmove.accel_t:
|
|
|
|
max_corner_v = fmove.cruise_v
|
|
|
|
else:
|
|
|
|
max_corner_v = max(
|
|
|
|
max_corner_v, fmove.start_v + fmove.accel * sum_t)
|
|
|
|
if max_corner_v >= cruise_v:
|
|
|
|
break
|
2017-01-28 06:32:12 +03:00
|
|
|
sum_t -= fmove.accel_t + fmove.cruise_t + fmove.decel_t
|
|
|
|
if sum_t <= 0.:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if lazy:
|
|
|
|
return i
|
|
|
|
move.extrude_max_corner_v = max_corner_v
|
|
|
|
return flush_count
|
2017-09-12 19:47:40 +03:00
|
|
|
def move(self, print_time, move):
|
2016-11-14 21:40:35 +03:00
|
|
|
if self.need_motor_enable:
|
2017-09-12 19:47:40 +03:00
|
|
|
self.stepper.motor_enable(print_time, 1)
|
2016-11-14 21:40:35 +03:00
|
|
|
self.need_motor_enable = False
|
2016-11-05 02:28:07 +03:00
|
|
|
axis_d = move.axes_d[3]
|
2018-06-13 23:53:25 +03:00
|
|
|
axis_r = axis_d / move.move_d
|
2017-04-04 19:31:03 +03:00
|
|
|
accel = move.accel * axis_r
|
|
|
|
start_v = move.start_v * axis_r
|
|
|
|
cruise_v = move.cruise_v * axis_r
|
2016-07-17 02:58:41 +03:00
|
|
|
accel_t, cruise_t, decel_t = move.accel_t, move.cruise_t, move.decel_t
|
|
|
|
|
|
|
|
# Update for pressure advance
|
2018-06-13 23:53:25 +03:00
|
|
|
extra_accel_v = extra_decel_v = 0.
|
2016-11-05 04:11:14 +03:00
|
|
|
start_pos = self.extrude_pos
|
2016-11-05 02:28:07 +03:00
|
|
|
if (axis_d >= 0. and (move.axes_d[0] or move.axes_d[1])
|
2016-07-17 02:58:41 +03:00
|
|
|
and self.pressure_advance):
|
2018-06-13 23:53:25 +03:00
|
|
|
# Calculate extra_accel_v
|
2017-01-28 06:32:12 +03:00
|
|
|
pressure_advance = self.pressure_advance * move.extrude_r
|
2016-11-05 04:11:14 +03:00
|
|
|
prev_pressure_d = start_pos - move.start_pos[3]
|
2018-06-13 23:53:25 +03:00
|
|
|
if accel_t:
|
2017-01-28 06:32:12 +03:00
|
|
|
npd = move.cruise_v * pressure_advance
|
2016-11-05 04:11:14 +03:00
|
|
|
extra_accel_d = npd - prev_pressure_d
|
|
|
|
if extra_accel_d > 0.:
|
2018-06-13 23:53:25 +03:00
|
|
|
extra_accel_v = extra_accel_d / accel_t
|
|
|
|
axis_d += extra_accel_d
|
2016-11-05 04:11:14 +03:00
|
|
|
prev_pressure_d += extra_accel_d
|
2018-06-13 23:53:25 +03:00
|
|
|
# Calculate extra_decel_v
|
2017-01-28 06:32:12 +03:00
|
|
|
emcv = move.extrude_max_corner_v
|
2018-06-13 23:53:25 +03:00
|
|
|
if decel_t and emcv < move.cruise_v:
|
2017-01-28 06:32:12 +03:00
|
|
|
npd = max(emcv, move.end_v) * pressure_advance
|
2018-06-13 23:53:25 +03:00
|
|
|
extra_decel_d = npd - prev_pressure_d
|
|
|
|
if extra_decel_d < 0.:
|
|
|
|
axis_d += extra_decel_d
|
2016-11-05 04:11:14 +03:00
|
|
|
extra_decel_v = extra_decel_d / decel_t
|
2016-07-17 02:58:41 +03:00
|
|
|
|
2018-06-13 23:53:25 +03:00
|
|
|
# Generate steps
|
|
|
|
self.extruder_move_fill(
|
|
|
|
self.cmove, print_time, accel_t, cruise_t, decel_t, start_pos,
|
|
|
|
start_v, cruise_v, accel, extra_accel_v, extra_decel_v)
|
|
|
|
self.stepper.step_itersolve(self.cmove)
|
|
|
|
self.extrude_pos = start_pos + axis_d
|
2018-04-01 22:26:25 +03:00
|
|
|
cmd_SET_PRESSURE_ADVANCE_help = "Set pressure advance parameters"
|
2018-05-20 19:52:19 +03:00
|
|
|
def cmd_default_SET_PRESSURE_ADVANCE(self, params):
|
|
|
|
extruder = self.printer.lookup_object('toolhead').get_extruder()
|
|
|
|
extruder.cmd_SET_PRESSURE_ADVANCE(params)
|
2018-04-01 22:26:25 +03:00
|
|
|
def cmd_SET_PRESSURE_ADVANCE(self, params):
|
|
|
|
self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
2018-04-21 04:41:13 +03:00
|
|
|
pressure_advance = gcode.get_float(
|
|
|
|
'ADVANCE', params, self.pressure_advance, minval=0.)
|
|
|
|
pressure_advance_lookahead_time = gcode.get_float(
|
|
|
|
'ADVANCE_LOOKAHEAD_TIME', params,
|
|
|
|
self.pressure_advance_lookahead_time, minval=0.)
|
|
|
|
self.pressure_advance = pressure_advance
|
|
|
|
self.pressure_advance_lookahead_time = pressure_advance_lookahead_time
|
2018-04-03 18:42:43 +03:00
|
|
|
msg = ("pressure_advance: %.6f\n"
|
2018-04-21 04:41:13 +03:00
|
|
|
"pressure_advance_lookahead_time: %.6f" % (
|
|
|
|
pressure_advance, pressure_advance_lookahead_time))
|
2018-04-03 18:42:43 +03:00
|
|
|
self.printer.set_rollover_info(self.name, "%s: %s" % (self.name, msg))
|
|
|
|
gcode.respond_info(msg)
|
2017-01-28 06:06:54 +03:00
|
|
|
|
|
|
|
# Dummy extruder class used when a printer has no extruder at all
|
|
|
|
class DummyExtruder:
|
2017-04-29 21:56:39 +03:00
|
|
|
def set_active(self, print_time, is_active):
|
|
|
|
return 0.
|
2017-01-28 06:06:54 +03:00
|
|
|
def motor_off(self, move_time):
|
|
|
|
pass
|
|
|
|
def check_move(self, move):
|
|
|
|
raise homing.EndstopMoveError(
|
|
|
|
move.end_pos, "Extrude when no extruder present")
|
|
|
|
def calc_junction(self, prev_move, move):
|
|
|
|
return move.max_cruise_v2
|
|
|
|
def lookahead(self, moves, flush_count, lazy):
|
|
|
|
return flush_count
|
2017-04-29 20:57:02 +03:00
|
|
|
|
|
|
|
def add_printer_objects(printer, config):
|
2017-04-29 21:56:39 +03:00
|
|
|
for i in range(99):
|
|
|
|
section = 'extruder%d' % (i,)
|
|
|
|
if not config.has_section(section):
|
|
|
|
if not i and config.has_section('extruder'):
|
2018-06-21 20:47:39 +03:00
|
|
|
pe = PrinterExtruder(config.getsection('extruder'))
|
|
|
|
printer.add_object('extruder0', pe)
|
2017-04-29 21:56:39 +03:00
|
|
|
continue
|
|
|
|
break
|
2018-06-21 20:47:39 +03:00
|
|
|
printer.add_object(section, PrinterExtruder(config.getsection(section)))
|
2017-04-29 21:56:39 +03:00
|
|
|
|
|
|
|
def get_printer_extruders(printer):
|
|
|
|
out = []
|
|
|
|
for i in range(99):
|
2018-01-20 06:22:17 +03:00
|
|
|
extruder = printer.lookup_object('extruder%d' % (i,), None)
|
2017-04-29 21:56:39 +03:00
|
|
|
if extruder is None:
|
|
|
|
break
|
|
|
|
out.append(extruder)
|
|
|
|
return out
|