Skip to content

Load cell gram scale#6729

Merged
KevinOConnor merged 2 commits intoKlipper3d:masterfrom
garethky:pr-load-cell-gram-scale
Mar 20, 2025
Merged

Load cell gram scale#6729
KevinOConnor merged 2 commits intoKlipper3d:masterfrom
garethky:pr-load-cell-gram-scale

Conversation

@garethky
Copy link
Copy Markdown
Contributor

@garethky garethky commented Nov 5, 2024

This PR adds the features needed to make a load cell actually work as a gram scale. This PR will let you weight filament or measure a force in grams. (no homing or probing yet!)

  • Convert raw sensor counts to grams and make this force data available via unix socket and printer object status
  • Track sensor calibration state and allow sensors to go from un-calibrated to calibrated at runtime
  • Add basic GCodes for tearing and reading the load cell
  • Add a guided calibration tool so users can calibrate a load cell with a known mass or measured force.
  • Add diagnostic GCode to check the health and performance of the load cell and sensor

This code has been tested by several people in the community and some last minute bug fixes and changes were landed:

  • The reverse option allows the polarity of the force readings to be inverted. You can have your probing collision graphs in either orientation now.
  • reference_tare_counts is written to config and read as an integer. link
  • Fixed crash when guided calibration tool being entered twice
  • Fixed crash when CALIBRATE was used before TARE

A lot of work (months) and bug fixing went into parts of this, particularly LOAD_CELL_DIAGNOSTIC and the LoadCellSampleCollector. These bits have to work when the sensor is buggy and still produce usable output. LoadCellSampleCollector also underpins later work for probing.

numpy is required for this PR. We could change that. But PR's for probing will absolutely need it. If you want to merge [load_cell] and [load_cell_probe] then it seems there is no point in trying to keep it out of this PR.

@garethky
Copy link
Copy Markdown
Contributor Author

garethky commented Nov 5, 2024

The tests failed because of the numpy dependency. I guess I could move the check out of __init__ to get them to pass.

@JamesH1978
Copy link
Copy Markdown
Collaborator

such a pity numpy is so bloaty and time consuming to just add it to requirements

@Sineos
Copy link
Copy Markdown
Collaborator

Sineos commented Nov 6, 2024

such a pity numpy is so bloaty and time consuming to just add it to requirements

I'm not sure that this has a real world impact. I guess most, or at least a significant portion also use input shaping and have it installed anyway.
Might be even better to add it to the requirements and call for a version <1.26 to avoid clashing with input shaping.

@JamesH1978
Copy link
Copy Markdown
Collaborator

i think its the sheer time to compile numpy is the issue if the host is sub par, it can take hours on some

@garethky
Copy link
Copy Markdown
Contributor Author

garethky commented Nov 6, 2024

