-
-
Save tspspi/359b4a25e945e9eafdd4d8f70ec841b8 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt | |
import math | |
from pyka3005p.ka3005pserial import KA3005PSerial | |
from time import sleep | |
from labdevices import powersupply | |
if __name__ == "__main__": | |
cfg = { | |
'portfile' : '/dev/ttyU0', | |
'repeat' : 5, | |
'stepsize' : 0.01, | |
'maxcurrent' : 2, | |
'debug' : True, | |
'plottitle' : 'Voltage - current', | |
'legendlabel' : '1N4001', | |
'errorbars' : True, | |
'dumpdata' : True | |
} | |
# ToDo: Implement command line processing and setting of parameters ... | |
with KA3005PSerial(cfg['portfile'], cfg['debug']) as psu: | |
nRuns = cfg['repeat'] | |
setVolts = [] | |
measVolts = [] | |
measAmps = [] | |
while nRuns > 0: | |
setVolts.append([]) | |
measVolts.append([]) | |
measAmps.append([]) | |
vCurrent = 0 | |
vStep = 0.01 | |
psu.setVoltage(0.0) | |
psu.setCurrent(cfg['maxcurrent']) | |
psu.setChannelEnable(True) | |
while ((psu.getLimitMode() != powersupply.PowerSupplyLimit.CURRENT) and (len(measVolts) == 1)) or ((len(measVolts) > 1) and (len(measVolts[len(measVolts)-1]) != len(measVolts[0]))): | |
psu.setVoltage(vCurrent) | |
vReal, vSet = psu.getVoltage() | |
aReal, aSet = psu.getCurrent() | |
measVolts[len(measVolts)-1].append(vReal) | |
measAmps[len(measVolts)-1].append(aReal) | |
setVolts[len(measVolts)-1].append(vSet) | |
vCurrent = vCurrent + vStep | |
psu.setChannelEnable(False) | |
sleep(1) | |
nRuns = nRuns - 1 | |
# Do statistics | |
setVolts = setVolts[0] | |
measVoltsMean = [] | |
measAmpsMean = [] | |
measVoltsDev = [] | |
measAmpsDev = [] | |
for iSetpoint in range(len(measVolts[0])): | |
vAvg = 0 | |
aAvg = 0 | |
for iMeasurement in range(len(measVolts)): | |
vAvg = vAvg + measVolts[iMeasurement][iSetpoint] / len(measVolts) | |
aAvg = aAvg + measAmps[iMeasurement][iSetpoint] / len(measVolts) | |
measVoltsMean.append(vAvg) | |
measAmpsMean.append(aAvg) | |
vDev = 0 | |
aDev = 0 | |
for iMeasurement in range(len(measVolts)): | |
vDev = vDev + (measVolts[iMeasurement][iSetpoint] - vAvg)*(measVolts[iMeasurement][iSetpoint] - vAvg) / len(measVolts) | |
aDev = aDev + (measAmps[iMeasurement][iSetpoint] - aAvg)*(measAmps[iMeasurement][iSetpoint] - aAvg) / len(measVolts) | |
vDev = math.sqrt(vDev) | |
aDev = math.sqrt(aDev) | |
measVoltsDev.append(vDev) | |
measAmpsDev.append(aDev) | |
fig, ax = plt.subplots() | |
if (cfg['repeat'] > 1) and cfg['errorbars']: | |
ax.errorbar(setVolts, measAmpsMean, yerr=measAmpsDev, label=cfg['legendlabel']) | |
else: | |
ax.plot(setVolts, measAmpsMean, label=cfg['legendlabel']) | |
ax.set_xlabel("Set voltage [V]") | |
ax.set_ylabel("Measured current [A]") | |
ax.set_title(cfg['plottitle']) | |
ax.legend() | |
plt.show() | |
if cfg['dumpdata']: | |
print("# Set voltage, Average measured voltage, Average measured current, Standard deviation voltage, Standard deviation current") | |
for i in range(len(setVolts)): | |
print("{} {} {} {} {}".format(setVolts[i], measVoltsMean[i], measAmpsMean[i], measVoltsDev[i], measAmpsDev[i])) |
Short question: How many samples can you query in 1second with this script?
I don't know this from the back of my head but since these powersupplies are pretty slow this usually doesn't matter anyways - we're using those supplies in a physics experiment to control coil currents for small magnetic fields and we have to wait up into the 500ms to seconds range for them to really stabilize the regulated current. In addition by default I have a 100ms delay after all serial commands in the library by default (one can change that but I have never tried how low) & run 4 commands each loop iteration so this won't be (much) faster than 2 points per second the way it's written in this gist. What are you trying to achieve?
What I also can say for sure: Those power supplies are no realtime devices so going much faster won't be possible anyways - when you try to get into the 100'th of milliseconds range you really see jitter while scanning currents for example (this is why I'm asking what you're trying to achieve / expecting)
Short question: How many samples can you query in 1second with this script?