def start(instr, name,
          dev):  # This function is run for every device from main.py.
    print("Measuring R of experiment %s at device %s" % (name, dev))
    [vdat, idat, _] = instr.sweep_linear(v_min=-0.4,
                                         v_max=0.4,
                                         num_datapoints=100,
                                         time_per_scan=0.2,
                                         num_average=10,
                                         auto_gain=True)
    R = linear_fit_resistance(vdat, idat)
    print("Resistance: %sOhm" % add_metric_prefix(R))
    if R < 0 or R > 5e8:
        return (R, '%sOhm' % add_metric_prefix(R), 'SKIP'
                )  # resistance not in range, skip next experiments
    return (R, '%sOhm' % add_metric_prefix(R), 'STOP')
def start(instr,name, dev): # This function is run for every device from main.py.
    print("Measuring IV of experiment %s at device %s" % (name,dev))
    data_IV = Data(name='%s_IV' % name, dev = dev,coordinates='Vsd',values='Isd')  #create the data file
    data_IV.plot()
    s=time.time()
    instr.set_gain(9)
    [vdat, idat, _] = instr.sweep_triangle(v_min=-0.4,
                                           v_max=0.4,
                                           num_datapoints=400,
                                           time_per_cycle=0.1,
                                           num_cycles=50,
                                           num_average=10)

    data_IV.fill(vdat,idat)
    print("Saved IV trace at time %.2f" % (time.time()-s))
        
    R = linear_fit_resistance(vdat,idat)
    
    data_IV.close()
    data_IV.plot()
    sleep(2)
    print("Measurement completed.")
    return (R, '%sOhm' % add_metric_prefix(R))
