Beispiel #1
0
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)
Beispiel #2
0
 def write_daqmx(self,ai):
     daqmx.WriteAnalogF64(self.AOtask,self.o_buffer,1,self.timeout,daqmx.Val_GroupByChannel,self.pa[ai,:]*3)
Beispiel #3
0
 def write_daqmx_zero(self):
     daqmx.WriteAnalogF64(self.AOtask,self.o_buffer,1,self.timeout,daqmx.Val_GroupByChannel,np.zeros(self.o_buffer))
Beispiel #4
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)

print written, "samples written"
print nread, "samples read"

plt.plot(dataread)

daqmx.StopTask(AItaskHandle)
daqmx.ClearTask(AItaskHandle)
daqmx.StopTask(AOtaskHandle)
Beispiel #5
0
    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 
Beispiel #6
0
EveryNCallback = daqmx.EveryNSamplesEventCallbackPtr(EveryNCallback_py)

daqmx.RegisterEveryNSamplesEvent(AItaskHandle, daqmx.Val_Acquired_Into_Buffer,
                                 buffsize, 0, EveryNCallback, id_data)


def DoneCallback_py(taskHandle, status, callbackData_ptr):
    print "Status", status.value
    return 0


DoneCallback = daqmx.DoneEventCallbackPtr(DoneCallback_py)

daqmx.RegisterDoneEvent(AItaskHandle, 0, DoneCallback, None)

written = daqmx.WriteAnalogF64(AOtaskHandle, buffsize, False, 10.0,
                               daqmx.Val_GroupByChannel, dataw)

daqmx.StartTask(AOtaskHandle)
daqmx.StartTask(AItaskHandle)

t0 = time.time()
elapsed = 0.0

while elapsed < period * numperiods:
    elapsed = time.time() - t0

daqmx.StopTask(AItaskHandle)
daqmx.ClearTask(AItaskHandle)
daqmx.StopTask(AOtaskHandle)
daqmx.ClearTask(AOtaskHandle)
Beispiel #7
0
    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