qtfp
Posts: 36
Joined: Sat May 25, 2013 2:00 pm

Pigpio HX711.py will hang

Mon Feb 19, 2018 3:36 pm

I used the example from the official website
http://abyz.me.uk/rpi/pigpio/code/HX711_py.zip
and modified the """if __name__ == "__main__":""" part as follow:

Code: Select all

if __name__ == "__main__":

   import time
   import pigpio
   import HX711

   def cbf(count, mode, reading):
      W =round((reading-(-295705.0))*50/(-317687-(-295705)),2)
      print(count, mode, reading,W)
      
   #pi = pigpio.pi()
   pi = pigpio.pi("192.168.1.76",8888)
   if not pi.connected:
      exit(0)

   print("start with CH_B_GAIN_32 and callback")

   s = HX711.sensor(pi, DATA=9, CLOCK=10, mode=CH_B_GAIN_32, callback=cbf)
   '''
      time.sleep(5)
   
      s.set_mode(CH_A_GAIN_64)
   
      print("Change mode to CH_A_GAIN_64")
   
      time.sleep(5)
   '''
   s.set_mode(CH_A_GAIN_128)

   print("Change mode to CH_A_GAIN_128")

   time.sleep(5)

   s.pause()

   print("Pause")

   time.sleep(5)

   s.start()

   print("Start")

   time.sleep(5)

   s.set_callback(None)

   print("Cancel callback and read manually")

   stop = time.time() + 50

   c, m, r = s.get_reading()
   max_=0
   min_=10000
   print(c)

   while time.time() < stop:
      count, mode, reading = s.get_reading()
      if count != c:
         c = count
         W =round((reading-(-295705.0))*50/(-317687-(-295705)),2)
         if W>max_:
            max_=W
         if W<min_:
            min_=W
         print(count, mode, reading, W, max_, min_,max_-min_)
      time.sleep(0.5)

   s.pause()

   s.cancel()

   pi.stop()
There will be unexpected hang. Sometimes, I will re-enter the program, it will work agian for a while. While sometimes, I need to reboot to make it work again. This example is really inspiring for bit-banging with chip by callback, but I am still too new to fixed the problem.

The program ran well until it hang:

Code: Select all

(4199, 1, -297205, 3.41, 4.15, 2.83, 1.3200000000000003)
(4203, 1, -297066, 3.1, 4.15, 2.83, 1.3200000000000003)
(4208, 1, -297240, 3.49, 4.15, 2.83, 1.3200000000000003)
(4213, 1, -297035, 3.03, 4.15, 2.83, 1.3200000000000003)
(4217, 1, -297146, 3.28, 4.15, 2.83, 1.3200000000000003)
(4222, 1, -297098, 3.17, 4.15, 2.83, 1.3200000000000003)
(4226, 1, -297206, 3.41, 4.15, 2.83, 1.3200000000000003)
(4231, 1, -297089, 3.15, 4.15, 2.83, 1.3200000000000003)
(4233, 1, -297150, 3.29, 4.15, 2.83, 1.3200000000000003)    <====Stop here
After it hang, I may encounter this situation and it need a reboot:

Code: Select all

start with CH_B_GAIN_32 and callback
Change mode to CH_A_GAIN_64
Change mode to CH_A_GAIN_128
Pause
Start
Is there anything I need to notice when I use the callback?

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pigpio HX711.py will hang

Mon Feb 19, 2018 4:12 pm

Could you post the complete amended code which hangs. If I get some time I'll give it a go and see if I can replicate the issue.

Which version of pigpio are you using? (pigs pigpv).

qtfp
Posts: 36
Joined: Sat May 25, 2013 2:00 pm

Re: Pigpio HX711.py will hang

Mon Feb 19, 2018 4:48 pm

joan wrote:
Mon Feb 19, 2018 4:12 pm
Could you post the complete amended code which hangs. If I get some time I'll give it a go and see if I can replicate the issue.

Which version of pigpio are you using? (pigs pigpv).
I don't actually amend anything important, I just prolong runtime and add some calculation for weights and it will hang.
pigs pigpv=64
I use wifi ssh to pi.

I also find that this program hang almost at begin when I start from remote computer and use like:

Code: Select all

pi = pigpio.pi("192.168.1.76",8888)



Code: Select all

#s = HX711.sensor(pi, DATA=9, CLOCK=11, mode=CH_B_GAIN_32, callback=cbf)
s = HX711.sensor(pi, DATA=6, CLOCK=5, mode=CH_B_GAIN_32, callback=cbf)

Code: Select all

#stop = time.time() + 5
 stop = time.time() + 5000

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pigpio HX711.py will hang

Mon Feb 19, 2018 5:46 pm

As I say, if you post the complete code that you are using I will have a look. I am not going to speculate though.

qtfp
Posts: 36
Joined: Sat May 25, 2013 2:00 pm

Re: Pigpio HX711.py will hang

Mon Feb 19, 2018 6:06 pm

joan wrote:
Mon Feb 19, 2018 5:46 pm
As I say, if you post the complete code that you are using I will have a look. I am not going to speculate though.
Thanks for your help!!!
I am stuck here for a while

Code: Select all

#!/usr/bin/env python

# HX711.py
# 2016-05-01
# Public Domain

CH_A_GAIN_64  = 0 # Channel A gain 64
CH_A_GAIN_128 = 1 # Channel A gain 128
CH_B_GAIN_32  = 2 # Channel B gain 32

import time

import pigpio # http://abyz.co.uk/rpi/pigpio/python.html

class sensor:

   """
   A class to read the HX711 24-bit ADC.
   """

   def __init__(self, pi, DATA, CLOCK, mode=CH_A_GAIN_128, callback=None):
      """
      Instantiate with the Pi, the data GPIO, and the clock GPIO.

      Optionally the channel and gain may be specified with the
      mode parameter as follows.

      CH_A_GAIN_64  - Channel A gain 64
      CH_A_GAIN_128 - Channel A gain 128
      CH_B_GAIN_32  - Channel B gain 32

      Optionally a callback to be called for each new reading may be
      specified.  The callback receives three parameters, the count,
      the mode, and the reading.  The count is incremented for each
      new reading.
      """
      self.pi = pi
      self.DATA = DATA
      self.CLOCK = CLOCK
      self.set_mode(mode)
      self.callback = callback

      self._paused = False
      self._data_level = 0
      self._clocks = -1
      self._value = 0
      self._reading = None
      self._count = 0

      pi.set_mode(CLOCK, pigpio.OUTPUT)
      pi.set_mode(DATA, pigpio.INPUT)

      pi.write(CLOCK, 1) # Pause the sensor.

      pi.wave_add_generic(
         [pigpio.pulse(1<<CLOCK, 0, 20), pigpio.pulse(0, 1<<CLOCK, 20)])

      self._wid = pi.wave_create()

      self._cb1 = pi.callback(DATA, pigpio.EITHER_EDGE, self._callback)
      self._cb2 = pi.callback(CLOCK, pigpio.FALLING_EDGE, self._callback)

      self._valid_after = time.time() + 0.4

      pi.write(CLOCK, 0) # Start the sensor.

   def get_reading(self):
      """
      Returns the current count, mode, and reading.

      The count is incremented for each new reading.
      """
      return self._count, self._mode, self._reading

   def set_callback(self, callback):
      """
      Sets the callback to be called for every new reading.
      The callback receives three parameters, the count,
      the mode, and the reading.  The count is incremented
      for each new reading.

      The callback can be cancelled by passing None.
      """
      self.callback = callback

   def set_mode(self, mode):
      """
      Sets the mode.

      CH_A_GAIN_64  - Channel A gain 64
      CH_A_GAIN_128 - Channel A gain 128
      CH_B_GAIN_32  - Channel B gain 32
      """
      self._mode = mode

      if mode == CH_A_GAIN_128:
         self._pulses = 25
      elif mode == CH_B_GAIN_32:
         self._pulses = 26
      elif mode == CH_A_GAIN_64:
         self._pulses = 27
      else:
         raise ValueError

      self._valid_after = time.time() + 0.4

   def get_mode(self):
      """
      Returns the current mode.
      """
      return self._mode

   def pause(self):
      """
      Pauses readings.
      """
      self._paused = True
      self.pi.wave_tx_stop()
      self.pi.write(self.CLOCK, 1)
      self.pi.set_watchdog(self.CLOCK, 0) # Cancel any timeout.

   def start(self):
      """
      Starts readings.
      """
      self.pi.write(self.CLOCK, 0)
      self._clocks = -1
      self._value = 0
      self._paused = False
      self._valid_after = time.time() + 0.4

   def cancel(self):
      """
      Cancels the sensor and release resources.
      """
      if self._cb1 is not None:
         self._cb1.cancel()
         self._cb1 = None

      if self._cb2 is not None:
         self._cb2.cancel()
         self._cb2 = None

      if self._wid is not None:
         self.pi.wave_delete(self._wid)
         self._wid = None

      self.pi.set_watchdog(self.CLOCK, 0) # cancel timeout

   def _callback(self, gpio, level, tick):

      if gpio == self.CLOCK:

         if level == 0:

            self._clocks += 1

            if self._clocks < 25:

               self._value = (self._value << 1) + self._data_level

         else: #  timeout

            self.pi.set_watchdog(self.CLOCK, 0) # cancel timeout

            if self._clocks == self._pulses:
               if self._value & 0x800000:
                  self._value |= ~0xffffff

               if not (self._paused) and (time.time() > self._valid_after):
                  self._reading = self._value
                  self._count += 1
                  if self.callback is not None:
                     self.callback(self._count, self._mode, self._reading)

            self._clocks = 0
            self._value = 0

      else:

         self._data_level = level

         if (level == 0) and (self._clocks == 0):

            if not self._paused:
               self.pi.wave_chain([255, 0, self._wid, 255, 1, self._pulses, 0])
               self.pi.set_watchdog(self.CLOCK, 2) # 2 ms timeout

