Skip to content

Instantly share code, notes, and snippets.

@pklaus
Last active September 9, 2019 14:10
Show Gist options
  • Save pklaus/dd5c921878c412af1e3dfa32f4dc7bb8 to your computer and use it in GitHub Desktop.
Save pklaus/dd5c921878c412af1e3dfa32f4dc7bb8 to your computer and use it in GitHub Desktop.
Balzers PKG 020 Vacuum Gauge Controller : Analog Out
#!/usr/bin/env python
tables = {
'ikr': [
[0.09, 5.00E-008],
[0.24, 1.00E-007],
[0.34, 1.50E-007],
[0.45, 2.00E-007],
[0.67, 3.00E-007],
[0.86, 4.00E-007],
[1.05, 5.00E-007],
[1.25, 6.00E-007],
[1.44, 7.00E-007],
[1.63, 8.00E-007],
[1.82, 9.00E-007],
[2.01, 1.00E-006],
[2.61, 1.50E-006],
[2.74, 2.00E-006],
[3.01, 3.00E-006],
[3.22, 4.00E-006],
[3.44, 5.00E-006],
[3.67, 6.00E-006],
[3.87, 7.00E-006],
[4.09, 8.00E-006],
[4.31, 9.00E-006],
[4.53, 1.00E-005],
[5.07, 1.50E-005],
[5.26, 2.00E-005],
[5.64, 3.00E-005],
[5.92, 4.00E-005],
[6.23, 5.00E-005],
[6.45, 6.00E-005],
[6.65, 7.00E-005],
[6.86, 8.00E-005],
[7.03, 9.00E-005],
[7.21, 1.00E-004],
[7.72, 1.50E-004],
[8.11, 2.00E-004],
[8.51, 3.00E-004],
[8.72, 4.00E-004],
[8.95, 5.00E-004],
[9.17, 6.00E-004],
[9.29, 8.00E-004],
[9.40, 1.00E-003],
[9.52, 1.50E-003],
[9.61, 2.00E-003],
[9.75, 3.00E-003],
[9.81, 4.00E-003],
[9.88, 5.00E-003]
],
'tpr2': [
[ 0.00, 1E-4],
[ 0.10, 2E-4],
[ 0.22, 1.5E-3],
[ 0.30, 2E-3],
[ 0.44, 3E-3],
[ 0.60, 4E-3],
[ 0.75, 5E-3],
[ 0.90, 6E-3],
[ 1.04, 7E-3],
[ 1.19, 8E-3],
[ 1.36, 9E-3],
[ 1.52, 1E-2],
[ 1.94, 1.5E-2],
[ 2.09, 2E-2],
[ 2.38, 3E-2],
[ 2.58, 4E-2],
[ 2.72, 5E-2],
[ 2.97, 6E-2],
[ 3.12, 7E-2],
[ 3.29, 8E-2],
[ 3.44, 9E-2],
[ 3.60, 1E-1],
[ 3.96, 1.5E-1],
[ 4.14, 2E-1],
[ 4.50, 3E-1],
[ 4.72, 4E-1],
[ 4.96, 5E-1],
[ 5.17, 6E-1],
[ 5.32, 7E-1],
[ 5.50, 8E-1],
[ 5.62, 9E-1],
[ 5.72, 1],
[ 5.96, 1.5],
[ 6.26, 2],
[ 6.58, 3],
[ 6.74, 4],
[ 6.92, 5],
[ 7.07, 6],
[ 7.21, 7],
[ 7.34, 8],
[ 7.51, 9],
[ 7.68, 10],
[ 8.25, 15],
[ 8.58, 20],
[ 8.99, 30],
[ 9.20, 40],
[ 9.37, 50],
[ 9.49, 60],
[ 9.56, 70],
[ 9.63, 80],
[ 9.70, 100],
[10.00, 1000]
]
}
#!/usr/bin/env python
def interpolate_numpy(value, value_table, exp=True):
"""
Convert from voltage to pressure using value_table.
Doing a vectorized conversion if value is an array of values.
Uses the interpolation function from numpy.
If exp = True the interpolation will be done on exponentiated
voltages (slightly more precise and correct).
"""
import numpy as np
#return np.interp(value, [row[0] for row in value_table], [row[1] for row in value_table])
value_table = np.array(value_table)
xp = value_table[:,0]
fp = value_table[:,1]
if exp:
value = np.exp(value)
xp = np.exp(xp)
return np.interp(value, xp, fp)
def interpolate_naive(value, value_table):
"""
Interpolate a function using a value_table.
In between the reference data points,
a simple linear interpolation is done.
"""
# don't extrapolate, return edge value if out of lookup range
if value >= value_table[-1][0]:
return value_table[-1][1]
if value <= value_table[0][0]:
return value_table[0][1]
# inside range - interpolate
for voltage, pressure in value_table:
if value > voltage:
lower_voltage, lower_pressure = voltage, pressure
continue
else:
dvoltage = voltage - lower_voltage
dpressure = pressure - lower_pressure
return lower_pressure + (value - lower_voltage) * dpressure/dvoltage
def interpolate_log_aware(value, value_table):
"""
Interpolate a value from a logarithmized value
using a lookup table.
"""
import math
# don't extrapolate, return edge value if out of lookup range
if value >= value_table[-1][0]:
return value_table[-1][1]
if value <= value_table[0][0]:
return value_table[0][1]
# inside range - interpolate
for voltage, pressure in value_table:
if value > voltage:
lower_voltage, lower_pressure = voltage, pressure
continue
else:
dvoltage = voltage - lower_voltage
log_pressure2 = math.log(pressure)
log_pressure1 = math.log(lower_pressure)
dlogpressure = log_pressure2 - log_pressure1
interp_log_pressure = log_pressure1 + (value - lower_voltage) * dlogpressure/dvoltage
return math.exp(interp_log_pressure)
# default interpolate function: The numpy version:
interpolate = interpolate_numpy
def main():
from balzerspkg020 import tables
import sys
while True:
line = sys.stdin.readline()
if not line: break
try:
num = float(line)
except:
sys.stderr.write("Couln't interpret this as a number: " + line)
continue
tpr2 = interpolate(num, tables['tpr2'])
ikr = interpolate(num, tables['ikr'])
print("As TPR2 value: %s" % tpr2)
print("As IKR value: %s" % ikr)
if __name__ == "__main__": main()
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/env python
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
from balzerspkg020_helpers import interpolate, interpolate_naive, interpolate_numpy, interpolate_log_aware
from balzerspkg020 import tables
x = np.linspace(0,10, 1000000, endpoint=True)
vect_ikr = np.vectorize(lambda x: interpolate_log_aware(x, tables['ikr']))
vect_tpr2 = np.vectorize(lambda x: interpolate_log_aware(x, tables['tpr2']))
#vect_ikr = np.vectorize(lambda x: interpolate_naive(x, tables['ikr']))
#vect_tpr2 = np.vectorize(lambda x: interpolate_naive(x, tables['tpr2']))
#vect_ikr = lambda x: interpolate_numpy(x, tables['ikr'])
#vect_tpr2 = lambda x: interpolate_numpy(x, tables['tpr2'])
y_ikr = vect_ikr(x)
y_tpr2 = vect_tpr2(x)
plt.plot(x, y_ikr, label='IKR')
plt.plot(x, y_tpr2, label='TPR2')
plt.yscale('log')
plt.title('Balzers PKG 020 Vacuum Gauge Controller : Analog Out')
plt.ylabel('Pressure [mbar]')
plt.xlabel('Analog Output Voltage [V]')
plt.legend(loc='upper left')
# Add a minor/major log grid to the plot
ax = plt.gca()
locmaj = mpl.ticker.LogLocator(base=10.0, subs=(1.0, ), numticks=100)
ax.yaxis.set_major_locator(locmaj)
locmin = mpl.ticker.LogLocator(base=10.0,subs=(0.2, 0.4, 0.6, 0.8),numticks=100)
ax.yaxis.set_minor_locator(locmin)
ax.yaxis.set_minor_formatter(mpl.ticker.NullFormatter())
plt.grid(which='major', linestyle='-', linewidth='0.5', color='black', alpha=0.4)
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='gray', alpha=0.2)
# done with grid
plt.savefig('conversion_curve.pdf')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment