def getY(self): self.lock.acquire() # Y1 = Y[0] #angle Y1_angle = comedi.comedi_data_read(self.device ,0 ,0 ,0 ,0) angle = comedi.comedi_to_phys(Y1_angle[1], self.range_info_input_1, self.maxdata_input_1) Y2_position = comedi.comedi_data_read(self.device ,0 ,1 ,0 ,0) position = comedi.comedi_to_phys(Y2_position[1], self.range_info_input_2, self.maxdata_input_2) Y = [position, 0.0, angle] self.lock.release() return Y
def get(self): data = c.comedi_data_read(self.device0,self.subdevice,self.channel,self.range_num, c.AREF_GROUND) self.position=(c.comedi_to_phys(data[1],self.range_ds,self.maxdata)*self.gain+self.offset) t=time.time() #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.position)
def get(self): """Read the signal""" data = c.comedi_data_read(self.device0, self.subdevice, self.channel, self.range_num, c.AREF_GROUND) self.position = ( c.comedi_to_phys(data[1], self.range_ds, self.maxdata) * self.gain + self.offset) t = time.time() return (t, self.position)
def capture_all(): captured = [[None for _ in range(n_ranges)] for _ in CHANNELS] for chan_i, ch in enumerate(CHANNELS): for range_i, ran in enumerate(range(n_ranges)): ret, data = c.comedi_data_read(dev, SUBDEVICE, ch, ran, REFERENCE) phydata = c.comedi_to_phys(data, ranges[ran], maxdata) captured[chan_i][range_i] = (data, phydata) return captured
def num_data_timer(self): # Print numerical data to treeview for i in range(32): # datal.append(c.comedi_to_phys(j, crange, maxdata)) self.liststore[i][1] = c.comedi_to_phys(self.analog_data[i][-1], self.comedi_range, (self.comedi_maxdata + 1)) return(True)
def get(self): data = c.comedi_data_read(self.device0, self.subdevice, self.channel, self.range_num, c.AREF_GROUND) self.position = ( c.comedi_to_phys(data[1], self.range_ds, self.maxdata) * self.gain + self.offset) t = time.time() #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.position)
def getData(self,channel_number="all"): """Read the signal for desired channel""" if channel_number=="all": result=[] for channel in range(self.nchans): data = c.comedi_data_read(self.device,self.subdevice, self.channels[channel], self.range_num[channel], c.AREF_GROUND) self.position=(c.comedi_to_phys(data[1],self.range_ds[channel], self.maxdata[channel])*self.gain[channel]+self.offset[channel]) result.append(self.position) t=time.time() return (t, result) else: data = c.comedi_data_read(self.device,self.subdevice, self.channels[channel_number], self.range_num[channel_number], c.AREF_GROUND) self.position=(c.comedi_to_phys(data[1],self.range_ds[channel_number], self.maxdata[channel_number])*self.gain[channel_number]+self.offset[channel_number]) t=time.time() return (t, self.position)
def get_data(self, channel="all"): """To read the value on input_channels. If channel is specified, it will only read and return these channels. 'all' (default) will read all opened channels""" if channel == 'all': to_read = self.channels else: if not isinstance(channel, list): channel = [channel] to_read = [self.channels[self.channels_dict[i]] for i in channel] data = [time()] for chan in to_read: data_read = c.comedi_data_read(self.device, self.subdevice, chan['num'], chan['range_num'], c.AREF_GROUND) val = c.comedi_to_phys(data_read[1], chan['range_ds'], chan['maxdata']) data.append(val * chan['gain'] + chan['offset']) return data
def streamer(device, subdevice, chans, comedi_range, shared_array): ''' read the channels defined in chans, on the device/subdevice, and streams the values in the shared_array. The shared_array has a lock, to avoid reading and writing at the same time and it's process-proof. device: '/dev/comedi0' subdevice : 0=in, 1=out chans : [0,1,2,3,4....] : BE AWARE the reading is done crescendo, no matter the order given here. It means that [0,1,2] and [2,0,1] will both have [0,1,2] as result, but you can ask for [0,1,5]. comedi_range: same size as chans, with the proper range for each chan. If unknown, try [0,0,0,....]. shared_array: same size as chans, must be defined before with multiprocess.Array: shared_array= Array('f', np.arange(len(chans))) ''' dev = c.comedi_open(device) if not dev: raise "Error openning Comedi device" fd = c.comedi_fileno(dev) #get a file-descriptor for use later BUFSZ = 10000 #buffer size freq = 8000 # acquisition frequency: if too high, set frequency to maximum. nchans = len(chans) #number of channels aref = [c.AREF_GROUND] * nchans mylist = c.chanlist(nchans) #create a chanlist of length nchans maxdata = [0] * (nchans) range_ds = [0] * (nchans) for index in range( nchans ): #pack the channel, gain and reference information into the chanlist object mylist[index] = c.cr_pack(chans[index], comedi_range[index], aref[index]) maxdata[index] = c.comedi_get_maxdata(dev, subdevice, chans[index]) range_ds[index] = c.comedi_get_range(dev, subdevice, chans[index], comedi_range[index]) cmd = c.comedi_cmd_struct() period = int(1.0e9 / freq) # in nanoseconds ret = c.comedi_get_cmd_generic_timed(dev, subdevice, cmd, nchans, period) if ret: raise "Error comedi_get_cmd_generic failed" cmd.chanlist = mylist # adjust for our particular context cmd.chanlist_len = nchans cmd.scan_end_arg = nchans cmd.stop_arg = 0 cmd.stop_src = c.TRIG_NONE t0 = time.time() j = 0 ret = c.comedi_command(dev, cmd) if ret != 0: raise "comedi_command failed..." #Lines below are for initializing the format, depending on the comedi-card. data = os.read(fd, BUFSZ) # read buffer and returns binary data data_length = len(data) #print maxdata #print data_length if maxdata[0] <= 65536: # case for usb-dux-D n = data_length / 2 # 2 bytes per 'H' format = ` n ` + 'H' elif maxdata[0] > 65536: #case for usb-dux-sigma n = data_length / 4 # 2 bytes per 'H' format = ` n ` + 'I' #print struct.unpack(format,data) # init is over, start acquisition and stream last_t = time.time() try: while True: #t_now=time.time() #while (t_now-last_t)<(1./frequency): #t_now=time.time() ##print t_now-last_t #last_t=t_now data = os.read(fd, BUFSZ) # read buffer and returns binary data #print len(data), data_length if len(data) == data_length: datastr = struct.unpack( format, data) # convert binary data to digital value if len(datastr ) == nchans: #if data not corrupted for some reason #shared_array.acquire() for i in range(nchans): shared_array[i] = c.comedi_to_phys( (datastr[i]), range_ds[i], maxdata[i]) #print datastr #shared_array.release() #j+=1 #print j #print "Frequency= ",(j/(time.time()-t0)) #print np.transpose(shared_array[:]) except (KeyboardInterrupt): c.comedi_cancel(dev, subdevice) ret = c.comedi_close(dev) if ret != 0: raise "comedi_close failed..."
time.sleep(PRE_MOTION_TIME) dev.set_vel_setpt(0) dev.set_mode('velocity') dev.start() start_time = time.time() t = 0 while t <= start_time + TOTAL_TIME_EXP: gin = stdscr.getch() if gin ==10: stdscr.addstr("experiment ended") stdscr.refresh() time.sleep(1) break else: rawAnalogIn = comedi.comedi_data_read(daq,subdev,chans[0],0,comedi.AREF_GROUND) voltsInLMR = comedi.comedi_to_phys(rawAnalogIn[1], cr[0], maxdata[0]) rawAnalogIn = comedi.comedi_data_read(daq,subdev,chans[1],0,comedi.AREF_GROUND) voltsInFreq = comedi.comedi_to_phys(rawAnalogIn[1], cr[1], maxdata[1]) vel = int(round(voltsInLMR*gain)) dev.set_dir_setpt(direction[(numpy.sign(vel)+1)/2], io_update=False) dev.set_vel_setpt(abs(vel)) t = time.time() pos = dev.get_pos() fd.write('%f %d %d %f %f\n'%(t,vel,pos,voltsInLMR,voltsInFreq)) fd.flush()
# Unpack data from long array and convert to volts array_list = [] for i in range(0, nchans): # Get channel information channel = config['channels'][i] gain = config['gains'][i] subdev = config['subdev'] maxdata = c.comedi_get_maxdata(dev, subdev, channel) cr = c.comedi_get_range(dev, subdev, channel, gain) # Convert to voltages temp_array = dataarray[i::nchans] temp_array = numpy.array( [c.comedi_to_phys(int(x), cr, maxdata) for x in temp_array]) temp_array = numpy.reshape(temp_array, (temp_array.shape[0], 1)) array_list.append(temp_array) # Form sample_num x nchans array samples = numpy.concatenate(tuple(array_list), 1) n, m = samples.shape if config['verbose']: print print 'acquired data array w/ size: %dx%d' % (n, m) # Create time array sample_t = (1.0 / float(config['sample_freq'])) sample_t_true = cmd.scan_begin_arg / NANO_SEC if config['verbose']:
def read(self, comediObject): ret, self.value = c.comedi_data_read(comediObject.device, comediObject.subdevice, self.channel, 0, 0) self.voltage = c.comedi_to_phys(self.value, comediObject.channelRange, comediObject.maxdata)
def get(self): """Read the signal""" data = c.comedi_data_read(self.device0,self.subdevice,self.channel,self.range_num, c.AREF_GROUND) self.position=(c.comedi_to_phys(data[1],self.range_ds,self.maxdata)*self.gain+self.offset) t=time.time() return (t, self.position)
crange = c.comedi_get_range(dev, subdev, channel, 0) print "comedi_get_range: %s" % str(crange) data = maxdata/2 rdata = 0 nChannels = 2 for i in range(100): for chan in range(nChannels): ret, rdata = c.comedi_data_read(dev, subdev, chan, 0, 0) #print "Read value: %d" % rdata voltage = c.comedi_to_phys(rdata, crange, maxdata); #print voltage x1 = voltage x2 = x1*voltage x3 = x2*voltage x4 = x3*voltage x5 = x4*voltage dist = -14.153*x5+110.18*x4-339.89*x3+538.13*x2-479.23*x1+243.35 print "Chan %d Distance: %f" % (chan, dist) print time.sleep(1) print "Closing %s" % str(dev)
def streamer(device,subdevice,chans,comedi_range,shared_array): ''' read the channels defined in chans, on the device/subdevice, and streams the values in the shared_array. The shared_array has a lock, to avoid reading and writing at the same time and it's process-proof. device: '/dev/comedi0' subdevice : 0=in, 1=out chans : [0,1,2,3,4....] : BE AWARE the reading is done crescendo, no matter the order given here. It means that [0,1,2] and [2,0,1] will both have [0,1,2] as result, but you can ask for [0,1,5]. comedi_range: same size as chans, with the proper range for each chan. If unknown, try [0,0,0,....]. shared_array: same size as chans, must be defined before with multiprocess.Array: shared_array= Array('f', np.arange(len(chans))) ''' dev=c.comedi_open(device) if not dev: raise "Error openning Comedi device" fd = c.comedi_fileno(dev) #get a file-descriptor for use later BUFSZ = 10000 #buffer size freq=8000# acquisition frequency: if too high, set frequency to maximum. nchans = len(chans) #number of channels aref =[c.AREF_GROUND]*nchans mylist = c.chanlist(nchans) #create a chanlist of length nchans maxdata=[0]*(nchans) range_ds=[0]*(nchans) for index in range(nchans): #pack the channel, gain and reference information into the chanlist object mylist[index]=c.cr_pack(chans[index], comedi_range[index], aref[index]) maxdata[index]=c.comedi_get_maxdata(dev,subdevice,chans[index]) range_ds[index]=c.comedi_get_range(dev,subdevice,chans[index],comedi_range[index]) cmd = c.comedi_cmd_struct() period = int(1.0e9/freq) # in nanoseconds ret = c.comedi_get_cmd_generic_timed(dev,subdevice,cmd,nchans,period) if ret: raise "Error comedi_get_cmd_generic failed" cmd.chanlist = mylist # adjust for our particular context cmd.chanlist_len = nchans cmd.scan_end_arg = nchans cmd.stop_arg=0 cmd.stop_src=c.TRIG_NONE t0 = time.time() j=0 ret = c.comedi_command(dev,cmd) if ret !=0: raise "comedi_command failed..." #Lines below are for initializing the format, depending on the comedi-card. data = os.read(fd,BUFSZ) # read buffer and returns binary data data_length=len(data) #print maxdata #print data_length if maxdata[0]<=65536: # case for usb-dux-D n = data_length/2 # 2 bytes per 'H' format = `n`+'H' elif maxdata[0]>65536: #case for usb-dux-sigma n = data_length/4 # 2 bytes per 'H' format = `n`+'I' #print struct.unpack(format,data) # init is over, start acquisition and stream last_t=time.time() try: while True: #t_now=time.time() #while (t_now-last_t)<(1./frequency): #t_now=time.time() ##print t_now-last_t #last_t=t_now data = os.read(fd,BUFSZ) # read buffer and returns binary data #print len(data), data_length if len(data)==data_length: datastr = struct.unpack(format,data) # convert binary data to digital value if len(datastr)==nchans: #if data not corrupted for some reason #shared_array.acquire() for i in range(nchans): shared_array[i]=c.comedi_to_phys((datastr[i]),range_ds[i],maxdata[i]) #print datastr #shared_array.release() #j+=1 #print j #print "Frequency= ",(j/(time.time()-t0)) #print np.transpose(shared_array[:]) except (KeyboardInterrupt): c.comedi_cancel(dev,subdevice) ret = c.comedi_close(dev) if ret !=0: raise "comedi_close failed..."
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 dist = -14.153*x5+110.18*x4-339.89*x3+538.13*x2-479.23*x1+243.35 #print "Distance: %f" % dist if dist > 50: wdata = outputMaxdata/2 + 1000 else: wdata = outputMaxdata/2