Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dmbutyugin's advanced features branch #69

Merged
merged 35 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
006b5b7
pa_test: Utility class to print PA tower and virtual_sdcard refactoring
dmbutyugin Sep 13, 2022
4c330b4
ringing_test: Utility class to print ringing tower
dmbutyugin Oct 16, 2022
86a4bfd
virtual_sdcard: Fixed stats reporting in PA and ringing tests
dmbutyugin May 30, 2023
a1cf7e7
pa_test: Some improvements for the PA test
dmbutyugin Dec 6, 2022
3899bae
pa_test: Changed PA test for square corner velocity testing
dmbutyugin Feb 11, 2023
58cccae
pa_test: Added medium velocity to the test
dmbutyugin Feb 15, 2023
057e158
pa_test: Replaced one of very slow speeds with dwell
dmbutyugin Feb 19, 2023
0b80134
pa_test: Added one dwell to the PA test on Y axis
dmbutyugin Apr 7, 2023
58afffc
extruder: Split extruder motion into X/Y/Z components
dmbutyugin Apr 2, 2023
eca1caa
extruder: Added support for time offset of extruder vs kinematic moves
dmbutyugin Feb 11, 2023
75313c3
extruder: Explicit PA velocity term calculation
dmbutyugin Jan 10, 2023
51ed06e
extruder: Improve numerical stability of time-weighted averaging
dmbutyugin Feb 15, 2023
dabfd2b
extruder: Added support for non-linear Pressure Advance
dmbutyugin Jan 10, 2023
813723a
extruder: Sync extruder motion with input shaping
dmbutyugin Apr 7, 2023
9661a23
motan: Report queued steps in extended format
dmbutyugin Apr 7, 2023
fe1304a
stepper: New optional high precision stepping protocol
dmbutyugin Apr 7, 2023
b8c0523
stepper: Optionally enable new stepcompress protocol in MCU firmware
dmbutyugin Apr 15, 2023
ad33469
input_shaper: Added custom input shapers support
dmbutyugin Apr 23, 2023
eebf356
input_shaper: Added support of smooth input shapers
dmbutyugin Apr 23, 2023
1ef3a53
input_shaper: Added some predefined input smoothers
dmbutyugin Apr 26, 2023
84e1228
integrate: Slightly more optimized versions of smoother integration
dmbutyugin May 6, 2023
961c22a
chelper: Added O3, NEON (for ARM) and native CPU optimizations flags
dmbutyugin May 6, 2023
dfdabe0
input_shaper: Updated and added some smoother definitions
dmbutyugin May 10, 2023
6943792
integrate: Faster integration via antiderivatives calculation
dmbutyugin May 10, 2023
c0aa668
scripts: Support smoothers in shaper calibration and plotting scripts
dmbutyugin May 21, 2023
1bb4236
input_shaper: Added smooth_zvd_ei smoother
dmbutyugin May 24, 2023
375633f
shaper_calibrate: Use system backwards velocity for shaper estimations
dmbutyugin May 24, 2023
8ae68cf
shaper_calibrate: A modified shaper calibration approach
dmbutyugin May 24, 2023
8d918bb
input_shaper: Updated minimum smoother frequencies
dmbutyugin May 24, 2023
1092d0d
input_shaper: Added customized smoothers for extruder
dmbutyugin Jun 11, 2023
7b74e5d
input_shaper: Moved shaper/smoother offset calculation functions
dmbutyugin Jun 21, 2023
d5938d0
ringing_test: Added velocity changing options and their validation
dmbutyugin Jun 21, 2023
4bbec27
shaper_calibrate: Small fix for input smoother max velocity estimation
dmbutyugin Jul 5, 2023
af24841
pa_test: Added more interesting velocity transitions
dmbutyugin Jul 12, 2023
40cc044
python format
rogerlz Oct 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ Features merged into the master branch:

