Reputation: 5
I made this simple robot using LEGO and it uses a Raspberry Pi as the computer. The code I wrote was in Python and basically what it does is use the ultrasonic sensor to to measure distance. Here is the code:
import RPi.GPIO as g
import time as t
g.setmode(g.BCM)
g.setwarnings(False)
# trig is the pin on the sensor which will emit a very fast pulse
trig = 21
# echo is the pin which will recieve the pulse from the trig
echo = 20
g.setup(trig, g.OUT)
g.setup(echo, g.IN)
def distance(dur):
global dis
start = 0
end = 0
g.output(trig, False)
t.sleep(0.01)
g.output(trig, True)
t.sleep(0.00001)
g.output(False)
while g.input(echo) == 0:
start = t.time()
while g.input(echo) == 1:
start = t.time()
duration = end - start
dis = duration * 17150
dis = round(dis,2)
print "Distance: " + dis
t.sleep(dur)
while True:
# so the function is being called, and the time between outputs is 0.01 seconds so it is very
# fast and quickly showing on the screen. If the distance is less than 5, then the program
# will print out "Hi" to show that. s
distance(0.01)
if dis < 5:
print "Hi"
Pretty straightforward right? But you see, the code executes flawlessly, it shows the distance and when I put my hand near the sensor and the variable dis becomes less than 5, the program prints out "Hi"... UNTIL THIS:
Ultrasonic Sensor Distance Output Picture. You can see that the output stream just stalls. It literally stops and that is it. No error message, nothing. And the worst part about it is that it does this randomly. It could stall when it is just printing out the distance, it can stall when it is printing "Hi" but I did notice that it stalled a lot more frequently when it was printing "Hi" and it stalls after a random number of outputs. So the next thing I do is press ctrl+c to stop the program and this is what it looks like. I also forgot to mention that three ultrasonic sensors are hooked together as one and uses just GPIO 21 and GPIO 20. It still works though and even when they had their own separate pairs of pins they still had the same stalling problem so it does not make a difference.
If anyone has an idea as to what is causing this, I would be so happy because I spend hours trying to fix it.
Upvotes: 0
Views: 1549
Reputation: 1
The problem is with your while loops. You check your cpu activity when your program suddenly paused. It must have gone to 100 percent suddenly. The reason for this is one of your while loop has become infinite loop as the condition you have given is not achieved, that is g.input(echo)=0 and g.input(echo)=1. This may happen due to wrong data send by the sensore may be due to some environment change or voltage fluctuations.
You have to add one condition in your while loops that if condition is not achieved then it should break the loop and start the whole process again otherwise your program will be trapped in a never ending while loop.
Also you should not put sleep command inside your while loop as it will change the time of echo signal received and gives you false distance value.
Upvotes: 0
Reputation: 46
Short Answer: Add a sleep of .01 in your while loop after the function The problem is the Pi is going faster than the sensor, causing no errors, just a time out.
Like what Tammo Heeren said, add a sleep. Ultra-Sonic sensors can only get and send data so fast (the speed of sound approximately) but the Pi can compute maybe even just an astronomically small amount faster, but it will be enough because the Pi and the sensor will be out of sync. The difference at the beginning is tiny, but gets bigger as time goes on. Your program stops randomly probably because the CPU isn't up to speed (a.k.a. lag) and just takes a little longer to quit.
Upvotes: 1