Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

distance calculating in Ultra-sonic ranger , please help !

datasheet information :

The SRF04 provides an echopulse proportional to distance. If the width of the pulse is measured in uS,then dividing by 58 will give you the distance in cm, or dividing by 148 willgive the distance in inches. uS/58=cm or uS/148=inches.

I wrote a VHDL program which i configured a clock counter for counting the clock cycles when echo pin is high.

my clock frequency is 50MHz. - it means the period time is : 20nSec or 0.02uSec .

in my opinion, for claculating the distance in cm , i have to do this calculation(theoreticly) : distance <= (0.02/58)*echo_counter ;

but - i want a synthesisable program

any one have an idea ?

thank you for help...

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You will probably want to find your distance in 10s of mm so your math is much easier (no need for fixed point math). You will end up with distance <= (echo_counter/29);

    At this point, you will probably want to write a pipelined division routine to get you your result. Just keep working at it, you'll get there. Good luck.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    You will end up with distance <= (echo_counter/29);

    --- Quote End ---

    Scaling the output to a fixed point number with higher resolution than whole mm seems reasonable. If you want to use decimal (0.1, 0.01) or binary scaling (2^-N) factors depends on the application. Dividing by an integer constant is the exact solution, but usually not the preferred method. You can save a lot of FPGA resources by replacing the division with a multiply and binary shift. 1/29 is e.g. very near to 565/2^14.