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/advanced-features v2 #262

Merged
merged 38 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6b6489b
pa_test: Utility class to print PA tower and virtual_sdcard refactoring
dmbutyugin May 19, 2024
abbcb0d
ringing_test: Utility class to print ringing tower
dmbutyugin May 19, 2024
f2cc0cc
pa_test: Changed PA test for square corner velocity testing
dmbutyugin May 19, 2024
0421e7a
extruder: Explicit PA velocity term calculation
dmbutyugin May 19, 2024
fc66490
extruder: Improve numerical stability of time-weighted averaging
dmbutyugin May 19, 2024
91b9d8e
extruder: Added support for non-linear Pressure Advance
dmbutyugin May 19, 2024
7d4e9c1
pa_test: Added medium velocity to the test
dmbutyugin May 19, 2024
3025b95
pa_test: Replaced one of very slow speeds with dwell
dmbutyugin May 19, 2024
74c74c8
extruder: Added support for time offset of extruder vs kinematic moves
dmbutyugin May 19, 2024
2b48157
pa_test: Added one dwell to the PA test on Y axis
dmbutyugin May 19, 2024
89c7369
extruder: Split extruder motion into X/Y/Z components
dmbutyugin May 19, 2024
8ae4bf5
extruder: Sync extruder motion with input shaping
dmbutyugin May 19, 2024
b3b0cc1
motan: Report queued steps in extended format
dmbutyugin May 19, 2024
31f9952
stepper: New optional high precision stepping protocol
dmbutyugin May 19, 2024
9dfc198
stepper: Optionally enable new stepcompress protocol in MCU firmware
dmbutyugin May 19, 2024
be69b99
input_shaper: Added custom input shapers support
dmbutyugin May 19, 2024
cd36857
input_shaper: Added support of smooth input shapers
dmbutyugin May 19, 2024
3da4f4a
input_shaper: Added some predefined input smoothers
dmbutyugin May 19, 2024
c38f2c9
integrate: Slightly more optimized versions of smoother integration
dmbutyugin May 19, 2024
4c604e4
chelper: Added O3, NEON (for ARM) and native CPU optimizations flags
dmbutyugin May 19, 2024
785e218
input_shaper: Updated and added some smoother definitions
dmbutyugin May 19, 2024
12a075a
integrate: Faster integration via antiderivatives calculation
dmbutyugin May 19, 2024
0e7c00b
scripts: Support smoothers in shaper calibration and plotting scripts
dmbutyugin May 19, 2024
c6d4c49
input_shaper: Added smooth_zvd_ei smoother
dmbutyugin May 19, 2024
861d808
shaper_calibrate: Use system backwards velocity for shaper estimations
dmbutyugin May 19, 2024
243bfb5
shaper_calibrate: A modified shaper calibration approach
dmbutyugin May 19, 2024
02134fa
input_shaper: Updated minimum smoother frequencies
dmbutyugin May 19, 2024
418506b
virtual_sdcard: Fixed stats reporting in PA and ringing tests
dmbutyugin May 19, 2024
6b57f7c
pa_test: Some improvements for the PA test
dmbutyugin May 19, 2024
61579b8
input_shaper: Added customized smoothers for extruder
dmbutyugin May 19, 2024
5492cf9
input_shaper: Moved shaper/smoother offset calculation functions
dmbutyugin May 19, 2024
bddf7c2
ringing_test: Added velocity changing options and their validation
dmbutyugin May 19, 2024
dcc5c44
shaper_calibrate: Small fix for input smoother max velocity estimation
dmbutyugin May 19, 2024
d471379
pa_test: Added more interesting velocity transitions
dmbutyugin May 19, 2024
f69d277
input_shaper: Explicit calculation of extruder smoothers
dmbutyugin May 19, 2024
cb87def
scripts: A script to plot extrusion width and extruder motion with IS
dmbutyugin May 19, 2024
6653088
input_shaper: Updated definitions of *EI input shapers
dmbutyugin May 19, 2024
273fd82
make ci happy
rogerlz May 19, 2024
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
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 @@ -165,14 +172,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 @@ -325,6 +347,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 <kevin@koconnor.net>
// Copyright (C) 2020-2023 Dmitry Butyugin <dmbutyugin@google.com>
//
// 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[12], c1[12], c2[12];
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
Loading