- [homing: sensorless minimum home distance](https://github.com/DangerKlippers/danger-klipper/pull/65)

If you're feeling adventurous, take a peek at the extra features in the bleeding-edge branch:

- [dmbutyugin's advanced-features branch](https://github.com/DangerKlippers/danger-klipper/pull/69) [dmbutyugin/advanced-features](https://github.com/dmbutyugin/klipper/commits/advanced-features)

- [stepper: high precision stepping protocol]()

- [extruder: sync extruder motion with input shaper]()

- [extruder: new print_pa_tower utility]()

- [input_shaper: smooth input shapers]()

- [input_shaper: new print_ringing_tower utility]()

"Dangerous Klipper for dangerous users"

Klipper is a 3d-Printer firmware. It combines the power of a general
Expand Down
34 changes: 29 additions & 5 deletions klippy/chelper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@

GCC_CMD = "gcc"
COMPILE_ARGS = (
"-Wall -g -O2 -shared -fPIC"
"-Wall -g -O3 -shared -fPIC"
" -flto -fwhole-program -fno-use-linker-plugin"
" -march=native -mcpu=native -mtune=native"
" -o %s %s"
)
SSE_FLAGS = "-mfpmath=sse -msse2"
NEON_FLAGS = "-mfpu=neon"
SOURCE_FILES = [
"pyhelper.c",
"serialqueue.c",
"stepcompress.c",
"stepcompress_hp.c",
"itersolve.c",
"trapq.c",
"pollreactor.c",
Expand All @@ -38,6 +41,7 @@
"kin_extruder.c",
"kin_shaper.c",
"kin_idex.c",
"integrate.c",
]
DEST_LIB = "c_helper.so"
OTHER_FILES = [
Expand All @@ -49,16 +53,19 @@
"trapq.h",
"pollreactor.h",
"msgblock.h",
"kin_shaper.h",
"integrate.h",
]

defs_stepcompress = """
struct pull_history_steps {
uint64_t first_clock, last_clock;
int64_t start_position;
int step_count, interval, add;
int step_count, interval, add, add2, shift;
};

struct stepcompress *stepcompress_alloc(uint32_t oid);
struct stepcompress *stepcompress_hp_alloc(uint32_t oid);
void stepcompress_fill(struct stepcompress *sc, uint32_t max_error
, int32_t queue_step_msgtag, int32_t set_next_step_dir_msgtag);
void stepcompress_set_invert_sdir(struct stepcompress *sc
Expand Down Expand Up @@ -161,14 +168,29 @@
defs_kin_extruder = """
struct stepper_kinematics *extruder_stepper_alloc(void);
void extruder_set_pressure_advance(struct stepper_kinematics *sk
, double pressure_advance, double smooth_time);
, int n_params, double params[], double time_offset);
struct pressure_advance_params;
double pressure_advance_linear_model_func(double position
, double pa_velocity, struct pressure_advance_params *pa_params);
double pressure_advance_tanh_model_func(double position
, double pa_velocity, struct pressure_advance_params *pa_params);
double pressure_advance_recipr_model_func(double position
, double pa_velocity, struct pressure_advance_params *pa_params);
void extruder_set_pressure_advance_model_func(struct stepper_kinematics *sk
, double (*func)(double, double, struct pressure_advance_params *));
int extruder_set_shaper_params(struct stepper_kinematics *sk, char axis
, int n, double a[], double t[]);
int extruder_set_smoothing_params(struct stepper_kinematics *sk, char axis
, int n, double a[], double t_sm, double t_offs);
double extruder_get_step_gen_window(struct stepper_kinematics *sk);
"""

defs_kin_shaper = """
double input_shaper_get_step_generation_window(
struct stepper_kinematics *sk);
double input_shaper_get_step_gen_window(struct stepper_kinematics *sk);
int input_shaper_set_shaper_params(struct stepper_kinematics *sk, char axis
, int n, double a[], double t[]);
int input_shaper_set_smoother_params(struct stepper_kinematics *sk
, char axis, int n, double a[], double t_sm);
int input_shaper_set_sk(struct stepper_kinematics *sk
, struct stepper_kinematics *orig_sk);
struct stepper_kinematics * input_shaper_alloc(void);
Expand Down Expand Up @@ -319,6 +341,8 @@ def get_ffi():
if check_build_code(srcfiles + ofiles + [__file__], destlib):
if check_gcc_option(SSE_FLAGS):
cmd = "%s %s %s" % (GCC_CMD, SSE_FLAGS, COMPILE_ARGS)
elif check_gcc_option(NEON_FLAGS):
cmd = "%s %s %s" % (GCC_CMD, NEON_FLAGS, COMPILE_ARGS)
else:
cmd = "%s %s" % (GCC_CMD, COMPILE_ARGS)
logging.info("Building C code module %s", DEST_LIB)
Expand Down
106 changes: 106 additions & 0 deletions klippy/chelper/integrate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Helpers to integrate the smoothing weight function
//
// Copyright (C) 2019-2020 Kevin O'Connor <[email protected]>
// Copyright (C) 2020-2023 Dmitry Butyugin <[email protected]>
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include "compiler.h" // unlikely
#include "integrate.h"
#include "trapq.h" // struct move

#include <string.h>

/****************************************************************
* Generic smoother integration
****************************************************************/

static double coeffs[] = {
1./1., 1./2., 1./3., 1./4., 1./5., 1./6., 1./7., 1./8., 1./9., 1./10.,
1./11., 1./12., 1./13., 1./14., 1./15., 1./16., 1./17., 1./18., 1./19.,
};

inline smoother_antiderivatives
calc_antiderivatives(const struct smoother* sm, double t)
{
int n = sm->n, i;
double it0 = (sm->c0[0] * t + sm->c0[1]) * t;
double it1 = (sm->c1[0] * t + sm->c1[1]) * t;
double it2 = (sm->c2[0] * t + sm->c2[1]) * t;
for (i = 2; i < n; ++i) {
it0 = (it0 + sm->c0[i]) * t;
it1 = (it1 + sm->c1[i]) * t;
it2 = (it2 + sm->c2[i]) * t;
}
it1 *= t;
it2 *= t * t;
return (smoother_antiderivatives) {
.it0 = it0, .it1 = it1, .it2 = it2 };
}

inline smoother_antiderivatives
diff_antiderivatives(const smoother_antiderivatives* ad1
, const smoother_antiderivatives* ad2)
{
return (smoother_antiderivatives) {
.it0 = ad2->it0 - ad1->it0,
.it1 = ad2->it1 - ad1->it1,
.it2 = ad2->it2 - ad1->it2 };
}

inline double
integrate_move(const struct move* m, int axis, double base, double t0
, const smoother_antiderivatives* s
, double* smooth_velocity)
{
double axis_r = m->axes_r.axis[axis - 'x'];
double start_v = m->start_v * axis_r;
double half_accel = m->half_accel * axis_r;
// Substitute the integration variable tnew = t0 - t to simplify integrals
double accel = 2. * half_accel;
base += (half_accel * t0 + start_v) * t0;
start_v += accel * t0;
double smooth_pos = base * s->it0 - start_v * s->it1 + half_accel * s->it2;
if (smooth_velocity)
*smooth_velocity = start_v * s->it0 - accel * s->it1;
return smooth_pos;
}

/****************************************************************
* Smoother initialization
****************************************************************/

int
init_smoother(int n, const double a[], double t_sm, struct smoother* sm)
{
if ((t_sm && n < 2) || n > ARRAY_SIZE(sm->c0))
return -1;
memset(sm, 0, sizeof(*sm));
sm->n = n;
sm->hst = 0.5 * t_sm;
if (!t_sm) return 0;
double inv_t_sm = 1. / t_sm;
double inv_t_sm_n = inv_t_sm;
int i, symm = n & 1;
for (i = 0; i < n; ++i) {
if ((i & 1) && a[i]) symm = 0;
double c = a[i] * inv_t_sm_n;
sm->c0[n-1-i] = c * coeffs[i];
sm->c1[n-1-i] = c * coeffs[i+1];
sm->c2[n-1-i] = c * coeffs[i+2];
inv_t_sm_n *= inv_t_sm;
}
sm->symm = symm;
double inv_norm = 1. / (calc_antiderivatives(sm, sm->hst).it0
- calc_antiderivatives(sm, -sm->hst).it0);
for (i = 0; i < n; ++i) {
sm->c0[i] *= inv_norm;
sm->c1[i] *= inv_norm;
sm->c2[i] *= inv_norm;
}
sm->p_hst = calc_antiderivatives(sm, sm->hst);
sm->m_hst = calc_antiderivatives(sm, -sm->hst);
sm->pm_diff = diff_antiderivatives(&sm->m_hst, &sm->p_hst);
sm->t_offs = sm->pm_diff.it1;
return 0;
}
29 changes: 29 additions & 0 deletions klippy/chelper/integrate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef INTEGRATE_H
#define INTEGRATE_H

typedef struct {
double it0, it1, it2;
} smoother_antiderivatives;

struct smoother {
double c0[10], c1[10], c2[10];
double hst, t_offs;
smoother_antiderivatives m_hst, p_hst, pm_diff;
int n, symm;
};

struct move;

int init_smoother(int n, const double a[], double t_sm, struct smoother* sm);

double integrate_move(const struct move* m, int axis, double base, double t0
, const smoother_antiderivatives* s
, double* smooth_velocity);

smoother_antiderivatives
calc_antiderivatives(const struct smoother* sm, double t);
smoother_antiderivatives
diff_antiderivatives(const smoother_antiderivatives* ad1
, const smoother_antiderivatives* ad2);

#endif // integrate.h
Loading