seminars.fb

import pandas as pd
import pyvisa as visa
import time
import os

os.add_dll_directory('C:\\Program Files (x86)\\Keysight\\IO Libraries Suite\\bin')
rm = visa.ResourceManager('ktvisa32')

# XXX|dutc: `contextlib.contextmanager`
print(rm.list_resources())
Tek_Scope = rm.open_resource('USB0::0x0699::0x0522::B011353::0::INSTR')
Chroma_Load_63206 = rm.open_resource('GPIB0::8::INSTR')
Chroma_Source_61512 = rm.open_resource('GPIB0::30::INSTR')

Vendor = 'Artesyn'
Version = 'EVT'
VoltageSweep = [300, 200]
#FrequencySweep = [50, 60]
Load_Frequency_Sweep= [50]#, 2000] #Permissible: 50Hz or 2000Hz
Full_Load_shelf= 530
Slew_Rate= '1A/us'
RestartVoltage= 250

Total_scenario=3

# XXX|dutc: `collections.namedtuple`
Tek_Config = [
    {'Ch': 2, 'Label': 'Input_VINac_Ph1', 'Vertical': '500', 'Position': '4', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 3, 'Label': 'Input_VINac_Ph2', 'Vertical': '500', 'Position': '3', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 4, 'Label': 'Input_VINac_Ph3', 'Vertical': '500', 'Position': '2', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 5, 'Label': 'Vout', 'Vertical': '0.5', 'Position': '0', 'Coupling': 'AC', 'Bandwidth': '20'},
    {'Ch': 6, 'Label': 'ILOAD_Left', 'Vertical': '50', 'Position': '-3.9', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 7, 'Label': 'ILOAD_Mid', 'Vertical': '50', 'Position': '-4', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 8, 'Label': 'ILOAD_Right', 'Vertical': '50', 'Position': '-4.1', 'Coupling': 'DC', 'Bandwidth': '20'}
]

# Oscilloscope screen setup:
Tek_Scope.write('ACQUIRE:STATE RUN')
Tek_Channel_df = pd.DataFrame(Tek_Config)
total_rows = len(Tek_Channel_df.index)

i = 0
while i < total_rows:
    Channel = Tek_Channel_df.loc[i, 'Ch']
    Label = str(Tek_Channel_df.loc[i, 'Label'])
    Vertical = Tek_Channel_df.loc[i, 'Vertical']
    Position = Tek_Channel_df.loc[i, 'Position']
    Coupling = Tek_Channel_df.loc[i, 'Coupling']
    Bandwidth = Tek_Channel_df.loc[i, 'Bandwidth']

    Tek_Scope.write('CH' + str(Channel) + ':LABEL:NAME "%s" ' % Label)
    Tek_Scope.write('CH' + str(Channel) + ':SCALE ' + Vertical)
    Tek_Scope.write('CH' + str(Channel) + ':POSITION ' + Position)
    Tek_Scope.write('CH' + str(Channel) + ':COUPLING ' + Coupling)
    Tek_Scope.write('CH' + str(Channel) + ':BANDWIDTH ' + Bandwidth)
    i = i + 1

# XXX|dutc: symbolic representation
# Oscilloscope Trigger setup:
TriggerChannel = '8'
TriggerMode = 'AUTO'
TriggerEdge = 'RISE'
Tek_Scope.write('TRIGGER:A:EDGE:COUPLING DC')
Tek_Scope.write('TRIGGER:A:TYPE EDGE')
Tek_Scope.write('TRIGGER:A:EDGE:SOURCE CH' + str(TriggerChannel))
#Tek_Scope.write('TRIGGER:A:MODE ' + str(TriggerMode))
Tek_Scope.write('TRIGGER:A:MODE AUTO')
Tek_Scope.write('TRIGGER:A:EDGE:SLOPE ' + str(TriggerEdge))
Tek_Scope.write('ACQUIRE:STATE RUN')

