2016-07-10 18:23:35 +02:00
|
|
|
# Code for handling printer nozzle extruders
|
|
|
|
#
|
2019-10-29 15:57:36 +01:00
|
|
|
# Copyright (C) 2016-2019 Kevin O'Connor <kevin@koconnor.net>
|
2016-07-10 18:23:35 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2017-01-03 01:22:32 +01:00
|
|
|
import math, logging
|
2018-06-13 22:53:25 +02:00
|
|
|
import stepper, homing, chelper
|
2016-07-10 18:23:35 +02:00
|
|
|
|
|
|
|
class PrinterExtruder:
|
2019-01-02 23:44:02 +01:00
|
|
|
def __init__(self, config, extruder_num):
|
2018-06-21 19:47:39 +02:00
|
|
|
self.printer = config.get_printer()
|
2018-04-03 17:42:43 +02:00
|
|
|
self.name = config.get_name()
|
2017-09-03 22:51:08 +02:00
|
|
|
shared_heater = config.get('shared_heater', None)
|
2018-06-21 19:47:39 +02:00
|
|
|
pheater = self.printer.lookup_object('heater')
|
2019-01-02 23:44:02 +01:00
|
|
|
gcode_id = 'T%d' % (extruder_num,)
|
2017-09-03 22:51:08 +02:00
|
|
|
if shared_heater is None:
|
2019-01-02 23:44:02 +01:00
|
|
|
self.heater = pheater.setup_heater(config, gcode_id)
|
2017-09-03 22:51:08 +02:00
|
|
|
else:
|
2018-04-03 23:01:10 +02:00
|
|
|
self.heater = pheater.lookup_heater(shared_heater)
|
2018-06-21 19:47:39 +02:00
|
|
|
self.stepper = stepper.PrinterStepper(config)
|
2017-04-11 17:37:09 +02:00
|
|
|
self.nozzle_diameter = config.getfloat('nozzle_diameter', above=0.)
|
|
|
|
filament_diameter = config.getfloat(
|
|
|
|
'filament_diameter', minval=self.nozzle_diameter)
|
2017-09-06 03:55:44 +02:00
|
|
|
self.filament_area = math.pi * (filament_diameter * .5)**2
|
2018-10-26 00:59:01 +02:00
|
|
|
def_max_cross_section = 4. * self.nozzle_diameter**2
|
|
|
|
def_max_extrude_ratio = def_max_cross_section / self.filament_area
|
2017-01-03 01:22:32 +01:00
|
|
|
max_cross_section = config.getfloat(
|
2018-10-26 00:59:01 +02:00
|
|
|
'max_extrude_cross_section', def_max_cross_section, above=0.)
|
2017-09-06 03:55:44 +02:00
|
|
|
self.max_extrude_ratio = max_cross_section / self.filament_area
|
2018-06-06 02:48:55 +02:00
|
|
|
logging.info("Extruder max_extrude_ratio=%.6f", self.max_extrude_ratio)
|
2018-06-21 19:47:39 +02:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
2017-09-03 21:17:02 +02:00
|
|
|
max_velocity, max_accel = toolhead.get_max_velocity()
|
2018-01-17 03:40:53 +01:00
|
|
|
self.max_e_velocity = config.getfloat(
|
2018-10-26 00:59:01 +02:00
|
|
|
'max_extrude_only_velocity', max_velocity * def_max_extrude_ratio
|
2017-09-03 21:17:02 +02:00
|
|
|
, above=0.)
|
2018-01-17 03:40:53 +01:00
|
|
|
self.max_e_accel = config.getfloat(
|
2018-10-26 00:59:01 +02:00
|
|
|
'max_extrude_only_accel', max_accel * def_max_extrude_ratio
|
2017-09-03 21:17:02 +02:00
|
|
|
, above=0.)
|
|
|
|
self.stepper.set_max_jerk(9999999.9, 9999999.9)
|
2017-04-11 17:37:09 +02:00
|
|
|
self.max_e_dist = config.getfloat(
|
|
|
|
'max_extrude_only_distance', 50., minval=0.)
|
2019-09-21 01:00:24 +02:00
|
|
|
self.instant_corner_v = config.getfloat(
|
|
|
|
'instantaneous_corner_velocity', 1., minval=0.)
|
2019-06-07 16:42:22 +02:00
|
|
|
gcode_macro = self.printer.try_load_module(config, 'gcode_macro')
|
|
|
|
self.activate_gcode = gcode_macro.load_template(
|
|
|
|
config, 'activate_gcode', '')
|
|
|
|
self.deactivate_gcode = gcode_macro.load_template(
|
|
|
|
config, 'deactivate_gcode', '')
|
2019-11-05 22:21:06 +01:00
|
|
|
self.pressure_advance = self.pressure_advance_smooth_time = 0.
|
|
|
|
pressure_advance = config.getfloat('pressure_advance', 0., minval=0.)
|
|
|
|
smooth_time = config.getfloat('pressure_advance_smooth_time',
|
|
|
|
0.040, above=0., maxval=.200)
|
|
|
|
self.extrude_pos = self.extrude_pa_pos = 0.
|
2018-06-13 22:53:25 +02:00
|
|
|
# Setup iterative solver
|
|
|
|
ffi_main, ffi_lib = chelper.get_ffi()
|
2019-10-29 15:57:36 +01:00
|
|
|
self.trapq = ffi_main.gc(ffi_lib.trapq_alloc(), ffi_lib.trapq_free)
|
2019-11-05 22:21:06 +01:00
|
|
|
self.trapq_append = ffi_lib.trapq_append
|
2019-10-29 15:57:36 +01:00
|
|
|
self.trapq_free_moves = ffi_lib.trapq_free_moves
|
2019-11-05 22:21:06 +01:00
|
|
|
self.sk_extruder = ffi_main.gc(ffi_lib.extruder_stepper_alloc(),
|
|
|
|
ffi_lib.free)
|
|
|
|
self.stepper.set_stepper_kinematics(self.sk_extruder)
|
2019-10-29 15:57:36 +01:00
|
|
|
self.stepper.set_trapq(self.trapq)
|
2019-11-07 16:59:35 +01:00
|
|
|
toolhead.register_step_generator(self.stepper.generate_steps)
|
2019-11-05 22:21:06 +01:00
|
|
|
self.extruder_set_pressure = ffi_lib.extruder_set_pressure
|
|
|
|
self._set_pressure_advance(pressure_advance, smooth_time)
|
2019-11-13 17:02:26 +01:00
|
|
|
# Register commands
|
2018-05-20 18:52:19 +02:00
|
|
|
gcode = self.printer.lookup_object('gcode')
|
2019-11-07 22:24:52 +01:00
|
|
|
if self.name == 'extruder':
|
2019-11-13 17:02:26 +01:00
|
|
|
toolhead.set_extruder(self, self.extrude_pos)
|
2018-05-20 18:52:19 +02:00
|
|
|
gcode.register_mux_command("SET_PRESSURE_ADVANCE", "EXTRUDER", None,
|
|
|
|
self.cmd_default_SET_PRESSURE_ADVANCE,
|
|
|
|
desc=self.cmd_SET_PRESSURE_ADVANCE_help)
|
2019-02-27 19:07:41 +01:00
|
|
|
gcode.register_mux_command("SET_PRESSURE_ADVANCE", "EXTRUDER",
|
|
|
|
self.name, self.cmd_SET_PRESSURE_ADVANCE,
|
2018-05-20 18:52:19 +02:00
|
|
|
desc=self.cmd_SET_PRESSURE_ADVANCE_help)
|
2019-09-18 04:53:17 +02:00
|
|
|
def update_move_time(self, flush_time):
|
2019-10-29 15:57:36 +01:00
|
|
|
self.trapq_free_moves(self.trapq, flush_time)
|
2019-11-05 22:21:06 +01:00
|
|
|
def _set_pressure_advance(self, pressure_advance, smooth_time):
|
|
|
|
old_smooth_time = self.pressure_advance_smooth_time * .5
|
|
|
|
if not self.pressure_advance:
|
|
|
|
old_smooth_time = 0.
|
|
|
|
new_smooth_time = smooth_time * .5
|
|
|
|
if not pressure_advance:
|
|
|
|
new_smooth_time = 0.
|
|
|
|
toolhead = self.printer.lookup_object("toolhead")
|
|
|
|
toolhead.note_step_generation_scan_time(new_smooth_time,
|
|
|
|
old_delay=old_smooth_time)
|
|
|
|
self.extruder_set_pressure(self.sk_extruder,
|
|
|
|
pressure_advance, new_smooth_time)
|
|
|
|
self.pressure_advance = pressure_advance
|
|
|
|
self.pressure_advance_smooth_time = smooth_time
|
2019-06-02 15:33:47 +02:00
|
|
|
def get_status(self, eventtime):
|
2019-11-05 22:21:06 +01:00
|
|
|
return dict(self.get_heater().get_status(eventtime),
|
|
|
|
pressure_advance=self.pressure_advance,
|
|
|
|
smooth_time=self.pressure_advance_smooth_time)
|
2019-11-25 01:02:49 +01:00
|
|
|
def get_name(self):
|
|
|
|
return self.name
|
2017-04-29 20:56:39 +02:00
|
|
|
def get_heater(self):
|
|
|
|
return self.heater
|
|
|
|
def set_active(self, print_time, is_active):
|
|
|
|
return self.extrude_pos
|
2017-05-20 03:05:46 +02:00
|
|
|
def get_activate_gcode(self, is_active):
|
|
|
|
if is_active:
|
2019-06-07 16:42:22 +02:00
|
|
|
return self.activate_gcode.render()
|
|
|
|
return self.deactivate_gcode.render()
|
2018-01-29 05:37:56 +01:00
|
|
|
def stats(self, eventtime):
|
|
|
|
return self.heater.stats(eventtime)
|
2016-09-30 20:47:45 +02:00
|
|
|
def check_move(self, move):
|
2019-09-21 01:00:24 +02:00
|
|
|
axis_r = move.axes_r[3]
|
2016-10-01 01:04:24 +02:00
|
|
|
if not self.heater.can_extrude:
|
2017-09-06 03:55:44 +02:00
|
|
|
raise homing.EndstopError(
|
|
|
|
"Extrude below minimum temp\n"
|
|
|
|
"See the 'min_extrude_temp' config option for details")
|
2019-09-21 01:00:24 +02:00
|
|
|
if (not move.axes_d[0] and not move.axes_d[1]) or axis_r < 0.:
|
2017-05-02 14:57:38 +02:00
|
|
|
# Extrude only move (or retraction move) - limit accel and velocity
|
2017-05-02 13:50:58 +02:00
|
|
|
if abs(move.axes_d[3]) > self.max_e_dist:
|
2017-09-06 03:55:44 +02: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))
|
2019-09-21 01:00:24 +02:00
|
|
|
inv_extrude_r = 1. / abs(axis_r)
|
|
|
|
move.limit_speed(self.max_e_velocity * inv_extrude_r,
|
|
|
|
self.max_e_accel * inv_extrude_r)
|
|
|
|
elif axis_r > self.max_extrude_ratio:
|
2018-01-08 18:23:44 +01:00
|
|
|
if move.axes_d[3] <= self.nozzle_diameter * self.max_extrude_ratio:
|
|
|
|
# Permit extrusion if amount extruded is tiny
|
|
|
|
return
|
2019-09-21 01:00:24 +02:00
|
|
|
area = axis_r * self.filament_area
|
2017-09-06 03:55:44 +02:00
|
|
|
logging.debug("Overextrude: %s vs %s (area=%.3f dist=%.3f)",
|
2019-09-21 01:00:24 +02:00
|
|
|
axis_r, self.max_extrude_ratio, area, move.move_d)
|
2017-09-06 03:55:44 +02:00
|
|
|
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 04:06:54 +01:00
|
|
|
def calc_junction(self, prev_move, move):
|
2019-09-21 01:00:24 +02:00
|
|
|
diff_r = move.axes_r[3] - prev_move.axes_r[3]
|
|
|
|
if diff_r:
|
|
|
|
return (self.instant_corner_v / abs(diff_r))**2
|
2017-01-28 04:06:54 +01:00
|
|
|
return move.max_cruise_v2
|
2017-09-12 18:47:40 +02:00
|
|
|
def move(self, print_time, move):
|
2019-11-06 14:41:50 +01:00
|
|
|
axis_r = move.axes_r[3]
|
2017-04-04 18:31:03 +02:00
|
|
|
accel = move.accel * axis_r
|
|
|
|
start_v = move.start_v * axis_r
|
|
|
|
cruise_v = move.cruise_v * axis_r
|
2019-11-05 22:21:06 +01:00
|
|
|
is_pa = 0.
|
|
|
|
if axis_r > 0. and (move.axes_d[0] or move.axes_d[1]):
|
|
|
|
is_pa = 1.
|
|
|
|
# Queue movement (x is extruder movement, y is movement with pa)
|
|
|
|
self.trapq_append(self.trapq, print_time,
|
|
|
|
move.accel_t, move.cruise_t, move.decel_t,
|
|
|
|
move.start_pos[3], self.extrude_pa_pos, 0.,
|
|
|
|
1., is_pa, 0.,
|
|
|
|
start_v, cruise_v, accel)
|
|
|
|
self.extrude_pos = move.end_pos[3]
|
|
|
|
if is_pa:
|
|
|
|
self.extrude_pa_pos += move.axes_d[3]
|
2018-04-01 21:26:25 +02:00
|
|
|
cmd_SET_PRESSURE_ADVANCE_help = "Set pressure advance parameters"
|
2018-05-20 18:52:19 +02: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 21:26:25 +02:00
|
|
|
def cmd_SET_PRESSURE_ADVANCE(self, params):
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
2018-04-21 03:41:13 +02:00
|
|
|
pressure_advance = gcode.get_float(
|
|
|
|
'ADVANCE', params, self.pressure_advance, minval=0.)
|
2019-11-05 22:21:06 +01:00
|
|
|
smooth_time = gcode.get_float(
|
|
|
|
'SMOOTH_TIME', params,
|
|
|
|
self.pressure_advance_smooth_time, minval=0., maxval=.200)
|
|
|
|
self._set_pressure_advance(pressure_advance, smooth_time)
|
2018-04-03 17:42:43 +02:00
|
|
|
msg = ("pressure_advance: %.6f\n"
|
2019-11-05 22:21:06 +01:00
|
|
|
"pressure_advance_smooth_time: %.6f" % (
|
|
|
|
pressure_advance, smooth_time))
|
2018-04-03 17:42:43 +02:00
|
|
|
self.printer.set_rollover_info(self.name, "%s: %s" % (self.name, msg))
|
2019-03-04 19:04:18 +01:00
|
|
|
gcode.respond_info(msg, log=False)
|
2017-01-28 04:06:54 +01:00
|
|
|
|
|
|
|
# Dummy extruder class used when a printer has no extruder at all
|
|
|
|
class DummyExtruder:
|
2017-04-29 20:56:39 +02:00
|
|
|
def set_active(self, print_time, is_active):
|
|
|
|
return 0.
|
2019-09-18 04:53:17 +02:00
|
|
|
def update_move_time(self, flush_time):
|
|
|
|
pass
|
2017-01-28 04:06:54 +01:00
|
|
|
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
|
2019-11-25 01:02:49 +01:00
|
|
|
def get_name(self):
|
|
|
|
return ""
|
2019-11-13 17:02:26 +01:00
|
|
|
def get_heater(self):
|
|
|
|
raise homing.CommandError("Extruder not configured")
|
2017-04-29 19:57:02 +02:00
|
|
|
|
2018-07-13 04:26:32 +02:00
|
|
|
def add_printer_objects(config):
|
|
|
|
printer = config.get_printer()
|
2017-04-29 20:56:39 +02:00
|
|
|
for i in range(99):
|
2019-11-07 22:24:52 +01:00
|
|
|
section = 'extruder'
|
|
|
|
if i:
|
|
|
|
section = 'extruder%d' % (i,)
|
2017-04-29 20:56:39 +02:00
|
|
|
if not config.has_section(section):
|
|
|
|
break
|
2019-02-27 19:07:41 +01:00
|
|
|
pe = PrinterExtruder(config.getsection(section), i)
|
|
|
|
printer.add_object(section, pe)
|