User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

What if sonar beam doesn't come back

Sat Nov 30, 2013 2:27 pm

I have made a raspberry pi car that uses sonar drive about autonomously. It repeatedly measures the distance between the first object it'll encounter when going straight. If the distance is more than 30cm, it'll continue going straight, if not, it'll spin on the spot. I will be demo-ing it at the Cambridge Raspberry Jam in a week, and today discovered it has a slight glitch. When the sonar beam for whatever reason does not come back, or the Pi fails to detect it, it gets stuck doing whatever it was doing at the time. Can anyone modify my code so that if the beam is taking a long time, it continues ignoring that measurement? Thanks.

My code is in this powerpoint on the 5th and 6th slides: https://drive.google.com/file/d/0B8pG7p ... sp=sharing
Don't be mean, I'm only thirteen 8-)

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 3:05 pm

From what I can gather looking at the code, you have a while loop that waits while the return is 0, setting the start time, and then while the return is 1 setting the stop time. You should add a condition to the start time loop that checks to see how long it has been since the waiting started, and stop if it doesn't find it.

In pseudo code:

time=0
while gpio==0 and time<x:
set start time
update time

Hope this give you the right idea on where to go.

Rick
There are 10 types of people in this world. Those that understand binary, and those that don't.

User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 3:41 pm

rickseiden wrote:From what I can gather looking at the code, you have a while loop that waits while the return is 0, setting the start time, and then while the return is 1 setting the stop time. You should add a condition to the start time loop that checks to see how long it has been since the waiting started, and stop if it doesn't find it.

In pseudo code:

time=0
while gpio==0 and time<x:
set start time
update time

Hope this give you the right idea on where to go.

Rick
Thant's the one. Mind went blank for a moment, thanks for reminding me. So it would be:

Code: Select all

start=time.time()
while GPIO.input(GPIO_ECHO)=0 and time.time()-start<0.1:
    start=time.time()
while GPIO.input(GPIO_ECHO)=1:
    stop=time.time()
Is that right? Should I also add 'and time.time()-start<0.1' to the other while statement? Have I added it to the wrong while statement?

Thanks again!
Don't be mean, I'm only thirteen 8-)

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 3:54 pm

I think you've got it just the way it is. I'm thinking that you won't need the timeout on the second while statement, because if the first one does time out, then GPIO.input(GPIO_ECHO) will still be 0 and the second while loop will just be skipped. You might need to add more code to it to handle that situation, btw.

Rick
There are 10 types of people in this world. Those that understand binary, and those that don't.

User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 4:15 pm

rickseiden wrote:I think you've got it just the way it is. I'm thinking that you won't need the timeout on the second while statement, because if the first one does time out, then GPIO.input(GPIO_ECHO) will still be 0 and the second while loop will just be skipped. You might need to add more code to it to handle that situation, btw.

Rick
Doubt it, the module can't just keep the echo high! Thanks for the help. Love your statement about the 10 types of people in the world!
Don't be mean, I'm only thirteen 8-)

User avatar
mikronauts
Posts: 2740
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 4:25 pm

It should work fine the way it is. The ultrasonic sensors "give up" when they don't get a response within an expected time (the return time for sound from their maximum distance, value depends on sensor).

I did something similar with a servo based Pi robot some time ago, so I know it will work for you.
ZacharyI123 wrote:I have made a raspberry pi car that uses sonar drive about autonomously. It repeatedly measures the distance between the first object it'll encounter when going straight. If the distance is more than 30cm, it'll continue going straight, if not, it'll spin on the spot. I will be demo-ing it at the Cambridge Raspberry Jam in a week, and today discovered it has a slight glitch. When the sonar beam for whatever reason does not come back, or the Pi fails to detect it, it gets stuck doing whatever it was doing at the time. Can anyone modify my code so that if the beam is taking a long time, it continues ignoring that measurement? Thanks.

My code is in this powerpoint on the 5th and 6th slides: https://drive.google.com/file/d/0B8pG7p ... sp=sharing
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 4:41 pm

mikronauts wrote:It should work fine the way it is. The ultrasonic sensors "give up" when they don't get a response within an expected time (the return time for sound from their maximum distance, value depends on sensor).

I did something similar with a servo based Pi robot some time ago, so I know it will work for you.
ZacharyI123 wrote:I have made a raspberry pi car that uses sonar drive about autonomously. It repeatedly measures the distance between the first object it'll encounter when going straight. If the distance is more than 30cm, it'll continue going straight, if not, it'll spin on the spot. I will be demo-ing it at the Cambridge Raspberry Jam in a week, and today discovered it has a slight glitch. When the sonar beam for whatever reason does not come back, or the Pi fails to detect it, it gets stuck doing whatever it was doing at the time. Can anyone modify my code so that if the beam is taking a long time, it continues ignoring that measurement? Thanks.

My code is in this powerpoint on the 5th and 6th slides: https://drive.google.com/file/d/0B8pG7p ... sp=sharing
The code is wrong. It keeps resetting the start time value so it'll never be 0.1 seconds away from the current time. Silly me! Instead it should be:

Code: Select all

start=time.time()
count=time.time()
while GPIO.input(GPIO_ECHO)=0 and time.time()-count<0.1:
    start=time.time()
while GPIO.input(GPIO_ECHO)=1:
    stop=time.time()
Don't be mean, I'm only thirteen 8-)

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 4:44 pm

ZacharyI123 wrote:The code is wrong. It keeps resetting the start time value so it'll never be 0.1 seconds away from the current time. Silly me! Instead it should be:

Code: Select all

start=time.time()
count=time.time()
while GPIO.input(GPIO_ECHO)=0 and time.time()-count<0.1:
    start=time.time()
while GPIO.input(GPIO_ECHO)=1:
    stop=time.time()
That's what happens when a the guy you're talking to in the forums doesn't look too hard at what you've posted. :o Sorry about that.

PS: Take it easy on me, I'm only 44.

PPS: Thanks for the like on my signature.
There are 10 types of people in this world. Those that understand binary, and those that don't.

User avatar
Burngate
Posts: 6182
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 4:59 pm

rickseiden wrote:... PS: Take it easy on me, I'm only 44.

PPS: Thanks for the like on my signature.
There are 10 types of people in this world. Those that understand binary, and those that don't.
I've never understood that. After all, in base-7-billion-plus, there's less than 10 people in total?
But then, I'm only 64

neilf
Posts: 72
Joined: Sun Nov 11, 2012 8:14 am

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 8:09 pm

Burngate wrote:
rickseiden wrote:... PS: Take it easy on me, I'm only 44.

PPS: Thanks for the like on my signature.
There are 10 types of people in this world. Those that understand binary, and those that don't.
I've never understood that. After all, in base-7-billion-plus, there's less than 10 people in total?
But then, I'm only 64
I took it as: binary 10 = 2 decimal. Ergo, there are 2 types of people in this world ....

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 8:39 pm

neilf wrote: I took it as: binary 10 = 2 decimal. Ergo, there are 2 types of people in this world ....
That's exactly it. :-)
There are 10 types of people in this world. Those that understand binary, and those that don't.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: What if sonar beam doesn't come back

Sat Nov 30, 2013 11:00 pm

This is the code I use in my Scratch handler
it assumes the ultrasonic is wired up for single pin mode
it takes 3 readings and then returns the middle value as a simple way of trying not to return a daft result :)

