tatobari / hx711py

HX711 Python Library for Raspberry Pi.
Apache License 2.0
211 stars 153 forks source link

Allow using both channels of a 2-channel HX711 module #14

Closed yesrod closed 5 years ago

yesrod commented 6 years ago

I modified hx711.py to make it easier to get data from both channels of an HX711 module.

While I could read channel 2 with the existing code by using set_gain(32), the offset and reference values were different because they had been set up for channel 1. I added separate offset and reference variables for channel A and B, and kept the old variables to make the code as backwards compatible as possible.

For each of the tare(), set_offset(), set_reference_unit(), get_value(), and get_weight() functions I added separate versions for channel A and B. The original functions call the channel A versions directly to maintain compatibility.

I also added a read_median() function that uses median instead of average when reading data. I was getting a lot of nasty spikes that I couldn't track down, and using the median of the data instead of the average seemed to help. I changed the tare() and get_value() functions to use read_median(), but I left the read_average() function in case someone is using it directly.

Finally, I did some general cleanup - added a basic setup.py to allow installing this as a proper module, added to the README.md, and added a .gitignore file to ignore leftovers from module building.

tatobari commented 6 years ago

Hey man! Nice contribution! I'll take a loon at it during next week since I'll be our of town for a few days.

yesrod commented 6 years ago

No problem. Let me know if you see anything that can be cleaned up.

yesrod commented 6 years ago

I'm going to close this PR for now and refactor to match your master. Once that's done, I'll resubmit.

yesrod commented 6 years ago

Sorry, shouldn't have closed this - still learning GitHub pull requests.

Anyway, my branch should now be updated enough to merge correctly. Let me know if you see anything horribly broken.

tatobari commented 6 years ago

Hey @yesrod, I will check this tonight. It's 10:26 here in Argentina.

yesrod commented 6 years ago

Thanks. I'm working on Python 3 compatibility but I'm doing that in a separate branch, so it shouldn't interfere with this request.

tatobari commented 6 years ago

@yesrod I'm merging this in a couple days. I'll be home with my Raspberry on Friday. Sorry for the delay.

yesrod commented 6 years ago

No problem, let me know if there's anything I can do to make it easier on you.

On Mon, Aug 13, 2018 at 4:54 PM tatobari notifications@github.com wrote:

@yesrod https://github.com/yesrod I'm merging this in a couple days. I'll be home with my Raspberry on Friday. Sorry for the delay.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tatobari/hx711py/pull/14#issuecomment-412660511, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYTp-xfcUZUjOMJbe46OD9HUoWUlCrnks5uQedwgaJpZM4S_ARL .

-- Adam Dorsey adam.dorsey@gmail.com

tatobari commented 6 years ago

I just need to test it. You've probably did and I'm sure it works but its the correct thing to do, besides the fact that I've abandoned this repository for months. Sorry about that, been under loads of work and personal issues. I'm back now, though.

yesrod commented 6 years ago

Understood. If you need any help or run into anything I screwed up let me know.

On Mon, Aug 13, 2018 at 5:04 PM tatobari notifications@github.com wrote:

I just need to test it. You've probably did and I'm sure it works but its the correct thing to do, besides the fact that I've abandoned this repository for months. Sorry about that, been under loads of work and personal issues. I'm back now, though.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tatobari/hx711py/pull/14#issuecomment-412663828, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYTpwJYuWSg1IQZy9I0zOIMAgR2hh81ks5uQensgaJpZM4S_ARL .

-- Adam Dorsey adam.dorsey@gmail.com

tatobari commented 6 years ago

Will do, thanks.

tatobari commented 5 years ago

Hey! I've just made a few modifications. The code I used as a reference was not intepreting the 24bits correctly. The value delivered by the HX711 was in Two's Complement format. Now, the code should supported. I've changed but never tested it. I have my Raspberry somewhere but I'm working on a big project, using PIC32. If you could resolve those conflicts, I'll merge immediately. Thanks!

yesrod commented 5 years ago

Thanks, I'll resolve that this evening and let you know when it's done. I have an ongoing setup measuring keg weights in my kegerator, so I can test on actual hardware.

