|
Edited by 1634718277 at 2021-10-22 09:50 PM
Hello,
My testing allowed me to determine that the problem was with a deadlock in the checkdist () function of the ultra.py module. Indeed the error happens because the GPIO.input (Ec) remains at 0 in the test of the first loop where it must go to 1. The error makes the loop infinite and the thread is blocked.
The main reason for triggering this anomaly is an obstacle placed on the ultrasonic. By the way, that's how I trigger it in a test by putting my hand on it.
Below is the original module that allows the error
- #! / usr / bin / python3
- # File name: Ultrasonic.py
- # Description: Detection distance and tracking with ultrasonic
- # Website: <a href="http://www.adeept.com" target="_blank">www.adeept.com</a>
- # E-mail: <a href="mailto:support@adeept.com">support@adeept.com</a>
- # Author: William
- # Date: 2019/02/23
- import RPi.GPIO as GPIO
- import time
- Tr = 11
- Ec = 8
- def checkdist (): #Reading distance
- GPIO.setwarnings (False)
- GPIO.setmode (GPIO.BCM)
- GPIO.setup (Tr, GPIO.OUT, initial = GPIO.LOW)
- GPIO.setup (Ec, GPIO.IN)
- GPIO.output (Tr, GPIO.HIGH)
- time.sleep (0.000015)
- GPIO.output (Tr, GPIO.LOW)
- while not GPIO.input (Ec):
- pass
- t1 = time.time ()
- while GPIO.input (Ec):
- pass
- t2 = time.time ()
- return (t2-t1) * 340/2>
Copy the Code
My tinkering led me to transform the module to unlock the "while not GPIO.input (Ec):" loop during the error.
- #! / usr / bin / python3
- # File name: Ultrasonic.py
- # Description: Detection distance and tracking with ultrasonic
- # Website: <a href="http://www.adeept.com" target="_blank">www.adeept.com</a>
- # E-mail: <a href="mailto:support@adeept.com">support@adeept.com</a>
- # Author: William
- # Date: 2019/02/23
- import RPi.GPIO as GPIO
- import time
- Tr = 11
- Ec = 8
- #dist = 0
- def checkdist (): #Reading distance
- GPIO.setwarnings (False)
- GPIO.setmode (GPIO.BCM)
- GPIO.setup (Tr, GPIO.OUT, initial = GPIO.LOW)
- GPIO.setup (Ec, GPIO.IN)
- GPIO.output (Tr, GPIO.HIGH)
- time.sleep (0.000015)
- GPIO.output (Tr, GPIO.LOW)
- global Error, totErr
-
- try:
- if totErr == 0:
- pass
- except:
- Error = False
- totErr = 0
-
- # giec = 0
- t0 = time.time ()
- # while not giec:
- while not GPIO.input (Ec):
- # giec = GPIO.input (Ec)
- if time.time () - t0> 1:
- # giec = 1
- Error = True
- totErr + = 1
- print (Error, 'totErr:', totErr)
- print ('forced b1 output')
- break
- pass
- t1 = time.time ()
- #print ('t1:', t1)
- #while giec:
- while GPIO.input (Ec):
- # giec = GPIO.input (Ec)
- pass
- t2 = time.time ()
- # print ('t2:', t2)
- # dist = (t2-t1) * 340 / 2A
- #print ('(t2-t1) * 340/2', dist)
- print (Error, 'totErr:', totErr)
-
- return (t2-t1) * 340/2
- Regarding the lines below, despite my multiple attempts I have not succeeded in making the Error and totErr variables initialized at the start of the main webServer.py module global.
- I'm sorry for the slowness of my python learning.
- <
- global Error, totErr
-
- try:
- if totErr == 0:
- pass
- except:
- Error = False
- totErr = 0
Copy the Code
Regarding the lines below, despite my multiple attempts I have not succeeded in making the Error and totErr variables initialized at the start of the main webServer.py module global.
I'm sorry for the slow pace of my python learning.
- <div>global Error, totErr
-
- try:
- if totErr == 0:
- pass
- except:
- Error = False
- totErr = 0</div>
Copy the Code
Once the module has been cleaned of all debugging lines, it remains
- #! / usr / bin / python3
- # File name: Ultrasonic.py
- # Description: Detection distance and tracking with ultrasonic
- # Website: <a href="http://www.adeept.com" target="_blank">www.adeept.com</a>
- # E-mail: <a href="mailto:support@adeept.com">support@adeept.com</a>
- # Author: William
- # Date: 2019/02/23
- import RPi.GPIO as GPIO
- import time
- Tr = 11
- Ec = 8
- def checkdist (): #Reading distance
- GPIO.setwarnings (False)
- GPIO.setmode (GPIO.BCM)
- GPIO.setup (Tr, GPIO.OUT, initial = GPIO.LOW)
- GPIO.setup (Ec, GPIO.IN)
- GPIO.output (Tr, GPIO.HIGH)
- time.sleep (0.000015)
- GPIO.output (Tr, GPIO.LOW)
- t0 = time.time ()
- while not GPIO.input (Ec):
- if time.time () - t0> 1:
- break
- pass
- t1 = time.time ()
- while GPIO.input (Ec):
- pass
- t2 = time.time ()
- return (t2-t1) * 340/2
Copy the Code
There is certainly a much more elegant solution to solving the problem. |
|