Update U180-ToolKit.py

This commit is contained in:
xans 2024-07-21 14:50:18 +02:00
parent ad89540c37
commit 33a0cfddbc

View File

@ -3,30 +3,39 @@ import os
import json
import time
import csv
import matplotlib.dates as mdates
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import datetime
import numpy
import GPIBPrologix
import git
import shutil
import bme280
import smbus2
GPIB = GPIBPrologix.ResourceManager("/dev/ttyACM0")
instObj = GPIB.open_resource(22)
instObj.write("BEEP")
instObj.write("CAL? 72")
print(instObj.read())
print(instObj.query("CAL? 72"))
time.sleep(60)
bus = smbus2.SMBus(1)
calibration_params = bme280.load_calibration_params(bus, 0x76)
## user customisable functions
def readFunc(instObj):
return instObj.read()
def writeFunc(instObj, input):
instObj.write(input)
time.sleep(1)
def queryFunc(instObj, input):
time.sleep(0.1)
return instObj.query(input)
output = instObj.query(input)
time.sleep(1)
print(input, output)
return output
def getEnvironment(instObj, i2cbus):
value = instObj.sample(i2cbus, 0x76, calibration_params)
return [str(round(value.humidity,2)),str(round(value.temperature,2)),str(round(value.pressure,2))]
## the main program functionalities, should not be adjusted
def readRunningConfigs():
@ -36,17 +45,24 @@ def readRunningConfigs():
return filenames
def doACAL(dcACAL, ohmACAL, acACAL):
output = queryFunc(instObj, "TEMP?")
writeFunc(instObj,"END 2")
writeFunc(instObj,"OFORMAT ASCII")
writeFunc(instObj,"BEEP")
if dcACAL == "yes":
print("Starting DCV cal")
writeFunc(instObj, "ACAL DCV")
time.sleep(140+20)
time.sleep(140+60)
if acACAL == "yes":
writeFunc(instObj, "ACAL ACV")
time.sleep(140+20)
print("Starting AC cal")
writeFunc(instObj, "ACAL AC")
time.sleep(240+60)
if ohmACAL == "yes":
print("Starting Ohm cal")
writeFunc(instObj, "ACAL OHMS")
time.sleep(600+60)
return output
time.sleep(720+60)
writeFunc(instObj, "DISP OFF, ;")
writeFunc(instObj,"DISP MSG,\" \"")
return 0
def interrogate3458A(instObj):
output = []
@ -66,6 +82,7 @@ def interrogate3458A(instObj):
output.append(queryFunc(instObj,"CAL? 59")) # get temperature from during calibration
output.append(queryFunc(instObj,"CAL? 97")) # get 1mamp ACAL gain constrant
output.append(queryFunc(instObj,"CAL? 72")) # get 10v ACAL gain constrant
print(output)
return output
## main program
@ -78,13 +95,19 @@ for file in readRunningConfigs():
if not os.path.exists(serialPath):
os.makedirs(serialPath)
with open(serialPath+configData['serial']+'.csv', 'a') as g:
heading = ["DateTime","TEMP","CAL RREF","CAL VREF","G10K","G100K", "G1V0","G0V1","OCOMP1K","OCOMP10K", "ACALTEMP", "CALTEMP", "G1mA", "G10V"]
if(configData['useBME']):
heading = output.append(['EnvHumidity', 'EnvTemp', 'EnvPressure'])
writer = csv.writer(g)
writer.writerow(["DateTime","TEMP","CAL RREF","CAL VREF","G10K","G100K", "G1V0","G0V1","OCOMP1K","OCOMP10K", "ACALTEMP", "CALTEMP", "G1mA", "G10V"])
writer.writerow(heading)
doACAL(configData['ACAL-DCV'],configData['ACAL-OHMS'],configData['ACAL-ACV'])
with open(serialPath+configData['serial']+'.csv', 'a') as f:
output = interrogate3458A(instObj)
if(configData['useBME']):
data = getEnvironment(bme280, bus)
output += data
writer = csv.writer(f)
writer.writerow((interrogate3458A(instObj)))
writeFunc(instObj,"DISP OFF,''")
writer.writerow(output)
## do the plotting
for file in readRunningConfigs():
@ -92,14 +115,54 @@ for file in readRunningConfigs():
with open(dataPath, "r") as read_file:
configData = json.load(read_file)
serialPath = os.path.realpath(os.path.dirname(__file__))+'/data/'+configData['serial']+'/'
df = pd.read_csv(serialPath+configData['serial']+'.csv')
df = pd.read_csv(serialPath+configData['serial']+'.csv',delimiter=',', encoding="utf-8-sig")
df.columns = df.columns.str.strip()
time_x = pd.to_datetime(df['DateTime'],format='%d-%m-%y %H:%M:%S')
coefficients = numpy.polyfit(df['TEMP'], df['G10V'], 1, rcond=None, full=False, w=None, cov=False)
polynomial = numpy.poly1d(coefficients)
#polynom_estimate = numpy.polyfit(mdates.date2num(time_x),df['G10V']-polynomial(df['TEMP']), 1, rcond=None, full=False, w=None, cov=False)
polynom_estimate = numpy.polyfit(mdates.date2num(time_x),df['G10V'], 1, rcond=None, full=False, w=None, cov=False)
p_est = numpy.poly1d(polynom_estimate)
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(x=time_x, y=df['G10V']-polynomial(df['TEMP']),mode='lines+markers',name='time vs cal72 w tempcomp'),row=1, col=1)
#fig.add_trace(go.Scatter(x=time_x, y=df['G10V']-polynomial(df['TEMP']),mode='lines+markers',name='time vs cal72 w tempcomp'),row=1, col=1)
fig.add_trace(go.Scatter(x=time_x, y=df['G10V'],mode='lines+markers',name='time vs cal72 w tempcomp'),row=1, col=1)
fig.add_trace(go.Scatter(x=time_x, y=p_est(mdates.date2num(time_x)),mode='lines',line = dict(color='gray', dash='dash'),name='Estimated 1st order'), row=1, col=1)
fig.add_trace(go.Scatter(x=df['TEMP'], y=df['G10V'],mode='lines+markers',name='temp vs cal72'),row=1, col=2)
fig.add_trace(go.Scatter(x=df['TEMP'], y=polynomial(df['TEMP']),mode='lines',line = dict(color='gray', dash='dash'),name='Tempco 1st order'), row=1, col=2)
datenow = datetime.datetime.now()
date = datenow + datetime.timedelta(days=1)
timeframe = (time_x.max()-time_x.min()).total_seconds()/(3600*24)
alphaday = (p_est(mdates.date2num(date))-p_est(mdates.date2num(datenow)))
driftrate = round((alphaday*1000000000)/(df.loc[:, 'G10V'].mean()*timeframe),2)
annotation = f'Estimated drift: {driftrate} ppb/day'
fig.add_annotation(dict(showarrow=False, text=annotation, xanchor='left', xref="paper", yref="paper", x=0, y=0))
tempcorate = round(((polynomial(1)-polynomial(0))*1000000000)/df.loc[:, 'G10V'].mean(),2)
annotation = f'Estimated tempco: {tempcorate} ppb/degC'
fig.add_annotation(dict(showarrow=False, text=annotation, xanchor='left', xref="paper", yref="paper", x=1, y=0))
fig.write_html(serialPath+configData['serial']+'.html')
fig.write_image(serialPath+configData['serial']+'.png')
fig.write_image(serialPath+configData['serial']+'.png',width=1280, height=720)
## do the saving
for file in readRunningConfigs():
localDataPath = os.path.realpath(os.path.dirname(__file__))+'/data/'
gitDataRepo = os.path.realpath(os.path.dirname(__file__))+'/CommitDataRepo/'
with open(dataPath, "r") as read_file:
configData = json.load(read_file)
if(configData['useGit']):
remote = f"https://{configData['gitUser']}:{configData['gitPassword']}@{configData['gitAddress']}"
if os.path.exists(gitDataRepo):
shutil.rmtree(gitDataRepo, ignore_errors=True)
git.Repo.clone_from(remote, gitDataRepo, b=configData['gitBranch'])
shutil.copytree(localDataPath, gitDataRepo+'data/',dirs_exist_ok=True)
repo = git.Repo(gitDataRepo)
repo.git.add(gitDataRepo)
repo.index.commit("Update")
repo.remotes[0].push()
## if all ok, blamk screen to save VFD
queryFunc(instObj, 'DISP OFF,""')