def run(self, dur): """Start DAQmx tasks.""" # Acquire and throwaway samples for alignment # Need to set these up on a different task? # Callback code from PyDAQmx class MyList(list): pass # List where the data are stored data = MyList() id_data = daqmx.create_callbackdata_id(data) def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): """Function called every N samples""" callbackdata = daqmx.get_callbackdata_from_id(callbackData_ptr) data, npoints = daqmx.ReadAnalogF64( taskHandle, self.nsamps, 10.0, daqmx.Val_GroupByChannel, self.nsamps, len(self.analogchans) ) callbackdata.extend(data.tolist()) self.data["torque_trans"] = np.append(self.data["torque_trans"], data[:, 0], axis=0) self.data["torque_arm"] = np.append(self.data["torque_arm"], data[:, 1], axis=0) self.data["drag_left"] = np.append(self.data["drag_left"], data[:, 2], axis=0) self.data["drag_right"] = np.append(self.data["drag_right"], data[:, 3], axis=0) self.data["t"] = np.arange(len(self.data["torque_trans"]), dtype=float) / self.sr carpos, cpoints = daqmx.ReadCounterF64(self.carpostask, self.nsamps, 10.0, self.nsamps) self.data["carriage_pos"] = np.append(self.data["carriage_pos"], carpos) turbang, cpoints = daqmx.ReadCounterF64(self.turbangtask, self.nsamps, 10.0, self.nsamps) self.data["turbine_angle"] = np.append(self.data["turbine_angle"], turbang) self.data["turbine_rpm"] = ts.smooth( fdiff.second_order_diff(self.data["turbine_angle"], self.data["t"]) / 6.0, 50 ) return 0 # The function should return an integer # Convert the python callback function to a CFunction EveryNCallback = daqmx.EveryNSamplesEventCallbackPtr(EveryNCallback_py) daqmx.RegisterEveryNSamplesEvent( self.analogtask, daqmx.Val_Acquired_Into_Buffer, self.nsamps, 0, EveryNCallback, id_data ) def DoneCallback_py(taskHandle, status, callbackData_ptr): print("Status", status.value) return 0 DoneCallback = daqmx.DoneEventCallbackPtr(DoneCallback_py) daqmx.RegisterDoneEvent(self.analogtask, 0, DoneCallback, None) # Start the tasks daqmx.StartTask(self.carpostask) daqmx.StartTask(self.turbangtask) daqmx.StartTask(self.analogtask) time.sleep(dur) self.clear()
def run(self): self.cleared = False self.AItaskHandle = daqmx.TaskHandle() daqmx.CreateTask("", self.AItaskHandle) daqmx.CreateAIVoltageChan(self.AItaskHandle, "Dev1/ai1", "", daqmx.Val_Diff, -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(self.AItaskHandle, "", self.sr, daqmx.Val_Rising, daqmx.Val_ContSamps, 20) class MyList(list): pass # list where the data are stored data = MyList() id_data = daqmx.create_callbackdata_id(data) def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): callbackdata = daqmx.get_callbackdata_from_id(callbackData_ptr) data, npoints = daqmx.ReadAnalogF64(taskHandle, 20, 10.0, daqmx.Val_GroupByChannel, 20, 1) callbackdata.extend(data.tolist()) self.npdata = np.append(self.npdata, data) return 0 # The function should return an integer # Convert the python function to a CFunction EveryNCallback = daqmx.EveryNSamplesEventCallbackPtr(EveryNCallback_py) daqmx.RegisterEveryNSamplesEvent(self.AItaskHandle, daqmx.Val_Acquired_Into_Buffer, 20, 0, EveryNCallback, id_data) def DoneCallback_py(taskHandle, status, callbackData_ptr): print "Status", status.value return 0 DoneCallback = daqmx.DoneEventCallbackPtr(DoneCallback_py) daqmx.RegisterDoneEvent(self.AItaskHandle, 0, DoneCallback, None) daqmx.StartTask(self.AItaskHandle) while True: pass
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 data = MyList() id_data = daqmx.create_callbackdata_id(data) def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): callbackdata = daqmx.get_callbackdata_from_id(callbackData_ptr) data, npoints = daqmx.ReadAnalogF64(taskHandle, buffsize, 10.0, daqmx.Val_GroupByChannel, buffsize, 1) callbackdata.extend(data.tolist()) print "Acquired %d samples"%len(data) saved_data = np.load(filename) new_saved_data = np.append(saved_data, np.asarray(data)) np.save(filename, new_saved_data) return 0 # The function should return an integer
def makewaves(self, height, period): self.cleared = False self.cycle = 0 buffsize = 1000 self.sr = buffsize/period t = np.linspace(0, 2*np.pi, buffsize) self.dataw = height/2*np.sin(t) self.AItaskHandle = daqmx.TaskHandle() self.AOtaskHandle = daqmx.TaskHandle() daqmx.CreateTask("", self.AItaskHandle) daqmx.CreateAIVoltageChan(self.AItaskHandle, "Dev1/ai1", "", daqmx.Val_Diff, -10.0, 10.0, daqmx.Val_Volts, None) daqmx.CfgSampClkTiming(self.AItaskHandle, "", self.sr, daqmx.Val_Rising, daqmx.Val_ContSamps, buffsize) trigName = daqmx.GetTerminalNameWithDevPrefix(self.AItaskHandle, "ai/StartTrigger") 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, buffsize) daqmx.CfgDigEdgeStartTrig(self.AOtaskHandle, trigName, daqmx.Val_Rising) class MyList(list): pass # list where the data are stored data = MyList() self.npdata = np.array([]) id_data = daqmx.create_callbackdata_id(data) def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): callbackdata = daqmx.get_callbackdata_from_id(callbackData_ptr) data, npoints = daqmx.ReadAnalogF64(taskHandle, buffsize/20, 10.0, daqmx.Val_GroupByChannel, buffsize/20, 1) callbackdata.extend(data.tolist()) self.npdata = np.append(self.npdata, data) return 0 # The function should return an integer # Convert the python function to a CFunction EveryNCallback = daqmx.EveryNSamplesEventCallbackPtr(EveryNCallback_py) daqmx.RegisterEveryNSamplesEvent(self.AItaskHandle, daqmx.Val_Acquired_Into_Buffer, buffsize/20, 0, EveryNCallback, id_data) def DoneCallback_py(taskHandle, status, callbackData_ptr): print "Status", status.value return 0 DoneCallback = daqmx.DoneEventCallbackPtr(DoneCallback_py) daqmx.RegisterDoneEvent(self.AItaskHandle, 0, DoneCallback, None) daqmx.WriteAnalogF64(self.AOtaskHandle, buffsize, False, 10.0, daqmx.Val_GroupByChannel, self.dataw) daqmx.StartTask(self.AOtaskHandle) daqmx.StartTask(self.AItaskHandle) while True: pass
def run(self, dur): """Start DAQmx tasks.""" # Acquire and throwaway samples for alignment # Need to set these up on a different task? # Callback code from PyDAQmx class MyList(list): pass # List where the data are stored data = MyList() id_data = daqmx.create_callbackdata_id(data) def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): """Function called every N samples""" callbackdata = daqmx.get_callbackdata_from_id(callbackData_ptr) data, npoints = daqmx.ReadAnalogF64(taskHandle, self.nsamps, 10.0, daqmx.Val_GroupByChannel, self.nsamps, len(self.analogchans)) callbackdata.extend(data.tolist()) self.data["torque_trans"] = np.append(self.data["torque_trans"], data[:, 0], axis=0) self.data["torque_arm"] = np.append(self.data["torque_arm"], data[:, 1], axis=0) self.data["drag_left"] = np.append(self.data["drag_left"], data[:, 2], axis=0) self.data["drag_right"] = np.append(self.data["drag_right"], data[:, 3], axis=0) self.data["t"] = np.arange(len(self.data["torque_trans"]), dtype=float) / self.sr carpos, cpoints = daqmx.ReadCounterF64(self.carpostask, self.nsamps, 10.0, self.nsamps) self.data["carriage_pos"] = np.append(self.data["carriage_pos"], carpos) turbang, cpoints = daqmx.ReadCounterF64(self.turbangtask, self.nsamps, 10.0, self.nsamps) self.data["turbine_angle"] = np.append(self.data["turbine_angle"], turbang) self.data["turbine_rpm"] \ = ts.smooth(fdiff.second_order_diff(self.data["turbine_angle"], self.data["t"])/6.0, 50) return 0 # The function should return an integer # Convert the python callback function to a CFunction EveryNCallback = daqmx.EveryNSamplesEventCallbackPtr(EveryNCallback_py) daqmx.RegisterEveryNSamplesEvent(self.analogtask, daqmx.Val_Acquired_Into_Buffer, self.nsamps, 0, EveryNCallback, id_data) def DoneCallback_py(taskHandle, status, callbackData_ptr): print("Status", status.value) return 0 DoneCallback = daqmx.DoneEventCallbackPtr(DoneCallback_py) daqmx.RegisterDoneEvent(self.analogtask, 0, DoneCallback, None) # Start the tasks daqmx.StartTask(self.carpostask) daqmx.StartTask(self.turbangtask) daqmx.StartTask(self.analogtask) time.sleep(dur) self.clear()
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 data = MyList() id_data = daqmx.create_callbackdata_id(data) def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): callbackdata = daqmx.get_callbackdata_from_id(callbackData_ptr) data, npoints = daqmx.ReadAnalogF64(taskHandle, buffsize, 10.0, daqmx.Val_GroupByChannel, buffsize, 1) callbackdata.extend(data.tolist()) print "Acquired %d samples" % len(data) saved_data = np.load(filename) new_saved_data = np.append(saved_data, np.asarray(data)) np.save(filename, new_saved_data)