def read(self): """Reads from the channels in the task.""" array_size_samps = self.sample_rate * self.samples_per_channel fillmode = daqmx.parameters[self.fillmode] if self.task_type == "analog input": self.newdata, self.samples_per_channel_received = daqmx.ReadAnalogF64( self.handle, self.samples_per_channel, self.timeout, fillmode, array_size_samps, len(self.channels)) if self.append_data: self.newdf = pd.DataFrame() if self.time_array: newtime = np.arange(self.samples_per_channel_received + 1) / self.sample_rate if len(self.data["time"]) == 0: self.newdf["time"] = newtime[:-1] else: last_time = self.data["time"].values[-1] newtime += last_time self.newdf["time"] = newtime[1:] for n, channel in enumerate(self.channels): self.newdf[channel.name] = self.newdata[:, n] self.data = self.data.append(self.newdf, ignore_index=False) if self.autolog: self.do_autolog() if self.autotrim: self.autotrim_dataframe() return self.newdata, self.samples_per_channel_received
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 get_data(self): while True: # Parameters for analog read samps_per_chan = int(self.sr/10) timeout = 10 fillmode = daqmx.Val_GroupByChannel array_size_samps = 1000 nchan = 1 data1, spc = daqmx.ReadAnalogF64(self.th, samps_per_chan, timeout, fillmode, array_size_samps, nchan) self.data = np.append(self.data, data1[:,0])
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
def get_observation(self): read_t, _ = daqmx.ReadAnalogF64(self.AItask,self.i_size,self.timeout,self.mode,self.i_size,self.num_i) return read_t / self.sens_coff
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) print written, "samples written" print nread, "samples read" plt.plot(dataread) daqmx.StopTask(AItaskHandle) daqmx.ClearTask(AItaskHandle) daqmx.StopTask(AOtaskHandle) daqmx.ClearTask(AOtaskHandle)
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 run(self, Nepisode, targetQN, train=True): self.setup_DAQmx() # Parameters for plotting # initiate seq = np.zeros((self.num,self.NICH)) read = np.zeros((0,self.NCH)) state_t = np.zeros((self.SPC,self.NICH)) b = np.ones(self.num_mave)/self.num_mave tilen = int(self.intispn / 0.01) # start read analog daqmx.StartTask(self.AItask, fatalerror=False) # first loop # you read # you do not act # this loop is for caliburation of reward # you need to satrt writeanalog in first loop # 'cause the function takes time to start for n in range(self.nloops): read_t, _ = daqmx.ReadAnalogF64(self.AItask,self.SPC,self.TO,self.MODE,self.ASS,self.NCH) read_t = read_t/self.sensor_coff # moving average filter for ich in range(self.NICH): state_t[self.num_mave-1:,ich] = np.convolve(read_t[:,self.IAIs[ich]], b, mode='vaild') state_ave = ( np.average(np.split(state_t,int(self.num/tilen),0),axis=1) + self.dynamic_pressre )/self.dynamic_pressre seq[self.num-int(self.num/tilen):self.num,:] = np.round(state_ave,2) # action # adopt output timing and action zero if n!=10 or n!=20 or n!=40 or n!=50 or n!=60 or n!=80 or n!=90: daqmx.WriteAnalogF64(self.AOtask,self.OB,1,self.TO,daqmx.Val_GroupByChannel,np.zeros(self.OB)) # reward reward_t = np.average(read_t[:,self.CH100]) self.memory.add_local(seq,0,reward_t,0) read = np.append(read,read_t,axis=0) seq = shift(seq,[-self.num/tilen,0],cval=0) # calibulate self.memory.calc_calibulation() # second loop # you read # you act for n in range(self.nloops): read_t, _ = daqmx.ReadAnalogF64(self.AItask,self.SPC,self.TO,self.MODE,self.ASS,self.NCH) read_t = read_t/self.sensor_coff # moving average filter for ich in range(self.NICH): state_t[self.num_mave-1:,ich] = np.convolve(read_t[:,self.IAIs[ich]], b, mode='vaild') state_ave = ( np.average(np.split(state_t,int(self.num/tilen),0),axis=1) + self.dynamic_pressre )/self.dynamic_pressre seq[self.num-int(self.num/tilen):self.num,:] = np.round(state_ave,2) # action ai = self.actor.get_action(seq.reshape(-1,self.num,self.NICH), self.QN,train) daqmx.WriteAnalogF64(self.AOtask,self.OB,1,self.TO,daqmx.Val_GroupByChannel,self.PA[ai,:]*3) # reward reward_t = np.average(read_t[:,self.CH100]) self.memory.add_local(seq,ai,reward_t,0) read = np.append(read,read_t,axis=0) seq = shift(seq,[-self.num/tilen,0],cval=0) # third loop # you read # you do not act # make sure PA turn off for n in range(self.nloops): read_t, _ = daqmx.ReadAnalogF64(self.AItask,self.SPC,self.TO,self.MODE,self.ASS,self.NCH) read_t = read_t/self.sensor_coff # moving average filter for ich in range(self.NICH): state_t[self.num_mave-1:,ich] = np.convolve(read_t[:,self.IAIs[ich]], b, mode='vaild') state_ave = ( np.average(np.split(state_t,int(self.num/tilen),0),axis=1) + self.dynamic_pressre )/self.dynamic_pressre seq[self.num-int(self.num/tilen):self.num,:] = np.round(state_ave,2) # action # action zero daqmx.WriteAnalogF64(self.AOtask,self.OB,1,self.TO,daqmx.Val_GroupByChannel,np.zeros(self.OB)) # reward reward_t = np.average(read_t[:,self.CH100]) self.memory.add_local(seq,0,reward_t,0) read = np.append(read,read_t,axis=0) seq = shift(seq,[-self.num/tilen,0],cval=0) # stop DAQmx self.stop_DAQmx() # edit experience in buffer self.memory.edit_experience_local() self.save(read,Nepisode) total_reward = self.memory.totalreward() # move current experience to global buffer self.memory.add_global(total_reward) if (self.memory.len() > self.batch_num) and train: self.loss=self.QN.replay(self.memory,self.batch_num,self.gamma,targetQN) self.actor.reduce_epsilon() return total_reward, self.loss
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) print adata print ddata daqmx.StopTask(diTaskHandle) daqmx.StopTask(aiTaskHandle) daqmx.ClearTask(diTaskHandle) daqmx.ClearTask(aiTaskHandle)