Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement a counter timer with DAQmx functions?

LabView:7 express
Hardware: NI PCI-6052E

In traditional DAQ Counter functions, a timer can be realized easily by using event/counter config.vi and counter read.vi ( in traditional DAQ functions). This can be used as a software control timer to track elapsed time between two events.
How to this with DAQmx functions?
0 Kudos
Message 1 of 11
(5,702 Views)
Hello,

Measuring the elapsed time between two pulse events is also possible with DAQmx. There are several different ways to do this:

1. The easiest way to do this is with the DAQ assistant. This is available from the function palette in Labview. On your function palette, go to �All Functions >> NI Measurements >> DAQmx � Data Acquisition�, and here there is a VI called �DAQ Assistant�. This is an express VI so it has a GUI-based configuration dialog that will custom configure a DAQ task for you. In this GUI you would choose �Counter Input�. From here you will be presented with a menu asking you to choose the type of counter measurement you�d like to make. I�d recommend choosing either �Period Measurement� or �Pulse Measurement�, since this will measure the time between two events on your input line. The GUI dialog will also ask you several other parameters such as which counter to use on your 6052E board, minimum and maximum time values for the measurement, and also how you would like to sample the counter (1 time, N times, or continuously). After going through this configuration, DAQ Assistant will generate code for you to accomplish this task. At this time the DAQ assistant will also create inputs and outputs to the VI that are specific to your task. You can also view the generated code by right clicking on the DAQ Assistant VI and choosing �Open Front Panel�. Please note when Opening the Front Panel that the Express VI is converted permanently to a regular subVI, and cannot be converted back to an Express VI (In other words you cannot configure the VI any longer with the GUI).

2. The second method of using DAQmx for counter measurements is to use the DAQmx VIs directly. These VIs are located on the Function Panel in the same location as the DAQ Assistant Express VI. The general process here would be to first create a DAQmx channel and then read data from this DAQmx channel. To create a DAQmx channel, use the �DAQmx Create Virtual Channel� VI. This VI defines what kind of measurement you would like to make, and which physical channel you�d like to use, etc. The measurement task can be changed by left clicking on the label at the bottom of the VI. Clicking this label will pop-up a menu that will allow you to choose between different tasks, such as analog input, analog output, digital I/O, and counter I/O. To make counter measurements of events you would choose either �Counter Input >> Pulse Width� or �Counter Input >> Period�. The VI will add the correct inputs and outputs to match the task type you choose. For instance for Pulse Width measurement, the VI would add inputs for the event edge and minimum/maximum time values. After configuring this VI, you need to add a �DAQmx Read� VI. This VI is also configurable just like the �DAQmx Create Virtual Channel� VI. For counter measurements you can choose to retrieve single or multiple samples from the counter and you can specify what data type these counter values should be. Again, the VI will reconfigure its inputs and outputs based on your choice of task. There are some excellent examples available for DAQmx in the Example Finder in Labview 7.0. These can be found by first opening the NI Example Finder, which can be accessed by going to �Help>>Find Examples� on the menu of any Labview window. When the example finder opens browse to the following location in the tree structure that appears in the middle of the Example Finder window: �Hardware Input and Output >> Counter Measurements >> Period or Pulse Width�. The examples in this area of the Example finder demonstrate how to use the DAQmx VIs for Period and Pulse measurement.

Hope this helps. Please respond back if anything is unclear. Have a great day!

Ken S.
National Instruments
Message 2 of 11
(5,702 Views)
Hi Ken:
Thank you very much for the information.
I have tried the methods that as suggested on DAQ Assistant ,DQAmx vis and DAQmx examples.
I have set my vi to measure the time between raising edges of events. I found this problem. Say I have for events (1,2,3,4), and want to know the time between each events i.e. (1,2), (2,3) and (3,4).
In the DAXmx I can only measure the time on (1,2) and (3,4). The time with (2,3) was not measured.
How can I measure all of them?
0 Kudos
Message 3 of 11
(5,702 Views)
Hello,

