Example #1
0
def main():
    logger.debug('Utility module number is ' + str(utils.utilnum))
    config = load_config(args.rcfile)
    global ch,fh # Need to modify console and file logger handlers
                 # with the config file, from inside main().  They
                 # thus must be made global.
    (ch,fh) = init_logger(config,ch,fh)
    # Trigger is hard coded to internal (auto trigger) for the
    # calibration code.
    trigdict = utils.get_trig_dict(3,0,0,0)
    cgr = utils.get_cgr(config) # Connect to the unit
    caldict = utils.load_cal(cgr, config['Calibration']['calfile'])
    gainlist = utils.set_hw_gain(
        cgr, [int(config['Inputs']['Aprobe']),
              int(config['Inputs']['Bprobe'])
          ]
    )

    utils.set_trig_level(cgr, caldict, gainlist, trigdict)
    utils.set_trig_samples(cgr,trigdict)
    [ctrl_reg, fsamp_act] = utils.set_ctrl_reg(
        cgr, float(config['Acquire']['rate']), trigdict
    )
    if not (fsamp_act == float(config['Acquire']['rate'])):
        logger.warning(
            'Requested sample frequency ' + '{:0.3f} kHz '.format(
                float(config['Acquire']['rate'])/1000
            )
            + 'adjusted to ' + '{:0.3f} kHz '.format(
                float(fsamp_act)/1000
            )
        )

    # Start the offset calibration
    caldict = get_offsets(cgr, ctrl_reg, gainlist, caldict, config)

    # Start the slope calibration
    caldict = get_slopes(cgr, ctrl_reg, gainlist, caldict, config)
    utils.write_cal(cgr, config['Calibration']['calfile'], caldict)
