def set_(self,wanted_position): """send a signal""" self.out=(wanted_position/self.gain)-self.offset out_a=c.comedi_from_phys(self.out,self.range_ds,self.maxdata) # convert the wanted_position c.comedi_data_write(self.device0,self.subdevice,self.channel,self.range_num,c.AREF_GROUND,out_a) # send the signal to the controler t=time.time() return (t,self.out)
def set_PID(self,wanted_position,sensor_input): """send a signal through a PID, based on the wanted command and the sensor_input""" self.time= time.time() self.out=(wanted_position/self.gain)-self.offset self.error=self.out-sensor_input self.I_term += self.Ki*self.error*(self.last_time-self.time) if self.I_term>self.out_max: self.I_term=self.out_max elif self.I_term<self.out_min: self.I_term=self.out_min self.out_PID=self.last_output+self.K*self.error+self.I_term-self.Kd*(sensor_input-self.last_sensor_input)/(self.last_time-self.time) if self.out_PID>self.out_max: self.out_PID=self.out_max elif self.out_PID<self.out_min: self.out_PID=self.out_min self.last_time=copy.copy(self.time) self.last_sensor_input=copy.copy(sensor_input) self.last_output=copy.copy(self.out_PID) out_a=c.comedi_from_phys(self.out_PID,self.range_ds,self.maxdata) # convert the wanted_position c.comedi_data_write(self.device0,self.subdevice,self.channel,self.range_num,c.AREF_GROUND,out_a) # send the signal to the controler t=time.time() return (t,self.out_PID)
def command(self,wanted_position): self.out=(wanted_position-self.offset)/self.gain out_a=c.comedi_from_phys(self.out,self.range_ds,self.maxdata) # convert the wanted_position c.comedi_data_write(self.device0,self.subdevice,self.channel,self.range_num,c.AREF_GROUND,out_a) # send the signal to the controler t_=datetime.datetime.now() t=(((((((t_.year*12)+t_.month)*30+t_.day)*24+t_.hour)*60+t_.minute)*60+t_.second)*1000000)+t_.microsecond return (t-t0,self.out)
def command_PID(self,wanted_position,sensor_input): self.time= time.time() self.out=(wanted_position-self.offset)/self.gain #print "sensor=%s" %sensor_input self.error=self.out-sensor_input self.I_term += self.Ki*self.error*(self.last_time-self.time) if self.I_term>self.out_max: self.I_term=self.out_max elif self.I_term<self.out_min: self.I_term=self.out_min self.out_PID=self.last_output+self.K*self.error+self.I_term-self.Kd*(sensor_input-self.last_sensor_input)/(self.last_time-self.time) if self.out_PID>self.out_max: self.out_PID=self.out_max elif self.out_PID<self.out_min: self.out_PID=self.out_min self.last_time=copy.copy(self.time) self.last_sensor_input=copy.copy(sensor_input) self.last_output=copy.copy(self.out_PID) #self.t.append(time.time()-t0) #print "I_term= %s, out_PID=%s" %(self.I_term, self.out_PID) out_a=c.comedi_from_phys(self.out_PID,self.range_ds,self.maxdata) # convert the wanted_position c.comedi_data_write(self.device0,self.subdevice,self.channel,self.range_num,c.AREF_GROUND,out_a) # send the signal to the controler t_=datetime.datetime.now() t=(((((((t_.year*12)+t_.month)*30+t_.day)*24+t_.hour)*60+t_.minute)*60+t_.second)*1000000)+t_.microsecond return (t-t0,self.out_PID)
def set_PID(self, wanted_position, sensor_input): """send a signal through a PID, based on the wanted command and the sensor_input""" self.time = time.time() self.out = (wanted_position / self.gain) - self.offset self.error = self.out - sensor_input self.I_term += self.Ki * self.error * (self.last_time - self.time) if self.I_term > self.out_max: self.I_term = self.out_max elif self.I_term < self.out_min: self.I_term = self.out_min self.out_PID = self.last_output + self.K * self.error + self.I_term - self.Kd * ( sensor_input - self.last_sensor_input) / (self.last_time - self.time) if self.out_PID > self.out_max: self.out_PID = self.out_max elif self.out_PID < self.out_min: self.out_PID = self.out_min self.last_time = copy.copy(self.time) self.last_sensor_input = copy.copy(sensor_input) self.last_output = copy.copy(self.out_PID) out_a = c.comedi_from_phys(self.out_PID, self.range_ds, self.maxdata) # convert the wanted_position c.comedi_data_write(self.device0, self.subdevice, self.channel, self.range_num, c.AREF_GROUND, out_a) # send the signal to the controler t = time.time() return (t, self.out_PID)
def putU(self, U): # Convert physical data to raw data U1_raw = comedi.comedi_from_phys(U[0],self.range_info_output_1, self.maxdata_output_1) # Write the raw data comedi.comedi_data_write(self.device , 1 , 0 , 0 , 0 , U1_raw) return U1_raw
def set_(self, wanted_position): """send a signal""" self.out = (wanted_position / self.gain) - self.offset out_a = c.comedi_from_phys(self.out, self.range_ds, self.maxdata) # convert the wanted_position c.comedi_data_write(self.device0, self.subdevice, self.channel, self.range_num, c.AREF_GROUND, out_a) # send the signal to the controler t = time.time() return (t, self.out)
def set_(self, wanted_position): self.out = (wanted_position - self.offset) / self.gain out_a = c.comedi_from_phys(self.out, self.range_ds, self.maxdata) # convert the wanted_position c.comedi_data_write(self.device0, self.subdevice, self.channel, self.range_num, c.AREF_GROUND, out_a) # send the signal to the controler #t_=datetime.datetime.now() #t=(((((((t_.year*12)+t_.month)*30+t_.day)*24+t_.hour)*60+t_.minute)*60+t_.second)*1000000)+t_.microsecond t = time.time()
def set_cmd(self, cmd): """Convert the tension value to a digital value and send it to the output.""" # self.out=(cmd/self.gain)-self.offset # out_a=c.comedi_from_phys(self.out,self.range_ds,self.maxdata) # convert the cmd to digital value self.out = (cmd * self.gain) + self.offset out_a = c.comedi_from_phys(self.out, self.range_ds, self.maxdata) # convert the cmd # print self.out, out_a c.comedi_data_write( self.device, self.subdevice, self.channel, self.range_num, c.AREF_GROUND, out_a ) # send the signal to the controler
def set_cmd(self, *cmd): """To set the value of the outputs (when specified) Takes as many argument as opened output channels""" assert len(cmd) == len(self.out_channels),\ "set_cmd takes {} args, but got {}".format( len(self.out_channels),len(cmd)) for val, chan in zip(cmd, self.out_channels): val = val * chan['gain'] + chan['offset'] out_a = c.comedi_from_phys(val, chan['range_ds'], chan['maxdata']) c.comedi_data_write(self.device, self.out_subdevice, chan['num'], chan['range_num'], c.AREF_GROUND, out_a)
def command_PID(self, wanted_position, sensor_input): self.time = time.time() self.out = (wanted_position - self.offset) / self.gain #print "sensor=%s" %sensor_input self.error = self.out - sensor_input self.I_term += self.Ki * self.error * (self.last_time - self.time) if self.I_term > self.out_max: self.I_term = self.out_max elif self.I_term < self.out_min: self.I_term = self.out_min self.out_PID = self.last_output + self.K * self.error + self.I_term - self.Kd * ( sensor_input - self.last_sensor_input) / (self.last_time - self.time) if self.out_PID > self.out_max: self.out_PID = self.out_max elif self.out_PID < self.out_min: self.out_PID = self.out_min self.last_time = copy.copy(self.time) self.last_sensor_input = copy.copy(sensor_input) self.last_output = copy.copy(self.out_PID) #self.t.append(time.time()-t0) #print "I_term= %s, out_PID=%s" %(self.I_term, self.out_PID) out_a = c.comedi_from_phys(self.out_PID, self.range_ds, self.maxdata) # convert the wanted_position c.comedi_data_write(self.device0, self.subdevice, self.channel, self.range_num, c.AREF_GROUND, out_a) # send the signal to the controler t_ = datetime.datetime.now() t = (((((( (t_.year * 12) + t_.month) * 30 + t_.day) * 24 + t_.hour) * 60 + t_.minute) * 60 + t_.second) * 1000000) + t_.microsecond return (t - t0, self.out_PID)