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)
def __init__(self, name=""): PyDaqMxTask.__init__(self) self.name = name self.handle = daqmx.TaskHandle() self.TaskHandle = self.handle self.taskHandle = self.handle daqmx.CreateTask(name.encode(), self.handle) self.sample_rate = 100.0 self.timeout = 10.0 self.samples_per_channel = 10 self.channels = [] self.sample_clock_source = "" self.sample_clock_active_edge = "rising" self.sample_mode = "continuous samples" self.sample_clock_configured = False self.fillmode = "group by channel" self.task_type = "" self.append_data = False self.autolog = False self.autotrim_limit = 100000 # Maximum number of samples to keep self.autotrim = False # Only applicable if appending data
def main(): # Initializing task taskhandle = daqmx.TaskHandle() daqmx.CreateTask("", taskhandle) phys_chan = "Dev1/ctr0" #Timing parameters rate = 200.0 initialdelay = 0 lowtime = 1 / rate / 2 hightime = 1 / rate / 2 daqmx.CreateCOPulseChanTime(taskhandle, phys_chan, "", daqmx.Val_Seconds, daqmx.Val_Low, initialdelay, lowtime, hightime, False) print "Sending trigger pulse to", phys_chan + "..." daqmx.StartTask(taskhandle, fatalerror=True) print "Success!" daqmx.WaitUntilTaskDone(taskhandle, timeout=10, fatalerror=False) daqmx.ClearTask(taskhandle, fatalerror=False)
def main(): taskhandle = daqmx.TaskHandle() daqmx.CreateTask("", taskhandle) phys_chan = "cDAQ9188-16D66BBMod3/ctr2" globalchan = "VecPulse" #Timing parameters rate = 200 # Pulse rate in Hz initialdelay = 0 lowtime = 1 / rate / 2 hightime = 1 / rate / 2 # daqmx.CreateCOPulseChanTime(taskhandle, phys_chan, "", daqmx.Val_Seconds, # daqmx.Val_Low, initialdelay, lowtime, hightime, # False) daqmx.AddGlobalChansToTask(taskhandle, globalchan) daqmx.CfgImplicitTiming(taskhandle, daqmx.Val_FiniteSamps, 1) # Set up communication with motion controller simulator = False # Parameters for plotting plotting = False plot_dynamic = False if simulator == True: hcomm = acsc.OpenCommDirect() else: hcomm = acsc.OpenCommEthernetTCP("10.0.0.100", 701) axis = 5 buffno = 7 target = 0 timeout = 10 # Initialize arrays for storing time and position t = np.array(0) x = np.array(0) if plotting == True and plot_dynamic == True: plt.ioff() fig = plt.figure() ax = fig.add_subplot(111) # plt.xlim(0, timeout) # plt.ylim(0, target) line, = ax.plot([], []) fig.show() if hcomm == acsc.INVALID: print "Cannot connect to controller. Error:", acsc.GetLastError() else: # acsc.Enable(hcomm, axis) rpos = acsc.GetRPosition(hcomm, axis) x = rpos t0 = time.time() if simulator == True: acsc.ToPoint(hcomm, 0, axis, target + 50000) else: acsc.RunBuffer(hcomm, buffno, None, None) while True: rpos = acsc.GetRPosition(hcomm, axis) if rpos >= target: break x = np.append(x, rpos) t = np.append(t, time.time() - t0) if plotting == True and plot_dynamic == True: line.set_xdata(t) line.set_ydata(x) ax.relim() ax.autoscale_view() fig.canvas.draw() print "Axis is", acsc.GetAxisState(hcomm, axis) + '...' if time.time() - t0 > timeout: print "Motion timeout" print "Final axis position:", rpos break time.sleep(0.001) print "Target reached. Sending trigger pulse to", globalchan + "..." daqmx.StartTask(taskhandle, fatalerror=False) daqmx.WaitUntilTaskDone(taskhandle, timeout=10, fatalerror=False) daqmx.ClearTask(taskhandle, fatalerror=False) acsc.CloseComm(hcomm) print "Triggered at", np.max(x) if plotting == True: if plot_dynamic == False: plt.plot(t, x) plt.show() return t, x
# -*- coding: utf-8 -*- """ Created on Fri Jun 07 12:58:41 2013 @author: Pete """ import daqmx import numpy as np from numpy import pi import time import matplotlib.pyplot as plt 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)
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)
def test_GetTrigSrcWithDevPrefix(): th = daqmx.TaskHandle() daqmx.CreateTask("", th) daqmx.AddGlobalChansToTask(th, "drag_left") print(daqmx.GetTrigSrcWithDevPrefix(th, "PFI0"))
def test_global_virtual_channel(): th = daqmx.TaskHandle() daqmx.CreateTask("", th) daqmx.AddGlobalChansToTask(th, "drag_left") daqmx.AddGlobalChansToTask(th, ["drag_right", "torque_trans"]) daqmx.ClearTask(th)
# -*- coding: utf-8 -*- """ 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,
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