Code: Select all

    def pinSonar(self, pin):
        self.pinUse[pin] = self.PSONAR
        GPIO.setup(pin,GPIO.OUT)
        ti = time.time()
        # setup a list to hold 3 values and then do 3 distance calcs and store them
        #print 'sonar started'
        distlist = [0.0,0.0,0.0]
        ts=time.time()
        for k in range(3):
            #print "sonar pulse" , k
            GPIO.output(pin, 1)    # Send Pulse high
            time.sleep(0.00001)     #  wait
            GPIO.output(pin, 0)  #  bring it back low - pulse over.
            t0=time.time() # remember current time
            GPIO.setup(pin,GPIO.IN)
            #PIN_USE[i] = PINPUT don't bother telling system
            
            t1=t0
            # This while loop waits for input pin (7) to be low but with a 0.04sec timeout 
            while ((GPIO.input(pin)==0) and ((t1-t0) < 0.02)):
                #time.sleep(0.00001)
                t1=time.time()
            t1=time.time()
            #print 'low' , (t1-t0).microseconds
            t2=t1
            #  This while loops waits for input pin to go high to indicate pulse detection
            #  with 0.04 sec timeout
            while ((GPIO.input(pin)==1) and ((t2-t1) < 0.02)):
                #time.sleep(0.00001)
                t2=time.time()
            t2=time.time()
            #print 'high' , (t2-t1).microseconds
            t3=(t2-t1)  # t2 contains time taken for pulse to return
            #print "total time " , t3
            distance=t3*343/2*100  # calc distance in cm
            distlist[k]=distance
            #print distance
            GPIO.setup(pin,GPIO.OUT)
        tf = time.time() - ts
        distance = sorted(distlist)[1] # sort the list and pick middle value as best distance
        
        #print "total time " , tf
        #for k in range(5):
            #print distlist[k]
        #print "pulse time" , distance*58
        #print "total time in microsecs" , (tf-ti).microseconds                    
        # only update Scratch values if distance is < 500cm
        if (distance > 280):
            distance = 299
        if (distance < 2):
            distance = 1

        return distance
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

Re: What if sonar beam doesn't come back

Sun Dec 01, 2013 7:49 am

simplesi wrote:This is the code I use in my Scratch handler
it assumes the ultrasonic is wired up for single pin mode
it takes 3 readings and then returns the middle value as a simple way of trying not to return a daft result :)

Code: Select all

    def pinSonar(self, pin):
        self.pinUse[pin] = self.PSONAR
        GPIO.setup(pin,GPIO.OUT)
        ti = time.time()
        # setup a list to hold 3 values and then do 3 distance calcs and store them
        #print 'sonar started'
        distlist = [0.0,0.0,0.0]
        ts=time.time()
        for k in range(3):
            #print "sonar pulse" , k
            GPIO.output(pin, 1)    # Send Pulse high
            time.sleep(0.00001)     #  wait
            GPIO.output(pin, 0)  #  bring it back low - pulse over.
            t0=time.time() # remember current time
            GPIO.setup(pin,GPIO.IN)
            #PIN_USE[i] = PINPUT don't bother telling system
            
            t1=t0
            # This while loop waits for input pin (7) to be low but with a 0.04sec timeout 
            while ((GPIO.input(pin)==0) and ((t1-t0) < 0.02)):
                #time.sleep(0.00001)
                t1=time.time()
            t1=time.time()
            #print 'low' , (t1-t0).microseconds
            t2=t1
            #  This while loops waits for input pin to go high to indicate pulse detection
            #  with 0.04 sec timeout
            while ((GPIO.input(pin)==1) and ((t2-t1) < 0.02)):
                #time.sleep(0.00001)
                t2=time.time()
            t2=time.time()
            #print 'high' , (t2-t1).microseconds
            t3=(t2-t1)  # t2 contains time taken for pulse to return
            #print "total time " , t3
            distance=t3*343/2*100  # calc distance in cm
            distlist[k]=distance
            #print distance
            GPIO.setup(pin,GPIO.OUT)
        tf = time.time() - ts
        distance = sorted(distlist)[1] # sort the list and pick middle value as best distance
        
        #print "total time " , tf
        #for k in range(5):
            #print distlist[k]
        #print "pulse time" , distance*58
        #print "total time in microsecs" , (tf-ti).microseconds                    
        # only update Scratch values if distance is < 500cm
        if (distance > 280):
            distance = 299
        if (distance < 2):
            distance = 1

        return distance
Thanks! Basically the same as my code but with a shorter time out. Sound travels at 300 m/s. Your code waits only a fiftieth of a second for the pulse to come back so that means the pulse can only travel 6m. The pulse has to travel there and back so the maximum distance that you will measure to the nearest object (not including the way back) is 3m. I think that's not very much (considering my sensor goes up to 500m) so I will probably stick my my current value.
Don't be mean, I'm only thirteen 8-)

