Transistor-Matcher-Script

MiK

MiK

Löten&Coden
So, nach meinem Transistor-Messgerät neulich gibts jetzt auch Software für dieses rein analog arbeitende Gerät :)

Folgendes Script liest die in CVS aufbereiteten Daten ein und ermittelt Paare aus möglichst gut zusammen passenden Transistoren. Die erlaubten Limits sind relativ weit oben im Script zu konfigurieren. Es fehlt ein wenig Intelligenz, also Gewichtung hFE versus Ube und auch sowas wie "Kompromiss-Matching", um lieber ein schlechteres Paar zu gewinnen, um woanders noch eins mehr bauen zu können, aber ich denke, es spart viel Zeit.

Eingabeformat sieht z.B. so aus:

Code:
600,303
600.7,365
600.5,302
600.4,255
599.8,306
598.5,280

Erster Wert ist die Ube, zweiter Wert hFE (das waren BC548B). Wer der Perlen mächtig ist, kann sich hier natürlich noch alles passend zurecht ändern, das ist mal wieder nur so ein Hack von mir, damit ich mal meine mittlerweile vielen gemessenen Transistoren zu Paaren schnüren kann.

Selbstverständlich braucht man nen Perl-Interpreter für dieses Machwerk, unter OSX ist das glaub per Default dabei, unter Linux und so ziemlich, und unter Windows gibts z.B. ActivePerl.

Viel Spaß damit!

Edit: habs nochn weng gepimpt, um die fehlende Intelligenz auszugleichen - man kann nun beliebig viele "Qualitätsklassen" definieren, also Paare maximaler Ube und hFE Differenzen.

Code:
#!/usr/bin/perl
#
# -----------------------------------------------------------------------------
# matcher.pl
# (C) 2008 Michael Kukat
# read transistor Ube and hFE from a CSV file and find good matching pairs
# within given tolerances
#
# Input format:
#
# <ube>,<hfe>
# where <ube> and <hfe> = [0-9.]+
# you may need to convert your CSV file to need those requirements or change
# the line extracting the data from the line
# -----------------------------------------------------------------------------

use strict;

# configuration

my @classes = ();

push(@classes, { 'maxubed' => 0.1, 'maxhfed' => 3 });
push(@classes, { 'maxubed' => 0.2, 'maxhfed' => 5 });
push(@classes, { 'maxubed' => 0.5, 'maxhfed' => 10 });
push(@classes, { 'maxubed' => 1.0, 'maxhfed' => 20 });

# variables

my $maxubed;            # maximum Ube difference (mV)
my $maxhfed;            # maximum hFE difference
my $class;              # class counter
my @values = ();        # transistor values array
my $num = 1;            # transistor number
my $pair = 1;           # pair number
my $main;               # main transistor values
my $sub;                # sub transistor values
my @candidates;         # match candidates
my $ccnt;               # candidates counter
my $mainube;            # main Ube
my $mainhfe;            # main hFE
my $subube;             # sub Ube
my $subhfe;             # sub hFE
my $ubed;               # Ube difference
my $hfed;               # hFE difference
my $tmp;                # temporary value

# check arguments

if($#ARGV < 0) {
        die "usage: $0 <filename>";
}

# open input file

open FH, "$ARGV[0]" or die "cannot open input file $ARGV[0]";

# read in all transistor data

while(<FH>) {
        chomp;
        /^([0-9.]+),([0-9.]+)/;
        
        my %trans = ();
        $trans{'number'} = $num;
        $trans{'ube'} = $1;
        $trans{'hfe'} = $2;
        $trans{'pair'} = 0;

        # if data is valid, insert transistor into array
        if($1 ne '' && $2 ne '' && $1 != 0 && $2 != 0) {
                push(@values, \%trans);
        }
        $num++;
}
close FH;

# print out status
print "read data of " . ($#values + 1) . " transistors, " . ($num - 1) . " entries in file\n";

# get pairs from tolerance classes
$class = 0;
while($class <= $#classes) {

        # set tolerance class
        $maxubed = $classes[$class]{'maxubed'};
        $maxhfed = $classes[$class]{'maxhfed'};
        print "\n";
        print "searching for pairs in class " . ($class + 1);
        print " max Ube diff = " . $maxubed;
        print " max hFE diff = " . $maxhfed;
        print "\n";

        # check for pairs
        $num = 0;       
        while($num <= $#values) {
                # get transistor
                $main = $values[$num];

                # check if transistor is not already used in a pair
                if($$main{'pair'} == 0) {

                        # initialize candidates search
                        @candidates = ();
                        $ccnt = $num + 1;
                        $mainube = $$main{'ube'};
                        $mainhfe = $$main{'hfe'};

                        while($ccnt <= $#values) {

                                # get transistor
                                $sub = $values[$ccnt];

                                # check if transistor is unused
                                if($$sub{'pair'} == 0) {

                                        # get transistor data
                                        $subube = $$sub{'ube'};
                                        $subhfe = $$sub{'hfe'};

                                        # calculate differences to main transistor
                                        $ubed = abs($mainube - $subube);
                                        $hfed = abs($mainhfe - $subhfe);
                                        
                                        # check differences
                                        if($ubed <= $maxubed && $hfed < $maxhfed) {
                                                push(@candidates, $ccnt);
                                        }
                                }

                                $ccnt++;
                        }

                        # if candidates were found, choose the best one
                        if($#candidates >= 0) {

                                # sort candidates by Ube and hFE
                                $ccnt = 0;
                                while($ccnt < $#candidates) {

                                        # get transistor
                                        $sub = $values[$candidates[$ccnt]];

                                        # get transistor data
                                        $subube = $$sub{'ube'};
                                        $subhfe = $$sub{'hfe'};

                                        # calculate differences to main transistor
                                        $ubed = abs($mainube - $subube);
                                        $hfed = abs($mainhfe - $subhfe);
                                        
                                        # get next transistor
                                        $sub = $values[$candidates[$ccnt + 1]];

                                        # check if correct sort order, swap and restart if wrong
                                        if($ubed > abs($mainube - $$sub{'ube'}) ||
                                           ($ubed == abs($mainube - $$sub{'ube'}) &&
                                            $hfed > abs($mainhfe - $$sub{'hfe'}))) {
                                                $tmp = $candidates[$ccnt];
                                                $candidates[$ccnt] = $candidates[$ccnt + 1];
                                                $candidates[$ccnt + 1] = $tmp;
                                                $ccnt =0;
                                        } else {
                                                $ccnt++;
                                        }
                                }

                                # after sorting, first candidate is best fit, use it
                                $sub = $values[$candidates[0]];

                                $$main{'pair'} = $pair;
                                $$sub{'pair'} = $pair;

                                # print out matched pair
                                print "found pair " . $pair . ":\n";
                                print "    number " . $$main{'number'} . " - Ube " . $$main{'ube'} . " - hFE " . $$main{'hfe'} . "\n";
                                print "    number " . $$sub{'number'} . " - Ube " . $$sub{'ube'} . " - hFE " . $$sub{'hfe'} . "\n";
                                $pair++;
                        }
                }

                $num++;
        }

        $class++;
}

# print out unused transistors
print "\n";
print "unused transistors: ";
$num = 0;
while($num <= $#values) {
        $main = $values[$num];
        print $$main{'number'} . " " if($$main{'pair'} == 0);
        $num++;
}
print "\n";
 


News

Zurück
Oben