On Thu, Nov 15, 2018 at 2:57 PM tatobari notifications@github.com wrote:

Hey! I've just made a few modifications. The code I used as a reference was not intepreting the 24bits correctly. The value delivered by the HX711 was in Two's Complement format. Now, the code should supported. I've changed but never tested it. I have my Raspberry somewhere but I'm working on a big project, using PIC32. If you could resolve those conflicts, I'll merge immediately. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tatobari/hx711py/pull/14#issuecomment-439169958, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYTp4Zuqbkkhdu3HPxVIhnkE_nZKKyMks5uvccJgaJpZM4S_ARL .

-- Adam Dorsey adam.dorsey@gmail.com

tatobari commented 5 years ago

@yesrod Awesome, thanks! Sorry for taking so long to take a look at this.

yesrod commented 5 years ago

No problem.

I merged this evening and did some testing. Some of the new variables started with numbers, which isn't allowed in Python (24BitMSBIndex, 32BitMSBIndex) so I changed those to MSBIndex24Bit and MSBIndex32Bit respectively.

Unfortunately, there's another issue related to the change to self.LSByte, I'm now getting the following stack trace:

Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main "main", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/home/pi/git/hoplite/hoplite/main.py", line 192, in main() File "/home/pi/git/hoplite/hoplite/main.py", line 189, in main h.main( config_file = config ) File "hoplite/hoplite.py", line 494, in main self.co2 = self.init_co2(self.config['co2']) File "hoplite/hoplite.py", line 154, in init_co2 co2 = self.init_hx711(co2_conf) File "hoplite/hoplite.py", line 130, in init_hx711 hx = HX711(dout, pd_sck) File "build/bdist.linux-armv6l/egg/hx711.py", line 39, in init File "build/bdist.linux-armv6l/egg/hx711.py", line 57, in set_gain File "build/bdist.linux-armv6l/egg/hx711.py", line 89, in read IndexError: list index out of range

I changed it back to the value in my code but that just broke weight reading in general. I'm going to have to take a closer look at this.

On Thu, Nov 15, 2018 at 6:43 PM tatobari notifications@github.com wrote:

@yesrod https://github.com/yesrod Awesome, thanks! Sorry for taking so long to take a look at this.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tatobari/hx711py/pull/14#issuecomment-439230805, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYTp3LtKDIRYokHXOP6Kpzf6uOqXGnKks5uvfwcgaJpZM4S_ARL .

-- Adam Dorsey adam.dorsey@gmail.com

yesrod commented 5 years ago

I committed and pushed, but the code doesn't work, I keep getting the same stacktrace I previously sent. I'm going to keep working on it and let you know.

According to the datasheet at https://cdn.sparkfun.com/datasheets/Sensors/ForceFlex/hx711_english.pdf the data read out should just be 24 bits, 2's complement, MSB first. I might try re-implementing the data collection and math bits and see what happens.

On Thu, Nov 15, 2018 at 7:44 PM Adam Dorsey adam.dorsey@gmail.com wrote:

No problem.

I merged this evening and did some testing. Some of the new variables started with numbers, which isn't allowed in Python (24BitMSBIndex, 32BitMSBIndex) so I changed those to MSBIndex24Bit and MSBIndex32Bit respectively.

Unfortunately, there's another issue related to the change to self.LSByte, I'm now getting the following stack trace:

Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main "main", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/home/pi/git/hoplite/hoplite/main.py", line 192, in main() File "/home/pi/git/hoplite/hoplite/main.py", line 189, in main h.main( config_file = config ) File "hoplite/hoplite.py", line 494, in main self.co2 = self.init_co2(self.config['co2']) File "hoplite/hoplite.py", line 154, in init_co2 co2 = self.init_hx711(co2_conf) File "hoplite/hoplite.py", line 130, in init_hx711 hx = HX711(dout, pd_sck) File "build/bdist.linux-armv6l/egg/hx711.py", line 39, in init File "build/bdist.linux-armv6l/egg/hx711.py", line 57, in set_gain File "build/bdist.linux-armv6l/egg/hx711.py", line 89, in read IndexError: list index out of range

