Page 1 of 1

Stop running after few minute

Posted: Sun Apr 03, 2016 4:02 am
by w004thn
Hi,

I built a circuits using 3 PING sensors to measure the distance. With 1 sensor, it works fine. When I use 2 or 3 sensors, they only run for few minutes and stop execute the code. The following codes are what I used. I also want to have the Pi start the code automatically as it is powered up. I appreciate any helps. Thank you.


print "Ultrasonic Measurement"
try:
while True:

# Set pins as output and input
GPIO.setup(GPIO_TRIGGER1,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO1,GPIO.IN) # Echo

GPIO.setup(GPIO_TRIGGER2,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO2,GPIO.IN) # Echo

GPIO.setup(GPIO_TRIGGER3,GPIO.OUT) # Trigger
GPIO.setup(GPIO_ECHO3,GPIO.IN) # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER1, False)
GPIO.output(GPIO_TRIGGER2, False)
GPIO.output(GPIO_TRIGGER3, False)
# Allow module to settle
time.sleep(0.5)

# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER1, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER1, False)
start = time.time()
while GPIO.input(GPIO_ECHO1)==0:
start1 = time.time()

while GPIO.input(GPIO_ECHO1)==1:
stop1 = time.time()

# Calculate pulse length
elapsed1 = stop1-start1

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance1 = elapsed1 * 34000

# That was the distance there and back so halve the value
distance1 = distance1 / 2

print "Distance 1 : %.1f" % distance

#sensor 2
# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER2, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER2, False)
start2 = time.time()
while GPIO.input(GPIO_ECHO2)==0:
start2 = time.time()

while GPIO.input(GPIO_ECHO2)==1:
stop2 = time.time()

# Calculate pulse length
elapsed2 = stop2-start2

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance2 = elapsed12 * 34000

# That was the distance there and back so halve the value
distance2 = distance2 / 2

print "Distance 2 : %.1f" % distance

#sensor 3
# Send 10ms pulse to trigger
GPIO.output(GPIO_TRIGGER3, True)
time.sleep(0.01)
GPIO.output(GPIO_TRIGGER3, False)
start3 = time.time()
while GPIO.input(GPIO_ECHO3)==0:
start3 = time.time()

while GPIO.input(GPIO_ECHO)==1:
stop3 = time.time()

# Calculate pulse length
elapsed3 = stop3-start3

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance3 = elapsed3 * 34000

# That was the distance there and back so halve the value
distance3 = distance3 / 2

print "Distance 3 : %.1f" % distance

except KeyBoardInterrupt:
# Reset GPIO settings
GPIO.cleanup()

Re: Stop running after few minute

Posted: Sun Apr 03, 2016 9:48 am
by RDS
Hi
You really need to display all your code using the code tab. (by selecting all your code and pressing the code tab) because this maintains all the indents which are essential in Python code and will enable your code to be studied correctly by the many Python experts on this forum.

Re: Stop running after few minute

Posted: Sun Apr 03, 2016 10:31 am
by gordon77
Do you get any errors ?

To start at Jessie bootup try putting the relevant command in ~/.config/lxsession/LXDE-PI/autostart

Eg. To run a python script use...
@sudo /usr/bin/python /home/pi/filename.py

To Make the file executable with sudo chmod +x filename.py

It will run after the gui starts.

Re: Stop running after few minute

Posted: Sun Apr 03, 2016 1:55 pm
by ghp
Hello,
your code waits for a pos edge (while low), then for a neg edge (while high). When there is no pulse arriving, your code keeps staying in the first loop.
I assume you use a HC-SR04 ? The start trigger is a 10us pulse min, so with a 10ms trigger it can happen that the echo pulse output is already finished when the code starts to wait for the pos edge...
Try to make the start trigger pulse much shorter 0.01 --> 0.00002, although this precision can't be achieved by python on linux, it will reduce the risk of missing the signal.
Precision in general is a topic here. With python, I made some tests a year ago to evaluate accuracy of such a setup. Used an arduino DUE to simulate the HC-SR04 with a precise 10ms 'simulated echo' pulse. And made 940 measurements with python. Disappointing results, see http://heppg.de/ikg/wordpress/?p=408.
My approach was to use an atmel328 as a slave processor as proposed in the article, providing reliable results. There are other solutions with the cost of extra software needed.
Regards,
Gerhard