Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

Sonar Range Finder in Visual Basic .Net


  • Please log in to reply
23 replies to this topic

#1 devanslj

devanslj
  • Members
  • 9 posts

Posted 14 April 2004 - 09:28 AM

I'm trying to interface a Devantech SRF04 Sonar Range Finder(http://www.acroname..../R93-SRF04.html) to my PC (for a robotics project) though the LabJack.

I'm using Visual Basic .Net as the programming language, but I have not had any success...

Some quick background on the sensor: "You only need to supply a short 10uS pulse to the trigger input to start the ranging. The SRF04 will send out an 8 cycle burst of ultrasound at 40khz and raise its echo line high. It then listens for an echo, and as soon as it detects one it lowers the echo line again. The echo line is therefore a pulse whose width is proportional to the distance to the object. By timing the pulse it is possible to calculate the range in inches/centimeters or anything else. If nothing is detected then the SRF04 will lower its echo line anyway after about 36mS."

I'm comfortable writing the VB code, but being fairly new to using the LabJack I'm not aware of how to provid the necessary pulses, etc.

A Basic Stamp program and details can be founf here:
http://www.acroname..../R93-SRF04p.pdf

Any assistance or pointers would be great appreciated.

Thanks. Dave...

#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 14 April 2004 - 10:33 AM

I think you will just use AIBurst (there is a VB6 example on the VB forum). Is there a maximum pulse width for the trigger? Does the trigger occur on the rising or falling edge of the pulse?

#3 devanslj

devanslj
  • Members
  • 9 posts

Posted 14 April 2004 - 12:13 PM

Thanks - I'll look at AIBurst, and the VB example. Re: the pulse, etc. here's what it says: "Supply a short 10uS pulse to the trigger input to start the ranging. The SRF04 will send out an 8 cycle burst of ultrasound at 40khz and raise its echo line high. It then listens for an echo, and as soon as it detects one it lowers the echo line again. The echo line is therefore a pulse whose width is proportional to the distance to the object. By timing the pulse it is possible to calculate the range in inches/centimeters or anything else. If nothing is detected then the SRF04 will lower its echo line anyway after about 36mS." I've also attached a diagram. Thanks, Dave...

Attached Images

  • timing.gif


#4 devanslj

devanslj
  • Members
  • 9 posts

Posted 14 April 2004 - 12:16 PM

I'm also thinking that the LJ may be too slow? AIBurst for example, executes in miliseconds, the sensor operates in micoseconds... any thoughts?

#5 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 14 April 2004 - 12:22 PM

It looks like it triggers on the falling edge (maybe). In this case you would set IO0 high using EDigitalOut, and then have AIBurst set IO0 low to start the sonar. You can get 1/8192 = 122 us time resolution with burst mode on the U12. Not sure what the time resolution is of the sonar, but you can figure out how much distance 122 us equals.

#6 devanslj

devanslj
  • Members
  • 9 posts

Posted 14 April 2004 - 01:36 PM

Great - thanks - I'l try that out tonight...

#7 devanslj

devanslj
  • Members
  • 9 posts

Posted 15 April 2004 - 08:46 AM

The following code now gets the sonar to emit :) (I can actually hear a barely audible chirp coming the sonar). If I connect an LED in series to the sonar I can see if dim and glow as I move my hand closer or further away - so, so far so good -the sonar can be controlled quite successfully under the LabJack. Here's my question though... I'm using AIBurstX but it's not clear to me how I do something meaningful with the string buffer that gets filled. In my code below, the variable voltsA is a string that I'm assuming is filled with voltage information? (The VB paramaters for this call are a little different that what is shown in the documentation, so I may simply be confused). In the VB call it's a string, in the documentation it says: *voltages ? Pointer to a 4096 by 4 array where voltage readings are returned. Send filled with zeros. What I think I need to do is return the minimum and maximum voltage from this variable so I can tell if an object is moving closer or farthe away from the sonar? Any pointers on how to pull data from this variable? (Code snippet below) 'I have a LED and a Sonar connected. The LED is so I can see visually what's going on - hence the two lines for each state change). ' Set to low to init sonar state = 0 Ljackuwx1.EDigitalOutX(idnum, demo, sonarIO, readD, state) Ljackuwx1.EDigitalOutX(idnum, demo, ledIO, readD, state) ' Set to high to initiate sonic pulse state = 1 Ljackuwx1.EDigitalOutX(idnum, demo, sonarIO, readD, state) Ljackuwx1.EDigitalOutX(idnum, demo, ledIO, readD, state) ' Set to low again as pulse is generated on the falling edge of this pulse state = 0 Ljackuwx1.EDigitalOutX(idnum, demo, sonarIO, readD, state) Ljackuwx1.EDigitalOutX(idnum, demo, ledIO, readD, state) stateIO = 0 UpdateIO = 0 NumChannels = 1 channels = 0 gains = 0 scanrate = 1 numScans = 1 DisableCal = 0 triggerIO = 1 triggerState = 0 timeout = 1 transfermode = 0 stateIOout = "" overVoltage = 0 optionBits = 0 Ljackuwx1.AIBurstX(idnum, NumChannels, channels, gains, scanrate, triggerIO, triggerState, numScans, timeout, VoltsA, stateIOout, overVoltage, optionBits)

#8 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 15 April 2004 - 08:59 AM

The OCX returns a string rather than an array, since ActiveX does not have an array type. The AIBurstX example parses this string into an array, and if you plot this array you should see the signal just like in the drawing above. If you are using VB.net, however, I think you want to use the .net drivers from our downloads page. In this case you will call AIBurst not AIBurstX, and you will get back an array.

#9 devanslj

devanslj
  • Members
  • 9 posts

Posted 16 April 2004 - 04:01 PM

Hi - do you have an example of code showing the declaration of the paramters required for the AIBurst call and the call itself? The example in the documentaion appears to be a C example? using pointers. I'm not having any luck figuring out the VB .net representation.

#10 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 16 April 2004 - 04:51 PM

Hi - do you have an example of code showing the declaration of the paramters required for the AIBurst call and the call itself? The example in the documentaion appears to be a C example? using pointers. I'm not having any luck figuring out the VB .net representation.

Here is an example that does a simple call to AIBurst using the .NET drivers and VB .NET.

Module Module1

    Public Sub Main()
        Dim lngErrorcode As Integer

        Dim lngIDNum As Integer
        Dim sngScanRate As Single
        Dim lngStateIOout(4096) As Integer
        Dim lngOverVoltage As Integer

        Dim alngChannels(4) As Integer
        Dim alngGains(4) As Integer
        Dim asngVoltages(4, 4096) As Single


        Dim cntr As Integer

        Console.WriteLine("Acquiring Data")

        For cntr = 0 To 4095
            asngVoltages(0, cntr) = 0
            asngVoltages(1, cntr) = 0
            asngVoltages(2, cntr) = 0
            asngVoltages(3, cntr) = 0
            lngStateIOout(4096) = 0
        Next cntr

        alngChannels(0) = 0
        alngChannels(1) = 1
        alngChannels(2) = 2
        alngChannels(3) = 3
        alngGains(0) = 0
        alngGains(1) = 0
        alngGains(2) = 0
        alngGains(3) = 0

        lngIDNum = -1
        sngScanRate = 2048

        lngErrorcode = lj.LabJack.AIBurst(lngIDNum, 0, 0, 0, 1, 4, alngChannels, alngGains, sngScanRate, 0, 0, 0, 1024, 10, asngVoltages, lngStateIOout, lngOverVoltage, 0)
        Console.WriteLine(Str(asngVoltages(0, 0)))
        Console.WriteLine(Str(asngVoltages(1, 0)))
        Console.WriteLine(Str(asngVoltages(2, 0)))
        Console.WriteLine(Str(asngVoltages(3, 0)))


    End Sub

End Module

You can see how each of the parameters are defined, and where they are called. In addition to this code you need to add a reference to the LJDotNet.dll that you can find on our downloads page.

#11 devanslj

devanslj
  • Members
  • 9 posts

Posted 16 April 2004 - 05:38 PM

Thank you! That helps a lot. I will give this a try...

#12 devanslj

devanslj
  • Members
  • 9 posts

Posted 24 April 2004 - 03:05 PM

Hi, Is there a way to measure the time between the trigger pulse on one IO and the Echo pulse on another IO? The length of this echo pulse is what gives me the actual distance. Done via a BASIC Stamp, it looks like this: INIT is IO pin 0 ECHO is IO pin 1 pulsout INIT,5 ' Send 10us init pulse on pin 0 pulsin ECHO,1,wDist ' Use pulsin to measure echo time, store value in wDist wDist=wDist/74 ' convert to inches The nature of the PULSIN command is; PULSIN pin, 1, var where the "1" indicates to measure the time from the next positive going transition to the subsequent negative going transition. Or, if it can't be done via the Lab Jack, is there a way to pass a value to the LabJack from a Basic Stamp? For example, I could connect my sonar to a Basic stamp, run the code above, and somehow (if possible) feed the wDist value to the LabJack, and in turn to my VB program. Any thoughts, or even VB .net code snippets would be very much appreciated. Thanks again!

