Exemple #1
0
class VoltaBox500Hz(VoltaBox):
    """ VoltaBox500Hz - works with plain-text 500hz box, grabs data and stores data to queue

    Attributes:
        source (string): path to data source, should be able to be opened by resource manager
            may be url, e.g. 'http://myhost.tld/path/to/file'
            may be device, e.g. '/dev/cu.wchusbserial1420'
            may be path to file, e.g. '/home/users/netort/path/to/file.data'
        sample_rate (int): volta box sample rate - depends on software and which type of volta box you use
        chop_ratio (int): chop ratio for incoming data, 1 means 1 second (500 for sample_rate 500)
        baud_rate (int): baud rate for device if device specified in source
        grab_timeout (int): timeout for grabber
    """
    def __init__(self, config):
        VoltaBox.__init__(self, config)
        self.source = config.get_option('volta', 'source')
        self.chop_ratio = config.get_option('volta', 'chop_ratio')
        self.sample_rate = config.get_option('volta', 'sample_rate', 500)
        self.baud_rate = config.get_option('volta', 'baud_rate', 115200)
        self.grab_timeout = config.get_option('volta', 'grab_timeout')
        # initialize data source
        try:
            self.source_opener = resource.get_opener(self.source)
        except:
            raise RuntimeError(
                'Device %s not found. Please check VoltaBox USB connection',
                self.source)
        self.source_opener.baud_rate = self.baud_rate
        self.source_opener.read_timeout = self.grab_timeout
        self.data_source = self.source_opener()
        logger.debug('Data source initialized: %s', self.data_source)
        self.pipeline = None
        self.grabber_q = None
        self.process_currents = None

    def start_test(self, results):
        """ Grab stage - starts grabber thread and puts data to results queue
        +clean up dirty buffer

            pipeline
                read source data ->
                chop by samplerate w/ ratio ->
                make pandas DataFrame ->
                drain DataFrame to queue `results`
        """
        self.grabber_q = results

        # clean up dirty buffer
        for _ in range(self.sample_rate):
            self.data_source.readline()

        self.reader = BoxPlainTextReader(self.data_source, self.sample_rate)
        self.pipeline = Drain(
            TimeChopper(self.reader, self.sample_rate, self.chop_ratio),
            self.grabber_q)
        logger.info('Starting grab thread...')
        self.pipeline.start()
        logger.debug('Waiting grabber thread finish...')

    def end_test(self):
        self.reader.close()
        self.pipeline.close()
        self.pipeline.join(10)
        self.data_source.close()

    def get_info(self):
        data = {}
        if self.pipeline:
            data['grabber_alive'] = self.pipeline.isAlive()
        if self.grabber_q:
            data['grabber_queue_size'] = self.grabber_q.qsize()
        return data
Exemple #2
0
class VoltaBoxBinary(VoltaBox):
    """ VoltaBoxBinary - works with binary box, grabs data and stores data to queue

    Attributes:
        source (string): path to data source, should be able to be opened by resource manager
            may be url, e.g. 'http://myhost.tld/path/to/file'
            may be device, e.g. '/dev/cu.wchusbserial1420'
            may be path to file, e.g. '/home/users/netort/path/to/file.data'
        sample_rate (int): volta box sample rate - depends on software and which type of volta box you use
        chop_ratio (int): chop ratio for incoming data, 1 means 1 second (500 for sample_rate 500)
        baud_rate (int): baud rate for device if device specified in source
        grab_timeout (int): timeout for grabber
    """
    def __init__(self, config):
        VoltaBox.__init__(self, config)
        self.source = config.get_option('volta', 'source')
        self.sample_rate = config.get_option('volta', 'sample_rate', 10000)
        self.chop_ratio = config.get_option('volta', 'chop_ratio')
        self.baud_rate = config.get_option('volta', 'baud_rate', 230400)
        self.grab_timeout = config.get_option('volta', 'grab_timeout')
        self.slope = config.get_option('volta', 'slope')
        self.offset = config.get_option('volta', 'offset')
        self.power_voltage = config.get_option('volta', 'power_voltage')
        # initialize data source
        self.source_opener = resource.get_opener(self.source)
        self.source_opener.baud_rate = self.baud_rate
        self.source_opener.read_timeout = self.grab_timeout
        self.data_source = self.source_opener()
        logger.debug('Data source initialized: %s', self.data_source)
        self.pipeline = None
        self.grabber_q = None
        self.process_currents = None

    def start_test(self, results):
        """ Grab stage - starts grabber thread and puts data to results queue
        + handshake w/ device, get samplerate

            pipeline
                read source data ->
                chop by samplerate w/ ratio ->
                make pandas DataFrame ->
                drain DataFrame to queue `results`

        Args:
            results: object answers to put() and get() methods
        """
        self.grabber_q = results

        # handshake
        while self.data_source.readline() != "VOLTAHELLO\n":
            pass

        volta_spec = json.loads(self.data_source.readline())
        self.sample_rate = volta_spec["sps"]
        logger.info('Sample rate handshake success: %s', self.sample_rate)

        while self.data_source.readline() != "DATASTART\n":
            pass

        self.reader = BoxBinaryReader(self.data_source, self.sample_rate,
                                      self.slope, self.offset)
        self.pipeline = Drain(
            TimeChopper(self.reader, self.sample_rate, self.chop_ratio),
            self.grabber_q)
        logger.info('Starting grab thread...')
        self.pipeline.start()
        logger.debug('Waiting grabber thread finish...')

    def end_test(self):
        self.reader.close()
        self.pipeline.close()
        self.pipeline.join(10)
        self.data_source.close()

    def get_info(self):
        data = {}
        if self.pipeline:
            data['grabber_alive'] = self.pipeline.isAlive()
        if self.grabber_q:
            data['grabber_queue_size'] = self.grabber_q.qsize()
        return data