def imagecapture_part(xb, yb, xe, ye, nx, ny, n): dev = comedi.comedi_open("/dev/comedi0") outsubdevice = 1 insubdevice = 0 xchannel = 0 ychannel = 1 inchannel = 0 analogref = comedi.AREF_GROUND arange = 0 image = numpy.zeros((nx,ny), float) for y in range(ny, 0, -1): y = y-1 write_data = int( (float(y)/(ny-1) *(ye-yb) + yb ) * 65535) msg = comedi.comedi_data_write(dev, outsubdevice, ychannel, arange, analogref, write_data) for x in range(0, nx): # print "( " + str(x) + ", " + str(y) + ")" write_data = int( (float(x)/(nx-1) * (xe-xb) + xb ) * 65535) msg = comedi.comedi_data_write(dev, outsubdevice, xchannel, arange, analogref, write_data) pixel = 0.0 for i in range(0, n): result = comedi.comedi_data_read(dev,insubdevice,inchannel,arange,analogref) pixel = pixel + float(result[1]) pixel = pixel/n # print pixel, msg=result[0] image[x][y] = pixel print comedi.comedi_close(dev) return image
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 getimage(nx, ny, n): dev = comedi.comedi_open("/dev/comedi0") outsubdevice = 1 insubdevice = 0 xchannel = 0 ychannel = 1 inchannel = 0 analogref = comedi.AREF_GROUND arange = 0 image = numpy.zeros((nx,ny), float) for y in range(0, ny): write_data = int( (float(y)/(ny-1) ) * 65535) msg = comedi.comedi_data_write(dev, outsubdevice, ychannel, arange, analogref, write_data) for x in range(0, nx): write_data = int( (float(x)/(nx-1) ) * 65535) msg = comedi.comedi_data_write(dev, outsubdevice, xchannel, arange, analogref, write_data) pixel = 0.0 for i in range(0, n): result = comedi.comedi_data_read(dev,insubdevice,inchannel,arange,analogref) pixel = pixel + float(result[1]) pixel = pixel/n # print pixel, msg=result[0] image[x][y] = pixel # print comedi.comedi_close(dev) return pixel
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_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_(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_(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_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 send(self, channel, value): ret = comedi.comedi_data_write(self.device, DIGITAL_OUTPUT_SUBDEVICE, channel, 0, 0, value) if PRINT_CONFIRMATIONS: print "attempted to send channel %i data %i, return value:\t%i" %( channel, value, ret) return ret
def setx(voltage): dev = comedi.comedi_open("/dev/comedi0") outsubdevice = 1 insubdevice = 0 xchannel = 0 ychannel = 1 inchannel = 0 analogref = comedi.AREF_GROUND arange = 0 write_data = int( (float(voltage)+10)/20*65535) msg = comedi.comedi_data_write(dev, outsubdevice, xchannel, arange, analogref, write_data) comedi.comedi_close(dev)
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 write(self, context): device, subdevice, channel = _phys_channel(context) chk = c.comedi_data_write(device, subdevice, channel, 0, 0, context.value) if chk < 0: raise ComediError("Failed to write on %s" % context.node)
subdev = c.comedi_find_subdevice_by_type(dev, c.COMEDI_SUBD_AO, 0) print "Subdevice: %d" % subdev print "Locked? %d" % c.comedi_lock(dev, subdev) nChannels = c.comedi_get_n_channels(dev, subdev) print "Num Channels: %d" % nChannels crange = c.comedi_get_range(dev, 0, 0, 0) print "Range: %s" % str(crange) data = maxdata/2 # start with no speed c.comedi_data_write(dev, subdev, 0, 0, 0, data) while True: usrin = raw_input("Enter a char: ") if usrin is 'q': break elif usrin is 'o': data = data + 10 elif usrin is 'l': data = data - 10 print 'Writin %d to the motor' % data c.comedi_data_write(dev, subdev, 0, 0, 0, data) print "Writing 0 to the channel: %d" % int(maxdata/2) c.comedi_data_write(dev, subdev, 0, 0, 0, maxdata/2)
subdev = c.comedi_find_subdevice_by_type(dev, c.COMEDI_SUBD_AO, 0) print "Subdevice: %d" % subdev print "Locked? %d" % c.comedi_lock(dev, subdev) nChannels = c.comedi_get_n_channels(dev, subdev) print "Num Channels: %d" % nChannels crange = c.comedi_get_range(dev, 0, 0, 0) print "Range: %s" % str(crange) data = maxdata / 2 # start with no speed c.comedi_data_write(dev, subdev, 0, 0, 0, data) while True: usrin = raw_input("Enter a char: ") if usrin is 'q': break elif usrin is 'o': data = data + 10 elif usrin is 'l': data = data - 10 print 'Writin %d to the motor' % data c.comedi_data_write(dev, subdev, 0, 0, 0, data) print "Writing 0 to the channel: %d" % int(maxdata / 2) c.comedi_data_write(dev, subdev, 0, 0, 0, maxdata / 2)
print "output range: : %s" % str(outputRange) print "Output locked? %d" % c.comedi_lock(comediDevice, outputSubdev) print "***** Done handling DEVICE(S), let's get 'er done! *****" rdata = 0 # read data wdata = outputMaxdata/2 theRange = 0 aref = 0 # start the motor at zero speed: c.comedi_data_write(comediDevice, outputSubdev, channel, theRange, aref, wdata) while True: startTime = time.time() ret, rdata = c.comedi_data_read(comediDevice, inputSubdev, channel, theRange, aref) voltage = c.comedi_to_phys(rdata, inputRange, inputMaxdata); #print "Voltage: %f" % voltage x1 = voltage x2 = x1*voltage x3 = x2*voltage x4 = x3*voltage x5 = x4*voltage