Skip to content

Instantly share code, notes, and snippets.

@sriharijayaram5
Created April 14, 2020 11:33
Show Gist options
  • Save sriharijayaram5/0b9829f939e5cd2d75869ce80e3572d1 to your computer and use it in GitHub Desktop.
Save sriharijayaram5/0b9829f939e5cd2d75869ce80e3572d1 to your computer and use it in GitHub Desktop.
pulse_test NI Card
import PyDAQmx as daq
import numpy as np
import time
from ctypes import *
import matplotlib.pyplot as plt
from pyvcam import pvc
from pyvcam.camera import Camera
# device = 'dev1'
# try:
# daq.DAQmxResetDevice(device)
# except:
# # self.log.exception('Could not reset NI device {0}'.format(device))
# retval = -1
try:
daq.DAQmxStopTask(main)
daq.DAQmxClearTask(main)
except BaseException:
pass
try:
daq.DAQmxStopTask(clock)
daq.DAQmxClearTask(clock)
except BaseException:
pass
pvc.init_pvcam()
camera = [camera for camera in Camera.detect_camera()][0]
camera.open()
camera.exp_mode = "Ext Trig Edge Rising"
camera.roi = (0, 50, 0, 50)
camera.exp_time = 10
base_period = (camera.exp_time / 1000.) + 0.2
# =========Create clock channel on Dev1/Ctr0
clock = daq.TaskHandle()
duty_cycle = 0.5
period = 0.2
period = base_period
_clock_channel = '/Dev1/Ctr1'
delay = 2
d = 0
daq.DAQmxCreateTask('myClockTask', daq.byref(clock))
# Create channel to generate digital pulses that freq and dutyCycle define
# and adds the channel to the task
daq.DAQmxCreateCOPulseChanFreq(
clock,
_clock_channel, # The name of the counter to use to create virtual channels
"myClockChannel", # The name to assign to the created virtual channel
daq.DAQmx_Val_Hz, # The units in which to specify freq.
daq.DAQmx_Val_Low, # The resting state of the output terminal.
# The amount of time in seconds to wait before generating the first pulse.
d + delay,
1 / float(period), # The frequency at which to generate pulses.
duty_cycle, # The width of the pulse divided by the pulse period.
)
j = 10
# # Sets only the number of samples to acquire or generate without specifying timing.
daq.DAQmxCfgImplicitTiming(
clock,
# daq.DAQmx_Val_FiniteSamps,
# Acquire or generate samples until you stop the task.
daq.DAQmx_Val_FiniteSamps,
j # the buffer size
)
##########################################################################
smiq = daq.TaskHandle()
duty_cycle = 0.1
period = 0.4
period = base_period * 2
_smiq_channel = '/Dev1/Ctr3'
d = 0
daq.DAQmxCreateTask('mySmiqTask', daq.byref(smiq))
# Create channel to generate digital pulses that freq and dutyCycle define
# and adds the channel to the task
daq.DAQmxCreateCOPulseChanFreq(
smiq,
_smiq_channel, # The name of the counter to use to create virtual channels
"mySmiqChannel", # The name to assign to the created virtual channel
daq.DAQmx_Val_Hz, # The units in which to specify freq.
daq.DAQmx_Val_Low, # The resting state of the output terminal.
# The amount of time in seconds to wait before generating the first pulse.
d,
1 / float(period), # The frequency at which to generate pulses.
duty_cycle, # The width of the pulse divided by the pulse period.
)
# Sets only the number of samples to acquire or generate without
# specifying timing.
daq.DAQmxCfgImplicitTiming(
smiq,
# daq.DAQmx_Val_FiniteSamps,
# Acquire or generate samples until you stop the task.
daq.DAQmx_Val_FiniteSamps,
j # the buffer size
)
daq.DAQmxCfgDigEdgeStartTrig(
smiq,
'/Dev1/Ctr1' + 'InternalOutput',
daq.DAQmx_Val_Rising
)
##########################################################################
switch = daq.TaskHandle()
duty_cycle = 0.5
period = 0.4
period = base_period * 2
_switch_channel = '/Dev1/Ctr2'
d = 0
daq.DAQmxCreateTask('mySwitchTask', daq.byref(switch))
# Create channel to generate digital pulses that freq and dutyCycle define
# and adds the channel to the task
daq.DAQmxCreateCOPulseChanFreq(
switch,
_switch_channel, # The name of the counter to use to create virtual channels
"mySwitchChannel", # The name to assign to the created virtual channel
daq.DAQmx_Val_Hz, # The units in which to specify freq.
daq.DAQmx_Val_High, # The resting state of the output terminal.
# The amount of time in seconds to wait before generating the first pulse.
d,
1 / float(period), # The frequency at which to generate pulses.
duty_cycle, # The width of the pulse divided by the pulse period.
)
# # Sets only the number of samples to acquire or generate without specifying timing.
daq.DAQmxCfgImplicitTiming(
switch,
# daq.DAQmx_Val_FiniteSamps,
# Acquire or generate samples until you stop the task.
daq.DAQmx_Val_FiniteSamps,
j # the buffer size
)
daq.DAQmxCfgDigEdgeStartTrig(
switch,
'/Dev1/Ctr1' + 'InternalOutput',
daq.DAQmx_Val_Rising
)
##########################################################################
cam = daq.TaskHandle()
duty_cycle = 0.1
period = 0.2
period = base_period
_cam_channel = '/Dev1/Ctr0'
d = 0.1
daq.DAQmxCreateTask('myCamTask', daq.byref(cam))
# Create channel to generate digital pulses that freq and dutyCycle define
# and adds the channel to the task
daq.DAQmxCreateCOPulseChanFreq(
cam,
_cam_channel, # The name of the counter to use to create virtual channels
"myCamChannel", # The name to assign to the created virtual channel
daq.DAQmx_Val_Hz, # The units in which to specify freq.
daq.DAQmx_Val_Low, # The resting state of the output terminal.
# The amount of time in seconds to wait before generating the first pulse.
d,
1 / float(period), # The frequency at which to generate pulses.
duty_cycle, # The width of the pulse divided by the pulse period.
)
# # Sets only the number of samples to acquire or generate without specifying timing.
daq.DAQmxCfgImplicitTiming(
cam,
# daq.DAQmx_Val_FiniteSamps,
# Acquire or generate samples until you stop the task.
daq.DAQmx_Val_FiniteSamps,
j # the buffer size
)
daq.DAQmxCfgDigEdgeStartTrig(
cam,
'/Dev1/Ctr1' + 'InternalOutput',
daq.DAQmx_Val_Rising
)
daq.DAQmxStartTask(clock)
daq.DAQmxStartTask(smiq)
daq.DAQmxStartTask(cam)
daq.DAQmxStartTask(switch)
t1 = time.time()
frame = camera.get_sequence(j)
print("In {} seconds, shape of {}".format(
time.time() - t1, np.shape(frame[0])))
print(camera.trigger_table)
camera.close()
pvc.uninit_pvcam()
daq.DAQmxWaitUntilTaskDone(
cam,
10
)
try:
daq.DAQmxStopTask(main)
daq.DAQmxClearTask(main)
except BaseException:
pass
try:
daq.DAQmxStopTask(clock)
daq.DAQmxClearTask(clock)
except BaseException:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment