ich habe ein etwas seltsames Problem. Ich möchte einen Kondensatorwert (c) anhand eines übergebenen Wertes (cload) berechnen. Der berechnete Wert soll mit einer Liste von Werten (self.basevalues) verglichen werden. Ausgewählt wird dann schließlich der Wert der am nähsten dranliegt.
Soviel zur Theorie. Wenn in ich dem Programm einen Wert cload von 19 übergebe, das Programm also wie folgt starte: "python programm.py -l 19", dann erhalte ich 30 pF als Wert den ich einsetzen soll und nicht 27 pF. Warum?
Liebe Grüße
Lisa
Code: Alles auswählen
#!/usr/bin/python
import argparse
import sys
class crystal():
def __init__(self, cload, cstray):
self.loadcap = cload # The crystal's load capacitance (see datasheet)
self.straycap = cstray # Stray capacitance on the PCB (approximately 5 pF)
self.basevalues = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8,
2.0, 2.2, 2.4, 2.7,
3.0, 3.3, 3.6, 3.9,
4.3, 4.7,
5.1, 5.6,
6.2, 6.8,
7.5,
8.2,
9.1]
def calc(self):
c = 2*(self.loadcap - self.straycap)
return c
def getFactor(self, c):
if (c/10000 > 1):
sys.exit('Not a realistic load capacitance.')
elif (c/1000 >= 1):
factor = 1000
elif (c/100 >= 1):
factor = 100
elif (c/10 >= 1):
factor = 10
else:
factor = 1
return factor
def fit(self, c):
print c
factor = self.getFactor(c)
cbase = c/factor
print 'cbase: %s' % cbase # DEDUG
i = 0
while 1:
print 'i: %s' % i # DEDUG
print 'cbase + i: %s' % (cbase + i) # DEDUG
print 'cbase - i: %s' % (cbase - i) # DEDUG
if (cbase + i) in self.basevalues:
return ((cbase + i) * factor)
if (cbase - i) in self.basevalues:
return ((cbase - i) * factor)
else:
i += 0.1
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--straycap', action='store',
help='Stray capacitance in pF, standard is 5 pF.')
parser.add_argument('-l', '--loadcap', action='store',
help='Load capacitance in pF.')
parser.add_argument('-d', '--different', action='store_true',
help='Use different caps for C1 and C2.')
results = parser.parse_args()
if results.straycap:
cstray = results.straycap
else:
cstray = 5.0
if not results.loadcap:
parser.print_help()
sys.exit()
if float(results.loadcap) <= cstray:
sys.exit('Error: Check the crystal\'s datasheet for the right load capacitance.')
mycrystal = crystal(float(results.loadcap), float(cstray))
ccalc = mycrystal.calc()
cfit = mycrystal.fit(ccalc)
print "Calculated capacitor value is %s pF, use %s pF." % (ccalc, cfit)