I changed it back to the value in my code but that just broke weight reading in general. I'm going to have to take a closer look at this.

On Thu, Nov 15, 2018 at 6:43 PM tatobari notifications@github.com wrote:

@yesrod https://github.com/yesrod Awesome, thanks! Sorry for taking so long to take a look at this.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tatobari/hx711py/pull/14#issuecomment-439230805, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYTp3LtKDIRYokHXOP6Kpzf6uOqXGnKks5uvfwcgaJpZM4S_ARL .

-- Adam Dorsey adam.dorsey@gmail.com

-- Adam Dorsey adam.dorsey@gmail.com

tatobari commented 5 years ago

Sorry Everyone! Lot's of things where wrong with this code. I had to refactor a lot of stuff. I rarely use Python. I usually work with NodeJS or C++ (for Arduino, barely). So, here are the updates on the last commit.

IMPORTANT NOTE: The typical green HX711 breakout board has a very big negative offset. The raw value is close to -8.000.000 (negative 8 million). As a result, you can apply a lot of force/weight to the load cell and this negative value will increase as you increase the applied force/weight.

However, if you apply a negative force (installing the load cell the other way round, inverting the load cell A- and A+ cables or pull up instead of pressing down), the values may get a bit crazy because an overflow happens. The green breakout boards have the HX711's electronic implementation like this for a reason which escapes me.

Anyway, I have schematics to reduce this offset (thanks to a real genius guy from Kiev, Ukraine). I'll see if I can publish them to people interested on fabricating their own breakout boards.

Sorry to everyone for the inconveniences caused and thanks for using this library.

PS: Next time, I'll work on a separate branch and test before committing.

yesrod commented 5 years ago

I just pulled and installed the new version and it appears to be working correctly. I need to recalibrate to make sure that I'm getting the right values, but other than that everything looks good.

Thanks again for your help and for providing this module!

On Fri, Nov 16, 2018 at 1:58 PM tatobari notifications@github.com wrote:

Sorry Everyone! Lot's of things where wrong with this code. I had to refactor a lot of stuff. I rarely use Python. I usually work with NodeJS or C++ (for Arduino, barely). So, here are the updates on the last commit.

  • It works. HX711 returns a Two's Complement formatted signed integer. Until this commit, that wasn't supported and this resulted in nonsense values from time to time.
  • Use hx.set_reading_format("MSB", "MSB"). It's the default so you don't really need to change this unless you're using a device with reversed endian (very rare cases). I realized I had a concept error and swapped the configuration of MSByte with LSByte.
  • hx.read_np_arr8() returns the raw response from the HX711. Until then, it hasn't been treated to support Two's Complement.
  • hx.read_long() is where the bits are handled to make the long negative if the returned value from the HX711 is negative. This is thanks to a global variable called self.isNegative which is calculated at the end of hx.read().

IMPORTANT NOTE: The typical green HX711 breakout board has a very big negative offset. The raw value is close to -8.000.000 (negative 8 million). As a result, you can apply a lot of force/weight to the load cell and this negative value will increase as you increase the applied force/weight.

However, if you apply a negative force (installing the load cell the other way round, inverting the load cell A- and A+ cables or pull up instead of pressing down), the values may get a bit crazy because an overflow happens. The green breakout boards have the HX711's electronic implementation like this for a reason which escapes me.

Anyway, I have schematics to reduce this offset (thanks to a real genius guy from Kiev, Ukraine). I'll see if I can publish them to people interested on fabricating their own breakout boards.

Sorry to everyone for the inconveniences caused and thanks for using this library.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tatobari/hx711py/pull/14#issuecomment-439492389, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYTpxhfnpnFvNnjqLwGQ3gcLddKrqmAks5uvwrAgaJpZM4S_ARL .

-- Adam Dorsey adam.dorsey@gmail.com

tatobari commented 5 years ago

Yeah, the idle value is very close to the 24-bit negative limit now. So, yeah, you'll have to recalibrate. Thanks for the patience!