Quantcast
Channel: Counter/Timer topics
Viewing all articles
Browse latest Browse all 1271

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 1271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>