def start(instr, name,
          dev):  # This function is run for every device from main.py.
    ####################
    #  Preset globals  #
    ####################
    ohmflag_counter = 0
    output = []
    ##########################################
    #  Measure the resistance of the device  #
    ##########################################)
    # do a quick sweep to measure the resistance between -0.1 and 0.1 V
    [v, i, _] = instr.sweep_linear(v_min=-0.4,
                                   v_max=0.4,
                                   num_datapoints=50,
                                   num_average=25,
                                   time_per_scan=0.5)

    R = linear_fit_resistance(v, i)
    print("R of device %s = %sOhm" % (dev, add_metric_prefix(R)))

    ###########################
    #  Electroburning cycles  #
    ###########################
    cont = True
    instr.burn_gain = 4
    if (R > 10 and R < R_crit and R > 0
            and cont):  # if R is a good value, create the burning file.
        print("Starting electroburning")
        output.append('Starting eburn')
        n = 1
        diff = 1
        info_burn = create_data_file(name='%s_burninfo_%s' % (name, dev),
                                     coordinates='n',
                                     values=('R', 'breakpoint', 'gain'))
        data_burn = create_data_file(name='%s_burn_%s' % (name, dev),
                                     coordinates=('Vsd', 'n'),
                                     values='Isd')

        plot_burn = qt.Plot2D(data_burn,
                              name='burn current',
                              coorddim=0,
                              valdim=2,
                              traceofs=0)
        bpv = 10.  # set the maximum voltage to 10 V
        # loop until the electroburning is completed (R should be higher than Rcrit at the end of the process)
        while (R < R_crit and R > 0 and cont and n < num_burn_cycles):
            # run the burning process (ElectroBurn.bas), returns the sweep data and flags
            [v, i,
             burn_flags] = instr.burn(n,
                                      ramp_up=0.5,
                                      ramp_down=150,
                                      max_voltage=max_voltage_increase(bpv),
                                      sigmoid_high=25,
                                      sigmoid_low=5,
                                      sigmoid_steepness=0.8,
                                      sigmoid_center=2.0,
                                      process_delay=30000)
            # remember the breakpoint voltage (it is the maximum voltage from the burn cycle)
            if not (burn_flags & ADWIN_FLAG_OVERLOAD):
                bpv = np.max(v)
            # save burn data
            if np.size(v) > 1:
                data_burn.add_data_point(v, n * np.ones(np.size(v)),
                                         np.array(i))
                data_burn.new_block()  # is this line required?
            plot_burn.update()
            # measure the new resistance between -0.1 and 0.1 V
            [v, i, _] = instr.sweep_linear(v_min=-0.1,
                                           v_max=0.1,
                                           num_datapoints=50,
                                           num_average=10,
                                           time_per_scan=0.1)
            R = linear_fit_resistance(v, i)
            info_burn.add_data_point(n, R, bpv, instr.burn_gain)

            # output information to the user
            print(
                'R (%s) = %sOhm, %d cycles, breakpoint = %.2f V, gain = 10^%s'
                % (dev, add_metric_prefix(R), n, bpv, instr.burn_gain))

            # update electroburn parameters
            n = n + 1
            diff = 4

            # handle the burn flags: underload, overload and ohmic junction flag
            if (auto_burn_gain):
                if (burn_flags & ADWIN_FLAG_UNDERLOAD):
                    # this should be difficult to measure with current gain, change the gain!
                    if (instr.burn_gain < 4):
                        instr.burn_gain += 1
                        print(
                            '> Note: gain too low, cannot measure current. Increasing gain to: %s'
                            % instr.burn_gain)
                elif (burn_flags & ADWIN_FLAG_OVERLOAD):
                    if (instr.burn_gain > 3):
                        instr.burn_gain -= 1
                        print(
                            '> Note: gain too high, cannot measure current. Reducing gain to: %s'
                            % instr.burn_gain)
                    else:
                        print("> Device too conductive, deserting...")
                        cont = False

            # if 10 V does not electroburn the junction, then stop trying after 3 tries.
            if (burn_flags & ADWIN_FLAG_BURN_OHMIC):
                print(
                    '> Device burned at 10 V for 5 seconds, junction too ohmic.'
                )
                ohmflag_counter = ohmflag_counter + 1
                if (ohmflag_counter > 2):
                    print('Three fails, deserting device...')
                    cont = False  # ohmic resistance found after hanging at 10 V for 5 seconds, stop burning.
            else:
                ohmflag_counter = 0
            if R < 1e4:
                bpv = 10
        data_burn.close_file()
        info_burn.close_file()
        output.append('cycles: %s' % n)
        output.append('R: %s' % add_metric_prefix(R))
        if ohmflag_counter > 2:
            output.append('too ohmic')
        if n == 60:
            output.append(
                'repeat'
            )  # signal user that this junction should be electroburned once more
        #############################################################
        #  Measure an IV trace after electroburning has terminated  #
        #############################################################
        print("Measuring IV of experiment " + name + " at device " + dev)
        data_IV = create_data_file(name='%s_IV_%s' % (name, dev),
                                   coordinates='Vsd',
                                   values='Isd')
        plot_IV = qt.Plot2D(data_IV,
                            name='IV',
                            coorddim=0,
                            valdim=1,
                            traceofs=0)
        instr.iv_gain = 9
        [vdat, idat, _] = instr.sweep_triangle(v_min=v_start,
                                               v_max=v_stop,
                                               num_datapoints=datapoints,
                                               time_per_cycle=scan_time,
                                               num_cycles=cycles)
        data_IV.add_data_point(vdat, idat)
        data_IV.close_file()
        plot_IV.update()
    print("Measurement completed.")
    return output  # return the burn output to the main file. The output will be saved in a file.
