from importlib import reload
import sys
from textwrap import dedent
from IPython.display import display_markdown
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Reload already loaded modules
for m in ("AD5522_pi", "AD7685_pi", "AD5522dev_pi"):
try:
mod = sys.modules[m]
except KeyError:
pass
else:
reload(mod)
from AD5522dev_pi import AD5522dev
from AD5522_pi import MeasMode
board = AD5522dev(vref=5.0)
currentranges = ("5µA", "20µA", "200µA", "2mA", "extres")
There are three metrics to calibrate. The force offset, the force gain and the measurement offset. The calibration is to be done in the following order:
For some calibration the channels have to be open, for other the channels have to be shorted:
So the following procedure can be used to calibrate all these metrics:
Open all the channels to run the voltage gain calibration.
voltagegain_out = tuple(
board.calibrate_voltagegain(channel=channel) for channel in range(len(board.channels))
)
s = """
| Channel | Gain error [V/V] | Offset [V] |
|----------|------------------|------------|
"""
for i, chout in enumerate(voltagegain_out):
s += "| {} | {} | {} |\n".format(i, round(chout[0], 5), round(chout[1], 4))
display_markdown(s, raw=True)
Short the channels to run the volage offset calibration.
voltageoffset_out = tuple(board.calibrate_voltageoffset(channel=channel) for channel in range(len(board.channels)))
s = """
| Channel | Offset [lsbV] | Verif. |
|---------|---------------|--------|
"""
for i, chout in enumerate(voltageoffset_out):
s += "| {} | {} | {} |\n".format(i, chout[0], "OK" if chout[1] else "**NOK**")
display_markdown(s, raw=True)
Keep the channels shorted to run the current gain calibration.
currentgain_out = tuple(
tuple(
(currentrange, *board.calibrate_currentgain(channel=channel, currentrange=currentrange))
for currentrange in currentranges
)
for channel in range(len(board.channels))
)
s = ""
for i, chout in enumerate(currentgain_out):
s += dedent("""
### Channel {}
| Range | Gain error [A/A] | Offset [µA] |
|-------|------------------|-------------|
""".format(i))
for irange, gain, offset in chout:
s += "| {} | {} | {} |\n".format(irange, round(gain, 5), round(1e6*offset, 4))
display_markdown(s, raw=True)
Open all the channels before running the current offset calibration procedure.
currentoffset_out = tuple(
board.calibrate_currentoffset(channel=channel)
for channel in range(len(board.channels))
)
ranges = tuple(rangeverif[0] for rangeverif in currentoffset_out[0][1])
s = "| Channel | Offset [lsbA] | " + " | ".join(range for range in ranges) + " |\n"
s += "|" + (len(ranges) + 2)*"-|" + "\n"
for i, (offset, verif) in enumerate(currentoffset_out):
s += (
"| {} | {} | ".format(i, offset)
+ " | ".join("OK" if v[1] else "**NOK**" for v in verif)
+ " |\n"
)
display_markdown(s, raw=True)
Keep all the channels open for running the voltage measurment offset calibration procedure.
voltagemeasoffset_out = tuple(
{
currentrange: board.calibrate_measoffset_fv(channel=channel, currentrange=currentrange)
for currentrange in currentranges
}
for channel in range(len(board.channels))
)
s = "**Voltage offset**\n\n"
s += "| Channel | " + " | ".join(currentranges) + " |\n"
s += "|" + (len(currentranges) + 1)*"-|" + "\n"
for i, offsets in enumerate(voltagemeasoffset_out):
s += "| {} | {} |\n".format(
i, " | ".join(str(offsets[currentrange][0]) for currentrange in currentranges),
)
s += "\n**Current offset**\n\n"
s += "| Channel | " + " | ".join(currentranges) + " |\n"
s += "|" + (len(currentranges) + 1)*"-|" + "\n"
for i, offsets in enumerate(voltagemeasoffset_out):
s += "| {} | {} |\n".format(
i, " | ".join(str(offsets[currentrange][1]) for currentrange in currentranges),
)
display_markdown(s, raw=True)
Finally close all the channels to run the current measurement offset calibration procedure.
currentmeasoffset_out = tuple(
{
currentrange: board.calibrate_measoffset_fi(channel=channel, currentrange=currentrange)
for currentrange in currentranges
}
for channel in range(len(board.channels))
)
s = "**Voltage offset**\n\n"
s += "| Channel | " + " | ".join(currentranges) + " |\n"
s += "|" + (len(currentranges) + 1)*"-|" + "\n"
for i, offsets in enumerate(currentmeasoffset_out):
s += "| {} | ".format(i) + " | ".join(
str(offsets[currentrange][0]) for currentrange in currentranges
) + " |\n"
s += "\n**Current offset**\n\n"
s += "| Channel | " + " | ".join(currentranges) + " |\n"
s += "|" + (len(currentranges) + 1)*"-|" + "\n"
for i, offsets in enumerate(currentmeasoffset_out):
s += "| {} | ".format(i) + " | ".join(
"{:.4e}".format(offsets[currentrange][1]) for currentrange in currentranges
) + " |\n"
display_markdown(s, raw=True)
# print(board.calibrate_currentoffset(channel=0, debug=True))
# print(board.calibrate_voltageoffset(channel=1, debug=True))
# print(board.calibrate_voltagegain(channel=1, debug=True))
# print(board.calibrate_currentgain(channel=1, currentrange="20µA", debug=True))
# for currentrange in ("5µA", "extres"):
# print(currentrange)
# print(board.calibrate_measoffset_fv(channel=0, currentrange=currentrange, debug=True))
# for currentrange in ("5µA", "2mA", "extres"):
# print(currentrange)
# print(board.calibrate_measoffset_fi(channel=0, currentrange="5µA", debug=True))