I would love to get on my "just require numpy" soap box (Binaries are installed automatically for 32bit, you don't need to compiling it. Its shockingly fast vs plain python. "Your job as a Python programmer is to write C") but... I want to ship this and be 'done' with the project. So what do I have to do?

I can push the numpy require check back to the point where its clear that you are configuring a probe. Most of what I'm using it for in this PR (average, min, max, unique) is relatively easy to replace or remove.

For probing, I need linalg.lstsq. There are some optimizations I discussed with @KevinOConnor to replace the bulk of those calls, but I still have to make several calls to plain least squares. numpy/c is so much faster than Python that any sort of optimizations that we do might be washed away because they aren't in c (plus the code is very complicated). There is also, very very likely, a numpy/vectorized version of what we want to do that will crush the pure Python version (it has loops, its probably 50x slower than numpy). If this code is slow, it causes noticeable pauses.

@Sineos
Copy link
Copy Markdown
Collaborator

Sineos commented Nov 6, 2024

Is there any way to provide / use a prebuilt v1.26 wheel?


This document describes Klipper's support for load cells and load cell based
probes.

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.

not knowing what a load cell does/means for a 3d printer, could you add a sentence or two describing it's usage/benefits for 3d printers/klipper?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made some updates to the documentation including a little bit more exposition at the top and a section about how to read the load cell gram force

@garethky garethky force-pushed the pr-load-cell-gram-scale branch 2 times, most recently from a3f6aa5 to dde606a Compare November 11, 2024 03:08
@garethky
Copy link
Copy Markdown
Contributor Author

Is there any way to provide / use a prebuilt v1.26 wheel?

For users: if you use Python 3.0 on a 32 bit RPI OS then PiWheels will just install a binary. That's the default behavior. You have to kinda work hard to not get that to happen. i.e. willfully use Python 2 or pick a 64 bit OS.

For the git build system: I'm not sure how that works.

@Sineos
Copy link
Copy Markdown
Collaborator

Sineos commented Nov 11, 2024

For users: if you use Python 3.0 on a 32 bit RPI OS then PiWheels will just install a binary.

PyPi has some arm64 wheels as well

For the git build system: I'm not sure how that works.

There are examples of project who built the necessary wheels as CI actions. E.g. https://github.com/matrix-org/synapse/pull/14212/files

Maybe it would be an option to built the needed wheels (Python 2 and Python3?) as Klipper CI and the install scripts (or KIAUH) could automatically draw upon them.

@garethky
Copy link
Copy Markdown
Contributor Author

This was updated to remove the numpy dependency

# Load Cells

This document describes Klipper's support for load cells. Basic load cell
functionality can be used to read force data and to weigh things like filament.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider adding a note that weighing things is only possible on printers with the load cells in the bed, and not those with the cell in the extruder.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't want to get into applications because there are many. You don't even have to put it in the printer! Several people want to use this functionality to weigh filament spools mounted outside the printer. E.g. https://klipper.discourse.group/t/filament-spool-scale-hx711/19800/

  • You could check before a print if there is enough filament on the spool based on the print weight
  • Track filament used by weight.
  • Track drying if the filament by weight lost
    In the future this may be in an application that is just a dry box and not even a printer.
    One community member is even working on runout detection in a resin printer.

Copy link
Copy Markdown
Collaborator

@KevinOConnor KevinOConnor left a comment

Choose a reason for hiding this comment

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

Thanks. Sorry for the delay in responding.

In general it seems fine to me. I have a few comments and questions - see below.

Cheers,
-Kevin

Comment on lines +399 to +370
### load_cell/dump_force

This endpoint is used to subscribe to force data produced by a load_cell.
Using this endpoint may increase Klipper's system load.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems odd that there is an additional endpoint added for this information. I would have thought the information could be added to the existing hx71x/dump_hx71x endpoint (or that endpoint replaced with this one).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well there are 2 ADC endpoints hx71x/dump_hx71x and ads1220/dump_ads1220.

My goal with this was to try to keep everything "load cell" related out of the ADC code. The load_cell is acting as a decorator to the raw ADC data stream. The raw stream is still useful as a diagnostic tool (or at least it was during development).

This goes with your other question about extending/updating the socket code. What we decide here is going to determine what to do about that comment.

At a high level, what are you thinking about here?

  • Is the performance penalty of the duplicate streams a concern?
  • Is the duplication of the raw counts data in 2 streams confusing?
  • Everything is one stream is desired but then the ADC stream is redundant/confusing?

Copy link
Copy Markdown
Collaborator

@KevinOConnor KevinOConnor Dec 1, 2024

Choose a reason for hiding this comment

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

A bit of all three. Mostly concerned with maintaining two different interfaces that provide redundant capabilities, and the resulting developer confusion on why there are two redundant interfaces.

Perhaps take a look at how ldc1612.py takes a calibration object and calls self.calibration.apply_calibration(samples), which then allows probe_eddy_current.py to populate a "Z" field in the samples.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I look at the ldc1612 code and I think the other option is a better fit for this use case. The "that endpoint replaced with this one" idea.

First, I think its more developer friendly to have the endpoint be named according to the load cell in the config and not the sensor implementation. That's a leaky abstraction. That way a front end doesn't need to look up the sensor type in the config and try to build the endpoint URI. The URI can be built from the load cell name alone and its always consistent. Force data comes from a thing that is about force. That feels consistent and logical. (You could argue that LDC1212 is only about Z so that's not necessarily inconsistent)

Second but less important: the 'B' channel on the HX717 is used to measure an RTD and a hall effect filament sensor in Prusa's new machines. I think it would make supporting that kind of thing less awkward later on if we deleted the endpoint from the sensors. Objects that consume the data (LoadCell, Temperature etc.) can transform and publish it (or not) as required.

I'll work on this first.

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.

less important: the 'B' channel on the HX717 is used to measure an RTD and a hall effect filament sensor in Prusa's new machines

Hey, that is important! ;)

I've currently got a very rough proof of concept implementation for the filament sensor, based on your load-cell-probe-community-testing branch -- ie, using the WebhooksHelper/WebhooksTransformer in this PR. Probably the main feature that would be useful for my code is being able to subscribe to a particular channel only. At the moment my code has 3 or 4 places all receiving the whole data stream and doing their own filtering on it.

In day-to-day usage the raw counts data is not that useful, especially to the user -- dropping the raw sensor API endpoint seems fine. However, if a load cell ADC were to be reused by existing modules that currently just rely on regular ADC pins (gcode_button, hall_filament_width_sensor), the user will need access to the raw counts or the normalized 0..1 value somehow during the setup process in order to calibrate their new gadgets, since they won't necessarily have specific commands like in this PR. For the filament sensors it doesn't have to be the existing endpoint, though. The values change dramatically enough that getting even just one data point each with filament inserted and not inserted would allow for a reliable configuration.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to not have the sensors register websocket endpoints.

There is still a class for handling both internal and websocket clients: ApiClientHelper. I need it in subsequent PR to emit events for the probe. If that looks like a class that should be in webhooks.py let me know.

For now I've kept things in separate commits so the changes are easier to see. But my intention is to squash them before its merged.

Comment on lines +408 to +378
`{"id": 123,"result":{"header":["time", "force (g)", "counts", "tare_counts"]}}`
and might later produce asynchronous messages such as:
`{"params":{"data":[[3292.432935, 40.65, 562534, -234467]]}}`
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just out of curiosity, does "tare_counts" change with each sample?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, it only changes when the tare command is executed.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Okay - seems a little odd to repeat this value for every sample. Maybe just export the information via get_status()?

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.

The load cell probe implementation currently re-tares before homing, so API consumers may find this valuable over time?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because a Tare can happen in the middle of a batch of results you wouldn't know which samples used what tare value. A UI would not correctly reflect whats really going on. And as pointed out, tare can happen frequently while probing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Correction: it cant happen in the middle of a batch. The tare value would always apply to a whole batch.

Comment on lines +4668 to +4772
#reverse:
# Reverses the polarity of the load cell. This is a boolean value, the
# default is False.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, is "reverse" effectively the same as a negative "counts_per_gram"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Basically. This lets you do what you wanted, where if the force graph is naturally negative you can reverse its polarity.

Comment on lines +708 to +755
### LOAD_CELL_DIAGNOSTIC
`LOAD_CELL_DIAGNOSTIC [LOAD_CELL=<config_name>]`: This command collects 10
seconds of load cell data and reports statistics that can help you verify proper
operation of the load cell. This command can be run on both calibrated and
uncalibrated load cells.

### CALIBRATE_LOAD_CELL
`CALIBRATE_LOAD_CELL [LOAD_CELL=<config_name>]`: Start the guided calibration
utility. Calibration is a 3 step process:
1. First you remove all load from the load cell and run the `TARE` command
1. Next you apply a known load to the load cell and run the
`CALIBRATE GRAMS=nnn` command
1. Finally use the `ACCEPT` command to save the results

You can cancel the calibration process at any time with `ABORT`.

### TARE_LOAD_CELL
`TARE_LOAD_CELL [LOAD_CELL=<config_name>]`: This works just like the tare button
on digital scale. It sets the current raw reading of the load cell to be the
zero point reference value. The response is the percentage of the sensors range
that was read and the raw value in counts.

### READ_LOAD_CELL load_cell="name"
`READ_LOAD_CELL [LOAD_CELL=<config_name>]`:
This command takes a reading from the load cell. The response is the percentage
of the sensors range that was read and the raw value in counts. If the load cell
is calibrated a force in grams is also reported.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

As a general comment, it's generally preferable to implement less commands. For example, could this be reduced to just two commands: LOAD_CELL_READ (for reading and diagnostics) and LOAD_CELL_CALIBRATE (for calibrating and changing the tare).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I can make those changes.

Copy link
Copy Markdown
Contributor Author

@garethky garethky Feb 25, 2025

Choose a reason for hiding this comment

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

Does the gcode parser work if you dont assign a value to things. Like, could this work:

LOAD_CELL READ
LOAD_CELL TARE
LOAD_CELL DIAGNOSTIC

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This does not work, it results in a !! Malformed command error. I don't see anywhere in the existing GCode command that tries to make 1 command perform different actions. The only way this would work would be:

LOAD_CELL READ=TRUE
LOAD_CELL TARE=TRUE
LOAD_CELL DIAGNOSTIC=TRUE

That looks horrible and is hard to use/discover in the front ends. I'd like to stick with separate commands. I have no problem prefixing them all with LOAD_CELL_, that actually improves discoverability. Looking at Bed Mesh, Angle, Accelerometer etc, that's the pattern they all follow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated commands to use a consistent prefix LOAD_CELL_

# Adapter for WebhooksHelper that transforms the response using a function
# Anything that implements the add_client contract can be a message source
# Outputs to its own clients
class WebhooksTransformer(WebhooksHelper):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you elaborate on what this does (as well as WebhooksHelper)?

At first glance, it seems a little odd to make a slightly different interface for just loadcell. If the BatchWebhooksClient code isn't flexible enough, maybe we should expand the webhooks interface and refactor the other users (like ldc1612 and accelerometers).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This goes back to your first comment, lets resolve what we want for a data stream structure. If this is still required I can do a refactor.

Comment on lines +427 to +393
self.printer.send_event("load_cell:calibrate", self)
if self.is_tared():
self.printer.send_event("load_cell:tare", self)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you elaborate on what these events will be used for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The probing code uses these events to get notified about the state of the gram scale. When the user interactively calibrates the load_cell for the first time it goes from uncalibrated -> calibrated. That state transition allows probing to work without a restart. Before that it will throw an error that you are not allowed to probe or home with a load cell that isn't calibrated.

I still have the probing code in a separate file because I believe this chunk of functionality is a nice cleanly separate unit. My intention is to pitch you on keeping them split up. These events are one part of the decoupling that makes that work.

@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.

@garethky garethky force-pushed the pr-load-cell-gram-scale branch 5 times, most recently from d12b00c to b0533b9 Compare February 26, 2025 23:48
@KevinOConnor
Copy link
Copy Markdown
Collaborator

KevinOConnor commented Mar 6, 2025

Okay, thanks. In general it seems fine to me. The only thing I noticed is that docs/API_Server.md should be updated now that the hx71x/ads1220 api is no longer available, and Config_Changes.md should have a note that the previous API endpoints were removed.

As for the commands, I was thinking something like LOAD_CELL_READ, LOAD_CELL_READ FULL_DIAGNOSTIC=1, LOAD_CELL_CALIBRATE, and LOAD_CELL_CALIBRATE UPDATE_TARE_ONLY=1. It's not a big deal though if you want to keep 4 commands.

FWIW, the "reverse" config option seems a little odd to me, though I'm not sure if a negative counts_per_gram would be better. It's not a big deal either way. Note that the added docs have described "reverse" twice.

Cheers,
-Kevin

@garethky
Copy link
Copy Markdown
Contributor Author

garethky commented Mar 7, 2025

Thanks for taking a look @KevinOConnor

  • I will address the comments about the docs, thanks for the feedback and the eyeballs
  • If its all the same to you I will keep the 4 commands. I think that's move consistent with existing gcodes
  • reverse has one advantage over negative counts_per_gram: it wont be overwritten in a calibration. The orientation stays the same between the calibrations with no user input. I could call it invert, maybe that's a better name.

🍻

@KevinOConnor
Copy link
Copy Markdown
Collaborator

I will address the comments about the docs, thanks for the feedback and the eyeballs

Thanks.

If its all the same to you I will keep the 4 commands. I think that's move consistent with existing gcodes

Okay.

reverse has one advantage over negative counts_per_gram: it wont be overwritten in a calibration. The orientation stays the same between the calibrations with no user input. I could call it invert, maybe that's a better name.

Okay. Maybe a longer name may help - for example: sensor_orientation: Use "positive" if the sensor reports an increasing value when it contacts the bed, or use "negative" if the sensor reports a decreasing value when it contacts the bed". Just "thinking out loud" - it's fine if you want to keep it the way it is.

Cheers,
-Kevin

@cab404
Copy link
Copy Markdown
Contributor

cab404 commented Mar 10, 2025

Just checked it. Appears to be working with hx711 — alas the calibration doesn't fix the sign for some reason.

@garethky
Copy link
Copy Markdown
Contributor Author

alas the calibration doesn't fix the sign for some reason.

Can you explain what you mean? Seems like are reporting a bug?

* Convert sensor counts to grams and make this available via unix socket and object status
* Basic GCodes for tearing and reading the load cell
* Guided Calibration
* Diagnostic gcode to check the health of the load cell

Signed-off-by: Gareth Farrington <gareth@waves.ky>

Refactor - remove WebhooksHelper and WebhooksTransformer

Signed-off-by: Gareth Farrington <gareth@waves.ky>

Capture errors and overflows in LoadCellSampleCollector

Signed-off-by: Gareth Farrington <gareth@waves.ky>

Refactor GCode Commands
@garethky garethky force-pushed the pr-load-cell-gram-scale branch 2 times, most recently from 759290d to fd9e9f9 Compare March 13, 2025 01:23
@garethky
Copy link
Copy Markdown
Contributor Author

  • hx71x & ads1220 API server doc entries deleted.
  • made the sensor_orientation change but went with normal and inverted. "If the scale goes negative set the orientation to inverted" just sounded right to me?
  • Squashed into 2 commits

I think this is ready to merge.

@KevinOConnor
Copy link
Copy Markdown
Collaborator

Okay, thanks. I'll give a few days to see if there are comments and otherwise look to commit.

#sensor_orientation:
#   Change the sensor's orientation. Can be either 'normal' or 'inverted'.
#   The default is 'inverted'.

I must admit, I got quite a chuckle out of that default.

Separately, if Load_Cell.md is a new document, it should also be added to docs/_klipper3d/mkdocs.yml (briefly mentioned at https://www.klipper3d.org/CONTRIBUTING.html#what-to-expect-in-a-review ).

Cheers,
-Kevin

* Add API server load_cell/dump_force endpoint
* Update [load_cell] config with calibration fields
* Add G-Code commands for working with load cells
* Add status reference for load_cell objects

Signed-off-by: Gareth Farrington <gareth@waves.ky>
@garethky garethky force-pushed the pr-load-cell-gram-scale branch from fd9e9f9 to e03740f Compare March 13, 2025 04:13
@garethky
Copy link
Copy Markdown
Contributor Author

garethky commented Mar 13, 2025

I must admit, I got quite a chuckle out of that default.

😂 I'm doing too many things today! Its 'normal'.

fixed mkdocs.yml

@cab404
Copy link
Copy Markdown
Contributor

cab404 commented Mar 13, 2025

alas the calibration doesn't fix the sign for some reason.

Can you explain what you mean? Seems like are reporting a bug?

02:11:47  $ CALIBRATE GRAMS=520
02:11:48  // Calibration value: -3.63% (-304549), Counts/gram: 324.90769,             Total capacity: +/- 25.40Kg
02:12:24  $ CALIBRATE GRAMS=926
02:12:25  // Calibration value: -5.00% (-419427), Counts/gram: 306.51296,             Total capacity: +/- 26.93Kg
02:13:50  $ CALIBRATE GRAMS=1458
02:13:51  // Calibration value: -6.13% (-514252), Counts/gram: 259.70850,             Total capacity: +/- 31.78Kg
02:13:56  $ ACCEPT
02:13:56  // Load cell calibration settings:
// 
// counts_per_gram: 259.708505
// reference_tare_counts: -135596

All of our measurements displayed going into negative values, yet inversion wasn't reflected in the final results, nor was asked to be attributed in config during calibration itself

@garethky
Copy link
Copy Markdown
Contributor Author

All of our measurements displayed going into negative values, yet inversion wasn't reflected in the final results, nor was asked to be attributed in config during calibration itself

In the calibration tool this is the expected output. Everything there that is negative is the raw counts from the sensor. The only measurement that gets inverted is the grams when reading the load cell.

@cab404
Copy link
Copy Markdown
Contributor

cab404 commented Mar 15, 2025

In the calibration tool this is the expected output. Everything there that is negative is the raw counts from the sensor. The only measurement that gets inverted is the grams when reading the load cell.

The problem is that after the calibration it shows negative values, cause the fact that counts per gram is negative is not saved)

@garethky
Copy link
Copy Markdown
Contributor Author

In the calibration tool this is the expected output. Everything there that is negative is the raw counts from the sensor. The only measurement that gets inverted is the grams when reading the load cell.

The problem is that after the calibration it shows negative values, cause the fact that counts per gram is negative is not saved)

