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

How to count pulses from sensor?

MatLab LabJack U3-HV Pasco Rotary Motion Sensor Count Pulses

  • Please log in to reply
15 replies to this topic

#1 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 11 April 2014 - 10:03 AM

Hi everyone, I'm a beginner and am trying to read in the data from a Pasco Rotary Motion Sensor (http://www.pasco.com...?TechNoteID=265) into my LabJack U3-HV

 

The sensor has two outputs, it pulses to an output depending on if it detects motion in one direction or another. So for example, if the sensor is moved clockwise, one of the lines will have a pulse for a set length, and if moved counter-clockwise, the other output will receive a pulse for every set length.

 

So if I can tally the pulses, I can effectively plot the motion. The problem is I'm very new to LabJack, and I'm using MatLab.

 

Any advice, or code examples you guys can recommend? I tried looking at the U3_simplestream.m but I couldn't figure out what parts of the code were necessary, and which I can omit for my purposes. I have been able to connect to the LabJack in MatLab without issue.

 

Am I to stream the output of the sensor and try to count? Or do I specify those channels as counters and let them do the work?

 

 

Any help would be appreciated.



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 11 April 2014 - 03:33 PM

I suggest you try using the counters on the U3 to count the pulses:

 

http://labjack.com/s...users-guide/2.9

 

Start by connecting the pulse signals to FIO4 and FIO5.  Open the test panel in LJControlPanel, click Config in the Timers and Counters box, enable Counter0 and Counter1, and then click OK to go back to the main test panel.  As the pulses come in you should see the counter values increment.

 

Once you do that to confirm hardware operation, you can go to matlab and try to configure and read the counters.  Start with a timer/counter example and refer to the following:

 

http://labjack.com/s...ers-guide/4.3.6

 

http://labjack.com/s...ers-guide/4.3.9



#3 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 18 April 2014 - 02:30 PM

Thanks for the help, I was able to get the pulse counting working. Now my problem is precision at the moment. So, as I mentioned before, the project is reading a Pasco sensor via a LabJack U3-HV, and I'm using Matlab to do all of this.

 

At the moment I'm using a MatLab timer object (http://www.mathworks...imer-class.html) to get the sensor data at regular intervals in 2 of the counters on the LabJack

 

I would like to get the data at 50-2000Hz (every 0.02 to 0.0005 seconds) The problem is that the MatLab timer only has 1 millisecond precision. Do the LabJack timers offer this amount of precision, and what's the procedure for triggering a read of the counters at a certain interval.



#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 18 April 2014 - 02:47 PM

Sounds like you want to configure the counter, and then use stream mode to read the counter:

 

http://labjack.com/s...users-guide/3.2

 

That will let you get the counter reads at fast hardware timed intervals.  Read the following for a general understanding of stream mode:

 

http://labjack.com/s...3/users-guide/3

 

http://labjack.com/s...ers-guide/4.3.7

 

Realize that the clock on the U3 is RC-based and only 1.5% accurate, so that means the scan rate in stream mode will only be 1.5% accurate.  If you specify 1000 scans/second, the actual rate could be 9985 to 1015 scans/second.  To improve on that you would need to move to the U6 or T7 which have quartz based clocks.



#5 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 20 April 2014 - 03:20 AM

I think this is exactly what I'm looking for. After reading the links, I understand that I can read in the counters at a specified rate.

 

I want to then plot that data in real-time as it comes in, is there a way to trigger a function at that same rate? For example, if I want my counters to be sampled at 50z Hz, what type of code do I need to make sure it's plotting as soon as the data is sampled?



#6 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 20 April 2014 - 06:25 PM

1. I wanted to be more specific on what I was asking in the last post. For example, in the pseudocode posted, to gather the streaming data, it uses a while(!done) loop. Is this the most appropriate way?

   a. Does the while loop keep pace with the streaming data sample rate? Is there a way to only trigger the loop when a new sample is ready?

   b. Are there alternatives to doing this while loop? Any way to iterate based on sample rate. So that this chunk of code runs every time data is sampled?

 

So I could do some code like:

while(!done)

   //read data from stream

   //plot data

   if( //reached end of plotting area)
      //stop stream, set done==TRUE
   end

end

 

2. If I want my x-axis to be time, and the y-axis to be the counter data I'm streaming. Is the best way to do this is, to start a timer when I start the stream, and query the timer to see what time the data came in? Or does the stream have a property that keeps track of the elapsed time since the stream had started?



#7 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 21 April 2014 - 07:12 AM

1.  The typical way to use stream is to read a large chunk of data once or twice per second.  For example, if you are streaming at 50 scans/second, you might do a stream read twice per second where you read 25 scans each time.  Does that type of loop structure work for you?

 

2.  You know the elapsed time for any scan in your stream data from the equation Time=ScanNumber/ScanRate.



#8 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 21 April 2014 - 08:38 AM

Thanks for the reply. So my initial goal was to plot in realtime, meaning as fast as the data comes in I plot it, is this a ridiculous thing to expect to do?

 

But I think the method you described should be ok. I think I will use MatLab's software timer to do the reading loop. If I'm only doing a read every 0.5 seconds, it will be well within it's capability.

 

I will get to work and update here if I have success/failure. Thanks again.



#9 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 21 April 2014 - 09:57 AM

Thanks for the reply. So my initial goal was to plot in realtime, meaning as fast as the data comes in I plot it, is this a ridiculous thing to expect to do?

 

 

In your range of 50-2000 Hz, it is possible to read stream data 1 scan at a time using special low-latency streaming techniques, but it is not the normal way of streaming.  It is more normal to read larger chunks once or twice a second, process that data, update the screen, and write to file.  If you try to update the screen and write to file 2000 times per second that might be too much for the computer to handle.  Also a screen updating 2000 times per second might look strange.

 

But I think the method you described should be ok. I think I will use MatLab's software timer to do the reading loop. If I'm only doing a read every 0.5 seconds, it will be well within it's capability.

 

 

If you use the NONE wait mode, then you use a timer to control the read loop.  It is more common, though, to use the SLEEP wait mode and thus the read call will control the timing of the loop.



#10 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 21 April 2014 - 10:46 AM

I'm having difficulty understanding what LSW and MSW are in relation to streaming the counter channels. (http://labjack.com/s...ers-guide/3.2.1)

 

What I want to do is stream in the difference in values of Counter0-Counter1.

 

I tried the call:

ljudObj.AddRequest (ljhandle, LabJack.LabJackUD.IO.ADD_STREAM_CHANNEL_DIFF, 210, 0, 211, 0);

 

but I can't make sense of the data it's grabbing. How can I just stream in the counter value difference?
 



#11 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 21 April 2014 - 12:11 PM

Counter0 & Counter1 are each 32-bit counters, but each stream channel can only hold 16-bits.  Reading 210 will get you the lower 16-bits of the counter (LSW), which will count from 0 to 65535 and then roll back to 0.  If you want the upper 16-bits (MSW) read channel 224 right after you read 210, and combine them as LSW + 65536*MSW.

 

There is no way to directly read the difference between Counter0 and Counter1.  You need to read both and then subtract them in software.

 

We are just about to post an update to the Matlab UD examples.  Watch the page this afternoon:

 

http://labjack.com/s...examples/matlab



#12 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 21 April 2014 - 12:29 PM

Fantastic! Thanks so much, I saw that the problem with the LJ_swSLEEP constant is now dealt with in the most recent update to the u3_simplestream.m code.

 

 

Now time for me to actually put all this together.



#13 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 22 April 2014 - 01:23 PM

Ok, I have a working code (sort of). I think I need optimization to fix little glitches I'm seeing. I still can't find the magic relationship between scanRate, numScans, & the timer delay.

 

I have the scanRate = 500;

I have a MatLab timer object set to iterate every 0.06 seconds (the timer period)

So my numScans = 2*scanRate*0.06 = 60

 

Here are my questions/problems:

1. When there is backlog, plotting doesn't plot the data points in the right place because I use the iteration # of the timer and the timerPeriod to figure out what time values are associated with the data coming in. Any ideas of how to better do this keeping track of time values associated with the data coming in?

2. Can I do a guaranteed method to avoid Backlog? Other than making the reading of the data longer spaced, which would kill the real-time plotting.



#14 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 22 April 2014 - 03:06 PM

Are you using NONE wait mode?  I suggest you use SLEEP wait mode instead.  If our Matlab example does not use SLEEP wait mode, it should have comments about how to change it to use SLEEP wait mode.

 

If using SLEEP wait mode, you would not use any sort of Matlab timer or delay.  Rather, if you want the read loop to execute twice per second you just request 250 scans each time when you do a read, and the read will sleep/block as long as needed while it waits for the data to be ready.



#15 unknownsector

unknownsector
  • Members
  • 12 posts

Posted 22 April 2014 - 03:58 PM

1. I'm currently using NONE wait mode because I felt that the timer would give me more direct control over the iteration, but now I realize I didn't fully explore the SLEEP mode. I did try SLEEP mode in a previous iteration of my code, but I believe I was requesting too many scans, so the plot was severely delayed in showing the motion of the sensor I was using. 

 

But you're right, if I do a scanRate of 500, and request half, it should update twice every second. I am interested in plotting "almost" real-time, I want to have enough scanRate so that the data is smooth, but also be able to get the data at a rate that is not delayed to the naked eye.

 

So if I want to grab the data every 0.05 seconds. I would do a scanRate of 500, and request  25 scans at a time. Will attempt this. Hopefully the "glitches" go away in the plotting.



#16 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 23 April 2014 - 11:24 AM

You want to look at the parameters LJ_chSTREAM_READS_PER_SECOND and LJ_chSTREAM_SAMPLES_PER_PACKET:

 

http://labjack.com/s...ers-guide/4.3.7

 

By default these are tuned for maximum sample rate, but sounds like you want to tune them more for minimum latency.

 

General low-latency technique:
 
1.  Set the low-level packet size to hold 1 scan.  Set LJ_chSTREAM_SAMPLES_PER_PACKET equal to the number of channels in your stream list and that will make it so each low-level packet contains exactly 1 scan.
 
2.  Tell the driver to only transfer 1 stream data packet per read that it is doing in the background. Set LJ_chSTREAM_READS_PER_SECOND equal to 999999999 to force the driver to read only 1 low-level packet per transfer.
 
3.  Use the SLEEP wait mode, and read 1 scan per iteration.
 
4.  Avoid doing much in the loop.  File I/O, hardware I/O, and screen updates can all take substantial time.




Also tagged with one or more of these keywords: MatLab, LabJack U3-HV, Pasco, Rotary Motion Sensor, Count Pulses

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users