181 lines
6.9 KiB
Python
181 lines
6.9 KiB
Python
import GPIBPrologix
|
|
import bme280
|
|
import smbus2
|
|
from requests import post
|
|
|
|
## Initialize GPIB adapter
|
|
GPIB = GPIBPrologix.ResourceManager("/dev/ttyACM0")
|
|
# Connect equipment
|
|
inst2 = GPIB.open_resource(30)
|
|
inst3 = GPIB.open_resource(22)
|
|
# Initialize BME280 temperature/humidity sensor
|
|
bus = smbus2.SMBus(1)
|
|
## Configure equipment
|
|
# BME280 temperature/humidity sensor
|
|
calibration_params = bme280.load_calibration_params(bus, 0x76)
|
|
|
|
# Datron 4805 Calibrator
|
|
inst2.query("F0=") #DCV
|
|
inst2.query("R6=") #10/100K Range
|
|
inst2.query("S1=") #RemoteSense
|
|
inst2.query("M0.0069=") #6.9mV Out, sanity check
|
|
inst2.query("O1=") #OutputON
|
|
#Setup 34401A
|
|
inst3.write("*RST")
|
|
inst3.write("SYST:PRES")
|
|
inst3.write("*CLS")
|
|
inst3.write("CONF:VOLT:DC 10")
|
|
inst3.write("VOLT:DC:NPLC 100")
|
|
inst3.write("ZERO:AUTO ON")
|
|
inst3.write("INP:IMP:AUTO ON")
|
|
inst3.query("READ?")
|
|
# Measurement functions
|
|
def readValue(instObj):
|
|
instObj.query("READ?")
|
|
value = ""
|
|
while value == "":
|
|
value = inst3.read()
|
|
print(value)
|
|
return value
|
|
def setValue(instObj, inputVar):
|
|
instObj.query("M"+str(inputVar)+"=")
|
|
return inputVar
|
|
def getEnvironment(instObj, i2cbus):
|
|
value = instObj.sample(i2cbus, 0x76, calibration_params)
|
|
return value
|
|
|
|
## Have gitpython pull in the repository
|
|
import os
|
|
import git
|
|
import shutil
|
|
username = input("Your 0xC6 git username: ")
|
|
password = input("Your 0xC6 git password: ")
|
|
remote = f"https://{username}:{password}@git.0xc6.com/xans/INL-Measurements.git"
|
|
if not os.path.exists(os.getcwd()+"/INL-Measurements"):
|
|
git.Repo.clone_from(remote, os.getcwd()+"/INL-Measurements/")
|
|
else:
|
|
shutil.rmtree(os.getcwd()+"/INL-Measurements/", ignore_errors=True)
|
|
git.Repo.clone_from(remote, os.getcwd()+"/INL-Measurements/")
|
|
|
|
## Get needed info to properly label everything
|
|
MeterManufacturer = input("Meter Manufacturer: ").lower()
|
|
MeterModel = input("Meter Model: ").upper()
|
|
MeterSN = input("Meter SN: ").upper()
|
|
SourceManufacturer = input("Source Manufacturer: ").lower()
|
|
SourceModel = input("Source Model: ").upper()
|
|
SourceSN = input("Source SN: ").upper()
|
|
measid = input("Personally assigned measurement ID: ").upper()
|
|
shellyIp = input("If you use shelly for protection, enter the IP: ").upper()
|
|
|
|
if shellyIp and os.system("ping -c 1 " + shellyIp):
|
|
print("shelly not found")
|
|
quit()
|
|
if shellyIp:
|
|
url = "{}://{}:{}/relay/0?turn=on".format("http", "192.168.0.101", "80")
|
|
post(url)
|
|
|
|
## Create proper directory structure
|
|
basePath = os.getcwd() + "/INL-Measurements/"
|
|
dutname = MeterManufacturer+'-'+MeterModel+'-'+MeterSN
|
|
if not os.path.exists(basePath+dutname):
|
|
os.makedirs(basePath+dutname)
|
|
sourcename = SourceManufacturer+'-'+SourceModel+'-'+SourceSN
|
|
if not os.path.exists(basePath+dutname+'/'+sourcename):
|
|
os.makedirs(basePath+dutname+'/'+sourcename)
|
|
if not os.path.exists(basePath+dutname+'/'+sourcename+"/data/"):
|
|
os.makedirs(basePath+dutname+'/'+sourcename+"/data/")
|
|
basePath = basePath+dutname+'/'+sourcename
|
|
print(basePath+"/data/")
|
|
|
|
import csv
|
|
import time
|
|
import datetime
|
|
import numpy as np
|
|
import pandas as pd
|
|
from math import sin
|
|
## Create logfile
|
|
logFileName = MeterManufacturer+'-'+MeterModel+'-'+MeterSN+'---'+SourceManufacturer+'-'+SourceModel+'-'+SourceSN+'---'+measid+'.csv'
|
|
with open(basePath+"/data/"+logFileName, 'a') as f:
|
|
writer = csv.writer(f)
|
|
writer.writerow(["DateTime","Set Volts","Measured Volts","Env Pressure","Env Temperature", "Env Humidity"])
|
|
print(basePath+"/data/"+logFileName)
|
|
|
|
sweep = np.arange(-11.5,12,0.5)
|
|
|
|
cntr = 0
|
|
d = datetime.datetime.now()
|
|
dlast = datetime.datetime.now()
|
|
## Collect data
|
|
time.sleep(3)
|
|
for x in sweep:
|
|
cntr = cntr + 1
|
|
try:
|
|
#Set volt and let accimatize
|
|
setPoint = setValue(inst2,round(x,5))
|
|
timebetween = (d-dlast) * (len(sweep)-cntr)
|
|
dlast = d
|
|
print(str(cntr)+'/'+str(len(sweep))+' Estimated Time Left: '+str(timebetween))
|
|
time.sleep(60)
|
|
for i in range(3):
|
|
try:
|
|
time.sleep(5)
|
|
#Get DUT value
|
|
readout = readValue(inst3)
|
|
#Get envirnmental values
|
|
data = getEnvironment(bme280, bus)
|
|
#Write to file
|
|
d = datetime.datetime.now()
|
|
dx = d - datetime.timedelta(microseconds=d.microsecond)
|
|
fields=[dx.strftime("%d-%m-%y %H:%M:%S"),setPoint,float(readout),round(data.humidity,2),round(data.temperature,2),round(data.pressure,2)]
|
|
print(fields)
|
|
with open(basePath+"/data/"+logFileName, 'a') as f:
|
|
writer = csv.writer(f)
|
|
writer.writerow(fields)
|
|
if shellyIp and float(data.temperature) > 30 or float(data.temperature) < 16:
|
|
url = "{}://{}:{}/relay/0?turn=off".format("http", "192.168.0.101", "80")
|
|
post(url)
|
|
except Exception as e:
|
|
time.sleep(15)
|
|
i = i-1
|
|
except Exception as e:
|
|
print(e)
|
|
time.sleep(1)
|
|
|
|
## Calculate high order polynomial
|
|
df = pd.read_csv(basePath+"/data/"+logFileName)
|
|
df = df.groupby(["Set Volts"], as_index=False).mean(numeric_only=True)
|
|
highOrderPolyFit = np.polyfit(df["Set Volts"], df["Set Volts"]-df["Measured Volts"], 12)
|
|
trendPoly = np.poly1d(highOrderPolyFit)
|
|
## Make plot and save
|
|
import sys
|
|
from matplotlib import pyplot as plt
|
|
plt.plot(df["Set Volts"], df["Set Volts"]-df["Measured Volts"])
|
|
polyIndex = np.arange(-11,11,0.01)
|
|
plt.plot(polyIndex, trendPoly(polyIndex))
|
|
plt.xlabel('Source Voltage')
|
|
plt.ylabel('Delta Set/Meas')
|
|
plt.title ('INLV sweep '+ logFileName[:-4])
|
|
plt.savefig(basePath+"/data/"+logFileName[:-4]+".jpg", bbox_inches='tight')
|
|
|
|
minVal = trendPoly(polyIndex)
|
|
maxVal = trendPoly(polyIndex)
|
|
minVal = min(minVal)
|
|
maxVal = min(maxVal)
|
|
## Write away values to result overview
|
|
if not os.path.exists(basePath+'/README.md'):
|
|
with open(basePath+'/README.md', 'a') as f:
|
|
f.write("| Meter Manufacturer | Meter Model | Meter SerialNumber | Source Manufacturer | Source Model | Source SerialNumber | MinMax | File | ID | User |\n")
|
|
f.write("|--------------------|-------------|--------------------|---------------------|--------------|---------------------|--------|------|----|------|\n")
|
|
f.close()
|
|
with open(basePath+'/README.md', 'a') as f:
|
|
linkToImg = "/data/"+logFileName[:-4]+".jpg"
|
|
print("|"+MeterManufacturer+"|"+MeterModel+"|"+MeterSN+"|"+SourceManufacturer+"|"+SourceModel+"|"+SourceSN+"|"+str(round((maxVal-minVal),10))+"|"+linkToImg+"|"+measid+"|"+username+"|\n")
|
|
f.write("|"+MeterManufacturer+"|"+MeterModel+"|"+MeterSN+"|"+SourceManufacturer+"|"+SourceModel+"|"+SourceSN+"|"+str(round((maxVal-minVal),10))+"|"+linkToImg+"|"+measid+"|"+username+"|\n")
|
|
f.close()
|
|
shutil.copy(os.getcwd()+'/'+sys.argv[0],basePath+"/data/")
|
|
repo = git.Repo(os.getcwd() + "/INL-Measurements/")
|
|
repo.git.add(basePath)
|
|
repo.index.commit("Add INL Sweep "+ logFileName)
|
|
repo.remotes[0].push()
|
|
|