def start(instr, name, dev, **kwargs):  # This function is run for every device from main.py.
    ####################
    #  Preset globals  #
    ####################
    fail_counter = 0
    num_burn_cycles = 100
    output = []
    
    ###########################
    #  Electroburning cycles  #
    ###########################
    cont = True
    R = 1e3
    instr.burn_gain = 4

    print("Starting electroburning")
    output.append('Starting eburn')
    n = 1
    info_burn = Data(name='%s_burninfo' % name, dev = dev, coordinates='n', values=('R','breakpoint','gain', 'flags'))
    data_burn = Data(name='%s_burn' % name, dev = dev, coordinates=('Vsd','n'), values='Isd')
    data_R = Data(name='%s_R' % name, dev = dev, coordinates=('Vsd','n'), values='Isd')

    data_R.plot2d()
    data_burn.plot2d()
    bpv = 10.  # set the maximum voltage to 10 V
    if R > 1e6:
        bpv = 1.
    # loop until the electroburning is completed (R should be higher than Rcrit at the end of the process)
    Rv = 0
    Ri = 0
    while (R < R_crit and R > 0 and cont and n < num_burn_cycles):
        check_user_input()  # check for user input (asynchronously)
        # run the burning process (ElectroBurn.bas), returns the sweep data and flags
        [v, i, burn_flags] = instr.eburn(v_rate_up=kwargs.get('v_rate_up',7.6), # V/s
                                         v_rate_down=2300, # V/s
                                         max_voltage=bpv, # V
                                         feedback_high=36.6, # mI / s
                                         feedback_low=6.1, # mI / s
                                         feedback_steepness=0.6, # sigmoidal curve steepness
                                         feedback_center=1.3, # V
                                         threshold_resistance=R_crit) # Ohm

        # remember the breakpoint voltage (it is the maximum voltage from the burn cycle)
        v_max = np.max(v)
        if not (burn_flags & ADWIN_FLAG_OVERLOAD or burn_flags & ADWIN_FLAG_BURN_I_OVERLOAD or burn_flags & ADWIN_FLAG_BURN_OHMIC): # in case of overload, dont change the bpv
            if not (burn_flags & ADWIN_FLAG_UNDERLOAD and instr.burn_gain < 8): # in case of underload at lowest gain, dont change the bpv
                bpv = v_max

        # save burn data
        if np.size(v) > 1:
            data_burn.fill(v, n * np.ones(np.size(v)), np.array(i))
            data_burn.new_block()  # is this line required?
        data_burn.plot()

        # measure the new resistance between -0.1 and 0.1 V
        [v, i, _] = instr.sweep_linear(v_min=-0.4, v_max=0.4, num_datapoints=50, num_average = 10, time_per_scan=0.2)
        R = abs(linear_fit_resistance(v, i))
        info_burn.fill(n, R, v_max, instr.burn_gain, burn_flags)
        data_R.fill(v, n * np.ones(np.size(v)), i)
        data_R.new_block()
        data_R.plot()

        # output information to the user
        print('cyc %d: bpv %.2f V, R (%s) %sOhm, gain %s, tr: %s' % (n, v_max, dev, add_metric_prefix(R), instr.burn_gain, trigger_flag_to_string(burn_flags)))

        # update electroburn parameters
        n = n + 1

        # handle the burn flags
        if (auto_burn_gain):
            if (burn_flags & ADWIN_FLAG_UNDERLOAD):
                # this should be difficult to measure with current gain, change the gain!
                if (instr.burn_gain < 9):
                    instr.burn_gain += 1
                    #print('> Note: gain too low, cannot measure current. Increasing gain to: %s' % instr.burn_gain)
            elif (burn_flags & ADWIN_FLAG_OVERLOAD or burn_flags & ADWIN_FLAG_BURN_I_OVERLOAD):
                if (instr.burn_gain > 3):
                    instr.burn_gain -= 1
                    num_burn_cycles += 1
                    #print('> Note: gain too high, cannot measure current. Reducing gain to: %s' % instr.burn_gain)

        # if the burn script overloads the I or the V, stop trying after 3 times
        if (burn_flags & ADWIN_FLAG_BURN_OHMIC or (burn_flags & ADWIN_FLAG_BURN_I_OVERLOAD and instr.burn_gain == 3)):
            fail_counter = fail_counter + 1
            if (fail_counter > 2):
                print('Three fails, deserting device...')
                cont = False  # ohmic resistance found after hanging at 10 V for 5 seconds, stop burning.
        else:
            fail_counter = 0

        if (burn_flags & ADWIN_FLAG_BURN_BREAKPOINT_V):
            bpv = max_voltage_increase(bpv)
        #cont = input('Continue? (1/0): ')

    data_burn.close()
    info_burn.close()
    data_R.close()

    output.append('cycles: %s' % n)
    output.append('R: %s' % add_metric_prefix(R))
    if fail_counter > 2:
        output.append('IorV overload')
        #output.append('SKIP')
    if n == num_burn_cycles:
        output.append('repeat')  # signal user that this junction should be electroburned once more
        #output.append('SKIP')
    print("Measurement completed.")
    return output  # return the burn output to the main file. The output will be saved in a file.