Example_Powersupply_advanced
This is an example of using the library.
1# %% ==========================================================================
2# Import and Definitions
3# =============================================================================
4import time
5import datetime
6import pandas as pd
7import numpy as np
8import matplotlib.pyplot as plt
9from tqdm import tqdm
10
11from dataclasses import dataclass
12from typing import Any, Union
13
14@dataclass
15class SourceCfg:
16 name: str
17 device: Any # or a Protocol for your instrument API
18 auto: bool
19 channel: Union[int, str]
20 set_voltage: float
21 current_limit: float
22
23 def apply_startup(self) -> None:
24 if self.auto:
25 self.device.set_Voltage(self.channel, self.set_voltage)
26 self.device.set_CurrentLimit(self.channel, self.current_limit)
27
28# Instrument Libraries Github: https://github.com/MartinMiroslavovMihaylov/Python_Instruments_Automation_Scripts
29# Install with:
30# pip install git+https://github.com/MartinMiroslavovMihaylov/Python_Instruments_Automation_Scripts.git
31
32# from Instruments_Libraries.GPP4323 import GPP4323
33from Instruments_Libraries.InstrumentSelect import PowerSupply_GPP4323
34# from Instruments_Libraries.KEITHLEY2612 import KEITHLEY2612
35from Instruments_Libraries.InstrumentSelect import SourceMeter
36
37# %% ==========================================================================
38# Select Instruments and Load Instrument Libraries
39# =============================================================================
40# myGPP4323 = GPP4323('COMXX') # replace with your COM Port
41myGPP4323 = PowerSupply_GPP4323()
42myGPP4323.reset()
43
44# myKEITHLEY2612 = KEITHLEY2612('COMXX') # replace with your COM Port
45myKEITHLEY2612 = SourceMeter()
46myKEITHLEY2612.reset()
47
48# %% ==========================================================================
49# Setup the Measurement
50# =============================================================================
51num_of_points = 10
52sleep_time = 0.5
53
54DC_Sources: dict[str, SourceCfg] = {
55 # dataclass name: SourceCfg(name, device, auto, channel, set_voltage, current_limit)
56 "VCC": SourceCfg("VCC", myKEITHLEY2612, True, 'a', 4.0, 0.310),
57 "bias": SourceCfg("bias", myGPP4323, True, 1, 2.9, 0.005),
58}
59
60# %% ==========================================================================
61# Configure the Instrument
62# =============================================================================
63for source in DC_Sources.values():
64 if source.auto == True:
65 source.apply_startup()
66
67myKEITHLEY2612.set_ChannelDisplay() # Display all channels
68myKEITHLEY2612.set_DisplayMeasurementFunction('a','amp') # measure current
69myKEITHLEY2612.set_DisplayMeasurementFunction('b','amp') # measure current
70
71# %% ==========================================================================
72# Measurement
73# =============================================================================
74for source in DC_Sources.values():
75 if source.auto == True: # if the source is set to auto
76 source.device.set_Out(source.channel, 'ON') # Turn on the channel
77
78records = [] # Empty list to store data and meta data
79for i in tqdm(range(num_of_points)):
80 rec = {} # single record
81 for source in DC_Sources.values():
82 rec[source.name + "_Voltage"] = source.device.ask_Voltage(source.channel)
83 rec[source.name + "_Current"] = source.device.ask_Current(source.channel)
84 rec[source.name + "_Power"] = source.device.ask_Power(source.channel)
85
86 rec["Timestamps"] = datetime.datetime.now()
87 records.append(rec)
88 time.sleep(sleep_time)
89
90# %% ==========================================================================
91# Create Dataframe
92# =============================================================================
93meas_df = pd.DataFrame.from_records(records)
94
95# %% ==========================================================================
96# Plot the Measurement
97# =============================================================================
98t0 = meas_df["Timestamps"].iloc[0]
99relative_time = (meas_df["Timestamps"] - t0).dt.total_seconds()
100
101plt.plot(relative_time, meas_df["VCC_Current"])
102plt.xlabel('Time (s)')
103plt.ylabel('Current (A)')
104plt.show()
105# %% ==========================================================================
106# Save Dataframe
107# =============================================================================
108# Save DataFrame to HDF5 (better than CSV)
109meas_df.to_hdf("measurements.h5", key="data", mode="w")
110# key="data" is like a "dataset name" inside the HDF5 file
111# (you can store multiple DataFrames in one file with different keys).
112# mode="w" overwrites the file. Use mode="a" if you want to append new datasets.
113
114# Later: Load it back
115loaded_df = pd.read_hdf("measurements.h5", key="data")
116print(loaded_df.head())
117
118#or
119
120# Save DataFrame to CSV
121meas_df.to_csv("measurements.csv", index=False)
122
123# Load it back, auto-parsing the "Timestamps" column as datetime
124loaded_df = pd.read_csv("measurements.csv", parse_dates=["Timestamps"])
125print(loaded_df.head())
126
127# %% ==========================================================================
128# Close Instrument
129# =============================================================================
130myGPP4323.Close()