NI-Counter Timers

Hi,

I'm using the "Pulse Width Measurement" operation with the general
purpose counter-timers on a NI-6052e board (following the hints given in
previous discussions on this list). 

The pulse-length measurement works OK, but I need to be able to poll the
instantaneous value of the counter *during* the pulse width measurement
operation. Neither "comedi_dio_read" nor an INSN_READ command returns
any sensible value until after the pulse width measurement is complete. 

Is there another way to poll the counter while an operation is in
progress?

I Also tried using "EventCount" mode to do the measurement (polling
during event counting works OK), but the gate (which should start and
stop the counter) doesn't appear to work in this mode, even when I
specify GPCT_SET_GATE as GPCT_EXT_PIN. 

In case it's useful to others, I've copied my GPCT python class below.

Bryan

--------------------------------------------------------------------

from comedi import *

Operations = {"EventCount": GPCT_SIMPLE_EVENT,
              "SinglePeriodMeas": GPCT_SINGLE_PERIOD,
              "SinglePulseWidthMeas": GPCT_SINGLE_PW,
              "ContPulseOut": GPCT_CONT_PULSE_OUT,
              "SinglePulseOut": GPCT_SINGLE_PULSE_OUT}
              

class GPCT:
    """general purpose counter-timer object"""
    def __init__(self, devfile="/dev/comedi0", subdevice=4, channel=0):
        self.subdev = subdevice
        self.channel = channel
        self.fd = comedi_open(devfile)
        if not self.fd: raise "error opening %s"%devfile
        
        PFI_chans=[3,4,5,8,9]
        for pin in PFI_chans:
            self._SetAsInputs(7, pin)
        
    def __del__(self):
        comedi_close(self.fd)
    
    def Reset(self):
        self._Config(GPCT_RESET)
        
    def SetValue(self, value):
        params = chanlist(1)
        params[0] = int(value)
        
        insn = comedi_insn_struct()
        insn.insn = INSN_WRITE
        insn.n = 1
        insn.data = params
        insn.subdev = self.subdev
        insn.chanspec = cr_pack(self.channel,0,0)
        
        ret = comedi_do_insn(self.fd, insn)
        if ret==-1: raise "error executing INSN write", ret
        
    def GetValue(self):
        ret = comedi_data_read(self.fd, self.subdev, self.channel,0,0)
        if ret[0]==-1: raise "error reading data"
        return ret[1]
        
    def SetSource(self, source):
        """
        SetSource(source)
           source - either 'internal' (i.e. internal clock) or
'external' (i.e. defined pin)
           
           External source pin is either PFI8 (GPCT0) or PFI3 (GPCT1)
        """
        if source=="internal": src=GPCT_INT_CLOCK
        elif source=="external": src=GPCT_EXT_PIN
        else: raise "invalid source; must be either 'internal' or
'external'"
        
        self._Config(GPCT_SET_SOURCE, src)
        
    def SetGate(self, gate):
        """
        SetGate(gate)
            gate - either "none" or "external"
            
            External gate pin is either PFI9 (GPCT0) or PFI4 (GPCT1)
        """
        if gate=="none": src=GPCT_NO_GATE
        elif gate=="external": src=GPCT_EXT_PIN
        else: raise "invalid gate-type; must be either 'none' or
'external'"
        
        self._Config(GPCT_SET_GATE, src)
        
    def SetDirection(self, dir):
        """
        either 'up' or 'down' or 'hwud'
        """
        if dir=="up": src=GPCT_UP
        elif dir=="down": src=GPCT_DOWN
        elif dir=="hwud": src=GPCT_HWUD
        else: raise "invalid direction; must be either 'up', 'down' or
'hwud'"
        
        self._Config(GPCT_SET_DIRECTION, src)
        
    def SetOperation(self, op, param=None):
        """
        Operation can be one of: 
            "EventCount"
            "SinglePeriodMeas"
            "SinglePulseWidthMeas"
            "ContPulseOut"
            "SinglePulseOut"
            
        The pulse-output operations take a second argument specifying
the length of the pulse
        """
        try:
            OpNo = Operations[op]
        except KeyError:
            print "Invalid Operation. Must be one of:"
            for n in Operations.keys(): print n
            return
        
        self._Config(GPCT_SET_OPERATION, OpNo, param)
        
    def _SetAsInputs(self, pfi_dev, pfi_chan):
        """
        inputs:
            pfi_dev - device number of PFI triggering (usually 7)
            pfi_chan - the PFI channel number to trigger off
        """
        params = chanlist(1)
        params[0] = COMEDI_INPUT
        
        insn = comedi_insn_struct()
        insn.insn = INSN_CONFIG
        insn.n = 1
        insn.data = params
        insn.subdev = pfi_dev
        insn.chanspec = cr_pack(pfi_chan,0,0)
        
        ret = comedi_do_insn(self.fd, insn)
        if ret==-1: raise "error executing INSN input config", ret
        
    def Arm(self):
        self._Config(GPCT_ARM)
        
    def Disarm(self):
        self._Config(GPCT_DISARM)
        
    def WaitDIOEvent(self, pin):
        timeout=5
        DIO_subdev=2
        ret = WaitDIOChangeState(self.fd, DIO_subdev, pin, timeout)
        if ret == -2:
            raise "DIO Wait error (timeout maybe?)"
        elif ret != 1:
            raise "Some DIO read error"
        
    def _Config(self, type, param=None, opt=None):
        length = 1
        if param: length += 1
        if opt: length += 1
        params = chanlist(length)
        params[0] = type
        if param: params[1]=param
        if opt: params[2]=opt
        
        insn = comedi_insn_struct()
        insn.insn = INSN_CONFIG
        insn.n = length
        insn.data = params
        insn.subdev = self.subdev
        insn.chanspec = cr_pack(self.channel,0,0)
        
        ret = comedi_do_insn(self.fd, insn)
        if ret==-1: raise "error executing INSN configure", ret

Received on 2005-10-18Z09:55:33