diff --git a/klippy/chelper/__init__.py b/klippy/chelper/__init__.py index 7366cc81..51f59e59 100644 --- a/klippy/chelper/__init__.py +++ b/klippy/chelper/__init__.py @@ -16,7 +16,7 @@ COMPILE_CMD = ("gcc -Wall -g -O2 -shared -fPIC" " -o %s %s") SOURCE_FILES = [ 'pyhelper.c', 'serialqueue.c', 'stepcompress.c', 'itersolve.c', - 'kin_cartesian.c', 'kin_corexy.c', 'kin_delta.c', + 'kin_cartesian.c', 'kin_corexy.c', 'kin_delta.c', 'kin_extruder.c' ] DEST_LIB = "c_helper.so" OTHER_FILES = [ @@ -71,6 +71,14 @@ defs_kin_delta = """ , double tower_x, double tower_y); """ +defs_kin_extruder = """ + struct stepper_kinematics *extruder_stepper_alloc(void); + void extruder_move_fill(struct move *m, double print_time + , double accel_t, double cruise_t, double decel_t, double start_pos + , double start_v, double cruise_v, double accel + , double extra_accel_v, double extra_decel_v); +""" + defs_serialqueue = """ #define MESSAGE_MAX 64 struct pull_queue_message { @@ -109,7 +117,7 @@ defs_std = """ defs_all = [ defs_pyhelper, defs_serialqueue, defs_std, defs_stepcompress, defs_itersolve, - defs_kin_cartesian, defs_kin_corexy, defs_kin_delta + defs_kin_cartesian, defs_kin_corexy, defs_kin_delta, defs_kin_extruder ] # Return the list of file modification times diff --git a/klippy/chelper/itersolve.c b/klippy/chelper/itersolve.c index e79d5f5f..72e6ad89 100644 --- a/klippy/chelper/itersolve.c +++ b/klippy/chelper/itersolve.c @@ -17,19 +17,6 @@ * Kinematic moves ****************************************************************/ -struct move_accel { - double c1, c2; -}; - -struct move { - double print_time, move_t; - double accel_t, cruise_t; - double cruise_start_d, decel_start_d; - double cruise_v; - struct move_accel accel, decel; - struct coord start_pos, axes_r; -}; - struct move * __visible move_alloc(void) { @@ -80,7 +67,7 @@ move_eval_accel(struct move_accel *ma, double move_time) } // Return the distance moved given a time in a move -static double +inline double move_get_distance(struct move *m, double move_time) { if (unlikely(move_time < m->accel_t)) diff --git a/klippy/chelper/itersolve.h b/klippy/chelper/itersolve.h index bd8fba0e..23b3201d 100644 --- a/klippy/chelper/itersolve.h +++ b/klippy/chelper/itersolve.h @@ -7,12 +7,26 @@ struct coord { double x, y, z; }; +struct move_accel { + double c1, c2; +}; + +struct move { + double print_time, move_t; + double accel_t, cruise_t; + double cruise_start_d, decel_start_d; + double cruise_v; + struct move_accel accel, decel; + struct coord start_pos, axes_r; +}; + struct move *move_alloc(void); void move_fill(struct move *m, double print_time , double accel_t, double cruise_t, double decel_t , double start_pos_x, double start_pos_y, double start_pos_z , double axes_d_x, double axes_d_y, double axes_d_z , double start_v, double cruise_v, double accel); +double move_get_distance(struct move *m, double move_time); struct coord move_get_coord(struct move *m, double move_time); struct stepper_kinematics; diff --git a/klippy/chelper/kin_extruder.c b/klippy/chelper/kin_extruder.c new file mode 100644 index 00000000..6b34980f --- /dev/null +++ b/klippy/chelper/kin_extruder.c @@ -0,0 +1,54 @@ +// Extruder stepper pulse time generation +// +// Copyright (C) 2018 Kevin O'Connor +// +// This file may be distributed under the terms of the GNU GPLv3 license. + +#include // malloc +#include // memset +#include "compiler.h" // __visible +#include "itersolve.h" // struct stepper_kinematics +#include "pyhelper.h" // errorf + +static double +extruder_calc_position(struct stepper_kinematics *sk, struct move *m + , double move_time) +{ + return m->start_pos.x + move_get_distance(m, move_time); +} + +struct stepper_kinematics * __visible +extruder_stepper_alloc(void) +{ + struct stepper_kinematics *sk = malloc(sizeof(*sk)); + memset(sk, 0, sizeof(*sk)); + sk->calc_position = extruder_calc_position; + return sk; +} + +// Populate a 'struct move' with an extruder velocity trapezoid +void __visible +extruder_move_fill(struct move *m, double print_time + , double accel_t, double cruise_t, double decel_t + , double start_pos + , double start_v, double cruise_v, double accel + , double extra_accel_v, double extra_decel_v) +{ + // Setup velocity trapezoid + m->print_time = print_time; + m->move_t = accel_t + cruise_t + decel_t; + m->accel_t = accel_t; + m->cruise_t = cruise_t; + m->cruise_start_d = accel_t * (.5 * (cruise_v + start_v) + extra_accel_v); + m->decel_start_d = m->cruise_start_d + cruise_t * cruise_v; + + // Setup for accel/cruise/decel phases + m->cruise_v = cruise_v; + m->accel.c1 = start_v + extra_accel_v; + m->accel.c2 = .5 * accel; + m->decel.c1 = cruise_v + extra_decel_v; + m->decel.c2 = -m->accel.c2; + + // Setup start distance + m->start_pos.x = start_pos; +} diff --git a/klippy/extruder.py b/klippy/extruder.py index 35a1140c..c6c351e7 100644 --- a/klippy/extruder.py +++ b/klippy/extruder.py @@ -4,7 +4,7 @@ # # This file may be distributed under the terms of the GNU GPLv3 license. import math, logging -import stepper, homing +import stepper, homing, chelper EXTRUDE_DIFF_IGNORE = 1.02 @@ -47,6 +47,13 @@ class PrinterExtruder: 'pressure_advance_lookahead_time', 0.010, minval=0.) self.need_motor_enable = True self.extrude_pos = 0. + # 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 gcode = self.printer.lookup_object('gcode') if self.name in ('extruder', 'extruder0'): gcode.register_mux_command("SET_PRESSURE_ADVANCE", "EXTRUDER", None, @@ -154,82 +161,42 @@ class PrinterExtruder: self.stepper.motor_enable(print_time, 1) self.need_motor_enable = False axis_d = move.axes_d[3] - axis_r = abs(axis_d) / move.move_d + axis_r = 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 accel_t, cruise_t, decel_t = move.accel_t, move.cruise_t, move.decel_t - accel_d = move.accel_r * axis_d - cruise_d = move.cruise_r * axis_d - decel_d = move.decel_r * axis_d - - retract_t = retract_d = retract_v = 0. - decel_v = cruise_v # Update for pressure advance + extra_accel_v = extra_decel_v = 0. start_pos = self.extrude_pos if (axis_d >= 0. and (move.axes_d[0] or move.axes_d[1]) and self.pressure_advance): - # Increase accel_d and start_v when accelerating + # Calculate extra_accel_v pressure_advance = self.pressure_advance * move.extrude_r prev_pressure_d = start_pos - move.start_pos[3] - if accel_d: + if accel_t: npd = move.cruise_v * pressure_advance extra_accel_d = npd - prev_pressure_d if extra_accel_d > 0.: - accel_d += extra_accel_d - start_v += extra_accel_d / accel_t + extra_accel_v = extra_accel_d / accel_t + axis_d += extra_accel_d prev_pressure_d += extra_accel_d - # Update decel and retract parameters when decelerating + # Calculate extra_decel_v emcv = move.extrude_max_corner_v - if decel_d and emcv < move.cruise_v: + if decel_t and emcv < move.cruise_v: npd = max(emcv, move.end_v) * pressure_advance - extra_decel_d = prev_pressure_d - npd - if extra_decel_d > 0.: + extra_decel_d = npd - prev_pressure_d + if extra_decel_d < 0.: + axis_d += extra_decel_d 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 - retract_t = -end_v / accel - 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 - # Prepare for steps - step_const = self.stepper.step_const - move_time = print_time - - # Acceleration steps - if accel_d: - step_const(move_time, start_pos, accel_d, start_v, accel) - start_pos += accel_d - move_time += accel_t - # Cruising steps - if cruise_d: - step_const(move_time, start_pos, cruise_d, cruise_v, 0.) - start_pos += cruise_d - move_time += cruise_t - # Deceleration steps - if decel_d: - step_const(move_time, start_pos, decel_d, decel_v, -accel) - start_pos += decel_d - move_time += decel_t - # Retraction steps - if retract_d: - step_const(move_time, start_pos, -retract_d, retract_v, accel) - start_pos -= retract_d - self.extrude_pos = start_pos + # 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 cmd_SET_PRESSURE_ADVANCE_help = "Set pressure advance parameters" def cmd_default_SET_PRESSURE_ADVANCE(self, params): extruder = self.printer.lookup_object('toolhead').get_extruder()