Example #2
0
def main():
    logger.debug('Utility module number is ' + str(utils.utilnum))
    config = load_config(args.rcfile)
    global ch,fh # Need to modify console and file logger handlers
                 # with the config file, from inside main().  They
                 # thus must be made global.
    (ch,fh) = init_logger(config,ch,fh)
    cgr = utils.get_cgr(config)
    caldict = utils.load_cal(cgr, config['Calibration']['calfile'])
    eeprom_list = utils.get_eeprom_offlist(cgr)
    # Configure the inputs for 10x gain
    if (int(config['Inputs']['gain']) == 10):
        gainlist = utils.set_hw_gain(cgr,[1,1])
    else:
        gainlist = utils.set_hw_gain(cgr,[0,0])
    meanvolts = get_input_means(cgr, gainlist, caldict)
    logger.debug('Channel A mean is ' + '{:0.3f}'.format(meanvolts[0]) + ' V')
    logger.debug('Channel B mean is ' + '{:0.3f}'.format(meanvolts[1]) + ' V')
    # Configure the trigger:
    #   Trigger on channel A
    #   Trigger at channel A's mean voltage
    #   Trigger on the rising edge
    #   Capture 512 points after trigger
    trigdict = utils.get_trig_dict(0,
                                   meanvolts[0],
                                   0,
                                   512
    )
    utils.set_trig_level(cgr, caldict, gainlist, trigdict)
    utils.set_trig_samples(cgr,trigdict)
    waveplot = wave_plot_init()
    magplot = magnitude_plot_init()
    realplot = real_plot_init()
    capplot = capacitance_plot_init()
    freqlist = get_sweep_list(config)
    drive_frequency_list = []
    impedance_list = []
    for progfreq in freqlist:
        # The actual frequency will be determined by the hardware
        actfreq = utils.set_sine_frequency(cgr, float(progfreq))
        drive_frequency_list.append(actfreq)
        logger.debug('Requested ' + '{:0.2f}'.format(float(progfreq)) +
                     ' Hz, set ' + '{:0.2f}'.format(actfreq) + ' Hz')
        if (progfreq == freqlist[0]):
            # Only set amplitude once
            actamp = utils.set_output_amplitude(cgr, float(config['Sweep']['amplitude']))
            logger.debug('Requested ' + '{:0.2f}'.format(float(config['Sweep']['amplitude'])) +
                         ' Vp, set ' + '{:0.2f}'.format(actamp) + ' Vp')
        actrate = set_sample_rate(cgr, config, actfreq, trigdict)
        logger.debug('Sample rate set to ' + '{:0.2f}'.format(actrate) +
                     ' Hz, for an acquisition time of ' + '{:0.2f}'.format(1024/actrate * 1000) +
                     ' milliseconds'
                     )
        for capturenum in range(int(config['Sweep']['averages'])):
            if trigdict['trigsrc'] == 3:
                # Internal trigger
                tracedata = utils.get_uncal_forced_data(cgr,ctrl_reg)
            elif trigdict['trigsrc'] < 3:
                # Trigger on a voltage present at some input
                tracedata = utils.get_uncal_triggered_data(cgr,trigdict)
            logger.info('Acquiring trace ' + str(capturenum + 1) + ' of ' +
                        str(int(config['Sweep']['averages']))     
            )
            if capturenum == 0:
                sumdata = tracedata
            else:
                sumdata = add(sumdata,tracedata)
            avgdata = divide(sumdata,float(capturenum +1))
            # Apply calibration
            voltdata = utils.get_cal_data(caldict,gainlist,avgdata)
            if (int(config['Inputs']['gain']) == 10):
                # Divide by 10 for 10x hardware gain with no probe
                voltdata = divide(voltdata,10)
            timedata = utils.get_timelist(actrate)
        sine_vectors = get_sine_vectors(actfreq, timedata, voltdata)
        logger.debug('Channel A amplitude is ' +
                     '{:0.3f}'.format(2*vector_length(sine_vectors[0])) +
                     ' Vp'
        )
        logger.debug('Channel B amplitude is ' +
                     '{:0.3f}'.format(2*vector_length(sine_vectors[1])) +
                     ' Vp'
        )     
        logger.debug('Channel A phase shift is ' +
                     '{:0.3f}'.format(vector_angle(sine_vectors[0]) * 180/pi) +
                     ' degrees'
        )
        logger.debug('Channel B phase shift is ' +
                     '{:0.3f}'.format(vector_angle(sine_vectors[1]) * 180/pi) +
                     ' degrees'
        )
        plot_wave_data(waveplot, timedata, voltdata, trigdict, actfreq, sine_vectors)        
        impedance = get_z_vector(config, actfreq, timedata, voltdata)
        logger.debug('Impedance magnitude is ' +
                     '{:0.3f}'.format(vector_length(impedance)) +
                     ' Ohms'
        )
        logger.debug('Impedance angle is ' +
                     '{:0.3f}'.format(vector_angle(impedance) * 180/pi) +
                     ' degrees'
        )
        impedance_list.append(impedance)
        if (len(drive_frequency_list) > 1):
            plot_magnitude_data(magplot, drive_frequency_list, impedance_list)
            plot_real_data(realplot, drive_frequency_list, impedance_list)
            plot_capacitance_data(capplot, drive_frequency_list, impedance_list)
    # Set amplitude to zero to end the sweep
    utils.set_output_amplitude(cgr, 0.01)
    raw_input('Press any key to close plot and exit...')
