Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 7 additions & 7 deletions docs/Code_Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ provides further information on the mechanics of moves.
is used to improve future guesses so that the process rapidly
converges to the desired time. The kinematic stepper position
formulas are located in the klippy/chelper/ directory (eg,
kin_cart.c, kin_corexy.c, kin_delta.c, kin_extruder.c).
kin_cartesian.c, kin_delta.c, kin_extruder.c).

* After the iterative solver calculates the step times they are added
to an array: `itersolve_gen_steps_range() -> stepcompress_append()`
Expand Down Expand Up @@ -376,12 +376,12 @@ Useful steps:
in cartesian coordinates to the movement on each stepper. One
should be able to copy one of these files as a starting point.
3. Implement the C stepper kinematic position functions for each
stepper if they are not already available (see kin_cart.c,
kin_corexy.c, and kin_delta.c in klippy/chelper/). The function
should call `move_get_coord()` to convert a given move time (in
seconds) to a cartesian coordinate (in millimeters), and then
calculate the desired stepper position (in millimeters) from that
cartesian coordinate.
stepper if they are not already available (see kin_cartesian.c, and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: stray comma in "see kin_cartesian.c, and kin_delta.c".

kin_delta.c in klippy/chelper/). The function should call
`move_get_coord()` to convert a given move time (in seconds) to a
cartesian coordinate (in millimeters), and then calculate the
desired stepper position (in millimeters) from that cartesian
coordinate.
4. Implement the `calc_position()` method in the new kinematics class.
This method calculates the position of the toolhead in cartesian
coordinates from the position of each stepper. It does not need to
Expand Down
24 changes: 6 additions & 18 deletions klippy/chelper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
SOURCE_FILES = [
'pyhelper.c', 'serialqueue.c', 'stepcompress.c', 'steppersync.c',
'itersolve.c', 'trapq.c', 'pollreactor.c', 'msgblock.c', 'trdispatch.c',
'kin_cartesian.c', 'kin_corexy.c', 'kin_corexz.c', 'kin_delta.c',
'kin_deltesian.c', 'kin_polar.c', 'kin_rotary_delta.c', 'kin_winch.c',
'kin_extruder.c', 'kin_shaper.c', 'kin_idex.c', 'kin_generic.c'
'kin_cartesian.c', 'kin_delta.c', 'kin_deltesian.c', 'kin_polar.c',
'kin_rotary_delta.c', 'kin_winch.c',
'kin_extruder.c', 'kin_shaper.c', 'kin_idex.c',
]
DEST_LIB = "c_helper.so"
OTHER_FILES = [
Expand Down Expand Up @@ -114,23 +114,12 @@
"""

defs_kin_cartesian = """
struct stepper_kinematics *cartesian_stepper_alloc(char axis);
"""
defs_kin_generic_cartesian = """
struct stepper_kinematics *generic_cartesian_stepper_alloc(double a_x
struct stepper_kinematics *cartesian_stepper_alloc(double a_x
, double a_y, double a_z);
void generic_cartesian_stepper_set_coeffs(struct stepper_kinematics *sk
void cartesian_stepper_set_coeffs(struct stepper_kinematics *sk
, double a_x, double a_y, double a_z);
"""

defs_kin_corexy = """
struct stepper_kinematics *corexy_stepper_alloc(char type);
"""

defs_kin_corexz = """
struct stepper_kinematics *corexz_stepper_alloc(char type);
"""

defs_kin_delta = """
struct stepper_kinematics *delta_stepper_alloc(double arm2
, double tower_x, double tower_y);
Expand Down Expand Up @@ -237,10 +226,9 @@
defs_all = [
defs_pyhelper, defs_serialqueue, defs_std, defs_stepcompress,
defs_steppersync, defs_itersolve, defs_trapq, defs_trdispatch,
defs_kin_cartesian, defs_kin_corexy, defs_kin_corexz, defs_kin_delta,
defs_kin_cartesian, defs_kin_delta,
defs_kin_deltesian, defs_kin_polar, defs_kin_rotary_delta, defs_kin_winch,
defs_kin_extruder, defs_kin_shaper, defs_kin_idex,
defs_kin_generic_cartesian,
]

# Update filenames to an absolute path
Expand Down
92 changes: 74 additions & 18 deletions klippy/chelper/kin_cartesian.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
// Cartesian kinematics stepper pulse time generation
// Generic cartesian kinematics stepper position calculation
//
// Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2018-2026 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2024 Dmitry Butyugin <dmbutyugin@google.com>
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include <stddef.h> // offsetof
#include <stdlib.h> // malloc
#include <string.h> // memset
#include "compiler.h" // __visible
#include "itersolve.h" // struct stepper_kinematics
#include "pyhelper.h" // errorf
#include "trapq.h" // move_get_coord

struct cartesian_stepper {
struct stepper_kinematics sk;
struct coord a;
};

// Optimized calc_position() calls for simple cartesian axes
static double
cart_stepper_x_calc_position(struct stepper_kinematics *sk, struct move *m
, double move_time)
Expand All @@ -32,20 +39,69 @@ cart_stepper_z_calc_position(struct stepper_kinematics *sk, struct move *m
return move_get_coord(m, move_time).z;
}

// Optimized calc_position() calls for common corexy axes
static double
corexy_stepper_plus_calc_position(struct stepper_kinematics *sk, struct move *m
, double move_time)
{
struct coord c = move_get_coord(m, move_time);
return c.x + c.y;
}

static double
corexy_stepper_minus_calc_position(struct stepper_kinematics *sk, struct move *m
, double move_time)
{
struct coord c = move_get_coord(m, move_time);
return c.x - c.y;
}

// Generic cartesian calc_position()
static double
cartesian_stepper_calc_position(struct stepper_kinematics *sk
, struct move *m, double move_time)
{
struct cartesian_stepper *cs = container_of(
sk, struct cartesian_stepper, sk);
struct coord c = move_get_coord(m, move_time);
return cs->a.x * c.x + cs->a.y * c.y + cs->a.z * c.z;
}

static sk_calc_callback
lookup_calc_position_cb(struct coord a)
{
if (a.x == 1. && a.y == 0. && a.z == 0.)
return cart_stepper_x_calc_position;
if (a.x == 0. && a.y == 1. && a.z == 0.)
return cart_stepper_y_calc_position;
if (a.x == 0. && a.y == 0. && a.z == 1.)
return cart_stepper_z_calc_position;
if (a.x == 1. && a.y == 1. && a.z == 0.)
return corexy_stepper_plus_calc_position;
if (a.x == 1. && a.y == -1. && a.z == 0.)
return corexy_stepper_minus_calc_position;
return cartesian_stepper_calc_position;
}

void __visible
cartesian_stepper_set_coeffs(struct stepper_kinematics *sk
, double a_x, double a_y, double a_z)
{
struct cartesian_stepper *cs = container_of(
sk, struct cartesian_stepper, sk);
cs->a.x = a_x;
cs->a.y = a_y;
cs->a.z = a_z;
cs->sk.active_flags= (a_x ? AF_X : 0) | (a_y ? AF_Y : 0) | (a_z ? AF_Z : 0);

cs->sk.calc_position_cb = lookup_calc_position_cb(cs->a);
}

struct stepper_kinematics * __visible
cartesian_stepper_alloc(char axis)
{
struct stepper_kinematics *sk = malloc(sizeof(*sk));
memset(sk, 0, sizeof(*sk));
if (axis == 'x') {
sk->calc_position_cb = cart_stepper_x_calc_position;
sk->active_flags = AF_X;
} else if (axis == 'y') {
sk->calc_position_cb = cart_stepper_y_calc_position;
sk->active_flags = AF_Y;
} else if (axis == 'z') {
sk->calc_position_cb = cart_stepper_z_calc_position;
sk->active_flags = AF_Z;
}
return sk;
cartesian_stepper_alloc(double a_x, double a_y, double a_z)
{
struct cartesian_stepper *cs = malloc(sizeof(*cs));
memset(cs, 0, sizeof(*cs));
cartesian_stepper_set_coeffs(&cs->sk, a_x, a_y, a_z);
return &cs->sk;
}
40 changes: 0 additions & 40 deletions klippy/chelper/kin_corexy.c

This file was deleted.

40 changes: 0 additions & 40 deletions klippy/chelper/kin_corexz.c

This file was deleted.

52 changes: 0 additions & 52 deletions klippy/chelper/kin_generic.c

This file was deleted.

2 changes: 1 addition & 1 deletion klippy/extras/force_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, config):
self.trapq_append = self.motion_queuing.lookup_trapq_append()
ffi_main, ffi_lib = chelper.get_ffi()
self.stepper_kinematics = ffi_main.gc(
ffi_lib.cartesian_stepper_alloc(b'x'), ffi_lib.free)
ffi_lib.cartesian_stepper_alloc(1., 0., 0.), ffi_lib.free)
# Register commands
self._enable_force_move = config.getboolean("enable_force_move", False)
if self._enable_force_move:
Expand Down
2 changes: 1 addition & 1 deletion klippy/extras/manual_stepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, config):
self.motion_queuing = self.printer.load_object(config, 'motion_queuing')
self.trapq = self.motion_queuing.allocate_trapq()
self.trapq_append = self.motion_queuing.lookup_trapq_append()
self.rail.setup_itersolve('cartesian_stepper_alloc', b'x')
self.rail.setup_itersolve('cartesian_stepper_alloc', 1., 0., 0.)
self.rail.set_trapq(self.trapq)
# Registered with toolhead as an axtra axis
self.axis_gcode_id = None
Expand Down
11 changes: 7 additions & 4 deletions klippy/kinematics/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def __init__(self, toolhead, config):
self.dual_carriage_rails = []
self.rails = [stepper.LookupMultiRail(config.getsection('stepper_' + n))
for n in 'xyz']
for rail, axis in zip(self.rails, 'xyz'):
rail.setup_itersolve('cartesian_stepper_alloc', axis.encode())
self.rails[0].setup_itersolve('cartesian_stepper_alloc', 1., 0., 0.)
self.rails[1].setup_itersolve('cartesian_stepper_alloc', 0., 1., 0.)
self.rails[2].setup_itersolve('cartesian_stepper_alloc', 0., 0., 1.)
ranges = [r.get_range() for r in self.rails]
self.axes_min = toolhead.Coord([r[0] for r in ranges])
self.axes_max = toolhead.Coord([r[1] for r in ranges])
Expand All @@ -27,8 +28,10 @@ def __init__(self, toolhead, config):
self.dual_carriage_axis = {'x': 0, 'y': 1}[dc_axis]
# setup second dual carriage rail
self.rails.append(stepper.LookupMultiRail(dc_config))
self.rails[3].setup_itersolve('cartesian_stepper_alloc',
dc_axis.encode())
amult = [1., 0., 0.]
if dc_axis == 'y':
amult = [0., 1., 0.]
self.rails[3].setup_itersolve('cartesian_stepper_alloc', *amult)
self.dc_module = idex_modes.DualCarriages(
self.printer, [self.rails[self.dual_carriage_axis]],
[self.rails[3]], axes=[self.dual_carriage_axis],
Expand Down
6 changes: 3 additions & 3 deletions klippy/kinematics/corexy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def __init__(self, toolhead, config):
self.rails[0].get_endstops()[0][0].add_stepper(s)
for s in self.rails[0].get_steppers():
self.rails[1].get_endstops()[0][0].add_stepper(s)
self.rails[0].setup_itersolve('corexy_stepper_alloc', b'+')
self.rails[1].setup_itersolve('corexy_stepper_alloc', b'-')
self.rails[2].setup_itersolve('cartesian_stepper_alloc', b'z')
self.rails[0].setup_itersolve('cartesian_stepper_alloc', 1., 1., 0.)
self.rails[1].setup_itersolve('cartesian_stepper_alloc', 1., -1., 0.)
self.rails[2].setup_itersolve('cartesian_stepper_alloc', 0., 0., 1.)
for s in self.get_steppers():
s.set_trapq(toolhead.get_trapq())
# Setup boundary checks
Expand Down
6 changes: 3 additions & 3 deletions klippy/kinematics/corexz.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def __init__(self, toolhead, config):
self.rails[2].get_endstops()[0][0].add_stepper(s)
for s in self.rails[2].get_steppers():
self.rails[0].get_endstops()[0][0].add_stepper(s)
self.rails[0].setup_itersolve('corexz_stepper_alloc', b'+')
self.rails[1].setup_itersolve('cartesian_stepper_alloc', b'y')
self.rails[2].setup_itersolve('corexz_stepper_alloc', b'-')
self.rails[0].setup_itersolve('cartesian_stepper_alloc', 1., 0., 1.)
self.rails[1].setup_itersolve('cartesian_stepper_alloc', 0., 1., 0.)
self.rails[2].setup_itersolve('cartesian_stepper_alloc', 1., 0., -1.)
for s in self.get_steppers():
s.set_trapq(toolhead.get_trapq())
# Setup boundary checks
Expand Down
Loading
Loading