use <gears/gears.scad>

// 0.5er - Zahnrad, das an der Stepperwelle befestigt wird
module stepper_zahnrad(tooth_number = 20, gear_modul = 0.5) {
  difference() {
    union() {
      translate([ 0, 0, 10 ]) spur_gear(
          modul = gear_modul, tooth_number = tooth_number, width = 3, bore = 0);
      cylinder(h = 10 + 3, d = 6.5 + 2);
    }
    difference() {
      cylinder(d = 6.5, h = 71.5);
      translate([ 6, 0, 0 ]) cube([ 6.5, 6.5, 71.5 * 2 ], center = true);
    }
  }
}

// Großer Steppermotor - Modell PSM42BYGHW603
module stepper() {
  union() {
    translate([ -56.5 / 2, -56.5 / 2, 0 ]) union() {
      difference() {  // Grundkörper
        cube([ 56.5, 56.5, 50.5 ]);

        // Einrundungen
        cylinder(h = 45, r = 9.5);
        translate([ 56.5, 0, 0 ]) cylinder(45, 9.5, 9.5);
        translate([ 0, 56.5, 0 ]) cylinder(45, 9.5, 9.5);
        translate([ 56.5, 56.5, 0 ]) cylinder(45, 9.5, 9.5);

        // Schrauben
        translate([ 4, 4, 45 ]) cylinder(h = 6, d = 5.5);
        translate([ 56.5 - 4, 4, 45 ]) cylinder(h = 6, d = 5.5);
        translate([ 4, 56.5 - 4, 45 ]) cylinder(h = 6, d = 5.5);
        translate([ 56.5 - 4, 56.5 - 4, 45 ]) cylinder(h = 6, d = 5.5);
      }

      // Welle
      translate([ 56.5 / 2, 56.5 / 2, 0 ]) union() {
        difference() {
          cylinder(d = 6.5, h = 71.5);
          translate([ 6, 0, 0 ]) cube([ 6.5, 6.5, 71.5 * 2 ], center = true);
        }
        translate([ 0, 0, 50.5 ]) cylinder(h = 1.5, d = 38);
      }
    }
    translate([ 0, 0, 71.5 - (10 + 3) ]) stepper_zahnrad();
  }
}

// Kleiner Steppermotor - Modell KP4M2-217
module stepper_small(grundkoerper_h,
                     befestigung_l,
                     befestigung_screw_d,
                     befestigung_screw_abstand,
                     gear_modul = 0.5) {
  grundkoerper_kante = 42.3;
  grundkoerper_cyl_d = 22;
  grundkoerper_cyl_h = 2;
  befestigung_h = 5;
  befestigung_b = 20;

  welle_h = 18;
  welle_d = 5;
  zahnrad_h = 13;

  union() {
    // Grundkörper
    union() {
      translate([ 0, 0, grundkoerper_h / 2 ])
          cube([ grundkoerper_kante, grundkoerper_kante, grundkoerper_h ],
               center = true);
      translate([ 0, 0, grundkoerper_h ])
          cylinder(h = grundkoerper_cyl_h, d = grundkoerper_cyl_d);
    }

    // Welle & Zahnrad
    translate([ 0, 0, grundkoerper_h + grundkoerper_cyl_h ]) {
      union() {
        translate([ 0, 0, welle_h - zahnrad_h ]) spur_gear(
            modul = gear_modul, tooth_number = 20, width = 13, bore = 0);
        cylinder(h = welle_h, d = welle_d);
      }
    }

    // Befestigung
    translate([
      -befestigung_l / 2, -befestigung_b / 2, grundkoerper_h - befestigung_h
    ]) {
      linear_extrude(height = befestigung_h) {
        difference() {
          square([ befestigung_l, befestigung_b ]);
          translate([ befestigung_screw_abstand, befestigung_b / 2 ])
              circle(d = befestigung_screw_d);
          translate(
              [ befestigung_l - befestigung_screw_abstand, befestigung_b / 2 ])
              circle(d = befestigung_screw_d);
        }
      }
    }
  }
}