#Oscilloscope Cursor setup:
Tek_Scope.write('DISplay:SELect:SOUrce CH5')
Tek_Scope.write('DISPLAY:WAVEVIEW1:CURSOR:CURSOR1:FUNCTION HBArs')
Tek_Scope.write('DISplay:WAVEView1:CURSor:CURSOR1:ASOUrce CH5')
Tek_Scope.write('DISplay:WAVEView1:CURSor:CURSOR1:BSOUrce CH5')


# XXX|dutc: loop structure
presentScenario=1
while presentScenario<=Total_scenario:

    # XXX|dutc: data structuring
    if presentScenario == 1:
        Current_High_perc = 50
        Current_Low_perc = 10
    elif presentScenario == 2:
        Current_High_perc = 100
        Current_Low_perc = 50
    else:
        Current_High_perc = 100
        Current_Low_perc = 0


    for Vin in VoltageSweep:
        if Vin == 300:
            Freq_AC = 60
        if Vin == 200:
            Freq_AC = 50

        for Freq_Load in Load_Frequency_Sweep:
            Chroma_Source_61512.write('OUTPUT OFF')
            Chroma_Load_63206.write('LOAD OFF')
            time.sleep(1)

            if Vin == 180:  # Startup cannot happen below 190V
                Vin = RestartVoltage

            ########### Configure Input AC power supply:
            Chroma_Source_61512.write('INST:PHAS THREE')  # Sets to 3phase mode
            time.sleep(0.5)
            Chroma_Source_61512.write('INST:EDIT ALL')  # its to set a command to all phase
            time.sleep(0.5)
            Chroma_Source_61512.write('VOLT:AC ' + str(Vin))  # Sets voltage
            time.sleep(1)
            Chroma_Source_61512.write('FREQ ' + str(Freq_AC))  # Sets frequency to 60Hz
            time.sleep(1)
            print('AC set')

            ############ Configure Load:
            Current_High = Full_Load_shelf * (Current_High_perc / 100)
            Current_Low = Full_Load_shelf * (Current_Low_perc / 100)


            Chroma_Load_63206.write('CURR:DYN:L1 ' + str(Current_High))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:L2 ' + str(Current_Low))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:T1 ' + str(1 / Freq_Load))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:T2 ' + str(1 / Freq_Load))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:RISE ' + str(Slew_Rate))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:FALL ' + str(Slew_Rate))
            time.sleep(0.5)
            print('Load set')

            ######### Configure Oscilloscope:

            # Trigger:

            if (Freq_Load == 50):
                Tek_Scope.write('HORIZONTAL:SCALE 10E-3')
            else:
                Tek_Scope.write('HORIZONTAL:SCALE 1E-3')
            time.sleep(1)

            # SYSTEM TURN ON and clear cursors
            Chroma_Load_63206.write('LOAD ON')
            time.sleep(1)
            Chroma_Load_63206.write('LOAD OFF')
            time.sleep(0.5)
            Chroma_Source_61512.write('OUTPUT ON')
            time.sleep(2)
            # print(Vin)

            Chroma_Load_63206.write('LOAD ON')
            time.sleep(3)
            print(
                'Starting ' + str(Vin) + 'V, ' + str(Freq_AC) + 'Hz, with fload=' + str(
                    Freq_Load) + 'Hz. Load Step=' +
                str(Current_High) + 'A to ' + str(Current_Low) + 'A')
            Tek_Scope.write('MEASUREMENT:DELETE "MEAS1"')
            Tek_Scope.write('MEASUREMENT:DELETE "MEAS2"')

            time.sleep(1)

            TriggerLevel = str((Current_High + Current_Low)/2 / 3)
            print(TriggerLevel)
            Tek_Scope.write('TRIGGER:A:LEVEL:CH' + str(TriggerChannel) + str(' ') + str(TriggerLevel))
            Tek_Scope.write('TRIGGER:A:MODE NORMAL')

            time.sleep(5)
            print('Sleeping, read to acquire')



            # Take oscilloscope measurements and place cursors:
            Tek_Scope.write('ACQUIRE:STATE STOP')
            Tek_Scope.write('MEASUREMENT:CH5:ADDMEAS MAXIMUM')
            Tek_Scope.write('MEASUREMENT:CH5:ADDMEAS MINIMUM')
            time.sleep(2)
            Tek_Scope.write('MEASUrement:MEAS1:MAXIMUM?')
            time.sleep(2)
            MaximumVal = Tek_Scope.read()
            time.sleep(2)
            Tek_Scope.write('MEASUrement:MEAS2:MINIMUM?')
            MinimumVal = Tek_Scope.read()
            print(MaximumVal)
            print(MinimumVal)
            Tek_Scope.write('DISplay:WAVEView1:CURSor:CURSOR1:HBArs:APOSition ' + str(MaximumVal))
            Tek_Scope.write('DISplay:WAVEView1:CURSor:CURSOR1:HBArs:BPOSition ' + str(MinimumVal))

            # Take screenshot and save file on computer:
            Tek_Scope.write('SAVE:IMAGE \"C:/Temp.png\"')
            Tek_Scope.write('HARDCOPY START')
            time.sleep(1)
            Tek_Scope.query('*OPC?')
            Tek_Scope.write('FILESystem:READFile \"C:/Temp.png\"')
            raw_data = Tek_Scope.read_raw(1024 * 1024)
            Filename = str(Vendor) + str(Version) + '_Transient_' + str(Current_High) + 'Ato' + str(
                Current_Low) + 'A_' + str(Vin) + 'V_' + str(Freq_AC) + \
                       'Hz_fload' + str(Freq_Load) + 'Hz'
            print(Filename)
            fid = open(Filename + '.png', 'wb')
            fid.write(raw_data)
            fid.close()
            print('File ' + Filename + '.png saved')

            # De-energize system
            Chroma_Load_63206.write('LOAD OFF')
            time.sleep(1)
            Chroma_Source_61512.write('OUTPUT OFF')
            if Vin == 180:  # Startup cannot happen below 190V
                Vin = RestartVoltage
            Chroma_Source_61512.write('VOLT:AC ' + str(Vin))  # Sets voltage
            time.sleep(5)

            Tek_Scope.write('ACQUIRE:STATE RUN')
            Tek_Scope.write('TRIGGER:A:MODE AUTO')
            time.sleep(1)

    presentScenario=presentScenario+1

