Skip to content

Flex Compensation for Winch Kinematics - #7093

Open
tobbelobb wants to merge 65 commits into
Klipper3d:masterfrom
tobbelobb:master
Open

tobbelobb wants to merge 65 commits into
Klipper3d:masterfrom
tobbelobb:master

Conversation

@tobbelobb

Copy link
Copy Markdown

I meant to try and talk to you before creating a full PR, but here it is.

After a lot of effort I managed to post something on your Discourse:
https://klipper.discourse.group/t/flex-compensation-for-winch-kinematics/25031/3
Please answer here, your Discourse is unusable for me since I'm a new user.

It supports any purely translational cable robot configuration that has a central effector, including SpiderCam-like setups (all anchors high up), Slideprinter (2D robots), HP3-like setups (one high, several low anchors). Up to 26 anchors.

The "Print Logo" and "Print Squares" over at https://tobbelobb.github.io/hp-sim5/hp-sim/ now do flex compensation.
See https://gitlab.com/tobben/hangprinter-flex-compensation/ for the algorithm development.
See https://github.com/tobbelobb/klipper for the klipper implementation.
See https://github.com/tobbelobb/klipper/blob/master/docs/Config_Reference.md#cable-winch-flex-compensation for documentation on how to configure flex compensation.

Screenshot from 2025-10-17 14-28-48 Screenshot from 2025-10-17 14-26-18 Screenshot from 2025-10-17 13-23-54 Screenshot from 2025-10-17 13-19-29

@KevinOConnor

Copy link
Copy Markdown
Collaborator

Interesting, thanks. I have some high-level comments (in no particular order).

There is a lot of dense math here that I wont be able to review nor maintain. It's still possible that this code could reside in the main Klipper repository, but if that is the goal you would need to agree to maintain the code (answer questions, run tests, and make updates well into the future). Alternatively, perhaps @dmbutyugin would be willing to review.

If you are looking to merge into the repo, the existing build test cases would need to pass. Also, note that the directory test/klippy/ is for black-box testing - it's okay to add unit tests but they should go in a new directory.