Example #3
0
def get_offsets(handle, ctrl_reg, gainlist, caldict, config):
    """Measure and record voltage offset coefficients.

    Inputs:
        handle -- serial object representing the CGR-101
        ctrl_reg -- value of the control register
        gainlist -- [cha_gain, chb_gain]
        caldict -- Dictionary of all calibration values
        config -- Configuration dictionary from rc file

    Calibrated data is calculated with:
        volts = (511 - (rawdata + offset)) * slopevalue
    ...so offsets are calculated with:
        offset = 511 - rawdata

    Offsets for eeprom can't have decimals, so I scale the values by
    eeprom_scaler.  This means that if I need an offset of 1.2 counts,
    I'll store a 1.2 * eeprom_scaler value in eeprom.

    Returns:
        caldict: The calibration factor dictionary with the relevant
                 offset factors filled in.

    """
    offset_list = []
    gainlist = utils.set_hw_gain(handle,gainlist)
    try:
        raw_input(
            '* Disconnect all inputs and press return.\n' +
            '  Control-C to skip offset calibration.'
        )
        for capturenum in range(int(config['Acquire']['averages'])):
            tracedata = utils.get_uncal_forced_data(handle, ctrl_reg)
            logger.info('Acquiring trace ' + str(capturenum + 1) +
                        ' of ' + str(config['Acquire']['averages']))
            if capturenum == 0:
                sumdata = tracedata
            else:
                sumdata = add(sumdata,tracedata)
            avgdata = divide(sumdata,float(capturenum +1))
        for channel in range(2):
            offset_list.append(511 - average(avgdata[channel]))
        # Measured offsets need to be with offmax of zero, otherwise
        # there's something wrong with the measurement.
        offmax = 20
        if gainlist[0] == 0: # Channel A set for 1x gain
            if ((offset_list[0] > -offmax) and (offset_list[0] < offmax)):
                caldict['chA_1x_offset'] = offset_list[0]
                logger.debug('Channel A file offset set to ' +
                             str(caldict['chA_1x_offset']) + ' counts.')
                caldict['chA_1x_eeprom'] = int(round(caldict['eeprom_scaler'] *
                                                     offset_list[0]))
                logger.debug('Channel A eeprom offset set to ' +
                             str(caldict['chA_1x_eeprom']) + ' counts.')
            else:
                # The offset exceeds the allowed range -- there's
                # probably something wrong.  Don't write this
                # calibration value.
                logger.error('Measured channel A offset of ' +
                             '{:0.2f}'.format(offset_list[0]) +
                             ' exceeds +/- ' + str(offmax) +
                             ' counts.  Ignoring measurement.')
        elif gainlist[0] == 1: # Channel A set for 10x gain
            if ((offset_list[0] > -offmax) and (offset_list[0] < offmax)):
                caldict['chA_10x_offset'] = offset_list[0]
                logger.debug('Channel A file offset set to ' +
                             str(caldict['chA_10x_offset']) + ' counts.')
                caldict['chA_10x_eeprom'] = int(round(caldict['eeprom_scaler'] *
                                                      offset_list[0]))
                logger.debug('Channel A eeprom offset set to ' +
                             str(caldict['chA_10x_eeprom']) + ' counts.')
            else:
                # The offset exceeds the allowed range -- there's
                # probably something wrong.  Don't write this
                # calibration value.
                logger.error('Measured channel A offset of ' +
                             '{:0.2f}'.format(offset_list[0]) +
                             ' exceeds +/- ' + str(offmax) +
                             ' counts.  Ignoring measurement.')
        if gainlist[1] == 0: # Channel B set for 1x gain
            if ((offset_list[1] > -offmax) and (offset_list[1] < offmax)):
                caldict['chB_1x_offset'] = offset_list[1]
                logger.debug('Channel B file offset set to ' +
                             str(caldict['chB_1x_offset']) + ' counts.')
                caldict['chB_1x_eeprom'] = int(round(caldict['eeprom_scaler'] *
                                                     offset_list[1]))
                logger.debug('Channel B eeprom offset set to ' +
                             str(caldict['chB_1x_eeprom']) + ' counts.')
            else:
                # The offset exceeds the allowed range -- there's
                # probably something wrong.  Don't write this
                # calibration value.
                logger.error('Measured channel B offset of ' +
                             '{:0.2f}'.format(offset_list[1]) +
                             ' exceeds +/- ' + str(offmax) +
                             ' counts.  Ignoring measurement.')
        elif gainlist[1] == 1: # Channel B set for 10x gain
            if ((offset_list[1] > -offmax) and (offset_list[1] < offmax)):
                caldict['chB_10x_offset'] = offset_list[1]
                logger.debug('Channel B file offset set to ' +
                             str(caldict['chB_10x_offset']) + ' counts.')
                caldict['chB_10x_eeprom'] = int(round(caldict['eeprom_scaler'] *
                                                      offset_list[1]))
                logger.debug('Channel B eeprom offset set to ' +
                             str(caldict['chB_10x_eeprom']) + ' counts.')
            else:
                # The offset exceeds the allowed range -- there's
                # probably something wrong.  Don't write this
                # calibration value.
                logger.error('Measured channel B offset of ' +
                             '{:0.2f}'.format(offset_list[1]) +
                             ' exceeds +/- ' + str(offmax) +
                             ' counts.  Ignoring measurement.')
    except KeyboardInterrupt:
        print(' ')
        logger.info('Offset calibration skipped')
    return caldict