if __name__ == "__main__":

   import time
   import pigpio
   import HX711

   def cbf(count, mode, reading):
      W =round((reading-(-295705.0))*50/(-317687-(-295705)),2)
      print(count, mode, reading,W)
      
   pi = pigpio.pi()
   #pi = pigpio.pi("192.168.1.76",8888)
   if not pi.connected:
      exit(0)

   print("start with CH_B_GAIN_32 and callback")

   s = HX711.sensor(pi, DATA=6, CLOCK=5, mode=CH_B_GAIN_32, callback=cbf)
   
   time.sleep(5)
   
   s.set_mode(CH_A_GAIN_64)
   
   print("Change mode to CH_A_GAIN_64")
   
   time.sleep(5)
   s.set_mode(CH_A_GAIN_128)

   print("Change mode to CH_A_GAIN_128")

   time.sleep(5)

   s.pause()

   print("Pause")

   time.sleep(5)

   s.start()

   print("Start")

   time.sleep(5)

   s.set_callback(None)

   print("Cancel callback and read manually")

   stop = time.time() + 50

   c, m, r = s.get_reading()
   max_=0
   min_=10000
   print(c)

   while time.time() < stop:
      count, mode, reading = s.get_reading()
      if count != c:
         c = count
         W =round((reading-(-295705.0))*50/(-317687-(-295705)),2)
         if W>max_:
            max_=W
         if W<min_:
            min_=W
         print(count, mode, reading, W, max_, min_,max_-min_)
      time.sleep(0.05)

   s.pause()

   s.cancel()

   pi.stop()



User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pigpio HX711.py will hang

Mon Feb 19, 2018 6:29 pm

I can't see anything obvious.

I'll need to find my HX711 and hook it up. Not sure how long this will take.