Do you get this effect with some of the DAQmx example programs? If so which one?

I would suggest trying this out with one of the buffered period measurement examples from the NI Example Finder. This should return an array of time values. The time values in this array should contain the time between each rising edge of the input signal (the input signal defaults to the Counter Gate input).

For your example from above the array should contain 4 time values:

Element 1: Ignore this value since it is the counter value from unknown start time
Element 2: Time between rising edge 1 and 2
Element 3: Time between rising edge 2 and 3
Element 4: Time between rising edge 3 and 4

Ken S.
National Instruments
0 Kudos
Message 4 of 11
(5,702 Views)
Hi Ken:
Yes, the example vi "Meas Dig Period-Low Freq 1 ctr.vi" will show the effect. Actually my application vi is based on this example vi.
If buffered the event (count or time) reading, the effect still there unless I have set infinite size of buffer. Say the buffer size is set for 4 events, this only good for 4 events (1,2,3,4) and restart to load another 4 events (5,6,7,8). The problem is the time between events 4 and 5. The vi will not able to record the time span between event 4 and 5.
The goal of my application is to measure the speed, rpm, of a wheel or rotating shaft with a magnet type sensor. The rotating motion is continuous.

thank you,
Min Shiei
0 Kudos
Message 5 of 11
(5,702 Views)
Hello Min,

I think for your application we should use a continuous period measurement example. In the NI Example Finder you can use the example called �Meas Dig Periods-Buffered-Continuous-High Freq 2 Ctr�. This example demonstrates how to continuously read the buffer of period measurements so that you should not lose any event data. Also note that you can change this to a Low frequency counter that only uses 1 of the onboard counters by choosing �Low Frequency 1 Counter� from the pull down box above the �DAQmx Create Channel� VI.

Let me know if this doesn�t fit your application.

Ken S.
National Instruments
0 Kudos
Message 6 of 11
(5,702 Views)
Hi Ken:
Appreciated very much for your information.

I have tried the example �Meas Dig Periods-Buffered-Continuous-High Freq 2 Ctr� with modifications for my application. The problem is the setup for "measurement time " & "samples to read". Unless the configuration is perfect, the time measurement will be off. Because the vi has to wait for the measurement and read so many samples as set, chances are not all the events will be caught. Another point is in the while loop where executes "DAQmx read.vi" has to wait for the "timeout", if I press the "stop" button on the application vi panel, the vi will not stop immediately until the timeout error message displayed.

For my application to measure the rpm of a rotating
object, the concept is simple. Use two counters one to count each event and the other to count the time span between two events. The traditional NI-DAQ functions are working perfectly.
For NI-DAQmx to work with this application, I will need to create some kind of timer. According to my research at this moment, there are no examples or library can be used with DAQmx.
Something I would like to try is to create a CIN for this purpose, if you can suggest any example regarding how to use DAQmx library with C/C++ programming will be big help.

thank you,
Min
0 Kudos
Message 7 of 11
(5,702 Views)
Hello Min,

I have a couple of questions for you about your application which might allow me to help you more. What is the approximate frequency of the pulse train you are measuring? Does this pulse train stop before you hit the Stop button on the VI?

I�ve attached a modified version of the continuous period counter example. This version allows you to set a minimum and maximum period value, it allows you to set the timeout value, and it uses only one counter. The basic idea of this VI is that the counter will continuously measure the period of the incoming pulses and will stored these values into a buffer. The DAQmx Read VI then reads the number of elements out of the buffer set by the �samples to read� control on the front panel. This VI should not lose any events that are coming in because the DAQ hardware is actually writing these values to a buffer and the DAQmx Read VI is just accessing data from this internal buffer after it is already written by the counter hardware.

