Example_Powersupply_GPP4323
This is an example of using the library.
1# %% ==========================================================================
2# Import and Definitions
3# =============================================================================
4import datetime
5import time
6
7import matplotlib.pyplot as plt
8import numpy as np
9import pandas as pd
10from tqdm import tqdm
11
12# Instrument Libraries Github: https://github.com/MartinMiroslavovMihaylov/Python_Instruments_Automation_Scripts
13# Install with:
14# pip install git+https://github.com/MartinMiroslavovMihaylov/Python_Instruments_Automation_Scripts.git
15from Instruments_Libraries.GPP4323 import GPP4323
16from Instruments_Libraries.InstrumentSelect import PowerSupply_GPP4323
17
18# %% ==========================================================================
19# Select Instruments and Load Instrument Libraries
20# =============================================================================
21myGPP4323 = GPP4323('COMXX')
22# myGPP4323: GPP4323 = PowerSupply_GPP4323() # noqa: N816
23myGPP4323.reset()
24
25# %% ==========================================================================
26# Setup the Measurement
27# =============================================================================
28num_of_points = 10
29sleep_time = 0.5
30vcc_ch_num = 1 # channel number for VCC
31vcc = 4.7 # V
32vcc_current_limit = 0.140 # A
33
34# %% ==========================================================================
35# Configure the Instrument
36# =============================================================================
37myGPP4323.set_voltage(vcc_ch_num, vcc)
38myGPP4323.set_current(vcc_ch_num, vcc_current_limit)
39
40# %% ==========================================================================
41# Measurement
42# =============================================================================
43myGPP4323.set_output(channel=vcc_ch_num, state='ON') # turn on the channel or
44# myGPP4323.set_all_outputs(state='ON') # turn on all channels
45
46records = [] # Empty list to store data and meta data
47for idx in tqdm(range(num_of_points)):
48 rec = {} # single record
49 rec["VCC_Voltage"] = myGPP4323.measure_voltage(vcc_ch_num)
50 rec["VCC_Current"] = myGPP4323.measure_current(vcc_ch_num)
51 rec["Power"] = myGPP4323.measure_power(vcc_ch_num)
52 rec["Timestamps"] = datetime.datetime.now()
53 records.append(rec)
54 time.sleep(sleep_time)
55 temp = idx*np.pi # do something with idx
56
57# %% ==========================================================================
58# Create Dataframe
59# =============================================================================
60meas_df = pd.DataFrame.from_records(records)
61
62# %% ==========================================================================
63# Plot the Measurement
64# =============================================================================
65t0 = meas_df["Timestamps"].iloc[0]
66relative_time = (meas_df["Timestamps"] - t0).dt.total_seconds()
67
68plt.plot(relative_time, meas_df["VCC_Current"])
69plt.xlabel('Time (s)')
70plt.ylabel('Current (A)')
71plt.show()
72# %% ==========================================================================
73# Save Dataframe
74# =============================================================================
75# Save DataFrame to HDF5 (better than CSV)
76meas_df.to_hdf("measurements.h5", key="data", mode="w")
77# key="data" is like a "dataset name" inside the HDF5 file
78# (you can store multiple DataFrames in one file with different keys).
79# mode="w" overwrites the file. Use mode="a" if you want to append new datasets.
80
81# Later: Load it back
82loaded_df = pd.read_hdf("measurements.h5", key="data")
83print(loaded_df.head())
84
85#or
86
87# Save DataFrame to CSV
88meas_df.to_csv("measurements.csv", index=False)
89
90# Load it back, auto-parsing the "Timestamps" column as datetime
91loaded_df = pd.read_csv("measurements.csv", parse_dates=["Timestamps"])
92print(loaded_df.head())
93
94# %% ==========================================================================
95# Close Instrument
96# =============================================================================
97myGPP4323.close()