-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example scripts supplied by Chris Lee-Messer. Also move all example code to nidaqmx/examples. Closes #4.
- Loading branch information
1 parent
ca6830e
commit e8c14d1
Showing
12 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
The following scripts are all (c) Chris Lee-Messer and were fetched from | ||
https://bitbucket.org/cleemesser/pylibnidaqmx-examples/src/. | ||
|
||
* system_info.py: an example which prints out the hardware configuration for | ||
the NI-DAQ boards on a computer | ||
|
||
* alternate_on_off_slow.py: Turn a digital output on and off based upon a | ||
system software timing (imprecise) | ||
|
||
* correlated_ai_do.py: Do high-performance digital output by using the sample | ||
clock while doing simulatenous analog input sampling | ||
|
||
* correlated_ao_dio.py: Do high-performance digital output by using the sample | ||
clock while doing simulatenous analog output | ||
|
||
* counter_out_cont_pulses.py | ||
|
||
* simultan_ai_ao.py: How to do simultaneous analog input and output using | ||
finite sampling. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# with relatively loose timing constraints | ||
# turn on and off a digital output | ||
|
||
from __future__ import division | ||
from numpy import * | ||
import labdaq.daqmx as daqmx | ||
import labdaq.daq as daq | ||
import threading,time | ||
|
||
def min2sec(minutes): | ||
return minutes*60.0 | ||
|
||
###### setup script parameters ######## | ||
|
||
long_duration = min2sec(1.0) # twenty minutes | ||
duration = 25.0 # sec | ||
onvoltage = 5.0 | ||
offvoltage = 0.0 | ||
onstate = False | ||
gCurrentTimer = None | ||
state_verbal= {True:'On', False: 'Off'} | ||
|
||
|
||
def change_voltage(onstate): | ||
print onstate | ||
if onstate: | ||
daq.set_voltage_ch0(onvoltage) | ||
else : | ||
daq.set_voltage_ch0(offvoltage) | ||
|
||
def cycle(onstate=onstate): | ||
print "hi! Starting up loop of alternating on voltage %f with off voltage of %f every %f seconds or %f minutes" % (onvoltage, offvoltage, duration, sec2min(duration)) | ||
while 1: | ||
onstate = not onstate | ||
change_voltage(onstate) | ||
time.sleep(duration) | ||
# gCurrentTimer = threading.Timer(duration, cycle, (onstate,)) | ||
|
||
|
||
if __name__=='__main__': | ||
cycle() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# using correlated digital output based off the analog sample clock | ||
# | ||
|
||
# need to start the dio task first! | ||
import time | ||
import numpy as np | ||
import nidaqmx | ||
|
||
# some useful parameters | ||
nsamples = 5000 # about 5 sec | ||
samplerate = 1000 | ||
TERMINALEND = 'nrse' # consider 'rse' (referenced single-ended),'nrse' (non-referenced single ended) | ||
# 'diff', or 'pseudodiff' as other options, can look at panel for hints | ||
analog_input = r'Dev1/ai15' # connect analog input to this terminal, customize as you wish | ||
ndigital = 2 # number of digital channels | ||
digital_output_str = r'Dev1/port0/line6:7' | ||
|
||
itask = nidaqmx.AnalogInputTask() | ||
itask.create_voltage_channel(analog_input, min_val=0.0,max_val=10.0, terminal=TERMINALEND) | ||
itask.configure_timing_sample_clock(rate=samplerate,sample_mode='finite',samples_per_channel=nsamples) | ||
|
||
|
||
# ok that's tee'd up | ||
# assume that these are uint8 | ||
|
||
ddata = np.zeros(nsamples*ndigital, dtype=np.uint8) | ||
onarr = np.ones(ndigital,dtype=np.uint8) | ||
offarr = np.zeros(ndigital,dtype=np.uint8) | ||
## for interleaved layout | ||
# ddata[0:10] = 1 # turn both on for 5 ticks | ||
# ddata[10:2*nsamples:100]=1 # now turn both on for 3ms every 100ms | ||
# ddata[11:2*nsamples:100]=1 | ||
# ddata[12:2*nsamples:100]=1 | ||
# ddata[13:2*nsamples:100]=1 | ||
# ddata[14:2*nsamples:100]=1 | ||
# ddata[15:2*nsamples:100]=1 | ||
# ddata[16:2*nsamples:100]=1 | ||
# # turn both off | ||
# ddata[-1] = 0 | ||
# ddata[-2] = 0 | ||
## for by_channel layout | ||
|
||
# 2ms pulses at 10hz | ||
ddata[0:nsamples:100] = 1 | ||
ddata[1:nsamples:100] = 1 | ||
ddata[nsamples-1] = 0 | ||
# 2nd channel setup, offset by 40ms do same thing | ||
offset = 40 | ||
ddata[nsamples+offset:ndigital*nsamples:100] =1 | ||
ddata[nsamples+offset+1:ndigital*nsamples:100] =1 | ||
ddata[-1] = 0 | ||
|
||
|
||
dotask = nidaqmx.DigitalOutputTask() | ||
dotask.create_channel(digital_output_str, name='line67' ) | ||
#print "dotask info:", dotask.get_info_str(True) | ||
print "itask info:", itask.get_info_str() | ||
# note must use r'ao/SampleClock' (can't prefix with /Dev1/ | ||
dotask.configure_timing_sample_clock(source=r'ai/SampleClock',rate=samplerate,sample_mode='finite',samples_per_channel=nsamples) | ||
dotask.write(ddata, auto_start=False, layout='group_by_channel') | ||
# layout='group_by_scan_number') | ||
print "digital task info:" | ||
print dotask.get_info_str() | ||
dotask.start() | ||
|
||
print "starting" | ||
itask.start() | ||
time.sleep(nsamples/samplerate) | ||
data = itask.read() # get data | ||
|
||
print "press return to end" | ||
c =raw_input() | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# output a sine wave of 1000 samples to terminal /Dev1/ao0 | ||
# while simultaneously outputing a 1ms digital pulse every 5 ms (duty cycle 20%) | ||
# using correlated digital output based off the analog sample clock | ||
|
||
# need to start the dio task first! | ||
|
||
import numpy as np | ||
import nidaqmx | ||
nsamples = 1000 | ||
samplerate = 1000 | ||
|
||
adata = 9.95*np.sin(np.arange(nsamples,dtype=np.float64)*2*np.pi/nsamples) | ||
|
||
atask = nidaqmx.AnalogOutputTask() | ||
atask.create_voltage_channel('Dev1/ao0', min_val=-10.0,max_val=10.0) | ||
atask.configure_timing_sample_clock(rate=samplerate,sample_mode='finite', samples_per_channel=1000) | ||
atask.write(adata, auto_start=False) | ||
# ok that's tee'd up | ||
|
||
ddata = np.zeros(nsamples, dtype=np.uint8) | ||
ddata[0:nsamples:5]=1 | ||
|
||
dotask = nidaqmx.DigitalOutputTask() | ||
dotask.create_channel('Dev1/port0/line0', name='line0') | ||
#print "dotask info:", dotask.get_info_str(True) | ||
print "atask info:", atask.get_info_str() | ||
# note must use r'ao/SampleClock' (can't prefix with /Dev1/ | ||
dotask.configure_timing_sample_clock(source=r'ao/SampleClock',rate=1000,sample_mode='finite',samples_per_channel=1000) | ||
dotask.write(ddata, auto_start=False) | ||
dotask.start() | ||
|
||
atask.start() | ||
|
||
print "press return to end" | ||
c =raw_input() | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import division | ||
import numpy as np | ||
import nidaqmx | ||
|
||
|
||
nsamples = 1000 | ||
samplerate = 1000 | ||
counter = nidaqmx.CounterOutputTask() | ||
|
||
device = nidaqmx.libnidaqmx.Device('Dev1') | ||
print "counter output channels", device.get_counter_output_channels() | ||
print "counter input channels", device.get_counter_input_channels() | ||
|
||
|
||
counter.create_channel_frequency(r'Dev1/ctr0', name='counter0', freq=samplerate) | ||
counter.configure_timing_implicit(sample_mode='continuous', samples_per_channel=nsamples) | ||
counter.start() | ||
|
||
|
||
import time | ||
print "waiting 10 seconds" | ||
time.sleep(10.0*(nsamples/samplerate)) | ||
print "I don't think it should be done" | ||
print "is task done?", counter.is_done() | ||
print "stopping task now" | ||
counter.stop() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
# do simultaneous analog input and output | ||
import numpy as np | ||
import nidaqmx | ||
|
||
analog_output_term = r'Dev1/ao0' | ||
analog_input = r'Dev1/ai15' # connect analog input to this terminal, customize as you wish | ||
TERMINALEND = 'nrse' # consider 'rse' (referenced single-ended),'nrse' | ||
# (non-referenced single ended) for configuration | ||
# of analog input | ||
|
||
samplerate = 10000 | ||
nsamples = 1000 # if samplemode = 'continuous' used to determine buffer size | ||
samplemode = 'finite' # or 'continuous' | ||
|
||
#outputdata = np.linspace(0.0,1000.0, num=nsamples) # assumine default to float64 | ||
outputdata = np.arange(nsamples,dtype='float64') # assumine default to float64 | ||
outputdata = np.sin(0.2*outputdata) | ||
|
||
print outputdata.shape | ||
|
||
|
||
|
||
|
||
itask = nidaqmx.AnalogInputTask() | ||
itask.create_voltage_channel(analog_input, min_val=0.0,max_val=10.0, | ||
terminal=TERMINALEND) | ||
itask.configure_timing_sample_clock(rate=samplerate, | ||
sample_mode=samplemode, | ||
samples_per_channel=nsamples) | ||
|
||
|
||
# print "input task buffer size", itask.get_buffer_size() | ||
# print itask.get_read_current_position() | ||
# get_regeneration | ||
|
||
otask = nidaqmx.AnalogOutputTask() | ||
|
||
otask.create_voltage_channel(analog_output_term, min_val=-10.0,max_val=10.0) | ||
|
||
otask.configure_timing_sample_clock(source=r'ai/SampleClock', | ||
rate=samplerate, | ||
sample_mode=samplemode, | ||
samples_per_channel=nsamples) | ||
|
||
otask.write(outputdata,auto_start=False) | ||
|
||
|
||
# start them both | ||
otask.start() | ||
itask.start() | ||
|
||
itask.wait_until_done(10.0) | ||
print "done" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# -*- coding: utf-8 -*- | ||
# do simultaneous analog input and output | ||
import time | ||
from ctypes import * | ||
import numpy as np | ||
import nidaqmx | ||
import labdaq.daqmx as mx | ||
|
||
|
||
def getCurWritePos(task): | ||
cur_wp = mx.uInt64(0) | ||
mx.CHK(mx.ni.DAQmxGetWriteCurrWritePos(otask, byref(cur_wp))) | ||
return cur_wp.value | ||
|
||
def relativeTo(task,newval=None): | ||
""" | ||
newval may be either DAQmx_Val_FirstSample 10424 | ||
or | ||
DAQmx_Val_CurrWritePos (10430) | ||
""" | ||
if not newval: | ||
val = mx.uInt64(0) | ||
mx.ni.DAQmxGetWriteRelativeTo(task, byref(val)) | ||
return val.value | ||
else: | ||
mx.CHK(mx.ni.DAQmxSetWriteRelativeTo(task,newval)) | ||
|
||
def resetRelativeTo(task): | ||
mx.CHK(mx.ni.DAQmxResetWriteRelativeTo(task,newval)) | ||
|
||
|
||
########## | ||
analog_output_term = r'Dev1/ao0' | ||
analog_input = r'Dev1/ai15' # connect analog input to this terminal, customize as you wish | ||
TERMINALEND = 'nrse' # consider 'rse' (referenced single-ended),'nrse' | ||
# (non-referenced single ended) for configuration | ||
# of analog input | ||
|
||
samplerate = 2000 | ||
nsamples = 1000 # if samplemode = 'continuous' used to determine buffer size | ||
samplemode = 'continuous' # or 'continuous' | ||
|
||
outputdata = np.arange(nsamples,dtype='float64') | ||
outputdata = np.sin(0.2*outputdata) | ||
output2 = np.sin(0.5*np.arange(nsamples,dtype='float64') ) | ||
print outputdata.shape | ||
|
||
|
||
|
||
|
||
itask = nidaqmx.AnalogInputTask() | ||
itask.create_voltage_channel(analog_input, min_val=0.0,max_val=10.0, | ||
terminal=TERMINALEND) | ||
itask.configure_timing_sample_clock(rate=samplerate, | ||
sample_mode=samplemode, | ||
samples_per_channel=nsamples) | ||
|
||
|
||
# print "input task buffer size", itask.get_buffer_size() | ||
# print itask.get_read_current_position() | ||
# get_regeneration | ||
|
||
otask = nidaqmx.AnalogOutputTask() | ||
|
||
otask.create_voltage_channel(analog_output_term, min_val=-10.0,max_val=10.0) | ||
|
||
otask.configure_timing_sample_clock(source=r'ai/SampleClock', | ||
rate=samplerate, | ||
sample_mode=samplemode, | ||
samples_per_channel=nsamples) | ||
|
||
otask.write(outputdata,auto_start=False) | ||
|
||
|
||
# start them both | ||
cur_wp = mx.uInt64(0) | ||
otask.start() | ||
itask.start() | ||
d1=itask.read(samples_per_channel=500) | ||
time.sleep(1.0) | ||
print "current read position", itask.get_read_current_position() | ||
mx.ni.DAQmxGetWriteCurrWritePos(otask, byref(cur_wp)) | ||
otask.write(output2) | ||
after = getCurWritePos(otask) | ||
time.sleep(0.1) | ||
# d2 = itask.read(samples_per_channel=500) | ||
# itask.wait_until_done(10.0) | ||
# print "otask.get_bufsize()",otask.get_bufsize() | ||
print "before cur_wp:",cur_wp.value, "and after:", after | ||
print "relativeTo:", relativeTo(otask) | ||
print "done" | ||
|
Oops, something went wrong.