It is important to set this �samples to read� value to something that is low enough that the counter will receive that number of pulses before the timeout expires. So for instance, if you are receiving 100 pulses per second and your timeout is 10 seconds you would want to read significantly less than 1000 samples per read (100 pulses per second * 10 seconds), so a good value to read would be 100 or 500. Also if this value is set right then you should not have timeouts occurring from the DAQmx Read since this read will just wait long enough to read a certain number of samples from the buffer before it goes on to the next loop iteration. If your pulses stop coming in to the counter before you hit the stop button then you will get a timeout (if this is expected behavior that shouldn't cause an error we can add some logic in to handle this error without stopping the program). Also if the stop button is not responsive enough (takes a timeout to make it exit) then you can lower the timeout value and the �samples to read� value to something lower (like from 10 seconds to 1 second).

I think we can probably get this sample VI to work for you.

As for C/C++ examples for DAQmx, you can get several counter examples from the NI-DAQ install directory. The default location is: �C:\Program Files\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Counter\Measure Period or Pulse Width�. There are several examples in this folder, one of which is an example of how to do buffered continuous period measurements.

Let me know if this VI works for you or if there is anything else I can help with.

Ken S.
National Instruments
0 Kudos
Message 8 of 11
(5,702 Views)
Good morning Ken:
Appreciated your enthusiastic help.
I think it's easier to answer your questions this way. My target is to create a speed(rpm) meter if possible. The object to be measured could be a motor shaft, bike wheel or other rotating objects. The sensor is a Hall effect magnet triggered sensor. The measured speed range could be several rpm to several hundred rpm. The user of this application vi can stop the vi as needed at any time. Also the vi should have the ability to plot the speed vs. time.

Your suggested example happens to be very similar to the one I have. Please see the attachment. The problem that I have is 2 folds.
First when I use DAQmx Timing set as Implicit, the time
measured was not correct. I tested with event time for more than 2 seconds but it only showed less a second. I don�t know how the DQAmx timing.vi, DAQmx read.vi converts the time.
Second as already discussed the application vi has to wait for DAQmx read to timeout if the stop button was pressed before timeout. This is not a fatal problem.

Finally, I will study the C++ examples.

thank you,
Min
0 Kudos
Message 9 of 11
(5,702 Views)
Hello Min,

I looked at your code and I think I may have found the source of the first issue with the period measurements being inaccurate. DAQmx varies from Traditional DAQ in that it automatically sets the best resolution for timer readings of period and pulse width measurements. Setting the minimum and maximum period values is VERY important for the �DAQmx Create Channel� VI. These values default to a range of 0.000001 � 0.1 seconds. I think this is what is causing the incorrect results in your period measurements.

If you have a min and max RPM of 1-300 RPM (for instance), and you get one pulse per revolution, then the minimum and maximum period values should be 60 - 0.2 seconds. Since the min and max were set much lower than this, the timer (which is 24 bits) would be rolling over frequently (every 0.1 s). So the maximum value you could read as your period with these settings would be 0.1 seconds which would obviously be wrong. I added control inputs to your VI to allow you to set the minimum and maximum values to whatever you choose.

As for the delay when stopping you VI, this will always happen if the source of the pulses (the wheel in this case) stops before you press the Stop button then the DAQmx Read VI will wait until its timeout before the loop is able to check the Stop button to see if it has been pressed. There are three ways to fix this: 1) Make sure to press the Stop button while still measuring incoming pulses (while the wheel is still spinning), or 2) if the Stop button can only be hit after pulses have stopped, then you can lower the timeout value. You must be careful not to lower the timeout to something that is too low or the loop will timeout while acquiring normal pulse data. 3) You can set your timeout value to 0 and filter out the timeout error. So in other words, you would expect the DAQmx Read VI to generate an error on most loop iterations but it would still update when a pulse was received.

I�ve also modified your code to demonstrate how to implement option 3.

Let me know if this doesn�t work for you.

Ken S.
National Instruments
0 Kudos
Message 10 of 11
(5,702 Views)