Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: dsheller on June 16, 2008, 01:33:17 PM

Title: PING))) Ultrasonic Question
Post by: dsheller on June 16, 2008, 01:33:17 PM
I bought this the other day, and have got it to work by doing the following:

send trigger pulse, wait for the echo holdoff, count until the pin i have goes high, then count how long it stays high

So this is working, as I am getting data which is distance dependent sent back to me over the UART. I am not sure if it is accurate yet, but I am about to try to use the following equation to convert it to cm.
cycles_high * 1/2 * ( 1s / 20 000 000 cycles ) * 340m / s

So my real question is... how hard is it to use ICP to get this to work, I'm using an atmega168. I should also note I would probably be using the other timers for driving my motors with PWM.


Title: Re: PING))) Ultrasonic Question
Post by: ceruleanplains on June 17, 2008, 10:03:28 AM
i use the PING myself.  first... does your MCU (microcontroller unit) have a clock on it? it's helpful to know what MCU you're using.  I have both the Basic STAMP 2 (BS2) and the Atmel 168 on the Arduinio Diec. board.  Otherwise your cycles ... if I interpret your paragraph...  could just be number of loops?

Title: Re: PING))) Ultrasonic Question
Post by: dsheller on June 17, 2008, 11:16:56 AM
My MCU is using an external 20 MHz clock.. and yes you are correct that my cycles are just the number of increments in a loop. I'm assuming I have to count in the comparing in the while statement as well as the incrememnt ... here is some code


while ( PINC & 1 == 1 ) {
      cycles_high++;
}


However, I think I should move to the interrupt driven based system, as it will probably end up being a better solution. If anyone has some advice that'd be great, if not I'm going to give it a shot myself.
Title: Re: PING))) Ultrasonic Question
Post by: krich on June 17, 2008, 11:57:37 AM
There's a really, really good tutorial on AVR Freaks (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=328546) that goes through using the counter, counting overflows via interrupt, etc.  I actually printed it out so that I can have it handy any time I need a refresher.  Good stuff.

The counter is only half your solution though.  You'll need to start it when the pin goes high and stop it when the pin goes low.  More interrupts, I guess, but shouldn't be terribly difficult.  I haven't gotten that far in my AVR experimentation, so I'm not much help there.
Title: Re: PING))) Ultrasonic Question
Post by: dsheller on June 17, 2008, 12:50:53 PM
Well I am trying to use the ICP feature of my MCU, and have found a weird issue... here is what i've done for testing:

I generated a 5 uS pulse train and examined it on my scope, worked out fine, timing was no issue as the pulses were exactly 5uS.  I fed them to the signal pin on the ultrasonic board, I could see the activity light, and could hear a faint noise -- why I can hear that frequency I don't know... so knowing that I then piped the signal line into PINB0. As soon as you connect the signal to PINB0 the PIND4 ( port i was generating the signal train on) goes high and stays high even after disconnecting PINB0. Not sure what is going on here, but it sounds like I didn't set a bit or something in a control register... or something equally stupid on my part.

Title: Re: PING))) Ultrasonic Question
Post by: dsheller on June 17, 2008, 08:03:01 PM
Wooo! Got it working with the ICP... does anyone know what kind of accuracy this thing has? I tried 18 different readings and got 15 / 18 within measurement error (my tape measure)... the other 3 were off by about 19%.  I know I could just take multiple readings, but was curious if anyone has any data / tricks to use this guy... ah well -- off to get the Sharp IR working!