Similarly, if looking to merge into this repo, a signed-off-by line containing your real full name would be needed ( https://www.klipper3d.org/CONTRIBUTING.html#format-of-commit-messages ). Any file copyrights added should also contain a full name.

Although I did not review the code, I get the impression that the new kin_winch.c code does a lot of processing on each "guess" of the iterative solver. That sounds rather cpu intensive. It's fine if that is what it takes, but there should be awareness of that if it is the case.

Note that there have been some other users of "cable winch" kinematics outside of the "hang printer" category. At least one person is using that kinematics for extreme speed prints. I'll followup on Discourse.

FYI, for this upstream repo, we basically never add old-style "M1234" type commands. All new commands are expected to be in the new format (eg, SET_CABLE_WINCH_KINEMATICS SOMEPARAM=123.4).

Cheers,
-Kevin

@dmbutyugin

Copy link
Copy Markdown
Collaborator

I'm afraid I would not be able to provide an in-depth review of the math here, but I could do a sanity-check review. That said, I may have misunderstood, but did you actually do real-life tests of this compensations besides simulation? If yes, it would be interesting to see the effects of this compensation on the real prints. And if not yet, could we do them?

@tobbelobb

Copy link
Copy Markdown
Author

Hi, thanks for the comments!

There's no rush to merge this now but your input is super useful, so I can work in that direction. We can look at it again when it's matured a little bit. I'm noting these todos:

  • Needs a person responsible. I'll do that once it's ready. If somebody else is willing to, I'd be happy to help them get up to speed with better docs and so on.
  • Line length, whitespace test cases, signed-off commit message. No problem.
  • Change F666 F0/1 to new format. No problem.
  • Some data regarding performance.
  • Testing. Move unit test to its own dir. Probably add black-box tests. Probably add plotting visual tests similar to what's in the development repo (see "plotstuff.sh").
  • A mathematical formulation of what I'm checking for in plots would be nice, so we can automate the "plotstuff" tests.
    • No force spikes.
    • Smooth force changes in all directions from all positions with all supported setups.
    • Not too large fraction of the build volume getting forces clamped in normal setups. For some definition of "too large" and "normal".
    • Gravity being supported.
    • The two algorithms giving fairly similar results at all points except near edges where they're expected to differ
  • Document effect on real prints. I don't have the setup to do this right now. Loosing my workshop is what has given me so much time to work on software stuff, so I'll probably need help on this one, and it might take a lot of time to test all supported setups. I guess that's ok, as long as interested potential users can find my fork in the meantime.

Some Notes on Performance

I don't expect performance will be a problem, but we'll see. The default QP algorithm and the other one ("Tikhonov", which uses gradient descent) have different performance properties. I'll try and get some real numbers today.

Does Klipper have a target machine or a minimum requirement on the users CPU?

Appendix Notes on Performance

You're right to be a bit suspicious about the numerical solving, it needs to be tightly measured/tuned/controlled, especially since performance/solve times varies in different areas/volumes:
* Easily reachable, vs
* Near singularities vs,
* Past singularities (physically unreachable positions).

To my knowledge the numerical iterations is what it takes to solve this (most often underdetermined) non-linear optimization problem in a stable, reliable, and general manner. Some setups have (potentially a lot of) closed form solutions but I don't think they're worth implementing individually.

It's possible to get forces faster by trading away perfection for speed. It depends on how close to "optimial" we want/need the (already correct) forces to be. I've used different definitions of "optimal" for the two algorithms:

QP forces are:

  • As close to min as possible, from above
  • Never below min
  • Never negative
  • Never above max
  • Always physically perfect (forces balance out perfectly, even near singularities)

Tikhonov forces are:

  • As close to min as possble
  • Preferrably not below min, and if below, still very close to min
  • Never negative if the configured min force is positive
  • Allowed to go negative if the user configured a negative min force
  • Never above max
  • Close to physically perfect, but not if that means sudden force changes or very high total force

@tobbelobb

Copy link
Copy Markdown
Author

I'm noting that you mention Raspberry models down to Raspberry Pi Zero2W in the FAQ docs here. Quad-core 1GHz Cortex-A53 processor. I'll see what Raspberrys I can get my hands on.

@tobbelobb

tobbelobb commented Oct 24, 2025 •

Copy link
Copy Markdown
Author

Ok I did some preliminary performace checks on my desktop machine. I did it like this.

Results in
klipper_winch_flex_perf_data.log

I used this gcode as the example object:
Hangprinter_logo6.gcode.log
It's a ca 800 mm X-wide and ca 940 mm Y-high and 1.8 mm Z-thick (three layers). 2050 s print time.

Main Result in the Slideprinter (three low anchors) Case

Out of 2050 s print time, the compute_flex function consumed ~5.0 s when QP was used, and ~8.5 s when Tikhonov was used. They were called 25 million times, so each call was around .2-.3 ns.

QP ignore_gravity: false, ignore_pretension: false:
ERROR:root:b'compute_flex calls=24883580, total=4999.317 ms, avg=0.000201 ms'
Tikhonov ignore_gravity: false, ignore_pretension: false:
ERROR:root:b'compute_flex calls=25092895, total=8214.606 ms, avg=0.000327 ms'

Comments About Number of Lines Effect on Performance

We currently calculate forces in all directions each time we calculate one single line length. We could optimize this. So for a machine with 3 lines we could cut the number of calls by 1/3 with some caching mechanism.

Results also depend a lot on the number of anchors and the max/min force limits we configure. I've used winch_min_force: 3N, winch_max_force: 170N on all anchors.

Increasing the number of anchors from 3 to 4 (standard HP3 setup) gave:

QP ignore_gravity: false, ignore_pretension: false:
ERROR:root:b'compute_flex calls=26639211, total=6580.517 ms, avg=0.000247 ms'
Tikhonov ignore_gravity: false, ignore_pretension: false:
ERROR:root:b'compute_flex calls=26848825, total=38785.034 ms, avg=0.001445 ms'

We see that Tikhonov does not scale as well as the default QP. This got me curious so I added another (high up) anchor and got:

QP:
ERROR:root:b'compute_flex calls=30377819, total=9812.713 ms, avg=0.000323 ms'
Tikhonov:
ERROR:root:b'compute_flex calls=31203995, total=65175.208 ms, avg=0.002089 ms'

The results are roughly the same if the 5 anchors are arranged like HP5 will have them, with 4 low and 1 high anchor:

QP:
ERROR:root:b'compute_flex calls=35115829, total=10850.151 ms, avg=0.000309 ms'
Tikhonov:
ERROR:root:b'compute_flex calls=37424930, total=76917.615 ms, avg=0.002055 ms'

We can conclude that the current Tikhonov algorithm is too slow for machines with more than 3 anchors. They should probably stick to the default QP algorithm.

Comments About Whether this Will Work On Raspberry Pi Zero2W

My desktop cpu: 12th Gen Intel(R) Core(TM) i7-12700K

The Pi Zero2W has a Quad-core 1GHz Cortex-A53 which is a lot slower. But if we can fit the matrices in the Zero's cache (which I think we can, the matrices are small), then 10s on my desktop will probably expand to ca 300 s on the Pi Zero2W (assuming we run single threaded). That's just my guess.

Is that ok, or would 300 s of flex calculation on a 2050 s print be too slow?

@KevinOConnor

Copy link
Copy Markdown
Collaborator

Thanks for the additional information.

Is that ok, or would 300 s of flex calculation on a 2050 s print be too slow?

For what it is worth, I don't think there is a hard requirement for CPU efficiency here. If it takes a lot of processing to get higher quality prints on these printers then that's what it takes. The main issue I think is being clear with users on what they should expect. Said another way, if the host computer needs to be a higher end computer to implement flex compensation then I think that is reasonable, but if it is the case then we should be clear on the hardware expectations in the documentation. (If 300/2050s ~= 15% increase, then that's not necessarily worth worrying about, but adding a blanket statement like "expect to use an Rpi5 or similar host machine if enabling flex compensation" is also fine.)

I'm noting these todos:

I think it would also be good to see at least a few different people using the implementation regularly on real world machines prior to merging into the mainline repo.

Cheers,
-Kevin

@github-actions

Copy link
Copy Markdown

Thank you for your contribution to Klipper. Unfortunately, a reviewer has not assigned themselves to this GitHub Pull Request. All Pull Requests are reviewed before merging, and a reviewer will need to volunteer. Further information is available at: https://www.klipper3d.org/CONTRIBUTING.html

There are some steps that you can take now:

  1. Perform a self-review of your Pull Request by following the steps at: https://www.klipper3d.org/CONTRIBUTING.html#what-to-expect-in-a-review
    If you have completed a self-review, be sure to state the results of that self-review explicitly in the Pull Request comments. A reviewer is more likely to participate if the bulk of a review has already been completed.
  2. Consider opening a topic on the Klipper Discourse server to discuss this work. The Discourse server is a good place to discuss development ideas and to engage users interested in testing. Reviewers are more likely to prioritize Pull Requests with an active community of users.
  3. Consider helping out reviewers by reviewing other Klipper Pull Requests. Taking the time to perform a careful and detailed review of others work is appreciated. Regular contributors are more likely to prioritize the contributions of other regular contributors.

Unfortunately, if a reviewer does not assign themselves to this GitHub Pull Request then it will be automatically closed. If this happens, then it is a good idea to move further discussion to the Klipper Discourse server. Reviewers can reach out on that forum to let you know if they are interested and when they are available.

Best regards,
~ Your friendly GitIssueBot

PS: I'm just an automated script, not a human being.

tobbelobb and others added 27 commits November 26, 2026 19:07
Some machines pull the effector towards one anchor with several
parallel lines, e.g. Hangprinter v4 with two lines on each of A, B, C
and three on D. These lines share the planned force, so each one
stretches less. Flex compensation treated every axis as a single line
and so commanded roughly 2-3x the intended pretension.

winch_lines_per_axis (default 1) divides the flex length by the number
of parallel lines: flex = -F / (k * mechanical_advantage * lines).
It is separate from mechanical advantage, which still also drives the
spool buildup model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Convert per-line stretch to effector distance by dividing by mechanical advantage a second time. Add a Hooke's-law regression covering MA 1, 2 and 4, parallel lines and guy-wire lengths.
Flex compensation planned line tensions for a point mover: three force
equations. On a mover held by single lines from spread out points, such
as a cube held from its eight corners, those tensions leave a moment
that the lines resolve by tilting the mover. On the simulated
CubeCorners machine that put the nozzle about 1 mm off.

New optional stepper options attachment_x, attachment_y and
attachment_z give where each line attaches to the mover, measured from
its centre of mass. When set, the tension solve adds three moment
equations, scaled by the RMS attachment distance.

Balancing moments can take large tension changes, so the existing ridge
solver gave up moment balance for lower tensions, and its optimality
test stops working with a ridge small enough to avoid that. The six
equation case therefore gets its own bounded solver, which solves the
6x6 system over the free tensions and tests bounds in Newton. The point
model keeps its solver and its results for the qp algorithm.

The tikhonov algorithm takes the same six equations. Its 3x3 inverse is
now a Cholesky solve of any size.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Use the existing bounded minimum-norm solver for point and attachment models.
Remove the stalled ridge solver and test five-anchor force optimality and
compensated inverse/forward round trips with direct and reduced winches.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Continue allocation after a released variable reaches its opposite bound. Respect physical output limits with pretension disabled and exercise that path through the real C helper.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
pulley_radius and swivel_axis_x/y/z make a winch anchor a swivel pulley whose
line leaves from a moving tangent point (swivel_pulley.h, shared with
RepRapFirmware and the development repo). Line lengths, flex force directions
and the forward solve follow the wrap. winch_home_tension adds back the stretch
lines already had when the motors were zeroed. Pinhole configs produce the
same MCU messages as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also flush all step generation before a batch-mode exit, so .serial output is complete.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…only in batch mode

Also reject an infinite winch_spring_constant with the spool options.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants