james-e-morris / hx711-multi

Sample multiple HX711 24-bit ADCs with Python 3 on a Raspberry Pi Rasperry Pi Zero, 2 or 3
MIT License
13 stars 5 forks source link

[Question] How to get value for set_weight_multiples? #7

Closed AndhikaWB closed 2 years ago

AndhikaWB commented 2 years ago

By looking at the library source code, there's a comment like this on set_weight_multiples() section:

Sets the weight mutliples for ADCs to be used when calculating weight
Example: scale indicates value of 5000 for 1 gram on scale, weight_multiple = 5000 (to convert to weight in grams)

How do I get this scale value? Should I use read_raw(), read_weight(), or something else?

On Gandalf15's original library I can achieve it using something like this:

data_est = hx711.get_data_mean()
# Get real weight from user
data_real = float(input("Please enter the real weight (gram): "))
# Calibrate readings from weight sensor
ratio = data_est / data_real
hx711.set_scale_ratio(ratio)

So what's the equivalent of get_data_mean() in this library? (I assume set_weight_multiples() and set_scale_ratio() behave similarly)

I'm sorry I had to ask this, I don't have the load cell yet but I'm planning to use your library.

EDIT: My bad, I didn't check the other issue. I assume the answer is read_raw()?

james-e-morris commented 2 years ago

Yes, read_raw() is going to be the values you get without the calibrated multiple. I separated it out in the example code to show the difference, but maybe I can make it more clear in the future. I also named them raw and weight to make the difference clear.

In my use case, I made a script that prompts the user for input like your example and then compares the measurement to that value, queries again, etc. You really want multiple known weights and multiple queries so that your multiple is more accurate. I use a set of weights ranging from 1 gram to 100 grams.

If I have some extra time, I'll incorporate this into the codebase as an example calibration script.

james-e-morris commented 2 years ago

I think i'll add 2 functions get_raw() and get_weight() which would just access the most recent measurements. The way it is now, read_raw() will always pull a new sample, but maybe you don't want to read a new sample, you just want the latest measurement values. read_weight() already does this if you pass in use_prev_read=True, so I could just separate that functionality into a get to clarify everything.

Edit: or maybe just add a use_prev_read to the read_raw() function? Whaddya think?

james-e-morris commented 2 years ago

Update: I have a new script in a dev branch for this calibration process, will merge after testing. I also added an input to the read_raw function for use_prev_read as mentioned above.

Also adding the get functions to double up on that functionality. This will be published as part of the next build.

AndhikaWB commented 2 years ago

You're right, I can just take the last measurement value without pulling a new sample again. Never thought of that before. The comment on use_prev_read could be emphasized more on 'without' keyword instead of 'new' (since I don't recognize how useful it is before), but the new comment on get_raw() and get_weight() makes it clear enough.

Also the new branch looks fine (just by looking, I haven't tested it yet). I think I like to use get_raw() and get_weight() more since it doesn't require extra parameters, but use_prev_read should stay as it may break other people's code if removed.

Thank you, I will close this.

james-e-morris commented 2 years ago

I tested my code and made a few improvements. I also rolled that entire script into my HX711 class so that you can call hx711.run_calibration() instead of needing to copy and write your own version of my example. The calibration example is still there and uses the run_calibration function. See ReadMe for some clarification on how that all works.

I also added the get_raw() and get_weight() functions.

The latest rev 1.2.0 includes all of this and a few other fixes.