Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Simultaneous pulse generation and edge counting with PCI-6221 - python scripting

Solved!
Go to solution

I am trying to use a PCI-6221 board to have a clocked counter. The idea is to generate a finite train of pulses from ctr1, hardwire this output to a given PFI (PFI7 in my case .. I understand that it may be possible to use some internal connection, but this will come afterwards), and use this as external clock for ctr0 to count in between sequence of rising edges. A gated counter.

 

Here it is my python script.

 

import ctypes
import numpy as np
from PyDAQmx import Task
from PyDAQmx.DAQmxConstants import *
from PyDAQmx.DAQmxTypes import *

null_ptr = POINTER(c_uint)()
null_char=c_wchar()

# acquisition variables
t_int=10e-3 # integration time
N_p=int(1e2) # pulse number

# output variables
data_written=POINTER(c_long)()
read_array=np.zeros(N_p,dtype=c_ulong())
read_single=POINTER(c_ulong)()

# configuration task variables
pulse_freq = 1/t_int # pulse frequency
pulse_duty = 0.5
samples_per_channel = N_p # Number of pulses to generate
device_channel_pg = "Dev3/ctr1"
device_channel_ctr = "Dev3/ctr0"

# Create Task
task_pg = Task()
task_ctr= Task()

# Create Pulse Train Channel
task_pg.CreateCOPulseChanFreq(device_channel_pg, "", DAQmx_Val_Hz, DAQmx_Val_Low, 0.0, pulse_freq, pulse_duty)
# Configure Timing
task_pg.CfgImplicitTiming(DAQmx_Val_FiniteSamps, samples_per_channel)

# Create Edge Counter Input Channel
task_ctr.CreateCICountEdgesChan(device_channel_ctr, "",DAQmx_Val_Rising,0,DAQmx_Val_CountUp)
# Configure Timing
task_ctr.CfgSampClkTiming('PFI7',10*pulse_freq,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,samples_per_channel)
task_ctr.CfgInputBuffer(samples_per_channel)

# single point acquisition
#task_ctr.ReadCounterScalarU32(1,read_single,null_ptr)

# clocked acquisiton
is_task_done = ctypes.c_ulong(0)
task_pg.StartTask()
#task_ctr.StartTask()
while not is_task_done.value:
task_pg.IsTaskDone(ctypes.byref(is_task_done))

task_pg.StopTask()
#task_ctr.StopTask()
task_ctr.ReadCounterU32(samples_per_channel,5,read_array,samples_per_channel,data_written,null_ptr)

# Clear Task
task_pg.ClearTask()
task_ctr.ClearTask()

 

Unfortunately, when I run this (and I tried many configurations) I end up with these errors:

- if the read command is before the pulse generation StopTask, I got: "PyDAQmx.DAQmxFunctions.PALResourceReservedError: The specified resource is reserved."

- if the read command is after the pulse generation StopTask, I end up with: "PyDAQmx.DAQmxFunctions.SamplesNotYetAvailableError: Some or all of the samples requested have not yet been acquired."

 

does anyone have any clue on how to solve this? I am pretty sure the logic should be ok. I also tested the equivalent scripting in matlab and at least there it worked for very low sample rate.

 

Thank you in advance for any help.

0 Kudos
Message 1 of 3
(442 Views)
Solution
Accepted by topic author simdp

The reasons given in the error text are correct, but they don't tell you the reason behind the reason.

 

You're facing a device hardware limitation.  M-series devices use up *both* onboard counters to produce a finite pulse train with one of them.  So there's none available for anything else.  If you were to generate a continuous pulse train instead, *then* you should be able to use the other counter for an edge counting task.  Further, you can still perform finite sampling this way by pairing a finite edge counting task with a continuous pulse train as its sample clock.

 

I also used to sometimes generate M-series finite clocks with a dummy analog output task that just generated 0 volts at an unwired output terminal.  Your 6221 doesn't have any AO, but you could accomplish the same kind of thing with a dummy analog input task if you didn't otherwise need the AI subsystem.

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
0 Kudos
Message 2 of 3
(425 Views)

Thank you very much Kevin! .. now I have a better understanding of the errors I got ..

 

I think I will try the infinite pulse train and a finite time analog task to cut on and off the pulse train to the desired pulse number. If I understood you correctly, hopefully this could work.

 

next step, buy a 4 counter card ..

0 Kudos
Message 3 of 3
(397 Views)