j_tomas

Screw cutting in C3 minilathe

Aug 8th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.91 KB | None | 0 0
  1. use strict;
  2. # Script to calculate gear combinations for thread cutting on Sieg C3 lathe
  3. # Available gears
  4. my %GearExist = (
  5.     0 => 1,
  6.     20 => 2,
  7.     30 => 1,
  8.     35 => 1,
  9.     40 => 2,
  10.     45 => 1,
  11.     50 => 1,
  12.     55 => 1,
  13.     57 => 1,
  14.     60 => 1,
  15.     63 => 1,
  16.     65 => 1,
  17.     80 => 2);
  18. # Not sure if all gears fit on all axes or if all combinations work. If not, reduce the lists below accordingly
  19. my @A=(20, 30, 35, 40, 50, 55, 57, 60, 63, 65, 80);
  20. my @B=(20, 30, 35, 40, 50, 55, 57, 60, 63, 65, 80);
  21. my @C=(0, 20, 30, 35, 40, 50, 55, 57, 60, 63, 65, 80); # C can be unused
  22. my @D=(20, 30, 35, 40, 50, 55, 57, 60, 63, 65, 80);
  23. my ($Ratio, $Pitch_int, $Pitch_fract, $Pitch, $Indicator_step, $i, $Indicator_product);
  24. my (@Works, $Found, $First, $Indicator_value, %GearCount);
  25. open (FILE, '>table.txt');
  26. print FILE "A\tB\tC\tD\tThread Pitch";
  27. print FILE "\tIndicator"x16;
  28. print FILE "\n";
  29. foreach my $Agear (@A) {
  30.     foreach my $Dgear (@D) {
  31.         foreach my $Cgear (@C) {
  32.             foreach my $Bgear(@B) {
  33.                 # Count the number of used gears and check availability
  34.                 $GearCount{$Agear}++;
  35.                 $GearCount{$Bgear}++;
  36.                 $GearCount{$Cgear}++;
  37.                 $GearCount{$Dgear}++;
  38.                 if ($GearCount{$Agear}<=$GearExist{$Agear} and
  39.                     $GearCount{$Bgear}<=$GearExist{$Bgear} and
  40.                     $GearCount{$Cgear}<=$GearExist{$Cgear} and
  41.                     $GearCount{$Dgear}<=$GearExist{$Dgear}) {
  42.                     if ($Cgear != 0) { # All gears used
  43.                         $Ratio=$Agear/$Bgear*$Cgear/$Dgear;
  44.                     }
  45.                     else { # If C is not used
  46.                         $Ratio=$Agear/$Dgear;
  47.                     }
  48.                     $Pitch=$Ratio*1.5; # For every chuck revolution, the toolbit is moved this amount with lead screw pitch 1.5 mm
  49.                     # Split in integer and fraction to be able to print values with decimal comma
  50.                     if ($Pitch=~m/\./) {
  51.                         $Pitch=~m/(\d+)\.(\d+)/;
  52.                         $Pitch_int=$1;
  53.                         $Pitch_fract=$2;
  54.                     }
  55.                     else { # The pitch is an integer
  56.                         $Pitch_int=$Pitch;
  57.                         $Pitch_fract=0;
  58.                     }
  59.                     # The gear exchange must work with the indicator i e an integer number 1-16
  60.                     $Indicator_step=1/$Ratio;
  61.                     $Found=0;
  62.                     for ($i=1;$i<=16;$i++) {
  63.                         $Indicator_product=$Indicator_step*$i;
  64.                         if($Indicator_product-int($Indicator_product)<1e-6) {
  65.                             $Works[$i]=1;
  66.                             $Found=1;
  67.                         }
  68.                         else {
  69.                             $Works[$i]=0;
  70.                         }
  71.                     }
  72.                     $First=0;
  73.                     # Only print combinations that work with the indicator
  74.                     if ($Found and $Pitch>=0.4 and $Pitch <=3) {
  75.                         print FILE "$Agear\t$Bgear\t$Cgear\t$Dgear\t$Pitch_int,$Pitch_fract\t";
  76.                         for ($i=1;$i<=16;$i++) {
  77.                             if ($Works[$i]==1) {
  78.                                 if($First==0) {
  79.                                     $First=$i;
  80.                                 }
  81.                                 $Indicator_value=($i-$First)/2+1;
  82.                                 print FILE "$Indicator_value\t";
  83.                             }
  84.                         }
  85.                         print FILE "\n";
  86.                     }
  87.                     if ($Cgear == 0) {
  88.                         last;
  89.                     }
  90.                 }
  91.                 # Reset gear count
  92.                 $GearCount{$Agear}=0;
  93.                 $GearCount{$Bgear}=0;
  94.                 $GearCount{$Cgear}=0;
  95.                 $GearCount{$Dgear}=0;
  96.             }
  97.         }
  98.     }
  99. }
  100. close(FILE);
Advertisement
Add Comment
Please, Sign In to add comment