Yes. If the wiring is 'backwards' it will always show negative grams, unless you go into the config and set sensor_orientation: inverted. This is very similar to how steppers work. If your stepper moves in the wrong direction, you can swap 2 wires or add a ! to the dir_pin.

@cab404
Copy link
Copy Markdown
Contributor

cab404 commented Mar 15, 2025

Yes. If the wiring is 'backwards' it will always show negative grams, unless you go into the config and set sensor_orientation: inverted. This is very similar to how steppers work

yeah, but if in case of steppers there is no automatic calibration, here code can actually deduce your orientation during calibration process

maybe this is actually good, and is an additional protection from assembly mistakes or something like that — but I still thought it was worth asking about

@KevinOConnor KevinOConnor merged commit 06d65ef into Klipper3d:master Mar 20, 2025
1 check passed
@KevinOConnor
Copy link
Copy Markdown
Collaborator

Thanks.

-Kevin

Transistor427 added a commit to Z-Bolt/klipper that referenced this pull request Apr 19, 2025
* stm32: Add support for USART6 on STM32F401

STM32F401 has USART6 on PA12/PA11 and PC7/PC6 with alternate
function mapping AF08. This can be used, for example, to connect
to the Elegoo Neptune 3, where PA12/PA11 are wired to an RJ10 plug
going to the stock screen.

Signed-off-by: Marius Petcu <marius@petcu.me>

* gcode_macro: Expand template syntax errors (Klipper3d#6839)

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* axis_twist_compensation: Remove the auto parameter
from axis_twist_compensation

Signed-off-by: Jorge Apaza Merma <yochiwarez@gmail.com>

* docs: Note AXIS_TWIST_COMPENSATION_CALIBRATE AUTO removal in Config_Changes.md

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* input_shaper: Fix for polar kinematics

Forward post_cb calls from itersolve to the original kinematics.

Signed-off-by: Dmitry Butyugin <dmbutyugin@google.com>

* axis_twist_compensation: allow compensating both axis at once

Restores the behavior before Klipper3d#6739 since people seemed to rely on it,
even if the math is not exact.

Signed-off-by: Philippe Daouadi <philippe@ud2.org>

* load_cell: Load cell gram scale (Klipper3d#6729)

* Add gram scale features to load_cell
* Convert sensor counts to grams and make this available via unix socket and object status
* Basic GCodes for tearing and reading the load cell
* Guided Calibration
* Diagnostic gcode to check the health of the load cell
* Update load_cell Documentation
* Add API server load_cell/dump_force endpoint
* Update [load_cell] config with calibration fields
* Add G-Code commands for working with load cells
* Add status reference for load_cell objects

Signed-off-by: Gareth Farrington <gareth@waves.ky>

* buttons: Debounce gcode_button and filament_switch_sensor (Klipper3d#6848)

Add `debounce_delay` config option which sets the debounce time, defaults to 0

Signed-off-by: Gareth Farrington <gareth@waves.ky>

* stepper: Support step on both edges with custom minimum pulse duration

Add support for "step on both edges" to the main stepper_event_full()
code.  This makes that mode of operation available even when the
micro-controller is not compiled for "optimized step on both edges".
It also enables the custom pulse duration support (step_pulse_ticks)
when in "step on both edges" mode.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* stepper: Support disabling optimized "step on both edges" in "make menuconfig"

Add a new "low level option" to allow users to configure if they want
to optimize for Trinamic drivers or traditional stepper motor drivers.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* buttons: fixes incorrect parameters

Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>

* config: Add LED definitions to generic-bigtreetech-skr-mini-mz.cfg

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* fan_generic: Fix handling of template rendering errors

Make sure to assign 'value' on a rendering error to avoid an internal
error.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* output_pin: Fix handling of template rendering errors

Make sure to assign 'value' on a rendering error to avoid an internal
error.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* rp2040: define spi bus on pins 12,11,10

Mellow FLY SHT36 Pro toolboard uses those pins

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* stm32: Added PH13/14 CAN pin option for stm32h743 (Klipper3d#6857)

Added the option to select PH13/PH14 as CAN pins.

Signed-off-by:  Christoph Frei <fryakatkop@gmail.com>

* ldc_1612: Supports configurable external crystal frequency (Klipper3d#6734)

You can use the 40Mhz crystal oscillator recommended by TI official manual to get the best performance.
refer to: [ldc1612.pdf](https://www.ti.com/cn/lit/ds/symlink/ldc1612.pdf) 7.3.4

Signed-off-by: Xiaokui Zhao <xiaok@zxkxzk.cn>

* docs: Fix typos in installation.md

Fixed typos, hyphenation, and minor phrasing for better readability.

Signed-off-by: Tobias Rumiz <TobiasRumiz@gmail.com>

* docs: Fix typo in docs generation documentation

Signed-off-by: Russell Cloran <rcloran@gmail.com>

* docs: Fix link syntax typo for bed_screws

Signed-off-by: Russell Cloran <rcloran@gmail.com>

* stm32: Fix RESERVE_PINS_CAN pin ordering in fdcan.c

Always report the reserved pins in the same order (rx,tx).

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* stm32: Add support for additional i2c bus

Signed-off-by: Russell Cloran <rcloran@gmail.com>

* stm32: Turn on can.c error interrupts

It seems both ERRIE and LECIE must be enabled to get hardware error
interrupts.  Without this, the rx_error and tx_error reports are
likely to always be zero.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* output_pin: Make it possible to assign dicts/lists as template parameters

The output_pin template code has a cache to speed up duplicate
rendering of templates.  However, this cache doesn't work if one of
the parameters is a Python list or dictionary.  Just disable the cache
in this case.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* mkdocs-requirements: Update to Jinja 3.1.6

A security vulnerability was found in Jinja 3.1.5 .  The software is
not impacted by this vulnerability, but there is no harm in updating
to the fixed version.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* icm20948: Fix sample rate and accels scale selection

To set a value in the SET_ACCEL_CONFIG register, you must first go to BANK_2.

Signed-off-by: Maksim Bolgov maksim8024@gmail.com

* icm20948: Transition from 8g to 16g accels scale

During standard resonance measurements, the icm20948 in 8g mode may reach the accels max threshold.

Signed-off-by: Maksim Bolgov maksim8024@gmail.com

* icm20948: Formatting refactor

Signed-off-by: Maksim Bolgov maksim8024@gmail.com

* adxl345: Allow read and write 127 register address

icm20948 accelerometer has an ACCEL_CONFIG register at address 127

Signed-off-by: Maksim Bolgov maksim8024@gmail.com

* docs: Add icm20948 description

Signed-off-by: Maksim Bolgov maksim8024@gmail.com

* temperature_combined: delay initialization

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* temperature_combined: avoid crash with temperature monitors

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* axis_twist_compensation: Fix AttributeError on klippy connect state (Klipper3d#6881)

Object 'configfile' has no attribute 'error'

Signed-off-by: Maksim Bolgov <maksim8024@gmail.com>

* docs: Fixup G-Codes.md so that sections are sorted alphabetically

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* i2c_software: Fix i2c_delay()

The i2c_delay() function did not properly handle counter rollovers.
It also performed an expensive run-time divide.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* force_move: Support a SET_HOMED parameter to SET_KINEMATIC_POSITION

Commit 7083879 added support for clearing the homing state in
SET_KINEMATIC_POSITION commands.  However, it can be difficult to use
that support as the default for SET_KINEMATIC_POSITION is to set all
axes as homed.

Add a new SET_HOMED parameter to allow one to explicitly request which
axes to consider in a homed state.

Also introduce a CLEAR_HOMED parameter and prefer that to the existing
CLEAR parameter.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* force_move: Make sure to use lower() on SET_KINEMATIC_POSITION CLEAR_HOMED

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* ci-install: Use prebuilt pru gcc binaries

Don't build the pru binaries directly in the build test cases, instead
use the upstream binaries provided.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* led: Fix off-by-one bug in SET_LED_TEMPLATE INDEX parameter

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* docs: Update Features.md to reflect recent work

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* docs: Note the release of v0.13.0

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Makefile: Don't disable gcc's use-linker-plugin option

This option seems to be confusing ld's region usage checks (builds
that could fit in small chips are being reported as not fitting).  The
option was disabled back in commit 4e8674d because it showed worse
results.  However, recent versions of gcc seem to produce the same
results even if this option is enabled, so change the build to avoid
disabling that option on newer versions of gcc (those that have the
-ffat-lto-objects option - which is needed to ensure
compile_time_requests sections can be extracted with objcopy).

The PRU build is dependent on -fuse-linker-plugin, so enable that
option explicitly in its build.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* test: Disable optional features in atmega328 build

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* workflows: Update github build-test.yaml to ubuntu-22.04

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Kconfig: Replace WANT_GPIO_BITBANGING with individual options

Support setting individual options instead of one global option (
WANT_BUTTONS, WANT_TMCUART, WANT_NEOPIXEL, WANT_PULSE_COUNTER,
WANT_HX71X).

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Kconfig: Replace WANT_DISPLAYS with individual options

Support setting WANT_ST7920 and WANT_HD44780 individually.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* avr: Enable gcc -Os option on CONFIG_HAVE_LIMITED_CODE_SIZE

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* stm32: Enable gcc -Os option on CONFIG_HAVE_LIMITED_CODE_SIZE

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Kconfig: Add new WANT_HARD_PWM option to reduce code size

Make it possible to not compile in support for hardware pwm on chips
with small flash sizes.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Kconfig: Add new WANT_SPI option to reduce code size

Make it possible to not compile in support for SPI on chips with small
flash sizes.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Kconfig: Add new WANT_I2C option to reduce code size

Make it possible to not compile in support for I2C on chips with small
flash sizes.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Kconfig: Add new WANT_ADC option to reduce code size

Make it possible to not compile in support for ADC on chips with small
flash sizes.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* stm32: Simplify Makefile

Breakout selection of timer and gpioperiph objects.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Kconfig: Add some user visible comments to the optional features menu

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* atsamd: Enable HAVE_LIMITED_CODE_SIZE on small atsamd chips

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* config: Update generic-bigtreetech-skr-2.cfg - SPI Drivers (Klipper3d#6895)

Added SPI tmc2130 driver config

Signed-off-by: James Hartley <james@hartleyns.com>

* test: Disable all additional features in atmega328 build

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Revert "Makefile: Don't disable gcc's use-linker-plugin option"

This reverts commit 8087200.

The change can break the build on some versions of gcc.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* spi_software: respect expected rate

On fast MCU software spi may violate maximally supported by TMC driver rate.

Add dynamic limits to overcome that.

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* software_spi: set rate limiting ticks from the host

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* i2c_software: allow freq adjust

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* i2c_software: reduce gpio calls count

gpio reset calls are heavy.
gpio state are persistent between calls.
Drop useless calls.
Avoid calls if SDA does not changed.

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* i2c_software: pass pulse ticks from host

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* tmc: add missing freewheel config options

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>

* hall_filament_width_sensor: Add filament switch values to status

`hall_filament_width_sensor` contains a runout sensor object internally.
This exposes those values in the API status result.

```
SEND: {"id":123,"method":"objects/query","params":{"objects":{"hall_filament_width_sensor":["enabled","filament_detected","is_active","Diameter","Raw"]}}}
GOT: b'{"id":123,"result":{"eventtime":199567.823596603,"status":{"hall_filament_width_sensor":{"enabled":true,"filament_detected":true,"is_active":true,"Diameter":1.9499999999999986,"Raw":6113}}}}'
```

The duplication of `is_active` and `enabled` seems confusing, but both
of these can be independently manipulated by GCode:

```
SEND: {"id":123,"method":"gcode/script","params":{"script":"DISABLE_FILAMENT_WIDTH_SENSOR"}}
GOT: b'{"id":123,"result":{}}'
SEND: {"id":123,"method":"objects/query","params":{"objects":{"hall_filament_width_sensor":["enabled","is_active"]}}}
GOT: b'{"id":123,"result":{"eventtime":199770.446013297,"status":{"hall_filament_width_sensor":{"enabled":true,"is_active":false}}}}'

SEND: {"id":123,"method":"gcode/script","params":{"script":"SET_FILAMENT_SENSOR SENSOR=hall_filament_width_sensor ENABLE=0"}}
GOT: b'{"id":123,"result":{}}'
SEND: {"id":123,"method":"objects/query","params":{"objects":{"hall_filament_width_sensor":["enabled","is_active"]}}}
GOT: b'{"id":123,"result":{"eventtime":199847.927726196,"status":{"hall_filament_width_sensor":{"enabled":false,"is_active":false}}}}'
```

Signed-off-by: Russell Cloran <rcloran@gmail.com>

* docs: Update Config_Changes.md to note software spi is now rate limited

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* stm32: Fix prescaler overflow check in hard_pwm.c

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* stm32: Change hard_pwm.c MAX_PWM to 257

Choose a value for MAX_PWM that avoids an expensive run-time division.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* lcd_hd44780: Make sure nsecs_to_ticks() is always inlined

It is a compile-time calculation that needs to be inlined to work.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* lcd_st7920: Make sure nsecs_to_ticks() is always inlined

It is a compile-time calculation that needs to be inlined to work.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* neopixel: Make sure nsecs_to_ticks() is always inlined

It is a compile-time calculation that needs to be inlined to work.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* Kconfig: Note which chips require software divide operations

Add a new HAVE_SOFTWARE_DIVIDE_REQUIRED that indicates which chips
require software divide.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* check-software-div: Add a new build check for software divide

Update the build checks to include a check for unexpected software
divide operations.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* neopixel: Add comments on timing

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* toolhead: Avoid LookAheadQueue calling back into toolhead class

Avoid lookahead.flush() calling back into toolhead._process_moves().
Instead, rename toolhead._process_moves() to
toolhead._process_lookahead(), have it call lookahead.flush(), and
consistently use it when flushing the lookahead queue.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* toolhead: Avoid toolhead.move() and toolhead._process_moves() in drip_move()

Implement move checking and trapq loading directly from drip_move().
This simplifies the interactions between these components.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* manual_stepper: Implement "drip moves" for manual stepper STOP_ON_ENDSTOP

Currently, `MANUAL_STEPPER STOP_ON_ENDSTOP=1` type commands will move
until hitting the endstop, but it will still always consume the total
amount of move time.  That is, following moves can't be started until
the total possible time of the homing move is completed.

Implement "drip moves" so that the code only schedules the movement in
small segments.  This allows following movements to be scheduled
without a significant delay.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe: Add a default probing_move() function to HomingViaProbeHelper

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe: Add a new ProbeEndstopSessionHelper class

Move the HomingViaProbeHelper() instance from ProbeSessionHelper to a
new ProbeEndstopSessionHelper class.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe: Add a new lookup_minimum_z() helper function

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe: Add a new ProbeParameterHelper class

Split multi-sample config reading from ProbeSessionHelper to a new
ProbeParameterHelper class.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe: Change probing_move() to pass a gcmd instead of (pos, speed)

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe: Convert probing_move() callback to use regular probe sessions system

Use the normal probe_session_start(), run_probe(),
pull_probed_results(), and end_probe_session() API from
ProbeSessionHelper.  This removes the custom probing_move() callback.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe: Add a new LookupZSteppers helper class

Split code to lookup the Z stepper from HomingViaProbeHelper to new
LookupZSteppers class.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe_eddy_current: Do not support QUERY_PROBE command

Report an error if a user issues a QUERY_PROBE command (instead of
always returning not-triggered).

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe_eddy_current: Separate probe style commands from homing operations

Separate homing operations (as called from probe:z_virtual_endstop)
from the normal probe command handling.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

* probe: Remove ProbeEndstopSessionHelper

Have all callers instantiate the individual helper classes directly.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>

---------

Signed-off-by: Marius Petcu <marius@petcu.me>
Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>
Signed-off-by: Jorge Apaza Merma <yochiwarez@gmail.com>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Signed-off-by: Dmitry Butyugin <dmbutyugin@google.com>
Signed-off-by: Philippe Daouadi <philippe@ud2.org>
Signed-off-by: Gareth Farrington <gareth@waves.ky>
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
Signed-off-by: Christoph Frei <fryakatkop@gmail.com>
Signed-off-by: Xiaokui Zhao <xiaok@zxkxzk.cn>
Signed-off-by: Tobias Rumiz <TobiasRumiz@gmail.com>
Signed-off-by: Russell Cloran <rcloran@gmail.com>
Signed-off-by: Maksim Bolgov maksim8024@gmail.com
Signed-off-by: Maksim Bolgov <maksim8024@gmail.com>
Signed-off-by: James Hartley <james@hartleyns.com>
Co-authored-by: Marius Petcu <marius@petcu.me>
Co-authored-by: Timofey Titovets <nefelim4ag@gmail.com>
Co-authored-by: yochiwarez <yochiwarez@gmail.com>
Co-authored-by: Kevin O'Connor <kevin@koconnor.net>
Co-authored-by: Dmitry Butyugin <dmbutyugin@google.com>
Co-authored-by: Philippe Daouadi <philippe@ud2.org>
Co-authored-by: Gareth Farrington <gareth@waves.ky>
Co-authored-by: Pedro Lamas <pedrolamas@gmail.com>
Co-authored-by: FrY Sennberg <fryakatkop@gmail.com>
Co-authored-by: XiaoK <xiaok@zxkxz.cn>
Co-authored-by: Tobias Rumiz <63514002+Ruminini@users.noreply.github.com>
Co-authored-by: Russell Cloran <rcloran@gmail.com>
Co-authored-by: MRX8024 <57844100+MRX8024@users.noreply.github.com>
Co-authored-by: JamesH1978 <87171443+JamesH1978@users.noreply.github.com>
Misterke pushed a commit to Misterke/klipper that referenced this pull request Aug 14, 2025
* Add gram scale features to load_cell
* Convert sensor counts to grams and make this available via unix socket and object status
* Basic GCodes for tearing and reading the load cell
* Guided Calibration
* Diagnostic gcode to check the health of the load cell
* Update load_cell Documentation
* Add API server load_cell/dump_force endpoint
* Update [load_cell] config with calibration fields
* Add G-Code commands for working with load cells
* Add status reference for load_cell objects

Signed-off-by: Gareth Farrington <gareth@waves.ky>
Co-authored-by: Gareth Farrington <gareth@waves.ky>
@github-actions github-actions bot locked and limited conversation to collaborators Mar 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants