MAX4172 Learning Notes - Part 6 (Calibration programs)
MatPlotLib programming
# diodeplot20i.py tlfong01 2020dec24hkt1136
import matplotlib
import matplotlib.pyplot as plt
masterDiodeDict01 = {
'Title' : 'Diode Dict',
'Version' : 'v0.8',
'Date' : '2020dec24hkt1136',
'Author' : 'tlfong01',
'Description' : 'Inputs a diode Dict and plots the I-V curve',
'Measurements' :
{ '2BS4B-01' : { 'DiodeName' : '2SB4B',
'SampleNum' : '1',
'I2cBusName' : 'I2cBus0',
'DvBaseAddrName' : '0x20',
'DataPoints' : { '0' : {'mV' : 0.10, 'mA': 0.02, 'Zone' : '1'},
'1' : {'mV' : 0.20, 'mA': 0.02, 'Zone' : '1'},
'2' : {'mV' : 0.30, 'mA': 0.03, 'Zone' : '1'},
'3' : {'mV' : 0.40, 'mA': 0.05, 'Zone' : '1'},
'4' : {'mV' : 0.50, 'mA': 0.06, 'Zone' : '1'},
'5' : {'mV' : 1.00, 'mA': 0.11, 'Zone' : '1'},
'6' : {'mV' : 2.00, 'mA': 0.24, 'Zone' : '1'},
'7' : {'mV' : 3.00, 'mA': 0.34, 'Zone' : '1'},
'8' : {'mV' : 4.00, 'mA': 0.46, 'Zone' : '1'},
'9' : {'mV' : 5.00, 'mA': 0.57, 'Zone' : '1'},
'10' : {'mV' : 6.00, 'mA': 0.80, 'Zone' : '1'},
'11' : {'mV' : 7.00, 'mA': 0.78, 'Zone' : '1'},
'12' : {'mV' : 9.00, 'mA': 1.01, 'Zone' : '1'},
'13' : {'mV' : 10.00, 'mA': 1.11, 'Zone' : '1'},
'14' : {'mV' : 11.00, 'mA': 1.21, 'Zone' : '1'},
'15' : {'mV' : 12.00, 'mA': 1.32, 'Zone' : '1'},
'16' : {'mV' : 13.00, 'mA': 1.41, 'Zone' : '1'},
'17' : {'mV' : 14.00, 'mA': 1.51, 'Zone' : '1'},
'18' : {'mV' : 15.00, 'mA': 1.60, 'Zone' : '1'},
'19' : {'mV' : 16.00, 'mA': 1.70, 'Zone' : '1'},
'20' : {'mV' : 194.00, 'mA': 1.88, 'Zone' : '2'},
'21' : {'mV' : 206.00, 'mA': 1.88, 'Zone' : '2'},
'22' : {'mV' : 221.00, 'mA': 1.88, 'Zone' : '2'},
'23' : {'mV' : 241.00, 'mA': 1.88, 'Zone' : '2'},
'24' : {'mV' : 250.00, 'mA': 1.88, 'Zone' : '2'},
'25' : {'mV' : 271.00, 'mA': 1.88, 'Zone' : '2'},
'26' : {'mV' : 486.00, 'mA': 1.88, 'Zone' : '3'},
'27' : {'mV' : 488.00, 'mA': 1.95, 'Zone' : '3'},
'28' : {'mV' : 490.00, 'mA': 2.06, 'Zone' : '3'},
'29' : {'mV' : 492.00, 'mA': 2.32, 'Zone' : '3'},
'30' : {'mV' : 495.00, 'mA': 2.32, 'Zone' : '3'},
'31' : {'mV' : 498.00, 'mA': 2.49, 'Zone' : '3'},
'32' : {'mV' : 500.00, 'mA': 2.63, 'Zone' : '3'},
},
},
}
}
masterCurrentSensorDict01 = {
'Title' : 'Current Sensor Dict',
'Version' : 'v0.10',
'Date' : '2020dec24hkt1059',
'Author' : 'tlfong01',
'Description' : 'Inputs a current sense Dict and plots the V-I curve',
'Measurements' :
{ 'MAX4172-01' : { 'CurrentSensorName' : 'MAX4172',
'SampleNum' : '1',
'I2cBusName' : 'N/A',
'DvBaseAddrName' : 'N/A',
'DataPoints' : { '0' : {'mA' : 0.00, 'V': 0.00},
'1' : {'mA' : 0.01, 'V': 0.06},
'2' : {'mA' : 0.10, 'V': 0.17},
'3' : {'mA' : 0.20, 'V': 0.29},
'4' : {'mA' : 0.29, 'V': 0.40},
'5' : {'mA' : 0.39, 'V': 0.52},
'6' : {'mA' : 0.49, 'V': 0.63},
'7' : {'mA' : 0.58, 'V': 0.75},
'8' : {'mA' : 0.68, 'V': 0.87},
'9' : {'mA' : 0.77, 'V': 0.98},
'10' : {'mA' : 0.86, 'V': 1.10},
'11' : {'mA' : 0.96, 'V': 1.21},
'12' : {'mA' : 1.05, 'V': 1.33},
'13' : {'mA' : 1.15, 'V': 1.44},
'14' : {'mA' : 1.24, 'V': 1.56},
'15' : {'mA' : 1.34, 'V': 1.67},
'16' : {'mA' : 1.43, 'V': 1.79},
'17' : {'mA' : 1.53, 'V': 1.90},
'18' : {'mA' : 1.62, 'V': 2.02},
'19' : {'mA' : 1.71, 'V': 2.13},
'20' : {'mA' : 1.81, 'V': 2.24},
'21' : {'mA' : 1.90, 'V': 2.35},
'22' : {'mA' : 1.99, 'V': 2.47},
'23' : {'mA' : 2.09, 'V': 2.58},
'24' : {'mA' : 2.18, 'V': 2.65},
'25' : {'mA' : 2.27, 'V': 2.68},
'26' : {'mA' : 2.36, 'V': 2.70},
'27' : {'mA' : 2.45, 'V': 2.71},
'28' : {'mA' : 2.54, 'V': 2.72},
'29' : {'mA' : 2.72, 'V': 2.73},
'30' : {'mA' : 2.90, 'V': 2.74},
'31' : {'mA' : 2.98, 'V': 2.75},
'32' : {'mA' : 3.06, 'V': 2.76},
'33' : {'mA' : 3.14, 'V': 2.76},
'34' : {'mA' : 3.29, 'V': 2.77},
'35' : {'mA' : 3.45, 'V': 2.78},
'36' : {'mA' : 3.60, 'V': 2.79},
'37' : {'mA' : 3.82, 'V': 2.80},
'38' : {'mA' : 3.97, 'V': 2.81},
'39' : {'mA' : 4.12, 'V': 2.82},
'40' : {'mA' : 4.27, 'V': 2.83},
'41' : {'mA' : 4.42, 'V': 2.84},
'42' : {'mA' : 4.50, 'V': 2.85},
'43' : {'mA' : 4.65, 'V': 2.86},
'44' : {'mA' : 4.75, 'V': 2.87},
'45' : {'mA' : 4.80, 'V': 2.88},
'46' : {'mA' : 4.87, 'V': 2.90},
'47' : {'mA' : 4.95, 'V': 2.92},
'48' : {'mA' : 5.02, 'V': 2.95},
'49' : {'mA' : 5.10, 'V': 2.98},
},
},
}
}
# *** Plotting Functions ***
def printMasterDiodeDict(masterDiodeDict, diodeName): # Just printing out Dict data for debugging - tlfong01 2020dec16hkt2040)
print('Begin printDiodeDict(), ...')
print(' Title = ', masterDiodeDict['Title'])
print(' Diode Name = ', masterDiodeDict['Measurements'][diodeName]['DiodeName'])
print(' I2cBusName = ', masterDiodeDict['Measurements'][diodeName]['I2cBusName'])
print(' DvBaseAddrName = ', masterDiodeDict['Measurements'][diodeName]['DvBaseAddrName'])
print(' DataPoints = Num mV mA')
for dataPointNum in range(len(masterDiodeDict['Measurements'][diodeName]['DataPoints'])):
print(' ', (str(dataPointNum)).rjust(3), \
('{:.2f}'.format(masterDiodeDict['Measurements'][diodeName]['DataPoints'][str(dataPointNum)]['mV'])).rjust(8), end = '')
print(('{:.2f}'.format(masterDiodeDict['Measurements'][diodeName]['DataPoints'][str(dataPointNum)]['mA'])).rjust(8), ' ')
print('End printDiodeDict().')
return
def printMasterCurrentSensorDict(masterCurrentSensorDict, currentSensorName):
print('Begin printCurrentSensorDict(), ...')
print(' Title = ', masterCurrentSensorDict['Title'])
print(' CurrentSensorName = ', masterCurrentSensorDict['Measurements'][currentSensorName]['CurrentSensorName'])
print(' I2cBusName = ', masterCurrentSensorDict['Measurements'][currentSensorName]['I2cBusName'])
print(' DvBaseAddrName = ', masterCurrentSensorDict['Measurements'][currentSensorName]['DvBaseAddrName'])
print(' DataPoints = Num mA V')
dictLength = len(masterCurrentSensorDict['Measurements'][currentSensorName]['DataPoints'])
for dataPointNum in range(dictLength):
print(' ', (str(dataPointNum)).rjust(3), \
('{:.2f}'.format(masterCurrentSensorDict['Measurements'][currentSensorName]['DataPoints'][str(dataPointNum)]['mA'])).rjust(8), end = '')
print(('{:.2f}'.format(masterCurrentSensorDict['Measurements'][currentSensorName]['DataPoints'][str(dataPointNum)]['V'])).rjust(8), ' ')
print('End printCurrentSensorDict().')
return
def getDiodeDict(masterDiodeDict, diodeName): # Just printing out Dict data for debugging - tlfong01 2020dec16hkt2040)
voltageList = []
currentList = []
dictLength = len(masterDiodeDict['Measurements'][diodeName]['DataPoints'])
for dataPointNum in range(dictLength):
voltageList.append(masterDiodeDict['Measurements'][diodeName]['DataPoints'][str(dataPointNum)]['mV'])
for dataPointNum in range(dictLength):
currentList.append(masterDiodeDict['Measurements'][diodeName]['DataPoints'][str(dataPointNum)]['mA'])
dict = { 'DiodeName': masterDiodeDict['Measurements'][diodeName]['DiodeName'],
'VoltageList' : voltageList,
'CurrentList' : currentList
}
return dict
def getCurrentSensorDict(masterCurrentSensorDict, currentSensorName):
voltageList = []
currentList = []
dictLength = len(masterCurrentSensorDict['Measurements'][currentSensorName]['DataPoints'])
for dataPointNum in range(dictLength):
voltageList.append(masterCurrentSensorDict['Measurements'][currentSensorName]['DataPoints'][str(dataPointNum)]['V'])
for dataPointNum in range(dictLength):
currentList.append(masterCurrentSensorDict['Measurements'][currentSensorName]['DataPoints'][str(dataPointNum)]['mA'])
dict = { 'CurrentSensorName': masterCurrentSensorDict['Measurements'][currentSensorName]['CurrentSensorName'],
'VoltageList' : voltageList,
'CurrentList' : currentList
}
return dict
def printDiodeXyLists(diodeDict):
print('')
print('Begin printDiodeXyLists(), ...')
diodeName = diodeDict['DiodeName']
voltageList = diodeDict['VoltageList']
currentList = diodeDict['CurrentList']
print('')
print('diodeName =', diodeName)
print('')
print('voltageList =', voltageList)
print('')
print('currentList =', currentList)
print('End printDiodeXyLists().')
return
def printCurrentSensorXyLists(currentSensorDict):
print('')
print('Begin printCurrentSensorXyLists(), ...')
currentSensorName = currentSensorDict['CurrentSensorName']
voltageList = currentSensorDict['VoltageList']
currentList = currentSensorDict['CurrentList']
print('')
print('currentSensorName =', currentSensorName)
print('')
print('voltageList =', voltageList)
print('')
print('currentList =', currentList)
print('End printCurrentSensorXyLists().')
return
def plotXlistYlist(xList, yList, xLabel, yLabel):
plt.plot(xList, yList)
plt.xlabel(xLabel)
plt.ylabel(yLabel)
plt.show()
def plotDiodeDict(diodeDict, startIndex, stopIndex):
xList = diodeDict['VoltageList'][startIndex: stopIndex]
yList = diodeDict['CurrentList'][startIndex: stopIndex]
xLabel = 'Tunnel Diode Voltage (mV)'
yLabel = 'Tunnel Diode Current (mA)'
plotXlistYlist(xList, yList, xLabel, yLabel)
return
def plotCurrentSensorDict(currentSensorDict, startIndex, stopIndex):
xList = currentSensorDict['VoltageList'][startIndex: stopIndex]
yList = currentSensorDict['CurrentList'][startIndex: stopIndex]
xLabel = 'CurrentSensor Voltage (V)'
yLabel = 'CurrentSensor Current (mA)'
plotXlistYlist(xList, yList, xLabel, yLabel)
return
def testTunnelDiode():
printMasterDiodeDict(masterDiodeDict01, '2BS4B-01')
diodeDict = getDiodeDict(masterDiodeDict01, '2BS4B-01')
printDiodeXyLists(diodeDict)
plotDiodeDict(diodeDict, startIndex = 0, stopIndex = 30)
return
def testCurrentSensor():
printMasterCurrentSensorDict(masterCurrentSensorDict01, 'MAX4172-01')
currentSensorDict = getCurrentSensorDict(masterCurrentSensorDict01, 'MAX4172-01' )
printCurrentSensorXyLists(currentSensorDict)
plotCurrentSensorDict(currentSensorDict, startIndex = 0, stopIndex = 15)
return
# *** Main Fnction ***
#testTunnelDiode()
testCurrentSensor()
# *** End of program ***
# Appendix B - Sample Output ***
# *** Begin sample output ***
'''
>>> %Run matplotlib20h.py
Begin printCurrentSensorDict(), ...
Title = Current Sensor Dict
CurrentSensorName = MAX4172
I2cBusName = N/A
DvBaseAddrName = N/A
DataPoints = Num mA V
0 0.00 0.00
1 0.01 0.06
2 0.10 0.17
3 0.20 0.29
4 0.29 0.40
5 0.39 0.52
6 0.49 0.63
7 0.58 0.75
8 0.68 0.87
9 0.77 0.98
10 0.86 1.10
11 0.96 1.21
12 1.05 1.33
13 1.15 1.44
14 1.24 1.56
15 1.34 1.67
16 1.43 1.79
17 1.53 1.90
18 1.62 2.02
19 1.71 2.13
20 1.81 2.24
21 1.90 2.35
22 1.99 2.47
23 2.09 2.58
24 2.18 2.65
25 2.27 2.68
26 2.36 2.70
27 2.45 2.71
28 2.54 2.72
29 2.72 2.73
30 2.90 2.74
31 2.98 2.75
32 3.06 2.76
33 3.14 2.76
34 3.29 2.77
35 3.45 2.78
36 3.60 2.79
37 3.82 2.80
38 3.97 2.81
39 4.12 2.82
40 4.27 2.83
41 4.42 2.84
42 4.50 2.85
43 4.65 2.86
44 4.75 2.87
45 4.80 2.88
46 4.87 2.90
47 4.95 2.92
48 5.02 2.95
49 5.10 2.98
End printCurrentSensorDict().
Begin printCurrentSensorXyLists(), ...
currentSensorName = MAX4172
voltageList = [0.0, 0.06, 0.17, 0.29, 0.4, 0.52, 0.63, 0.75, 0.87, 0.98, 1.1, 1.21, 1.33, 1.44, 1.56, 1.67, 1.79, 1.9, 2.02, 2.13, 2.24, 2.35, 2.47, 2.58, 2.65, 2.68, 2.7, 2.71, 2.72, 2.73, 2.74, 2.75, 2.76, 2.76, 2.77, 2.78, 2.79, 2.8, 2.81, 2.82, 2.83, 2.84, 2.85, 2.86, 2.87, 2.88, 2.9, 2.92, 2.95, 2.98]
currentList = [0.0, 0.01, 0.1, 0.2, 0.29, 0.39, 0.49, 0.58, 0.68, 0.77, 0.86, 0.96, 1.05, 1.15, 1.24, 1.34, 1.43, 1.53, 1.62, 1.71, 1.81, 1.9, 1.99, 2.09, 2.18, 2.27, 2.36, 2.45, 2.54, 2.72, 2.9, 2.98, 3.06, 3.14, 3.29, 3.45, 3.6, 3.82, 3.97, 4.12, 4.27, 4.42, 4.5, 4.65, 4.75, 4.8, 4.87, 4.95, 5.02, 5.1]
End printCurrentSensorXyLists().
Note: must close graph or save graph to /Documents to terminate python porgram
>>>
'''
# *** End sample output ***
# *** End of program, appendices, and sample output ***
.END
No comments:
Post a Comment