def open(self): """Starts commmunication with the device, must be called before any set_cmd or get_data""" self.device = c.comedi_open(self.device_name) for chan in self.channels: chan['maxdata'] = c.comedi_get_maxdata(self.device, self.subdevice, chan['num']) chan['range_ds'] = c.comedi_get_range(self.device, self.subdevice, chan['num'], chan['range_num']) for chan in self.out_channels: chan['maxdata'] = c.comedi_get_maxdata(self.device, self.out_subdevice, chan['num']) chan['range_ds'] = c.comedi_get_range(self.device, self.out_subdevice, chan['num'], chan['range_num']) c.comedi_dio_config(self.device, 2, chan['num'], 1) c.comedi_dio_write(self.device, 2, chan['num'], 1) if any([i['make_zero'] for i in self.channels]): off = self.eval_offset() for i, chan in enumerate(self.channels): if chan['make_zero']: chan['offset'] += off[i]
def __init__(self): # Open device self.device = comedi.comedi_open('/dev/comedi0') # Set max values as numbers comedi.comedi_set_global_oor_behavior(comedi.COMEDI_OOR_NUMBER) # Get channel range info self.range_info_input_1 = comedi.comedi_get_range(self.device, 0 , 0 , 0) self.range_info_input_2 = comedi.comedi_get_range(self.device, 0 , 1 , 0) self.range_info_output_1 = comedi.comedi_get_range(self.device, 1 , 0 , 0) # Get channel max inputs self.maxdata_input_1 = comedi.comedi_get_maxdata(self.device,0 ,0) self.maxdata_input_2 = comedi.comedi_get_maxdata(self.device,0 ,1) self.maxdata_output_1 = comedi.comedi_get_maxdata(self.device,1 ,0) self.lock = threading.Lock()
def __init__(self, subdevice = c.COMEDI_SUBD_AI): self.device = c.comedi_open('/dev/comedi0') self.subdevice = c.comedi_find_subdevice_by_type(self.device, subdevice, 0) self.numChannels = c.comedi_get_n_channels(self.device, self.subdevice) self.numRanges = c.comedi_get_n_channels(self.device, self.subdevice) self.channelRange = c.comedi_get_range(self.device, self.subdevice, 0, 0) self.maxdata = c.comedi_get_maxdata(self.device, 0, 0)
def __init__(self, K=1, Ki=0, Kd=0, device='/dev/comedi0', subdevice=1, channel=0, range_num=1, gain=1, offset=0, out_min=0, out_max=4.095): self.subdevice = subdevice self.channel = channel self.range_num = range_num self.device0 = c.comedi_open(device) self.maxdata = c.comedi_get_maxdata(self.device0, self.subdevice, self.channel) self.range_ds = c.comedi_get_range(self.device0, self.subdevice, self.channel, self.range_num) self.out = 0 self.gain = gain self.offset = offset self.I_term = 0 self.last_sensor_input = 0 self.K = K self.Ki = Ki self.Kd = Kd self.last_time = time.time() self.out_min = out_min self.out_max = out_max self.last_output = 0
def __init__(self, device="/dev/comedi0", subdevice=1, channel=0, range_num=0, gain=1, offset=0): """Convert wanted tension value into digital values and send it to the output of some Comedi-controlled card. Output is (command * gain) + offset. Parameters ---------- device : str, default = '/dev/comedi0' Path to the device. subdevice : int, default = 1 Subdevice 1 is the output. channel : int, default = 0 The desired output channel. range_num : int, default = 0 See the comedi documentation for different values. gain : float, default = 1 Multiplication gain for the output. offset : float, default = 0 Add this value to your output. """ self.subdevice = 1 # delete as argument self.channel = channel self.range_num = range_num self.gain = gain self.offset = offset self.device = c.comedi_open(device) self.maxdata = c.comedi_get_maxdata(self.device, self.subdevice, self.channel) self.range_ds = c.comedi_get_range(self.device, self.subdevice, self.channel, self.range_num) c.comedi_dio_config(self.device, 2, self.channel, 1)
def __init__(self, subdevice=c.COMEDI_SUBD_AI): self.device = c.comedi_open('/dev/comedi0') self.subdevice = c.comedi_find_subdevice_by_type( self.device, subdevice, 0) self.numChannels = c.comedi_get_n_channels(self.device, self.subdevice) self.numRanges = c.comedi_get_n_channels(self.device, self.subdevice) self.channelRange = c.comedi_get_range(self.device, self.subdevice, 0, 0) self.maxdata = c.comedi_get_maxdata(self.device, 0, 0)
def __init__(self,device='/dev/comedi0',subdevice=0,channel=1,range_num=0,gain=1,offset=0): self.subdevice=subdevice self.channel=channel self.range_num=range_num self.device0=c.comedi_open(device) self.maxdata=c.comedi_get_maxdata(self.device0,self.subdevice,self.channel) self.range_ds=c.comedi_get_range(self.device0,self.subdevice,self.channel,self.range_num) self.gain=gain self.offset=offset
def __init__(self, dev, no, subdev, fd, devrange, aref=aref_ground, calibrate=False, configpath=None, maxbuffer=131072): self.no = no self.dev = dev self.subdev = subdev self.fd = fd self.devrange = devrange self.aref = aref if calibrate: sys.stdout.write("NICOMEDI: Parsing calibration file... ") sys.stdout.flush() calib = c.comedi_parse_calibration_file(configpath) if calib is None or calib == 0: sys.stdout.write("FAIL\n") else: self.convpoly = c.comedi_polynomial_t() conv = c.comedi_get_softcal_converter( \ self.subdev, self.no, self.devrange, c.COMEDI_TO_PHYSICAL, calib, self.convpoly) c.comedi_cleanup_calibration(calib) if conv < 0: sys.stdout.write("FAIL\n") else: sys.stdout.write("success\n") sys.stdout.flush() self.convpoly_order = int(self.convpoly.order.real) self.convpoly_expansion_origin = self.convpoly.expansion_origin.real c_coeff = ctypes.c_double * (int(self.convpoly.order.real) + 1) c_coeff = c_coeff.from_address(int(self.convpoly.coefficients)) self.convpoly_coefficients = np.array( [coeff.real for coeff in c_coeff]) else: self.convpoly = None self.maxdata = c.comedi_get_maxdata(self.dev, self.subdev, self.no) if self.maxdata == 0: comedi_errcheck() self.prange = c.comedi_get_range(self.dev, self.subdev, self.no, self.devrange) if self.prange == 0 or self.prange is None: comedi_errcheck() self.maxbuffer = c.comedi_get_max_buffer_size(self.dev, self.subdev) if self.maxbuffer == -1: comedi_errcheck() self.buffer = c.comedi_get_buffer_size(self.dev, self.subdev) sys.stdout.write("NICOMEDI: Buffer size: %d\n" % self.buffer) sys.stdout.flush()
def new(self): """ Gather range and maxdata for all specified channels. This is only called on init.""" self.maxdata=[0]*self.nchans self.range_ds=[0]*self.nchans for i in range(self.nchans): self.maxdata[i]=c.comedi_get_maxdata(self.device,self.subdevice, self.channels[i]) self.range_ds[i]=c.comedi_get_range(self.device,self.subdevice, self.channels[i],self.range_num[i])
def __init__(self, dev, no, subdev, fd, devrange, aref=aref_ground, calibrate=False, configpath=None, maxbuffer=131072): self.no = no self.dev = dev self.subdev = subdev self.fd = fd self.devrange = devrange self.aref = aref if calibrate: sys.stdout.write("NICOMEDI: Parsing calibration file... ") sys.stdout.flush() calib = c.comedi_parse_calibration_file(configpath) if calib is None or calib==0: sys.stdout.write("FAIL\n") else: self.convpoly = c.comedi_polynomial_t() conv = c.comedi_get_softcal_converter( \ self.subdev, self.no, self.devrange, c.COMEDI_TO_PHYSICAL, calib, self.convpoly) c.comedi_cleanup_calibration(calib) if conv < 0: sys.stdout.write("FAIL\n") else: sys.stdout.write("success\n") sys.stdout.flush() self.convpoly_order = int(self.convpoly.order.real) self.convpoly_expansion_origin = self.convpoly.expansion_origin.real c_coeff = ctypes.c_double * (int(self.convpoly.order.real)+1) c_coeff = c_coeff.from_address(int(self.convpoly.coefficients)) self.convpoly_coefficients = np.array([ coeff.real for coeff in c_coeff]) else: self.convpoly = None self.maxdata = c.comedi_get_maxdata(self.dev, self.subdev, self.no) if self.maxdata == 0: comedi_errcheck() self.prange = c.comedi_get_range(self.dev, self.subdev, self.no, self.devrange) if self.prange == 0 or self.prange is None: comedi_errcheck() self.maxbuffer = c.comedi_get_max_buffer_size(self.dev, self.subdev) if self.maxbuffer == -1: comedi_errcheck() self.buffer = c.comedi_get_buffer_size(self.dev, self.subdev) sys.stdout.write("NICOMEDI: Buffer size: %d\n" % self.buffer) sys.stdout.flush()
def __init__(self, device='/dev/comedi0', subdevice=0, channel=1, range_num=0, gain=1, offset=0): self.subdevice = subdevice self.channel = channel self.range_num = range_num self.device0 = c.comedi_open(device) self.maxdata = c.comedi_get_maxdata(self.device0, self.subdevice, self.channel) self.range_ds = c.comedi_get_range(self.device0, self.subdevice, self.channel, self.range_num) self.gain = gain self.offset = offset
def __init__(self,K=1,Ki=0,Kd=0,device='/dev/comedi0',subdevice=1,channel=0,range_num=1,gain=1,offset=0,out_min=0,out_max=4.095): self.subdevice=subdevice self.channel=channel self.range_num=range_num self.device0=c.comedi_open(device) self.maxdata=c.comedi_get_maxdata(self.device0,self.subdevice,self.channel) self.range_ds=c.comedi_get_range(self.device0,self.subdevice,self.channel,self.range_num) self.out=0 self.gain=gain self.offset=offset self.I_term=0 self.last_sensor_input=0 self.K=K self.Ki=Ki self.Kd=Kd self.last_time=time.time() self.out_min=out_min self.out_max=out_max self.last_output=0
def __init__(self, dev, no, subdev, fd, devrange, aref=aref_ground, calibrate=False, configpath=None, maxbuffer=131072): self.no = no self.dev = dev self.subdev = subdev self.fd = fd self.devrange = devrange self.aref = aref if calibrate: sys.stdout.write("NICOMEDI: Parsing calibration file... ") sys.stdout.flush() calib = c.comedi_parse_calibration_file(configpath) if calib is None or calib==0: sys.stdout.write("FAIL\n") else: self.convpoly = c.comedi_polynomial_t() conv = c.comedi_get_softcal_converter( \ self.subdev, self.no, self.devrange, c.COMEDI_TO_PHYSICAL, calib, self.convpoly) c.comedi_cleanup_calibration(calib) if conv < 0: sys.stdout.write("FAIL\n") else: sys.stdout.write("success\n") sys.stdout.flush() else: self.convpoly = None self.maxdata = c.comedi_get_maxdata(self.dev, self.subdev, self.no) if self.maxdata == 0: comedi_errcheck() self.prange = c.comedi_get_range(self.dev, self.subdev, self.no, self.devrange) if self.prange == 0 or self.prange is None: comedi_errcheck() self.maxbuffer = c.comedi_get_max_buffer_size(self.dev, self.subdev) if self.maxbuffer == -1: comedi_errcheck() self.buffer = c.comedi_get_buffer_size(self.dev, self.subdev) sys.stdout.write("NICOMEDI: Buffer size: %d\n" % self.buffer) sys.stdout.flush()
'sample_num'] * nchans, 'bytes' if bytes_read == bytes_total: read_done = True # Convert from string to integers dataarray = numpy.fromstring(datastr, numpy.uint16) # 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
import comedi as c dev = c.comedi_open('/dev/comedi0') print dev maxdata = c.comedi_get_maxdata(dev, 0, 0) print "Max Data: %d" % maxdata 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':
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..."
def pci_6033e_init(self, dev_name): self.dev = c.comedi_open(dev_name) if not(self.dev): self.warn_dialog("Unable to open device: " + dev_name) return(-1) ret = c.comedi_lock(self.dev, SUBDEVICE) if (ret < 0): self.warn_dialog("Could not lock comedi device") return(-1) # get a file-descriptor for use later self.fd = c.comedi_fileno(self.dev) if (self.fd <= 0): self.warn_dialog("Error obtaining Comedi device file descriptor") c.comedi_close(self.dev) return(-1) # Channel range (0-5V) if (c.comedi_range_is_chan_specific(self.dev, SUBDEVICE) != 0): self.warn_dialog("Comedi range is channel specific!") c.comedi_close(self.dev) return(-1) self.comedi_range = c.comedi_get_range(self.dev, SUBDEVICE, 0, CHAN_RANGE) self.comedi_maxdata = c.comedi_get_maxdata(self.dev, SUBDEVICE, 0) board_name = c.comedi_get_board_name(self.dev) if (board_name != "pci-6033e"): print("Opened wrong device!") # Prepare channels, gains, refs self.comedi_num_chans = NUM_CHANNELS chans = range(self.comedi_num_chans) gains = [0]*self.comedi_num_chans aref = [c.AREF_GROUND]*self.comedi_num_chans chan_list = c.chanlist(self.comedi_num_chans) # Configure all the channels! for i in range(self.comedi_num_chans): chan_list[i] = c.cr_pack(chans[i], gains[i], aref[i]) # The comedi command self.cmd = c.comedi_cmd_struct() # 1.0e9 because this number is in nanoseconds for some reason period = int(1.0e9/float(SCAN_FREQ)) # Init cmd ret = c.comedi_get_cmd_generic_timed(self.dev, SUBDEVICE, self.cmd, self.comedi_num_chans, period) if (ret): self.warn_dialog("Could not initiate command") c.comedi_close(self.dev) return(-1) # Populate command self.cmd.chanlist = chan_list self.cmd.chanlist_len = self.comedi_num_chans self.cmd.scan_end_arg = self.comedi_num_chans self.cmd.stop_src = c.TRIG_NONE self.cmd.stop_arg = 0 print("real timing: %d ns" % self.cmd.convert_arg) print("Real scan freq: %d Hz" % (1.0/(float(self.cmd.convert_arg)*32.0*1.0e-9))) #print("period: %d ns" % period) print_cmd(self.cmd) # Test command out. ret = c.comedi_command_test(self.dev, self.cmd) if (ret < 0): self.warn_dialog("Comedi command test failed!") c.comedi_close(self.dev) return(-1) print("Command test passed") return(0)
import numpy as np sens_to_f = np.genfromtxt('sensor_transf_matrix_FT4714.csv', delimiter=',') # read the matrix that transforms sensor data into force data. # note: the columns here are the sensor dimensions, the rows are the force dimensions. #open a comedi device dev = c.comedi_open(DEVICE) if not dev: raise Exception("Error opening Comedi device") # get maxdata & range # - these values are used to convert integer to physical values i = SUBDEVICE maxdata = c.comedi_get_maxdata(dev, SUBDEVICE, 0) #print("\tmax data value: %d" % (maxdata)) ranges = [] #n_ranges = c.comedi_get_n_ranges(dev,i,0) #print("\t\tall chans:") n_ranges = c.comedi_get_n_ranges(dev, SUBDEVICE, 0) # get how many range options we have for j in range(n_ranges): rng = c.comedi_get_range(dev, SUBDEVICE, 0, j) ranges.append(rng) print([(rng.min, rng.max) for rng in ranges]) s = ttk.Style() s.theme_use('clam') s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
print("\tconvert begin: %s" % (cmd_src(cmd.convert_src,buf))) print("\tscan_end: %s" % (cmd_src(cmd.scan_end_src,buf))) print("\tstop: %s" % (cmd_src(cmd.stop_src,buf))) probe_max_1chan(dev,s) print("-----subdevice characteristics-----") for i in range(n_subdevices): print("subdevice %d:" % (i)) type = c.comedi_get_subdevice_type(dev,i) print("\ttype: %d (%s)" % (type,subdevice_types[type])) if (type == c.COMEDI_SUBD_UNUSED): continue n_chans = c.comedi_get_n_channels(dev,i) print("\tnumber of channels: %d" % ( n_chans)) if not(c.comedi_maxdata_is_chan_specific(dev,i)): print("\tmax data value: %d" % (c.comedi_get_maxdata(dev,i,0))) else: print("max data value is channel specific") for j in range(n_chans): print("\tchan: %d: %d" % (j,c.comedi_get_maxdata(dev,i,j))) print("\tranges: ") if not(c.comedi_range_is_chan_specific(dev,i)): n_ranges = c.comedi_get_n_ranges(dev,i,0) print("\t\tall chans:") for j in range(n_ranges): rng = c.comedi_get_range(dev,i,0,j) print("\t\t[%g,%g]" % (rng.min, rng.max)) else: for chan in range(n_chans): n_ranges = c.comedi_get_n_ranges(dev,i,chan) print("\t\tchan: %d" % (chan))
print "\tscan_end: %s" % (cmd_src(cmd.scan_end_src, buf)) print "\tstop: %s" % (cmd_src(cmd.stop_src, buf)) probe_max_1chan(dev, s) print "-----subdevice characteristics-----" for i in range(n_subdevices): print "subdevice %d:" % (i) type = c.comedi_get_subdevice_type(dev, i) print "\ttype: %d (%s)" % (type, subdevice_types[type]) if (type == c.COMEDI_SUBD_UNUSED): continue n_chans = c.comedi_get_n_channels(dev, i) print "\tnumber of channels: %d" % (n_chans) if not (c.comedi_maxdata_is_chan_specific(dev, i)): print "\tmax data value: %d" % (c.comedi_get_maxdata(dev, i, 0)) else: print "max data value is channel specific" for j in range(n_chans): print "\tchan: %d: %d" % (j, c.comedi_get_maxdata(dev, i, j)) print "\tranges: " if not (c.comedi_range_is_chan_specific(dev, i)): n_ranges = c.comedi_get_n_ranges(dev, i, 0) print "\t\tall chans:" for j in range(n_ranges): rng = c.comedi_get_range(dev, i, 0, j) print "\t\t[%g,%g]" % (rng.min, rng.max) else: for chan in range(n_chans): n_ranges = c.comedi_get_n_ranges(dev, i, chan) print "\t\tchan: %d" % (chan)
# ------------------------------------------------------------------------- # --------- DAQ initialization # ------------------------------------------------------------------------- chans = [0,1] subdev = 0 chrange = 0 # Open and configure device daq = comedi.comedi_open('/dev/comedi0') if not daq: raise "Error openning Comedi device" maxdata = [0,0] cr = [0,0] maxdata[0] = comedi.comedi_get_maxdata(daq,subdev, chans[0]) cr[0] = comedi.comedi_get_range(daq,subdev,chans[0],chrange) maxdata[1] = comedi.comedi_get_maxdata(daq,subdev, chans[1]) cr[1] = comedi.comedi_get_range(daq,subdev,chans[1],chrange) # ------------------------------------------------------------------------- # --------- polarizer initialization # ------------------------------------------------------------------------- NUM_TEETH_PASSIVE_GEAR = 80.0 # number of teeth on gear surrounding polarizer NUM_TEETH_DRIVE_GEAR = 64.0 # number of teeth on gear attached to motor shaft STEPS_PER_ROT_MOTOR = 6400.0 # setable on driver RAMP_ACCEL = 15000 stepsPerRotation = STEPS_PER_ROT_MOTOR*NUM_TEETH_PASSIVE_GEAR/NUM_TEETH_DRIVE_GEAR
import comedi as c dev = c.comedi_open('/dev/comedi0') print dev maxdata = c.comedi_get_maxdata(dev, 0, 0) print "Max Data: %d" % maxdata 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':
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..."
# comedi_combo_test.py import comedi as c import time channel = 0 comediDevice = c.comedi_open('/dev/comedi0') print comediDevice print "***** HANDLING INPUT DEVICE(S) *****" inputSubdev = c.comedi_find_subdevice_by_type(comediDevice, c.COMEDI_SUBD_AI, 0) print "input subdevice: %d" % inputSubdev inputMaxdata = c.comedi_get_maxdata(comediDevice, inputSubdev, channel) print "Input max Data: %d" % inputMaxdata nInputChannels = c.comedi_get_n_channels(comediDevice, inputSubdev) print "Num input Channels: %d" % nInputChannels nInputRanges = c.comedi_get_n_ranges(comediDevice, inputSubdev, channel) print "number Input Ranges: %d" % nInputRanges inputRange = c.comedi_get_range(comediDevice, inputSubdev, channel, 0) print "input range: : %s" % str(inputRange) print "Input locked? %d" % c.comedi_lock(comediDevice, inputSubdev) print "***** HANDLING OUTPUT DEVICE(S) *****"