Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Generate a Specified Number of Pulse Signals Using NI USB-6211 with MATLAB

Hello all,
I need to generate a specified number of pulse signals using NI USB6211 and MATLAB DAQ toolbox to control the stepper motor. For example, 6e6 pulses are in need in about 1min of time. I'm now using code as follow. I use
DurationInSeconds
ch.Frequency
to control the number of pulse. But if the Frequency is high and Time is long, the number of pulse may not agree with my expect.
test = daq.createSession('ni');
ch=addCounterOutputChannel(test,Device_ID,'ctr0','PulseGeneration');
ch.Frequency=100000; % Palse per second
test.DurationInSeconds=60;
Can I generate a specified number of pulse signals with Counter output port in USB-6211 directly with MATLAB code? Just like what we can do in LabView.
HZY20_0-1624413619389.png

 

I need precise control of number of the counter pulse and not too care about the time of pulse train. (Precise in pulse number means precise position control of the stepper motor)
Thanks for your help.
 
大家好,我需要用MATLAB驱动USB6211产生指定数量的脉冲信号用于控制步进电机。我现在的使用方法是用脉冲总时间+脉冲频率的方法控制脉冲数量(见上述代码)。但是当脉冲频率较高或时间较长时,总脉冲数量与我期望的数量总有一定的偏差。请问大家有没有什么好方法能够直接用MATLAB控制采集卡的counter输出指定个数的脉冲?就像Labview中能够直接控制脉冲数量一样。
我比较关心脉冲的数量的准确性,脉冲重复频率与脉冲时间并不需要严格把控。
感谢大家的回复,祝大家工作顺利!
0 Kudos
Message 1 of 4
(1,898 Views)

You *might* be running into an integer overflow situation.  With your device's default 80 MHz timebase, your device's 32-bit counter will reach its max u32 value and rollover in a little less than 54 seconds (2^32 / 80e6).

 

A finite pulsetrain on your M-series device will use both counters -- the one you specify in the config is the one that produces the output pulses while the other "helper" counter acts as a timing gate to control the duration of time those pulses are generated.

 

I think I recall a long ago thread where it was necessary to configure things more explicitly in order to force the helper to use a slower-than-default timebase in order to prevent the rollover effect.   But I can't seem to find it now.

 

Experiment a bit and see whether there's a breaking point between 50 sec worth of pulses and 60 sec worth.  If so, that's a pretty good indicator that rollover is the culprit.

 

On the other hand, I have no idea what Matlab does with your "DurationInSeconds" setting.  I don't know the Matlab API at all, but it ought to support the DAQmx functionality for a finite pulse train with a specific exact # steps.  Maybe you just need to do your Matlab-based configuration differently.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy coming to an end (finally!). Permanent license pricing remains WIP. Tread carefully.
0 Kudos
Message 2 of 4
(1,861 Views)

Thanks Kevin.

This might not be the rollover effect. I can generate the pulse train of the same finite number in same time consumption using LabView VIs like this.

HZY20_0-1624583415300.png

 

This is mainly caused by the inaccurate clock signal or pulse frequency when I use MATLAB to control the card. (This also may caused by my wrong used code)

For example, I want to generate 10000 pulse in about 2 second, in LabView, I set 'Sample per channel' to 10000 and the  ' low Time' & ' high Time' to 1e-4. This program give me just the number of pulse and the time consumption I expect. In MATLAB, I set the Frequency (pulse per second) to 5000 and the time duration to 2s, but it gives wrong output pulse number. (The LabView code indicated the card had the ability to achieve this)

In fact, I focus mainly on the Number of pulse rather than the time duration (I need exactly 10000 pulse and the time of 2s is not such necessary). I want a method that I could directly control the number of the pulse in MATLAB rather use the product of time and pulse frequency.

I think this can be also achieves by MATLAB, but I didn't find any solution yet. Thank You Anyway.

 

HZY20

 

这应该不是采集卡的"rollover effect"导致的,因为我在LabView中能够在相同时间内产生相同数量的脉冲信号。MATLAB中我用脉冲频率×时间的方式产生指定数量的脉冲,我觉得是因为脉冲频率或者持续时间没那么准确导致的。我需要精确的脉冲数量控制,时间只要差不多就行。我暂时没有找到MATLAB中直接控制脉冲数量的函数或者程序。

感谢大家的帮助,祝大家工作顺利!

0 Kudos
Message 3 of 4
(1,847 Views)

Does your Matlab code produce slightly different total #'s of pulses on different runs?   If so, it sounds like the config you've used is measuring the 60 sec duration in software somehow, then reacting with a software command to stop what was otherwise a continuous pulse train.

 

Take note of the fact that your LabVIEW config had to set up several distinct parameters and properties that are not accounted for in the Matlab code.  Particularly, nothing similar to your call to DAQmx Timing where you can choose Finite Samples (rather than Continuous), while explicitly setting the # samples to generate (and have that # controlled at  the hardware level).

 

Dig a little deeper in the Matlab API, I expect there are functions to let you do the config you're after.  I just don't have any idea what they're called or where to find them. 

 

 

-Kevin P

 

ALERT! LabVIEW's subscription-only policy coming to an end (finally!). Permanent license pricing remains WIP. Tread carefully.
0 Kudos
Message 4 of 4
(1,839 Views)