User avatar
Ferdinand
Posts: 236
Joined: Sun Dec 01, 2013 2:24 pm
Location: Leiderdorp, NL

Re: What if sonar beam doesn't come back

Sun Dec 01, 2013 2:45 pm

The code has been a little bit modified. I placed comments near the important statements. To speed up the while True routine remove the comments and / or move it to the start of while True routine because Python is an interpreter!

Code: Select all

while True:
   time.sleep(0.1)
   #Send pulse to trigger
   GPIO.output(GPIO_TRIGGER, True)
   time.sleep(0.00001)
   GPIO.output(GPIO_TRIGGER, False)
#==>Remove this line -- start = time.time()

   #The echo line is still low

   while GPIO.input(GPIO_ECHO)==0:
      pass
      #looping while the echo line is low, see specs of the ultrasonic device

   #Echo line is high now so start timer

   start = time.time()

   while GPIO.input(GPIO_ECHO)==1:
      pass
      #looping while the echo line is high, see specs of the ultrasonic device
   
   #Echo line is low now so stop timer
   
   stop = time.time()
   
   #Workout pulse length
   #pulse_length = stop - start
   #replace pulse_length by elapsed
   elapsed = stop - start
   #Work out distance pulse travelled
   #Distance in cm = speed in cm (speed of sound) * time
   distance = elapsed * 34000

   #Half the value to work out the distance one way
   distance = distance / 2

   #Important
   #remove comments to speed up this while loop because Python is an interpreter

   # ...

Good luck with your car project!
Ferdinand
Success with your project!
Ferdinand

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: What if sonar beam doesn't come back

Sun Dec 01, 2013 3:26 pm

If we are talking about the little £5 ultrasonics then they are basically rated at 5 metres max. I don't think there is anything around that can bounce a sound beam of a rock face 500m away and pick up the echo reliably :)

The reason for the timeout is so that the code doesn't sit there blocking the next thing from running, waiting for a sound that's not coming back :)

If your sensor is of the 5m variety and you want to use the full range, then make your timeout stop after 0.034 secs

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

Re: What if sonar beam doesn't come back

Sun Dec 01, 2013 4:02 pm

simplesi wrote:If we are talking about the little £5 ultrasonics then they are basically rated at 5 metres max. I don't think there is anything around that can bounce a sound beam of a rock face 500m away and pick up the echo reliably :)

The reason for the timeout is so that the code doesn't sit there blocking the next thing from running, waiting for a sound that's not coming back :)

If your sensor is of the 5m variety and you want to use the full range, then make your timeout stop after 0.034 secs

Simon
I meant cm so yes it is 5m. But his limit was 0.02 which only measures up to 3m.
Don't be mean, I'm only thirteen 8-)

User avatar
aTao
Posts: 1087
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: What if sonar beam doesn't come back

Sun Dec 01, 2013 4:25 pm

What if sonar beam doesn't come back
Call it a stick.
>)))'><'(((<

User avatar
Burngate
Posts: 6182
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: What if sonar beam doesn't come back

Sun Dec 01, 2013 4:31 pm

aTao wrote:
What if sonar beam doesn't come back
Call it a stick.
I tried that; also a useless wazock and other names. It still wouldn't come.
Eventually found it at the back of the wardrobe. It had made a nest. Now we've got lots of little sonar beams running round.

User avatar
ZacharyI123
Posts: 263
Joined: Sun Jun 17, 2012 1:09 pm
Location: Ilford, Essex
Contact: Website

Re: What if sonar beam doesn't come back

Sun Dec 01, 2013 8:04 pm

Burngate wrote:
aTao wrote:
What if sonar beam doesn't come back
Call it a stick.
I tried that; also a useless wazock and other names. It still wouldn't come.
Eventually found it at the back of the wardrobe. It had made a nest. Now we've got lots of little sonar beams running round.
Now you probably get very weird readings from your modules.
Don't be mean, I'm only thirteen 8-)

Return to “Automation, sensing and robotics”