For my DIY microscope I had a task - generate a train of digital pulses which simulate camera trigger, so that other devices (galvo and laser) are synched. I wanted to do it in Python, so that it seamlessly integrates in my data acquisition and analysis Jupyter notebook.
After some quick search I found a PyDAQmx library which seemed mature and had good examples to begin with.
Installation was smooth: download, unzip, open Anaconda prompt,
python setup.py install
After only 30 min fiddling, I was able to solve my problem in just a few lines of code:
Holy crap, it just works, out of the box. Oscilloscope shows nice digital pulses every 100 ms, each 1 ms long. The code is much shorter and cleaner than would be in C, C#, or LabView.
PyDAQmx appears to be a full-power wrapper around native NI DAQmx drivers (yes, they need to be installed), so presumably it can do all that can be done in C or even LabView (this statement needs to be tested).
One can use PyDAQmx to control galvos with fast analog output waveforms, as shown by @kyleellefsen in his code.
Many thanks to @clade for writing PyDAQmx! Really made my day..
My system was: Windows 10, Python 2.7 (x32), NI PCIe-6321 board.
After some quick search I found a PyDAQmx library which seemed mature and had good examples to begin with.
Installation was smooth: download, unzip, open Anaconda prompt,
python setup.py install
After only 30 min fiddling, I was able to solve my problem in just a few lines of code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple digital pulse generation with PyDAQmx library and NI DAQmx board | |
# Requires NI DAQmx drivers, Python 2.7 installed, Windows. | |
# Tested on NI PCIe-6321 board | |
# Gist author: Nikita Vladimirov @nvladimus | |
import PyDAQmx as pd | |
ctr_ini_delay = 0 # sec | |
ctr_period = 0.1 # sec | |
ctr_duty_cycle = 0.01 | |
task = pd.Task() | |
task.CreateCOPulseChanFreq("Dev1/ctr0", "CamTrigger", pd.DAQmx_Val_Hz, pd.DAQmx_Val_Low, ctr_ini_delay, 1/float(ctr_period), ctr_duty_cycle) | |
task.CfgImplicitTiming(pd.DAQmx_Val_ContSamps, 1000) | |
task.StartTask() | |
# Go and measure the pulses with oscilloscope. | |
# Output will be at terminal pin "Ctr0 OUT" | |
# check your NI DAQmx board pinout diagram for terminal location | |
# It was "PFI 12" on my board NI PCIe-6321. | |
task.StopTask() | |
task.ClearTask() |
PyDAQmx appears to be a full-power wrapper around native NI DAQmx drivers (yes, they need to be installed), so presumably it can do all that can be done in C or even LabView (this statement needs to be tested).
One can use PyDAQmx to control galvos with fast analog output waveforms, as shown by @kyleellefsen in his code.
Many thanks to @clade for writing PyDAQmx! Really made my day..
My system was: Windows 10, Python 2.7 (x32), NI PCIe-6321 board.
Comments
Post a Comment