2016-07-10 18:23:35 +02:00
|
|
|
# Code for handling printer nozzle extruders
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2017-01-03 01:22:32 +01:00
|
|
|
import math, logging
|
2016-10-01 01:04:24 +02:00
|
|
|
import stepper, heater, homing
|
2016-07-10 18:23:35 +02:00
|
|
|
|
2017-01-28 04:06:54 +01:00
|
|
|
EXTRUDE_DIFF_IGNORE = 1.02
|
|
|
|
|
2016-07-10 18:23:35 +02:00
|
|
|
class PrinterExtruder:
|
|
|
|
def __init__(self, printer, config):
|
2017-01-03 00:58:45 +01:00
|
|
|
self.config = config
|
2016-10-01 00:49:38 +02:00
|
|
|
self.heater = heater.PrinterHeater(printer, config)
|
2016-11-17 23:24:03 +01:00
|
|
|
self.stepper = stepper.PrinterStepper(printer, config, 'extruder')
|
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-01-03 01:22:32 +01:00
|
|
|
filament_area = math.pi * (filament_diameter * .5)**2
|
|
|
|
max_cross_section = config.getfloat(
|
2017-04-11 17:37:09 +02:00
|
|
|
'max_extrude_cross_section', 4. * self.nozzle_diameter**2
|
|
|
|
, above=0.)
|
2017-01-03 01:22:32 +01:00
|
|
|
self.max_extrude_ratio = max_cross_section / filament_area
|
2017-04-11 17:37:09 +02:00
|
|
|
self.max_e_dist = config.getfloat(
|
|
|
|
'max_extrude_only_distance', 50., minval=0.)
|
2017-01-03 00:58:45 +01:00
|
|
|
self.max_e_velocity = self.max_e_accel = None
|
2017-05-20 03:05:46 +02:00
|
|
|
self.activate_gcode = config.get('activate_gcode', '')
|
|
|
|
self.deactivate_gcode = config.get('deactivate_gcode', '')
|
2017-04-11 17:37:09 +02:00
|
|
|
self.pressure_advance = config.getfloat(
|
|
|
|
'pressure_advance', 0., minval=0.)
|
2017-02-22 16:09:25 +01:00
|
|
|
self.pressure_advance_lookahead_time = 0.
|
|
|
|
if self.pressure_advance:
|
|
|
|
self.pressure_advance_lookahead_time = config.getfloat(
|
2017-04-11 17:37:09 +02:00
|
|
|
'pressure_advance_lookahead_time', 0.010, minval=0.)
|
2016-11-14 19:40:35 +01:00
|
|
|
self.need_motor_enable = True
|
2016-07-17 01:58:41 +02:00
|
|
|
self.extrude_pos = 0.
|
2017-01-03 00:58:45 +01:00
|
|
|
def set_max_jerk(self, max_xy_halt_velocity, max_velocity, max_accel):
|
|
|
|
self.max_e_velocity = self.config.getfloat(
|
2017-04-11 17:37:09 +02:00
|
|
|
'max_extrude_only_velocity', max_velocity * self.max_extrude_ratio
|
|
|
|
, above=0.)
|
2017-01-03 00:58:45 +01:00
|
|
|
self.max_e_accel = self.config.getfloat(
|
2017-04-11 17:37:09 +02:00
|
|
|
'max_extrude_only_accel', max_accel * self.max_extrude_ratio
|
|
|
|
, above=0.)
|
2016-12-01 21:29:26 +01:00
|
|
|
self.stepper.set_max_jerk(9999999.9, 9999999.9)
|
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:
|
|
|
|
return self.activate_gcode
|
|
|
|
return self.deactivate_gcode
|
2016-07-10 18:23:35 +02:00
|
|
|
def motor_off(self, move_time):
|
|
|
|
self.stepper.motor_enable(move_time, 0)
|
2016-11-14 19:40:35 +01:00
|
|
|
self.need_motor_enable = True
|
2016-09-30 20:47:45 +02:00
|
|
|
def check_move(self, move):
|
2017-01-28 04:06:54 +01:00
|
|
|
move.extrude_r = move.axes_d[3] / move.move_d
|
2017-01-28 04:32:12 +01:00
|
|
|
move.extrude_max_corner_v = 0.
|
2016-10-01 01:04:24 +02:00
|
|
|
if not self.heater.can_extrude:
|
2016-11-18 18:42:39 +01:00
|
|
|
raise homing.EndstopMoveError(
|
|
|
|
move.end_pos, "Extrude below minimum temp")
|
2017-05-02 14:57:38 +02: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 13:50:58 +02:00
|
|
|
if abs(move.axes_d[3]) > self.max_e_dist:
|
2017-01-03 01:22:32 +01:00
|
|
|
raise homing.EndstopMoveError(
|
2017-05-02 14:57:38 +02:00
|
|
|
move.end_pos, "Extrude move too long")
|
|
|
|
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)
|
|
|
|
elif (move.extrude_r > self.max_extrude_ratio
|
|
|
|
and move.axes_d[3] > self.nozzle_diameter*self.max_extrude_ratio):
|
2017-03-20 02:38:59 +01:00
|
|
|
logging.debug("Overextrude: %s vs %s" % (
|
|
|
|
move.extrude_r, self.max_extrude_ratio))
|
2017-01-03 01:22:32 +01:00
|
|
|
raise homing.EndstopMoveError(
|
|
|
|
move.end_pos, "Move exceeds maximum extrusion cross section")
|
2017-01-28 04:06:54 +01:00
|
|
|
def calc_junction(self, prev_move, move):
|
2017-03-20 19:39:06 +01: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 04:06:54 +01: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 04:32:12 +01:00
|
|
|
def lookahead(self, moves, flush_count, lazy):
|
2017-02-22 16:09:25 +01:00
|
|
|
lookahead_t = self.pressure_advance_lookahead_time
|
|
|
|
if not lookahead_t:
|
2017-01-28 04:32:12 +01: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 16:09:25 +01:00
|
|
|
sum_t = lookahead_t
|
2017-01-28 04:32:12 +01:00
|
|
|
for j in range(i+1, flush_count):
|
|
|
|
fmove = moves[j]
|
|
|
|
if not fmove.max_start_v2:
|
|
|
|
break
|
2017-02-22 16:09:25 +01:00
|
|
|
if fmove.cruise_v > max_corner_v:
|
2017-03-03 20:13:35 +01: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 16:09:25 +01: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 04:32:12 +01: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
|
2016-07-10 18:23:35 +02:00
|
|
|
def move(self, move_time, move):
|
2016-11-14 19:40:35 +01:00
|
|
|
if self.need_motor_enable:
|
|
|
|
self.stepper.motor_enable(move_time, 1)
|
|
|
|
self.need_motor_enable = False
|
2016-11-05 00:28:07 +01:00
|
|
|
axis_d = move.axes_d[3]
|
2017-04-04 18:31:03 +02:00
|
|
|
axis_r = abs(axis_d) / move.move_d
|
|
|
|
accel = move.accel * axis_r
|
|
|
|
start_v = move.start_v * axis_r
|
|
|
|
cruise_v = move.cruise_v * axis_r
|
|
|
|
end_v = move.end_v * axis_r
|
2016-07-17 01:58:41 +02:00
|
|
|
accel_t, cruise_t, decel_t = move.accel_t, move.cruise_t, move.decel_t
|
2016-11-05 00:28:07 +01:00
|
|
|
accel_d = move.accel_r * axis_d
|
|
|
|
cruise_d = move.cruise_r * axis_d
|
|
|
|
decel_d = move.decel_r * axis_d
|
2016-07-17 01:58:41 +02:00
|
|
|
|
|
|
|
retract_t = retract_d = retract_v = 0.
|
|
|
|
decel_v = cruise_v
|
|
|
|
|
|
|
|
# Update for pressure advance
|
2016-11-05 02:11:14 +01:00
|
|
|
start_pos = self.extrude_pos
|
2016-11-05 00:28:07 +01:00
|
|
|
if (axis_d >= 0. and (move.axes_d[0] or move.axes_d[1])
|
2016-07-17 01:58:41 +02:00
|
|
|
and self.pressure_advance):
|
|
|
|
# Increase accel_d and start_v when accelerating
|
2017-01-28 04:32:12 +01:00
|
|
|
pressure_advance = self.pressure_advance * move.extrude_r
|
2016-11-05 02:11:14 +01:00
|
|
|
prev_pressure_d = start_pos - move.start_pos[3]
|
2017-01-28 04:32:12 +01:00
|
|
|
if accel_d:
|
|
|
|
npd = move.cruise_v * pressure_advance
|
2016-11-05 02:11:14 +01:00
|
|
|
extra_accel_d = npd - prev_pressure_d
|
|
|
|
if extra_accel_d > 0.:
|
|
|
|
accel_d += extra_accel_d
|
|
|
|
start_v += extra_accel_d / accel_t
|
|
|
|
prev_pressure_d += extra_accel_d
|
2016-07-17 01:58:41 +02:00
|
|
|
# Update decel and retract parameters when decelerating
|
2017-01-28 04:32:12 +01:00
|
|
|
emcv = move.extrude_max_corner_v
|
|
|
|
if decel_d and emcv < move.cruise_v:
|
|
|
|
npd = max(emcv, move.end_v) * pressure_advance
|
|
|
|
extra_decel_d = prev_pressure_d - npd
|
2016-11-05 02:11:14 +01:00
|
|
|
if extra_decel_d > 0.:
|
|
|
|
extra_decel_v = extra_decel_d / decel_t
|
|
|
|
decel_v -= extra_decel_v
|
|
|
|
end_v -= extra_decel_v
|
|
|
|
if decel_v <= 0.:
|
|
|
|
# The entire decel phase is replaced with retraction
|
|
|
|
retract_t = decel_t
|
|
|
|
retract_d = -(end_v + decel_v) * 0.5 * decel_t
|
|
|
|
retract_v = -decel_v
|
|
|
|
decel_t = decel_d = 0.
|
|
|
|
elif end_v < 0.:
|
|
|
|
# Split decel phase into decel and retraction
|
2017-04-04 18:31:03 +02:00
|
|
|
retract_t = -end_v / accel
|
2016-11-05 02:11:14 +01:00
|
|
|
retract_d = -end_v * 0.5 * retract_t
|
|
|
|
decel_t -= retract_t
|
|
|
|
decel_d = decel_v * 0.5 * decel_t
|
|
|
|
else:
|
|
|
|
# There is still only a decel phase (no retraction)
|
|
|
|
decel_d -= extra_decel_d
|
2016-07-17 01:58:41 +02:00
|
|
|
|
2016-11-10 18:44:04 +01:00
|
|
|
# Prepare for steps
|
2016-11-14 19:40:35 +01:00
|
|
|
mcu_stepper = self.stepper.mcu_stepper
|
|
|
|
mcu_time = mcu_stepper.print_to_mcu_time(move_time)
|
2016-07-17 01:58:41 +02:00
|
|
|
|
2016-11-10 18:44:04 +01:00
|
|
|
# Acceleration steps
|
|
|
|
if accel_d:
|
2017-04-07 18:51:52 +02:00
|
|
|
mcu_stepper.step_const(mcu_time, start_pos, accel_d, start_v, accel)
|
2017-04-04 18:31:03 +02:00
|
|
|
start_pos += accel_d
|
2016-08-24 22:42:25 +02:00
|
|
|
mcu_time += accel_t
|
2016-11-10 18:44:04 +01:00
|
|
|
# Cruising steps
|
|
|
|
if cruise_d:
|
2017-04-07 18:51:52 +02:00
|
|
|
mcu_stepper.step_const(mcu_time, start_pos, cruise_d, cruise_v, 0.)
|
2017-04-04 18:31:03 +02:00
|
|
|
start_pos += cruise_d
|
2016-08-24 22:42:25 +02:00
|
|
|
mcu_time += cruise_t
|
2016-11-10 18:44:04 +01:00
|
|
|
# Deceleration steps
|
|
|
|
if decel_d:
|
2017-04-07 18:51:52 +02:00
|
|
|
mcu_stepper.step_const(mcu_time, start_pos, decel_d, decel_v, -accel)
|
2017-04-04 18:31:03 +02:00
|
|
|
start_pos += decel_d
|
2016-11-10 18:44:04 +01:00
|
|
|
mcu_time += decel_t
|
|
|
|
# Retraction steps
|
|
|
|
if retract_d:
|
2017-04-07 18:51:52 +02:00
|
|
|
mcu_stepper.step_const(
|
2017-04-04 18:31:03 +02:00
|
|
|
mcu_time, start_pos, -retract_d, retract_v, accel)
|
|
|
|
start_pos -= retract_d
|
|
|
|
self.extrude_pos = start_pos
|
2017-01-28 04:06:54 +01:00
|
|
|
|
|
|
|
# Dummy extruder class used when a printer has no extruder at all
|
|
|
|
class DummyExtruder:
|
|
|
|
def set_max_jerk(self, max_xy_halt_velocity, max_velocity, max_accel):
|
|
|
|
pass
|
2017-04-29 20:56:39 +02:00
|
|
|
def set_active(self, print_time, is_active):
|
|
|
|
return 0.
|
2017-01-28 04:06:54 +01: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 19:57:02 +02:00
|
|
|
|
|
|
|
def add_printer_objects(printer, config):
|
2017-04-29 20:56:39 +02:00
|
|
|
for i in range(99):
|
|
|
|
section = 'extruder%d' % (i,)
|
|
|
|
if not config.has_section(section):
|
|
|
|
if not i and config.has_section('extruder'):
|
|
|
|
printer.add_object('extruder0', PrinterExtruder(
|
|
|
|
printer, config.getsection('extruder')))
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
printer.add_object(section, PrinterExtruder(
|
|
|
|
printer, config.getsection(section)))
|
|
|
|
|
|
|
|
def get_printer_extruders(printer):
|
|
|
|
out = []
|
|
|
|
for i in range(99):
|
|
|
|
extruder = printer.objects.get('extruder%d' % (i,))
|
|
|
|
if extruder is None:
|
|
|
|
break
|
|
|
|
out.append(extruder)
|
|
|
|
return out
|
2017-07-04 18:24:11 +02:00
|
|
|
|
|
|
|
def get_printer_heater(printer, name):
|
|
|
|
if name == 'heater_bed':
|
|
|
|
return printer.objects.get(name)
|
|
|
|
if name == 'extruder':
|
|
|
|
name = 'extruder0'
|
|
|
|
extruder = printer.objects.get(name)
|
|
|
|
if extruder is None:
|
|
|
|
return None
|
|
|
|
return extruder.get_heater()
|