/* Parametric flange model */

/* All units are millimeters */

$fn = 64;

num_mounting_holes = 4;
mounting_hole_dia = 2.7;
/* center of shaft to center of mounting holes */
mounting_hole_shaft_radius = 8.0;
mounting_hole_cutout_dia = 5.0;
base_height = 4.0;
base_dia_extra = 2.0;
base_dia = 2*mounting_hole_shaft_radius + 2*base_dia_extra + mounting_hole_dia;

shaft_holder_height = 20.0;
shaft_holder_dia = 14.0;

shaft_dia = 4.0;

grubscrew_count = 1;
grubscrew_heights = 2;
grubscrew_hole_dia = 3.2;
grubscrew_insert_dia = 6.5;
grubscrew_insert_height = 3.0;
grubscrew_lowest_offset = 5.0;
grubscrew_pitch = 10.0;

difference() {
    union() {
        difference() {
            cylinder(h=base_height, d=base_dia);
            for ( alpha = [ 0 : 360 / num_mounting_holes : 360 ] ) {
                rotate(alpha, [0, 0, 1])
                translate([-mounting_hole_shaft_radius, 0, 0])
                cylinder(h=base_height, d=mounting_hole_dia);
            }
        }

        difference() {
            cylinder(h=shaft_holder_height, d=shaft_holder_dia);
            cylinder(h=shaft_holder_height, d=shaft_dia);
            /* cutouts for mounting hole screw heads */
            for ( alpha = [ 0 : 360 / num_mounting_holes : 360 ] ) {
                rotate(alpha, [0, 0, 1])
                translate([-mounting_hole_shaft_radius, 0, 0])
                cylinder(h=shaft_holder_height, d=mounting_hole_cutout_dia);
            }
        }
    }

    rotate(-90, [0, 1, 0]) {
        for ( alpha = [ 0 : 360 / grubscrew_count : 360 ] ) {
            for ( i = [ 0 : grubscrew_heights] ) {
                rotate(alpha + (360 / num_mounting_holes/2), [1, 0, 0])
                translate([grubscrew_lowest_offset + i*grubscrew_pitch, 0, 0]) {
                    cylinder(d=grubscrew_hole_dia, h=base_dia/2); 
                    cylinder(d=grubscrew_insert_dia, h=grubscrew_insert_height + shaft_dia/2);
                }
            }
        }
    }
}