def configure_sample_clock(self, sample_clock_source=None, sample_rate=None, sample_clock_active_edge=None, sample_mode=None, samples_per_channel=None): """Configures sample clock timing for task.""" if sample_clock_source: self.sample_clock_source = sample_clock_source if sample_rate: self.sample_rate = sample_rate if sample_clock_active_edge: self.sample_clock_active_edge = sample_clock_active_edge if sample_mode: self.sample_mode = sample_mode if samples_per_channel: self.samples_per_channel = samples_per_channel else: self.samples_per_channel = int(self.sample_rate / 10) sample_mode = daqmx.parameters[self.sample_mode] sample_clock_active_edge = daqmx.parameters[ self.sample_clock_active_edge] daqmx.CfgSampClkTiming(self.handle, self.sample_clock_source, self.sample_rate, sample_clock_active_edge, sample_mode, self.samples_per_channel) self.sample_clock_configured = True
def main(period): taskHandle = daqmx.TaskHandle() daqmx.CreateTask("", taskHandle) physicalChannel = "Dev1/ao0" t = np.linspace(0, 2 * pi / period, 1000) data = 2 * np.sin(t) daqmx.CreateAOVoltageChan(taskHandle, physicalChannel, "", -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(taskHandle, "", 1000.0, daqmx.Val_Rising, daqmx.Val_ContSamps, 1000) daqmx.WriteAnalogF64(taskHandle, 1000, 0, 10.0, daqmx.Val_GroupByChannel, data) daqmx.StartTask(taskHandle) for n in range(100): time.sleep(0.3) daqmx.StopTask(taskHandle) daqmx.ClearTask(taskHandle)
def setup_DAQmx(self): # daqmx tasks self.AItask = daqmx.TaskHandle() self.AOtask = daqmx.TaskHandle() daqmx.CreateTask("", self.AItask) daqmx.CreateTask("", self.AOtask) # AI task setup for i in range(self.num_i): daqmx.CreateAIVoltageChan(self.AItask, self.i_channels[i], "", daqmx.Val_Diff, -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(self.AItask, "", self.i_frequency, daqmx.Val_Rising, daqmx.Val_ContSamps, self.i_size) # AO task setup daqmx.CreateAOVoltageChan(self.AOtask, self.o_channel, "", -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(self.AOtask, "", self.o_fre, daqmx.Val_Rising, daqmx.Val_ContSamps, self.o_buffer) daqmx.SetWriteRegenMode(self.AOtask,daqmx.Val_DoNotAllowRegen)
def setup_DAQmx(self): # daqmx tasks self.AItask = daqmx.TaskHandle() self.AOtask = daqmx.TaskHandle() daqmx.CreateTask("", self.AItask) daqmx.CreateTask("", self.AOtask) # AI task setup for i in range(self.NCH): daqmx.CreateAIVoltageChan(self.AItask, self.AIs[i], "", daqmx.Val_Diff, -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(self.AItask, "", self.IF, daqmx.Val_Rising, daqmx.Val_ContSamps, self.ASS) # AO task setup daqmx.CreateAOVoltageChan(self.AOtask, self.O1, "", -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(self.AOtask, "", self.OF, daqmx.Val_Rising, daqmx.Val_ContSamps, self.OB) daqmx.SetWriteRegenMode(self.AOtask,daqmx.Val_DoNotAllowRegen)
def start_daq(self): # Task parameters self.th = daqmx.TaskHandle() daqmx.CreateTask("", self.th) self.sr = 100.0 daqmx.CreateAIVoltageChan(self.th, self.phys_chan, "", daqmx.Val_Diff, -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(self.th, "", self.sr, daqmx.Val_Rising, daqmx.Val_ContSamps, 1000) daqmx.StartTask(self.th, fatalerror=False)
AItaskHandle = daqmx.TaskHandle() AOtaskHandle = daqmx.TaskHandle() daqmx.CreateTask("", AItaskHandle) daqmx.CreateTask("", AOtaskHandle) sr = 100.0 t = np.linspace(0, 100.0, 1000) data = 2 * np.sin(t) daqmx.CreateAOVoltageChan(AOtaskHandle, "Dev1/ao0", "", -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(AOtaskHandle, "", sr, daqmx.Val_Rising, daqmx.Val_ContSamps, 1000) daqmx.CreateAIVoltageChan(AItaskHandle, "Dev1/ai1", "", daqmx.Val_Diff, -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(AItaskHandle, "", sr, daqmx.Val_Rising, daqmx.Val_ContSamps, 1000) written = daqmx.WriteAnalogF64(AOtaskHandle, 1000, 0, 10.0, daqmx.Val_GroupByChannel, data) dataread, nread = daqmx.ReadAnalogF64(AItaskHandle, 1000, 10.0, daqmx.Val_GroupByChannel, 1000, 1) daqmx.StartTask(AOtaskHandle) daqmx.StartTask(AItaskHandle)
def main(): # Task parameters taskhandle = daqmx.TaskHandle() daqmx.CreateTask("", taskhandle) phys_chan = b"Dev1/ai0" nchan = 1 sr = 100.0 dt = 1 / sr totaltime = 3 nloops = int(sr * totaltime / 10) # Analog read parameters samps_per_chan = 10 timeout = 10 fillmode = daqmx.Val_GroupByChannel array_size_samps = 1000 daqmx.CreateAIVoltageChan(taskhandle, phys_chan, "", daqmx.Val_Diff, -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(taskhandle, "", sr, daqmx.Val_Rising, daqmx.Val_ContSamps, 1000) # Parameters for plotting plotting = True plot_dynamic = True if plotting == True and plot_dynamic == True: plt.ioff() fig = plt.figure() ax = fig.add_subplot(111) line, = ax.plot([], ) fig.show() daqmx.StartTask(taskhandle, fatalerror=False) v = np.array([]) for n in range(nloops): data, spc = daqmx.ReadAnalogF64(taskhandle, samps_per_chan, timeout, fillmode, array_size_samps, nchan) v = np.append(v, data[:, 0]) time.sleep(0.001) t = np.float64(range(len(v))) * dt if plotting == True and plot_dynamic == True: line.set_xdata(t) line.set_ydata(v) ax.relim() ax.autoscale_view() fig.canvas.draw() daqmx.ClearTask(taskhandle) print("Task complete") if plotting == True: if plot_dynamic == False: plt.plot(t, v) plt.show() return t, data
def __init__(self, usetrigger=True): # Some parameters for the thread self.usetrigger = usetrigger self.collect = True # Create some meta data for the run self.metadata = {} # Initialize sample rate self.sr = 2000.0 self.metadata["Sample rate (Hz)"] = self.sr self.nsamps = int(self.sr / 10) # Create a dict of arrays for storing data self.data = { "turbine_angle": np.array([]), "turbine_rpm": np.array([]), "torque_trans": np.array([]), "torque_arm": np.array([]), "drag_left": np.array([]), "drag_right": np.array([]), "t": np.array([]), "carriage_pos": np.array([]) } # Create one analog and one digital task # Probably should be a bridge task in there too! self.analogtask = daqmx.TaskHandle() self.carpostask = daqmx.TaskHandle() self.turbangtask = daqmx.TaskHandle() # Create tasks daqmx.CreateTask("", self.analogtask) daqmx.CreateTask("", self.carpostask) daqmx.CreateTask("", self.turbangtask) # Add channels to tasks self.analogchans = [ "torque_trans", "torque_arm", "drag_left", "drag_right" ] self.carposchan = "carriage_pos" self.turbangchan = "turbine_angle" daqmx.AddGlobalChansToTask(self.analogtask, self.analogchans) daqmx.AddGlobalChansToTask(self.carpostask, self.carposchan) daqmx.AddGlobalChansToTask(self.turbangtask, self.turbangchan) # Get channel information to add to metadata self.chaninfo = {} for channame in self.analogchans: self.chaninfo[channame] = {} scale = channame + "_scale" self.chaninfo[channame]["Scale name"] = scale self.chaninfo[channame]["Scale slope"] = \ daqmx.GetScaleLinSlope(scale) self.chaninfo[channame]["Scale y-intercept"] = \ daqmx.GetScaleLinYIntercept(scale) self.chaninfo[channame]["Scaled units"] = \ daqmx.GetScaleScaledUnits(scale) self.chaninfo[channame]["Prescaled units"] = \ daqmx.GetScalePreScaledUnits(scale) self.chaninfo[self.turbangchan] = {} self.chaninfo[self.turbangchan]["Pulses per rev"] = \ daqmx.GetCIAngEncoderPulsesPerRev(self.turbangtask, self.turbangchan) self.chaninfo[self.turbangchan]["Units"] = \ daqmx.GetCIAngEncoderUnits(self.turbangtask, self.turbangchan) self.chaninfo[self.carposchan] = {} self.chaninfo[self.carposchan]["Distance per pulse"] = \ daqmx.GetCILinEncoderDisPerPulse(self.carpostask, self.carposchan) self.chaninfo[self.carposchan]["Units"] = \ daqmx.GetCILinEncoderUnits(self.carpostask, self.carposchan) self.metadata["Channel info"] = self.chaninfo # Configure sample clock timing daqmx.CfgSampClkTiming(self.analogtask, "", self.sr, daqmx.Val_Rising, daqmx.Val_ContSamps, self.nsamps) # Get source for analog sample clock trigname = daqmx.GetTerminalNameWithDevPrefix(self.analogtask, "ai/SampleClock") daqmx.CfgSampClkTiming(self.carpostask, trigname, self.sr, daqmx.Val_Rising, daqmx.Val_ContSamps, self.nsamps) daqmx.CfgSampClkTiming(self.turbangtask, trigname, self.sr, daqmx.Val_Rising, daqmx.Val_ContSamps, self.nsamps) # If using trigger for analog signals set source to chassis PFI0 if self.usetrigger: daqmx.CfgDigEdgeStartTrig(self.analogtask, "/cDAQ9188-16D66BB/PFI0", daqmx.Val_Falling) # Set trigger functions for counter channels daqmx.SetStartTrigType(self.carpostask, daqmx.Val_DigEdge) daqmx.SetStartTrigType(self.turbangtask, daqmx.Val_DigEdge) trigsrc = \ daqmx.GetTrigSrcWithDevPrefix(self.analogtask, "ai/StartTrigger") print("Trigger source:", trigsrc) daqmx.SetDigEdgeStartTrigSrc(self.carpostask, trigsrc) daqmx.SetDigEdgeStartTrigSrc(self.turbangtask, trigsrc) daqmx.SetDigEdgeStartTrigEdge(self.carpostask, daqmx.Val_Rising) daqmx.SetDigEdgeStartTrigEdge(self.turbangtask, daqmx.Val_Rising)
Created on Fri Jun 07 00:29:49 2013 @author: Pete """ import daqmx import time import numpy as np aiTaskHandle = daqmx.TaskHandle() diTaskHandle = daqmx.TaskHandle() daqmx.CreateTask("", aiTaskHandle) daqmx.CreateAIVoltageChan(aiTaskHandle, "Dev1/ai0", "", daqmx.Val_Diff, 0.0, 10.0, daqmx.Val_Volts) daqmx.CfgSampClkTiming(aiTaskHandle, "", 100.0, daqmx.Val_Rising, daqmx.Val_ContSamps, 1000) trigname = daqmx.GetTerminalNameWithDevPrefix(aiTaskHandle, "ai/SampleClock") daqmx.CreateTask("", diTaskHandle) daqmx.CreateDIChan(diTaskHandle, "Dev1/port0", "", daqmx.Val_ChanForAllLines) daqmx.CfgSampClkTiming(diTaskHandle, trigname, 100.0, daqmx.Val_Rising, daqmx.Val_ContSamps, 1000) daqmx.StartTask(diTaskHandle) daqmx.StartTask(aiTaskHandle) adata = daqmx.ReadAnalogF64(aiTaskHandle, 1000, 10.0, daqmx.Val_GroupByChannel, 1000, 1) ddata = daqmx.ReadDigitalU32(diTaskHandle, 1000, 10.0, daqmx.Val_GroupByChannel, 1000, 1)
numperiods = 3 period = 2.0 buffsize = 2000 sr = buffsize / period t = np.linspace(0, 4 * np.pi, buffsize) dataw = 2 * np.sin(t) #+ 0.5*np.random.random(len(t)) AItaskHandle = daqmx.TaskHandle() AOtaskHandle = daqmx.TaskHandle() daqmx.CreateTask("", AItaskHandle) daqmx.CreateAIVoltageChan(AItaskHandle, "Dev1/ai1", "", daqmx.Val_Diff, -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(AItaskHandle, "", sr, daqmx.Val_Rising, daqmx.Val_ContSamps, buffsize) trigName = daqmx.GetTerminalNameWithDevPrefix(AItaskHandle, "ai/StartTrigger") daqmx.CreateTask("", AOtaskHandle) daqmx.CreateAOVoltageChan(AOtaskHandle, "Dev1/ao0", "", -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(AOtaskHandle, "", sr / 2, daqmx.Val_Rising, daqmx.Val_ContSamps, buffsize) daqmx.CfgDigEdgeStartTrig(AOtaskHandle, trigName, daqmx.Val_Rising) class MyList(list): pass # list where the data are stored
def run(self): self.rampeddown = False self.cleared = False self.making = False # Compute the voltage time series associated with the wave self.wave.gen_ts_volts() # Get parameters from the wave object self.period = self.wave.period self.height = self.wave.height self.buffsize = self.wave.sbuffsize self.sr = self.wave.sr # Get data to write from the wave object self.ts_plot = self.wave.ts_elev self.dataw = self.wave.ts_volts # If random waves, divide up time series into 120 256 sample parts if self.wavetype != "Regular": tsparts = np.reshape(self.dataw, (120, 256)) self.dataw = tsparts[0, :] # Compute spectrum for plot if self.wavetype == "Regular": self.outf, self.outspec = self.wave.comp_spec() else: self.outf, self.outspec = self.wave.f, self.wave.spec # Ramp time series rampup_ts = ramp_ts(self.dataw, "up") # Set making variable true self.making = True self.AOtaskHandle = daqmx.TaskHandle() daqmx.CreateTask("", self.AOtaskHandle) daqmx.CreateAOVoltageChan(self.AOtaskHandle, "Dev1/ao0", "", -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(self.AOtaskHandle, "", self.sr, daqmx.Val_Rising, daqmx.Val_ContSamps, self.buffsize) daqmx.SetWriteRegenMode(self.AOtaskHandle, daqmx.Val_DoNotAllowRegen) # Setup a callback function to run once the DAQmx driver finishes def DoneCallback_py(taskHandle, status, callbackData_ptr): self.rampeddown = True return 0 DoneCallback = daqmx.DoneEventCallbackPtr(DoneCallback_py) daqmx.RegisterDoneEvent(self.AOtaskHandle, 0, DoneCallback, None) # Output the rampup time series daqmx.WriteAnalogF64(self.AOtaskHandle, self.buffsize, False, 10.0, daqmx.Val_GroupByChannel, rampup_ts) daqmx.StartTask(self.AOtaskHandle) # Wait a second to allow the DAQmx buffer to empty if self.wavetype == "Regular": time.sleep(self.period * 0.99) # was self.period*0.4 else: time.sleep(0.99) # Set iteration variable to keep track of how many chunks of data # have been written i = 1 # Main running loop that writes data to DAQmx buffer while self.enable: writeSpaceAvail = daqmx.GetWriteSpaceAvail(self.AOtaskHandle) if writeSpaceAvail >= self.buffsize: if self.wavetype != "Regular": if i >= 120: self.dataw = tsparts[i % 120, :] else: self.dataw = tsparts[i, :] daqmx.WriteAnalogF64(self.AOtaskHandle, self.buffsize, False, 10.0, daqmx.Val_GroupByChannel, self.dataw) i += 1 if self.wavetype == "Regular": time.sleep(0.99 * self.period) else: time.sleep(0.99) # Was self.period # After disabled, initiate rampdown time series if self.wavetype != "Regular": if i >= 120: self.rampdown_ts = ramp_ts(tsparts[i % 120, :], "down") else: self.rampdown_ts = ramp_ts(tsparts[i, :], "down") else: self.rampdown_ts = ramp_ts(self.dataw, "down") # Write rampdown time series daqmx.WriteAnalogF64(self.AOtaskHandle, self.buffsize, False, 10.0, daqmx.Val_GroupByChannel, self.rampdown_ts) # Keep running, part of PyDAQmx callback syntax while True: pass