qtfp
Posts: 36
Joined: Sat May 25, 2013 2:00 pm

Re: Pigpio HX711.py will hang

Thu Feb 22, 2018 4:02 pm

joan wrote:
Mon Feb 19, 2018 6:29 pm
I can't see anything obvious.

I'll need to find my HX711 and hook it up. Not sure how long this will take.
Right now, I use raspi <pigpio bb_serial< arduino nano <HX711. And it work fine, but still looking forward to know what's wrong~~
In the logic analyzer, The 25 pulses sending from raspi.pigpio will suddenly disappear or be stopped.
Thanks for pigpio :D :D :D

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pigpio HX711.py will hang

Thu Feb 22, 2018 4:44 pm

I haven't forgotten but I can't find my HX711. I'm using the opportunity to try to get all my components into some sort of order. Hopefully I'll find the HX711 during that process.

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pigpio HX711.py will hang

Sun Feb 25, 2018 5:53 pm

I wonder if it is the transition between channels causing the problem. The example script reads the two A channels and the B channel.

Could you comment out the example __main__ code channels you are not using so it is not switching between channels for no reason.

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pigpio HX711.py will hang

Mon Mar 05, 2018 4:30 pm

I think I know why it hangs. I have updated the code at

http://abyz.me.uk/rpi/pigpio/examples.h ... n_HX711_py

Only tested on a HX711 configured for 10 samples per second.

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Pigpio HX711.py will hang

Tue May 01, 2018 8:43 am

There is a nice little differential pressure breakout board (https://www.ebay.com.au/itm/Digital-Bar ... 2749.l2649) using the HX711s sibling the H710B. My reading of the data sheets is that they are sufficiently similar that HX11_py might have worked with it but alas no. So near & yet so far!

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pigpio HX711.py will hang

Tue May 01, 2018 10:17 am

PlanB wrote:
Tue May 01, 2018 8:43 am
There is a nice little differential pressure breakout board (https://www.ebay.com.au/itm/Digital-Bar ... 2749.l2649) using the HX711s sibling the H710B. My reading of the data sheets is that they are sufficiently similar that HX11_py might have worked with it but alas no. So near & yet so far!
Do you have a link to an English translation of the datasheet?

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Pigpio HX711.py will hang

Wed May 02, 2018 1:08 am

The translators I tried mashed the .PDFs pretty bad but it was the same 2s complement output & clocking methodology @ 10/40Hz. I'm loath to email you the dogs breakfast I waded through, I'll see if I can come up with a more legible translation.

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pigpio HX711.py will hang

Wed May 02, 2018 7:27 am

Did you get no readings or just no readings which made sense? Perhaps use piscope to capture what the device is spitting out. If you save the data in piscope format it can be played back and used as an input to test software. If you do capture data try to capture at least 30 seconds worth.

PlanB
Posts: 102
Joined: Tue Oct 28, 2014 6:44 am

Re: Pigpio HX711.py will hang

Thu May 10, 2018 12:17 am

I got https://www.ebay.com.au/itm/Digital-Bar ... 2749.l2649 working with this https://hackaday.io/project/25848-a-che ... ython-code for the time being.

I'm puzzed by the results. Since this is a differential pressure sensor I expected the output to be stable with no airline connected but it wanders around randomly to the tune of about 4% over 24 hours. When I applied a static source at about half range (20kPa) I get this weird sawtooth where the reading gradually drops over a period of hours then jumps up to the original value to repeat the decay. The variation is ~15%

My only experience with strain gauges to date has been with off-the-shelf submersible pressure sensors. I've notice they have long settling times with a typically ~1% drift over the first few minutes after power up. But this thing is unusable as is. Am I missing something? Should I be recycling power to it after each reading?

Am I missing the point completely? Are these chips not meant to have any long term stability? Is the idea that you take a tare reading, then your source reading then divide the two? If that's the case, it's hopeless as a pressure sensor because it requires you to disconnect the source for each tare reading.

Return to “Python”