Chroma_Source_61512.write('OUTPUT OFF')
Chroma_Load_63206.write('LOAD OFF')
from collections import namedtuple

class ChannelParam(namedtuple('ChannelParamBase', 'channel label vertical position coupling bandwidth')):
    def __new__(cls, channel, label, vertical, position, coupling='DC', bandwidth=20):
        if 100 <= vertical < 1000:
            ...
        return super().__new__(cls, channel, label, vertical, position, coupling, bandwidth)
    @property
    def commands(self):
        yield Command.channel_config(channel, 'name', self.label)
        yield Command.channel_config(channel, 'scale', self.vertical)
        yield Command.channel_config(channel, 'position', self.position)
        yield Command.channel_config(channel, 'coupling', self.coupling)
        yield Command.channel_config(channel, 'bandwidth', self.bandwidth)

class Command:
    def channel_config(self, channel, label, value):
        return f'CH{channel}:LABEL:{label} "{self.value}" '
    def measurement(self, channel, measure):
        return f'MEASUREMENT:CH{channel}:ADDMEAS {measure}'

channels = [
    ChannelParam(2, 'Input_VINac_Ph1', scale=500, position=4),
    ChannelParam(3, 'Input_VINac_Ph2', scale=500, position=3),
    ChannelParam(4, 'Input_VINac_Ph3', scale=500, position=2),
]

for ch in channels:
    for cmd in ch.commands:
        ...

class Scenario(namedtuple('ScenarioBase', 'high_perc low_perc')):
    def current_high(self, full_load):
        return full_load * (self.high_perc / 100)
    def current_low(self, full_load):
        return full_load * (self.low_perc / 100)

