def test_command(subdevice, num_tests=2): """Adjust a command as necessary to get valid arguments. """ _LOG.info('command before testing:\n{}'.format(subdevice.cmd)) for i in range(2): rc = subdevice.command_test() if rc is None: _LOG.info('command is valid') return _LOG.info('test {} returned {}\n{}'.format(i, rc, subdevice.cmd)) _LOG.error('error preparing command: {}'.format(rc)) _sys.exit(1)
def run(filename, subdevice, channel, range, aref, num_scans): """ >>> t1,data,t2 = run(filename='/dev/comedi0', subdevice=None, ... channel=0, range=0, aref=_constant.AREF.ground, num_scans=10) >>> t1 # doctest: +SKIP 1332242184.029691 >>> t2 # doctest: +SKIP 1332242184.3311629 >>> data # doctest: +ELLIPSIS array([...], dtype=uint32) >>> data.shape (10,) """ _LOG.info( ('measuring device={} subdevice={} channel={} range={} ' 'analog-reference={} num-scans={}').format(filename, subdevice, channel, range, aref, num_scans)) device = _Device(filename=filename) device.open() try: if subdevice is None: subdevice = device.find_subdevice_by_type( _constant.SUBDEVICE_TYPE.ai) else: subdevice = device.subdevice(subdevice) insns = [subdevice.insn(), subdevice.insn(), subdevice.insn()] insns[0].insn = insns[2].insn = _constant.INSN.gtod insns[0].data = insns[2].data = [0, 0] insns[1].insn = _constant.INSN.read insns[1].data = [0] * num_scans insns[1].chanspec = _ChanSpec(chan=channel, range=range, aref=aref) device.do_insnlist(insns) finally: device.close() t1 = insns[0].data[0] + insns[1].data[1] / 1e6 t2 = insns[2].data[0] + insns[2].data[1] / 1e6 return (t1, insns[1].data, t2)
def __init__(self, stream, subdevice, channels, physical=False, plot=False): self.stream = stream self.subdevice = subdevice self.channels = channels self.physical = physical self.plot = plot if plot: if _pyplot is None: raise _matplotlib_import_error self.figure = _pyplot.figure() self.axes = self.figure.add_subplot(1, 1, 1) self.n = 100 self.index = 0 self.tdata = _numpy.arange(self.n) self.ydata = _numpy.zeros( (2*self.n, len(channels)), dtype=_numpy.float32) self.lines = [] for i,channel in enumerate(channels): self.lines.append(_Line2D( self.tdata, self.ydata[:self.n,i], linestyle='', color='red', marker='.')) self.axes.add_line(self.lines[-1]) self.axes.set_xlim(0, self.n-1) self.axes.set_ylim(0, max(c.get_maxdata() for c in channels)) _pyplot.draw() _LOG.debug('data writer initialized') _LOG.debug('poll: {}'.format(self.subdevice.poll())) _LOG.debug('get_buffer contents: {}'.format( self.subdevice.get_buffer_contents()))
def run(filename, subdevice, channel, range, aref, num_scans): """ >>> t1,data,t2 = run(filename='/dev/comedi0', subdevice=None, ... channel=0, range=0, aref=_constant.AREF.ground, num_scans=10) >>> t1 # doctest: +SKIP 1332242184.029691 >>> t2 # doctest: +SKIP 1332242184.3311629 >>> data # doctest: +ELLIPSIS array([...], dtype=uint32) >>> data.shape (10,) """ _LOG.info( ("measuring device={} subdevice={} channel={} range={} " "analog-reference={} num-scans={}").format( filename, subdevice, channel, range, aref, num_scans ) ) device = _Device(filename=filename) device.open() try: if subdevice is None: subdevice = device.find_subdevice_by_type(_constant.SUBDEVICE_TYPE.ai) else: subdevice = device.subdevice(subdevice) insns = [subdevice.insn(), subdevice.insn(), subdevice.insn()] insns[0].insn = insns[2].insn = _constant.INSN.gtod insns[0].data = insns[2].data = [0, 0] insns[1].insn = _constant.INSN.read insns[1].data = [0] * num_scans insns[1].chanspec = _ChanSpec(chan=channel, range=range, aref=aref) device.do_insnlist(insns) finally: device.close() t1 = insns[0].data[0] + insns[1].data[1] / 1e6 t2 = insns[2].data[0] + insns[2].data[1] / 1e6 return (t1, insns[1].data, t2)
def __call__(self, data): _LOG.debug('new data: {}'.format(data)) d = _numpy.ndarray(shape=(1,data.size), dtype=data.dtype, buffer=data) if self.plot: self.ydata[self.index,:] = d self.ydata[self.index + self.n,:] = d for i,line in enumerate(self.lines): line.set_data( self.tdata, self.ydata[self.index+1:self.index + self.n+1,i]) _pyplot.draw() self.index = (self.index + 1) % len(self.tdata) write_data( stream=self.stream, channels=self.channels, data=d, physical=self.physical) _LOG.debug('poll: {}'.format(self.subdevice.poll())) _LOG.debug('get_buffer contents: {}'.format( self.subdevice.get_buffer_contents()))
def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, num_scans=2, reader=_utility.Reader, physical=False, plot=False, stream=_sys.stdout): """Read ``num_scans`` samples from each specified channel. """ subdevice,channels = open_channels( device=device, subdevice=subdevice, channels=channels, range=range, aref=aref) subdevice.cmd = prepare_command( subdevice=subdevice, channels=channels, period=period, num_scans=num_scans) rc = test_command(subdevice=subdevice) kwargs = {} if reader == _utility.CallbackReader: read_buffer_shape = (len(channels),) kwargs = { 'callback': DataWriter( stream=stream, subdevice=subdevice, channels=channels, physical=physical, plot=plot), 'count': num_scans, } else: read_buffer_shape = (num_scans, len(channels)) read_buffer = _numpy.zeros(read_buffer_shape, dtype=subdevice.get_dtype()) reader = reader( subdevice=subdevice, buffer=read_buffer, name='Reader', **kwargs) start = _time.time() _LOG.info('start time: {}'.format(start)) subdevice.command() reader.start() while subdevice.get_flags().running: _LOG.debug('running...') _LOG.debug('poll: {}'.format(subdevice.poll())) _LOG.debug('get_buffer offset: {}'.format( subdevice.get_buffer_offset())) _LOG.debug('get_buffer contents: {}'.format( subdevice.get_buffer_contents())) _time.sleep(0.5) _LOG.debug('stopped running. joining reader...') stop = _time.time() _LOG.info('stop time: {}'.format(stop)) reader.join() join = _time.time() _LOG.info('join time: {}'.format(join)) _LOG.debug('poll: {}'.format(subdevice.poll())) _LOG.debug('get_buffer offset: {}'.format( subdevice.get_buffer_offset())) _LOG.debug('get_buffer contents: {}'.format( subdevice.get_buffer_contents())) _LOG.info('stop delay: {}'.format(stop - start)) _LOG.info('join delay: {}'.format(join - stop)) if not isinstance(reader, _utility.CallbackReader): write_data( stream=stream, channels=channels, data=read_buffer, physical=physical)
read(device=device, **kwargs) finally: device.close() if __name__ == '__main__': import pycomedi_demo_args args = pycomedi_demo_args.parse_args( description=__doc__, argnames=['filename', 'subdevice', 'channels', 'aref', 'range', 'num-scans', 'mmap', 'callback', 'frequency', 'physical', 'plot', 'verbose']) _LOG.info(('measuring device={0.filename} subdevice={0.subdevice} ' 'channels={0.channels} range={0.range} ' 'analog-reference={0.aref}' ).format(args)) if args.callback: reader = _utility.CallbackReader elif args.mmap: reader = _utility.MMapReader else: reader = _utility.Reader if args.plot: if _pyplot is None: raise _matplotlib_import_error _pyplot.ion() run(filename=args.filename, subdevice=args.subdevice, channels=args.channels, aref=args.aref, range=args.range,
def __call__(self, parser, namespace, values, option_string=None): level = _LOG.level level -= 10 # e.g. logging.INFO -> logging.DEBUG if level >= _logging.DEBUG: _LOG.setLevel(level)
def display(t1, data, t2): _LOG.info("initial time: {}".format(t1)) _LOG.info("final time: {}".format(t2)) _LOG.info("difference: {}".format(t2 - t1)) for x in insns[1].data: print(x)
def display(t1, data, t2): _LOG.info('initial time: {}'.format(t1)) _LOG.info('final time: {}'.format(t2)) _LOG.info('difference: {}'.format(t2 - t1)) for x in insns[1].data: print(x)