resistorswitchzeug.py


1
#!/usr/bin/python
2
from e_series import e_series
3
4
#http://www.mikrocontroller.net/topic/331314
5
6
#Thevenin-Aequiv. zu Spannungsteiler aus R1=R6=10k mit R3=R4=0
7
Vx=2.5
8
Rx=5000.0
9
10
#list with the differences of consecutive list elements
11
def listdiff(t):
12
    return [j-i for i, j in zip(t[:-1], t[1:])]
13
14
#value of two parallel resistors
15
def par(a, b):
16
    return (a * b) / (a + b)
17
18
#factor of voltage divider
19
def vdiv(top, bottom):
20
    return bottom / (top + bottom)
21
22
# Lin. ueberlagerung der Spannungen von Schaltern und Spgsteiler
23
# fuer einen Schalterzustand
24
# ra = R2, rb = R5
25
def voltage(sa, sb, ra, rb):
26
    if sa == None and sb == None:
27
        return Vx
28
    if sa == None:
29
        return Vx * vdiv(Rx, rb) + sb * vdiv(rb, Rx)
30
    if sb == None:
31
        return Vx * vdiv(Rx, ra) + sa * vdiv(ra, Rx)
32
    return Vx * vdiv(Rx, par(ra, rb)) + sa * vdiv(ra, par(rb, Rx)) + sb * vdiv(rb, par(Rx, ra))
33
34
# alle moeglichen Schaltzustaende. None heisst offen
35
def switch_states():
36
    for a in [0.0, None, 5.0]:
37
        for b in [0.0, None, 5.0]:
38
            yield a, b
39
40
# list mit allen Spannungswerten am ADC, die bei den Widerstaenden ra und rb moeglich sind
41
def all_voltages(ra, rb):
42
    t = list()
43
    for sa, sb in switch_states():
44
        t.append(voltage(sa, sb, ra, rb))
45
    return t
46
47
# kleinste Spannungsdifferenz
48
def smallest_voltagediff(ra, rb):
49
    vs = all_voltages(ra, rb)
50
    vs.sort()
51
    diffs = listdiff(vs)
52
    return min(diffs)
53
54
if __name__ == '__main__':
55
    a = list()
56
    # 100R bis 220k sollten reichen:
57
    for ra in e_series(96, 100.0, 220000.0, adj = True):
58
        for rb in e_series(96, ra, 220000.0, adj = True):
59
            a.append([ra, rb, smallest_voltagediff(ra, rb)])
60
    # die groesste kleinste spannungsdifferenz suchen:
61
    best_res = max(a, key=lambda el: el[2])
62
    print best_res