scenarios = [
    Scenario(50, 10),
    Scenario(100, 50),
    Scenario(100, 0),
]

for sc in scenarios:
    print(f'{sc = }')
    print(f'{sc.high_perc = }')
    print(f'{sc.low_perc  = }')
    print(f'{sc.current_high(10) = }')
    print(f'{sc.current_high(20) = }')

# teardown commands
...


```python
import pandas as pd
import pyvisa as visa
import time
import os

os.add_dll_directory('C:\\Program Files (x86)\\Keysight\\IO Libraries Suite\\bin')
rm = visa.ResourceManager('ktvisa32')

# XXX|dutc: `contextlib.contextmanager`
print(rm.list_resources())
Tek_Scope = rm.open_resource('USB0::0x0699::0x0522::B011353::0::INSTR')
Chroma_Load_63206 = rm.open_resource('GPIB0::8::INSTR')
Chroma_Source_61512 = rm.open_resource('GPIB0::30::INSTR')

Vendor = 'Artesyn'
Version = 'EVT'
VoltageSweep = [300, 200]
#FrequencySweep = [50, 60]
Load_Frequency_Sweep= [50]#, 2000] #Permissible: 50Hz or 2000Hz
Full_Load_shelf= 530
Slew_Rate= '1A/us'
RestartVoltage= 250

Total_scenario=3

# XXX|dutc: `collections.namedtuple`
Tek_Config = [
    {'Ch': 2, 'Label': 'Input_VINac_Ph1', 'Vertical': '500', 'Position': '4', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 3, 'Label': 'Input_VINac_Ph2', 'Vertical': '500', 'Position': '3', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 4, 'Label': 'Input_VINac_Ph3', 'Vertical': '500', 'Position': '2', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 5, 'Label': 'Vout', 'Vertical': '0.5', 'Position': '0', 'Coupling': 'AC', 'Bandwidth': '20'},
    {'Ch': 6, 'Label': 'ILOAD_Left', 'Vertical': '50', 'Position': '-3.9', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 7, 'Label': 'ILOAD_Mid', 'Vertical': '50', 'Position': '-4', 'Coupling': 'DC', 'Bandwidth': '20'},
    {'Ch': 8, 'Label': 'ILOAD_Right', 'Vertical': '50', 'Position': '-4.1', 'Coupling': 'DC', 'Bandwidth': '20'}
]

# Oscilloscope screen setup:
Tek_Scope.write('ACQUIRE:STATE RUN')
Tek_Channel_df = pd.DataFrame(Tek_Config)
total_rows = len(Tek_Channel_df.index)

i = 0
while i < total_rows:
    Channel = Tek_Channel_df.loc[i, 'Ch']
    Label = str(Tek_Channel_df.loc[i, 'Label'])
    Vertical = Tek_Channel_df.loc[i, 'Vertical']
    Position = Tek_Channel_df.loc[i, 'Position']
    Coupling = Tek_Channel_df.loc[i, 'Coupling']
    Bandwidth = Tek_Channel_df.loc[i, 'Bandwidth']

    Tek_Scope.write('CH' + str(Channel) + ':LABEL:NAME "%s" ' % Label)
    Tek_Scope.write('CH' + str(Channel) + ':SCALE ' + Vertical)
    Tek_Scope.write('CH' + str(Channel) + ':POSITION ' + Position)
    Tek_Scope.write('CH' + str(Channel) + ':COUPLING ' + Coupling)
    Tek_Scope.write('CH' + str(Channel) + ':BANDWIDTH ' + Bandwidth)
    i = i + 1

# XXX|dutc: symbolic representation
# Oscilloscope Trigger setup:
TriggerChannel = '8'
TriggerMode = 'AUTO'
TriggerEdge = 'RISE'
Tek_Scope.write('TRIGGER:A:EDGE:COUPLING DC')
Tek_Scope.write('TRIGGER:A:TYPE EDGE')
Tek_Scope.write('TRIGGER:A:EDGE:SOURCE CH' + str(TriggerChannel))
#Tek_Scope.write('TRIGGER:A:MODE ' + str(TriggerMode))
Tek_Scope.write('TRIGGER:A:MODE AUTO')
Tek_Scope.write('TRIGGER:A:EDGE:SLOPE ' + str(TriggerEdge))
Tek_Scope.write('ACQUIRE:STATE RUN')

#Oscilloscope Cursor setup:
Tek_Scope.write('DISplay:SELect:SOUrce CH5')
Tek_Scope.write('DISPLAY:WAVEVIEW1:CURSOR:CURSOR1:FUNCTION HBArs')
Tek_Scope.write('DISplay:WAVEView1:CURSor:CURSOR1:ASOUrce CH5')
Tek_Scope.write('DISplay:WAVEView1:CURSor:CURSOR1:BSOUrce CH5')


# XXX|dutc: loop structure
presentScenario=1
while presentScenario<=Total_scenario:

    # XXX|dutc: data structuring
    if presentScenario == 1:
        Current_High_perc = 50
        Current_Low_perc = 10
    elif presentScenario == 2:
        Current_High_perc = 100
        Current_Low_perc = 50
    else:
        Current_High_perc = 100
        Current_Low_perc = 0


    for Vin in VoltageSweep:
        if Vin == 300:
            Freq_AC = 60
        if Vin == 200:
            Freq_AC = 50

        for Freq_Load in Load_Frequency_Sweep:
            Chroma_Source_61512.write('OUTPUT OFF')
            Chroma_Load_63206.write('LOAD OFF')
            time.sleep(1)

            if Vin == 180:  # Startup cannot happen below 190V
                Vin = RestartVoltage

            ########### Configure Input AC power supply:
            Chroma_Source_61512.write('INST:PHAS THREE')  # Sets to 3phase mode
            time.sleep(0.5)
            Chroma_Source_61512.write('INST:EDIT ALL')  # its to set a command to all phase
            time.sleep(0.5)
            Chroma_Source_61512.write('VOLT:AC ' + str(Vin))  # Sets voltage
            time.sleep(1)
            Chroma_Source_61512.write('FREQ ' + str(Freq_AC))  # Sets frequency to 60Hz
            time.sleep(1)
            print('AC set')

            ############ Configure Load:
            Current_High = Full_Load_shelf * (Current_High_perc / 100)
            Current_Low = Full_Load_shelf * (Current_Low_perc / 100)


            for cmd in ...:
                Choma_Load_623206.write(cmd)
                sleep(0.4)

            Chroma_Load_63206.write('CURR:DYN:L1 ' + str(Current_High))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:L2 ' + str(Current_Low))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:T1 ' + str(1 / Freq_Load))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:T2 ' + str(1 / Freq_Load))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:RISE ' + str(Slew_Rate))
            time.sleep(0.5)
            Chroma_Load_63206.write('CURR:DYN:FALL ' + str(Slew_Rate))
            time.sleep(0.5)
            print('Load set')

            ######### Configure Oscilloscope:

            # Trigger:

            if (Freq_Load == 50):
                Tek_Scope.write('HORIZONTAL:SCALE 10E-3')
            else:
                Tek_Scope.write('HORIZONTAL:SCALE 1E-3')
            time.sleep(1)

            # SYSTEM TURN ON and clear cursors
            Chroma_Load_63206.write('LOAD ON')
            time.sleep(1)
            Chroma_Load_63206.write('LOAD OFF')
            time.sleep(0.5)
            Chroma_Source_61512.write('OUTPUT ON')
            time.sleep(2)
            # print(Vin)

            Chroma_Load_63206.write('LOAD ON')
            time.sleep(3)
            print(
                'Starting ' + str(Vin) + 'V, ' + str(Freq_AC) + 'Hz, with fload=' + str(
                    Freq_Load) + 'Hz. Load Step=' +
                str(Current_High) + 'A to ' + str(Current_Low) + 'A')
            Tek_Scope.write('MEASUREMENT:DELETE "MEAS1"')
            Tek_Scope.write('MEASUREMENT:DELETE "MEAS2"')

            time.sleep(1)

            TriggerLevel = str((Current_High + Current_Low)/2 / 3)
            print(TriggerLevel)
            Tek_Scope.write('TRIGGER:A:LEVEL:CH' + str(TriggerChannel) + str(' ') + str(TriggerLevel))
            Tek_Scope.write('TRIGGER:A:MODE NORMAL')

            time.sleep(5)
            print('Sleeping, read to acquire')



            # Take oscilloscope measurements and place cursors:
            Tek_Scope.write('ACQUIRE:STATE STOP')
            Tek_Scope.write('MEASUREMENT:CH5:ADDMEAS MAXIMUM')
            Tek_Scope.write('MEASUREMENT:CH5:ADDMEAS MINIMUM')
            time.sleep(2)
            Tek_Scope.write('MEASUrement:MEAS1:MAXIMUM?')
            time.sleep(2)
            MaximumVal = Tek_Scope.read()
            time.sleep(2)
            Tek_Scope.write('MEASUrement:MEAS2:MINIMUM?')
            MinimumVal = Tek_Scope.read()
            print(MaximumVal)
            print(MinimumVal)
            Tek_Scope.write('DISplay:WAVEView1:CURSor:CURSOR1:HBArs:APOSition ' + str(MaximumVal))
            Tek_Scope.write('DISplay:WAVEView1:CURSor:CURSOR1:HBArs:BPOSition ' + str(MinimumVal))

            # Take screenshot and save file on computer:
            Tek_Scope.write('SAVE:IMAGE \"C:/Temp.png\"')
            Tek_Scope.write('HARDCOPY START')
            time.sleep(1)
            Tek_Scope.query('*OPC?')
            Tek_Scope.write('FILESystem:READFile \"C:/Temp.png\"')
            raw_data = Tek_Scope.read_raw(1024 * 1024)
            Filename = str(Vendor) + str(Version) + '_Transient_' + str(Current_High) + 'Ato' + str(
                Current_Low) + 'A_' + str(Vin) + 'V_' + str(Freq_AC) + \
                       'Hz_fload' + str(Freq_Load) + 'Hz'
            print(Filename)
            fid = open(Filename + '.png', 'wb')
            fid.write(raw_data)
            fid.close()
            print('File ' + Filename + '.png saved')

            # De-energize system
            Chroma_Load_63206.write('LOAD OFF')
            time.sleep(1)
            Chroma_Source_61512.write('OUTPUT OFF')
            if Vin == 180:  # Startup cannot happen below 190V
                Vin = RestartVoltage
            Chroma_Source_61512.write('VOLT:AC ' + str(Vin))  # Sets voltage
            time.sleep(5)

            Tek_Scope.write('ACQUIRE:STATE RUN')
            Tek_Scope.write('TRIGGER:A:MODE AUTO')
            time.sleep(1)

    presentScenario=presentScenario+1

Chroma_Source_61512.write('OUTPUT OFF')
Chroma_Load_63206.write('LOAD OFF')
with TekScope() as ts:
    for p in channel_params:
        ts.write(p)

    with ChromaSource() as cs, ChromaLoad() as cl:
        for sc in scenarios:
            try:
                for vs in voltage_sweep:
                    for ls in load_sweep:
                        ...
            except Exception:
                pass
from queue import Queue
from collections import namedtuple, defaultdict
from itertools import product
from random import choice

Test = namedtuple('Test', 'voltage load')
queue = Queue()
for v, l in product([10, 20, 30], [300, 400, 500]):
    t = Test(v, l)
    queue.put(t)

results = defaultdict(list)
while not queue.empty():
    t = queue.get()
    try:
        print(f'Trying {t = }')
        if choice([False, False, True]):
            0 / 0
        print('\tSuccess!')
        results[t].append('results')
    except Exception:
        results[t].append('fail')
        queue.put(t)
        print('\tFailure!')

for t,rs in results.items():
    print(f'{t = }')
    print(f'\t{rs = }')