Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: Steel_monkey on September 13, 2008, 03:19:04 PM

Title: Short story about sonar
Post by: Steel_monkey on September 13, 2008, 03:19:04 PM
This winter, I decided to make ultrasonic rangefinder (later UR). Conventional rangefinders, like MAXsonar and not bad and even not very overpriced, but they are sold online (I have no credit card), and delivery would have doubled the price. The idea was making cheap and “smart” UR using ADC, not just comparator. I bought Murata MA40B8R and MA40B8S receiver transmitter pair http://pdf1.alldatasheet.com/datasheet-pdf/view/116599/MURATA/MA40E8-2.html (http://pdf1.alldatasheet.com/datasheet-pdf/view/116599/MURATA/MA40E8-2.html)  (~4$ each). In Mouse the junkbot project I found LM386 sound amplifier. It was cheap, had good gain (200) and suitable bandwidth. For transmitter, conventional MAX232 level converter was used to get higher voltage from common 5 Volts.
(http://www.roboforum.ru/download/file.php?id=4728&mode=view)
But performance was poor, especially measurable distance.
(http://www.roboforum.ru/download/file.php?id=4729&t=1)
Here is the image of wall 2 meters away (up), and sent pulse (down). As you can see, it is weak, too weak to be a wall J. I got stuck for some time, and replaced LM386 with two opamps (TLV272) in conventional single-supply position. The answer was Lm requires higher supply voltage, and saturates with 3 Volts on output.

After that, voltage limiter on Schottkey diodes was added to enlarge dynamic range. High voltages would be dropped on resistor, while small on diodes only. This will allow using higher gain in next stage. However, in reality, this not works – gain in the first stage is too low. Then, conventional rectifier was added. After that, I tried ADC sampling. 12 MHz crystal allowed me 25 and 70 KHz sample rate. Both are too low to sense 40 KHz, so I added analog filter from two RC stages. It is possible to use active filters, but I don’t like them, and passive woks great.  Analog path ends with buffer (AD820) and go to ADC.
/ There is divider after rectifier – it is used not to saturate poor LM358. It was the only thing I had that time. In etched board I will use quad rail-to-rail opamp/
(http://www.roboforum.ru/download/file.php?id=5837&mode=view)
The next step was receiving of sampled signal. To do that, RS232 was used with 57,6 Kbps speed. As ADC sample rate was 25 KHz, it was impossible to send sampled byte each time between measurements (it requires nearly 250 Kbps, but my PC failed to support it). So I just made buffer for 900 bytes in RAM and sent it whole after it is full. This allowed scanning nearly 6 meters - more than enough.
Now, I am working on signal processing. 25 KHz sample rate means 480 crystal clocks between samples. This allow to process echo(es) in real time, but suitable algorithm required. I try to use simple IF (previous lesser then current) AND (next lesser then current) statements, this helps, but not very much. Depending of distance and target reflectivity and size, echo signal from one object can be very sharp, but smooth from another. I tried to use differences, but it doesn’t work at all. So, if anybody knows better way of maximum detection, help is very appreciated.
(http://www.roboforum.ru/download/file.php?id=5871&mode=view)
Here is the signal of the second amplifier stage recorded with soundcard. Sample rate is 96 KHz sow and this allow CoolEdit to reconstruct 40KHz. First left maximum is direct coupled signal. Receiver is tight together with transmitter, and receiver catch signal during and after transmitter oscillation. Next peak is a side of the bed, 1.5 meters away from UR, and much lower. Next is a pillow (not of winds  ;D , but main echoes are close ;) ), standing straight on bed, 1.9 meters away from sonar. The last peak is a Wall 2.7 meters away. I fail to explain received signal after that point, and interpret it as noise.
(http://www.roboforum.ru/download/file.php?id=5872&t=1)
Here is sampled signal. Same thee echoes. Looks good, and SNR is higher than I expected. Pink graph relates to my peak detection algorithm processing. As you can see, small oscillations are also told to be maximums. Now I am working on next stage of processing.

Other problem is small distance detection. In that situation, direct coupled signal mixes with reflected signal. Minimum range with detectors tight together is more than 15 cm. Perhaps, with transmitter and receiver taken further (4-5 cm like on conventional URs ), this problem could be minimized. I will do so on etched board.   
(http://www.roboforum.ru/download/file.php?id=5839&t=1)
In this measurement, objet is moving towards the UR. As you can see, peaks are getting closer.
(http://www.roboforum.ru/download/file.php?id=5840&mode=view)
Object is 10 cm. away from UR. No detecion. One intensive peak is just second reflection, not primary echo.

In attachment you can see sample signal from bed, pillow and wall with higher resolution.
Second is high resolution version of obstacle moving towards UR. Sharp single peaks before signals mean new measurement cycle started, they are not related with signal itself.
Title: Re: Short story about sonar
Post by: Admin on September 16, 2008, 10:45:22 AM
Quote
but suitable algorithm required. I try to use simple IF (previous lesser then current) AND (next lesser then current) statements, this helps, but not very much. Depending of distance and target reflectivity and size, echo signal from one object can be very sharp, but smooth from another. I tried to use differences, but it doesn’t work at all. So, if anybody knows better way of maximum detection, help is very appreciated.

I don't quite understand the question. You mean detecting a signal vs. noise? The signal amplitude is proportional to the detected object distance.

If you want more 'intelligent' detection, you'd have to use multiple sonar at the same time. Or put your sonar on a scanning servo or something . . .
Title: Re: Short story about sonar
Post by: Steel_monkey on September 18, 2008, 11:05:44 AM
When MCU samples signal, it should be able to determine where signal has local maxima. Do determite this, I use simple expression ( example for sample n(i) with number i )
IF ( [n(i-1)<=n(i)] AND [n(i+1)<=n(i)]) THEN n(i) is maximum and it corresponds to detected object. But this algorithm is too sensitive, shows any 3 points where middle sample is higher than side ones. Then, I tried to add more samples to compare : n(i-2) and n(i+2). This rejected some insufficient maxima. Adding more samples in compare statement sometimes rejects maxima of interest. So, I try to find another algorithm of peak finding, based on derivative sign change, but it doesn't seem to work properly in my implementation.
Title: Re: Short story about sonar
Post by: Admin on September 18, 2008, 12:33:58 PM
This is how I'd do it:

Code: [Select]
//command sonar
sonar_burst();

//read sonar return really fast, store only highest detected value
while(timer<time_between_bursts)
    {
    if (ADC_sonar > maximum)
        {
        maximum = ADC_sonar;//store max value
        max_time = get_timer();//get time at max value
        }

    timer=get_timer();
    }

//use maximum as the max value here for your algorithm
do_stuff();

maximum=0;//reset max
Title: Re: Short story about sonar
Post by: Steel_monkey on September 18, 2008, 01:01:05 PM
Thanks a lot. It is interesting firt determine biggest value of all, and then choose whether other values are important, or not. I'll add this algorithm and also try somethink like dynamik threshhold.