#13 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 25 April 2004 - 04:20 PM

The U12 does not have a timer function, but by looking at the AIBurst data you can determine the time, whether you use an IO or AI channel. You should be able to talk to a basic stamp if you prefer. You would use the Asynch or Synch funtion of the U12.

#14 devanslj

devanslj
  • Members
  • 9 posts

Posted 25 April 2004 - 04:32 PM

Thanks very much. Any pointers on how would I determine the time using AIBurst? I thought AIBurst simply returned a number of scans? I guess I'm not clear on how time would play into it... I would prefer for simplicitys sake to just use the LJ... (vs. also a stamp). I'm hoping somehow I can get this sonar think to work... Have you heard of anyone trying to interface a sonar to a LJ? I'm even open to using a different sonar if it's easier to interface... Thanks!

#15 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 25 April 2004 - 07:00 PM

A few other customers have asked about this same thing. I believe they managed to get it going fine with AIBurst. AIBurst collects data at a certain rate. This is a hardware timed acquisition based on the U12's crystal, so the timing is quite accurate. If you collect 1 channel of data at 8192 scans/second, you know that the samples are spaced by 1/8192 or 122 us. Connect your sonar signal to AI0, and start a 1024 sample burst at 8192 scans/second. When you call AIBurst, you tell it to set IO0 low and thus the sonar is started at the same time as the data acquisition. When the data is returned, you look at the analog data and find the falling edge which is the end of the echo pulse.

#16 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 25 April 2004 - 07:16 PM

I just ordered a sonar and compass. We will try it ourselves when they arrive.

#17 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 13 July 2004 - 11:22 AM

Here is a small, simple example for the sonar ranger in VB.net.

Attached Files



#18 benshwap

benshwap
  • Members
  • 3 posts

Posted 29 October 2004 - 11:50 AM

I am trying to use the same ultrasonic range finder with my lab jack using visual basic 6. Im a little unsure how the program would look and the sample code that you have provided is for .net only. Could you make a visual basic version of your sample code? thank you

#19 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 29 October 2004 - 01:04 PM

Look at the 7th post in this topic. The user gives an example of what to do. Also check out the AIBurstX example on the forum which shows how to parse the burst data into an array:

https://forums.labja...p?showtopic=331

#20 benshwap

benshwap
  • Members
  • 3 posts

Posted 31 October 2004 - 09:22 PM

Using the previous posts i have been able to get the ultrasonic sensor to emit the pulse. I have a few questions about the collection of the echo. I was wondering if i would be able to use the timer in visual basic to time the difference between the emitted sound and the echo instead of using the AIburst? I am trying to use the labjack/pc as the controller for a robot so i need to have the program detect the distance automaticly instead of looking at the outputs from AIburst. Thanks for your help

#21 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 01 November 2004 - 10:06 AM

If you look towards the beginning of these topic you will see that burst mode is needed to get decent time resolution. If you just poll the signal with EDigitalIn and use a software timer in VB, you will only get maybe 20 ms resolution.

http://www.labjack.c...2datarates.html


I am trying to use the labjack/pc as the controller for a robot so i need to have the program detect the distance automaticly instead of looking at the outputs from AIburst.


You mean you need more of a real-time response. I think burst is still your best option. You can just shorten the length of the burst to reduce your latency as small as possible.

#22 benshwap

benshwap
  • Members
  • 3 posts

Posted 03 November 2004 - 12:05 PM

When you use AIburst you have to look at the results from AIburst to get the distance correct? what i would like to do is have the program be able to get the time and then use the data to make a decission in controling the robot. I was thinking of using a OOpic microcontroller board for the sensor and use the synch function to get the distance from the OOPIC into my VB program. I'm a little confused how the synch function works...what type of data can the labjack reveive using the synch function? would i be able to send the labjack distance data?

#23 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 03 November 2004 - 12:15 PM

Yes. Whatever method you choose, you make some sort of measurement, calculate the distance, and then do something based on that distance. The Synch function just sends and received bytes. The meaning of the bytes is up to you. You would program the PIC to measure the signal, calculate the distance, then return the distance to the U12 as bytes using a format you determine.

#24 borisone

borisone
  • Members
  • 1 posts

Posted 05 November 2004 - 10:29 AM

Regarding microcontroller interfacing with this board, if you do a web search for 'PROTON+ Experimenters Notebook' you will find an interesting document (about 768Kb pdf file) regarding interfacing the sonar board towards the end (page 16 onwards). Hope this is useful Cheers Steve


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users