Example #4
0
def get_slopes(handle, ctrl_reg, gainlist, caldict, config):
    """Measure and record voltage slope coefficients.

    This doesn't measure all slope coeffients -- just those for the
    gain settings being used.

    Calibrated data is calculated with:
        volts = (511 - (rawdata + offset)) * slopevalue
    ...so slopes are calculated with:
        slopevalue = calvolt/(offset corrected data)

    Arguments:
      handle -- serial object representing the CGR-101
      ctrl_reg -- value of the control register
      gainlist -- [cha_gain, chb_gain]
      caldict -- Dictionary of all calibration values
      config -- Configuration dictionary from rc file

    """
    calvolt = float(config['Calibration']['voltage'])
    slope_list = []
    gainlist = utils.set_hw_gain(handle,gainlist)
    try:
        raw_input(
            '* Connect ' + '{:0.3f}'.format(calvolt) +
            'V calibration voltage and press return.\n' +
            '  Control-C to skip slope calibration'
        )
        for capturenum in range(int(config['Acquire']['averages'])):
            tracedata = utils.get_uncal_forced_data(handle, ctrl_reg)
            logger.info('Acquiring trace ' + str(capturenum + 1) +
                        ' of ' + str(config['Acquire']['averages']))
            if capturenum == 0:
                sumdata = tracedata
            else:
                sumdata = add(sumdata,tracedata)
            avgdata = divide(sumdata,float(capturenum +1))
        offcal_data = get_offcal_data(caldict,gainlist,avgdata)
        # Measured slope needs to be within 5 of 45 mV/count
        slopemax = 0.005
        for channel in range(2):
            slope_list.append(calvolt/(average(offcal_data[channel])))
        if gainlist[0] == 0: # Channel A set for 1x gain
            if ((slope_list[0] > (0.045 - slopemax)) and
                (slope_list[0] < (0.045 + slopemax))):
                caldict['chA_1x_slope'] = slope_list[0]
                logger.debug('Channel A 1x slope set to ' +
                             '{:0.1f}'.format(1000 * caldict['chA_1x_slope']) +
                             ' millivolts per count.'
                )
            else:
                # The slope exceeds the allowed range -- there's
                # probably something wrong.  Don't write this
                # calibration value.
                logger.error('Measured channel A slope of ' +
                             '{:0.2f}'.format(slope_list[0]) +
                             ' not within +/- ' + '{:0.0f}'.format(1000 * slopemax) +
                             ' mV of 45 mV per count.  Ignoring measurement.')
        elif gainlist[0] == 1: # Channel A set for 10x gain
            if ((slope_list[0] > (0.045 - slopemax)) and
                (slope_list[0] < (0.045 + slopemax))):
                caldict['chA_10x_slope'] = slope_list[0]
                logger.debug('Channel A 10x slope set to ' +
                             '{:0.1f}'.format(1000 * caldict['chA_10x_slope']) +
                             ' millivolts per count.'
                )
            else:
                # The slope exceeds the allowed range -- there's
                # probably something wrong.  Don't write this
                # calibration value.
                logger.error('Measured channel A slope of ' +
                             '{:0.2f}'.format(slope_list[0]) +
                             ' not within +/- ' + '{:0.0f}'.format(1000 * slopemax) +
                             ' mV of 45 mV per count.  Ignoring measurement.')
        if gainlist[1] == 0: # Channel B set for 1x gain
            if ((slope_list[1] > (0.045 - slopemax)) and
                (slope_list[1] < (0.045 + slopemax))):
                caldict['chB_1x_slope'] = slope_list[1]
                logger.debug('Channel B 1x slope set to ' +
                             '{:0.1f}'.format(1000 * caldict['chB_1x_slope']) +
                             ' millivolts per count.'
                )
            else:
                # The slope exceeds the allowed range -- there's
                # probably something wrong.  Don't write this
                # calibration value.
                logger.error('Measured channel B slope of ' +
                             '{:0.2f}'.format(slope_list[1]) +
                             ' not within +/- ' + '{:0.0f}'.format(1000 * slopemax) +
                             ' mV of 45 mV per count.  Ignoring measurement.')
        elif gainlist[1] == 1: # Channel B set for 10x gain
            if ((slope_list[1] > (0.045 - slopemax)) and
                (slope_list[1] < (0.045 + slopemax))):
                caldict['chB_10x_slope'] = slope_list[1]
                logger.debug('Channel B 10x slope set to ' +
                             '{:0.1f}'.format(1000 * caldict['chB_10x_slope']) +
                             ' millivolts per count.'
                )
            else:
                # The slope exceeds the allowed range -- there's
                # probably something wrong.  Don't write this
                # calibration value.
                logger.error('Measured channel B slope of ' +
                             '{:0.2f}'.format(slope_list[1]) +
                             ' not within +/- ' + '{:0.0f}'.format(1000 * slopemax) +
                             ' mV of 45 mV per count.  Ignoring measurement.')
    except KeyboardInterrupt:
        print(' ')
        logger.info('Slope calibration skipped')
    return caldict
