Page 1 of 1

Best method for capturing time when state changes occur?

Posted: Thu Nov 18, 2021 6:46 pm
by beparnas
I'm creating a system where a number of state changes are logged on a DAQ1301 Digital input module. My goal is to capture a time-series log of what inputs change, and when they change. The project is written in Python, and I've implemented a method using a Python Queue object to store information about state changes. For system time capture I am using time.perf_counter. Here is a snippet of my "onStateChange" handler as it's currently implemented:

Code: Select all

class stateChange():
    '''
    @class stateChange
    @brief an event of changing state
    '''
    state: Union[bool,int,float,str]
    lastState: Union[bool,int,float,str]

    time: perf_counter
    ch_name:str

    def __init__(   self,
                    state:Union[bool,int,float,str],
                    lastState:Union[bool,int,float,str],
                    ch_name:str) -> None:
        self.state = state
        self.lastState = lastState
        self.time = perf_counter()
        self.ch_name = ch_name
        
class customPhidgetsClass():
    channelMap:list #list of the names I've assigned to each channel
    stateChanges:queue #queue object for storing all state changes, to be parsed separately
    channels:list[Phidget] #list of phidget objects (digitaInput in this case)
          
    def onStateChange_Din(self,phidget:DigitalInput,state):
        try:
            channelName = self.channelMap[phidget.getChannel()]
        except KeyError:
            raise Exception("Input {} was not expected\
             to be mapped".format(phidget.getChannel()))
        #add state change to queue
        self.stateChanges.append(stateChange(state=state,
             lastState=self.channels[channelName].state,
             ch_name=channelName))
        self.channels[channelName].state = state


This implementation does work, and I can capture events occuring with pretty good accuracy (within a few ms of the change occuring electrically). But I'm curious if this is the best method for capturing event time? I don't believe the Phidgets API has a method built in for reading the time at which something occured, but maybe I've missed something there? are there better objects in Python than Queue to store this data in?