Example #5
0
def main():
    logger.debug('Utility module number is ' + str(utils.utilnum))
    config = load_config(args.rcfile)
    global ch,fh # Need to modify console and file logger handlers
                 # with the config file, from inside main().  They
                 # thus must be made global.
    (ch,fh) = init_logger(config,ch,fh)
    trigdict = utils.get_trig_dict( int(config['Trigger']['source']),
                                     float(config['Trigger']['level']),
                                     int(config['Trigger']['polarity']),
                                     int(config['Trigger']['points'])
    )
    cgr = utils.get_cgr(config)
    caldict = utils.load_cal(cgr, config['Calibration']['calfile'])
    eeprom_list = utils.get_eeprom_offlist(cgr)
    gainlist = utils.set_hw_gain(
        cgr, [int(config['Inputs']['Aprobe']),
              int(config['Inputs']['Bprobe'])
          ]
    )

    utils.set_trig_level(cgr, caldict, gainlist, trigdict)
    utils.set_trig_samples(cgr,trigdict)
    [ctrl_reg, fsamp_act] = utils.set_ctrl_reg(
        cgr, float(config['Acquire']['rate']), trigdict
    )
    if not (fsamp_act == float(config['Acquire']['rate'])):
        logger.warning(
            'Requested sample frequency ' + '{:0.3f} kHz '.format(
                float(config['Acquire']['rate'])/1000
            )
            + 'adjusted to ' + '{:0.3f} kHz '.format(
                float(fsamp_act)/1000
            )
        )

    # Wait for trigger, then return uncalibrated data
    gplot = plotinit() # Create plot object
    for capturenum in range(int(config['Acquire']['averages'])):
        if trigdict['trigsrc'] == 3:
            # Internal trigger
            tracedata = utils.get_uncal_forced_data(cgr,ctrl_reg)
        elif trigdict['trigsrc'] < 3:
            # Trigger on a voltage present at some input
            tracedata = utils.get_uncal_triggered_data(cgr,trigdict)
        logger.info('Acquiring trace ' + str(capturenum + 1) + ' of ' +
                    str(config['Acquire']['averages']))
        if capturenum == 0:
            sumdata = tracedata
        else:
            sumdata = add(sumdata,tracedata)
        avgdata = divide(sumdata,float(capturenum +1))
        # Apply calibration
        voltdata = utils.get_cal_data(
            caldict,gainlist,[avgdata[0],avgdata[1]]
        )
        timedata = utils.get_timelist(fsamp_act)
        logger.debug(
            'Plotting average of ' + str(capturenum + 1) + ' traces.'
        )
        plotdata(gplot, timedata, voltdata, trigdict)



    savedata(config, timedata, voltdata)
    raw_input('Press any key to close plot and exit...')