rod_diameter = 8;
rod_separation = 70; // center to center
rod_length = 400; 

bushing_diameter = 15.4;
bushing_length = 16.8;
bushing_separation = 24; // edge to edge; original carriage: 8

belt_width = 6 ; // a little more, maybe 6.2
belt_thickness = 1.5;
belt_separation = 10; // edge to edge

belt_length = rod_length;

belt_rod_separation = 20; // center to center
belt_y_offset = rod_separation / 2 - belt_rod_separation;

bed_x = 320;
bed_y = 155;

bed_thickness = 5;

bed_z_home = -16; // distance between center of rods and top of heat bed when Z axis is homed

wall_thickness = 3;
carriage_length = bushing_separation + bushing_length * 2 + wall_thickness * 2;

motor_length = 30;
motor_y_offset = 6;
motor_z_offset = 30;

module printer() {
    // rods
    translate([0,  rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=rod_diameter, h=rod_length, $fn=64); 
    translate([0, -rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=rod_diameter, h=rod_length, $fn=64);

    // print bed
    translate([rod_length/2 - bed_x/2, -bed_y/2, bed_z_home - bed_thickness]) cube([bed_x, bed_y, bed_thickness]);
    
    // belts
    color([0,0,0,0.3]) translate([0,  belt_y_offset - belt_width/2, belt_separation/2]) cube([belt_length, belt_width, belt_thickness]);
    color([0,0,0,0.3]) translate([0,  belt_y_offset - belt_width/2, -belt_separation/2 - belt_thickness]) cube([belt_length, belt_width, belt_thickness]);
    
    // endstop switch
    translate([rod_length - 10, 2, 30]) color([0, 0, 0, 0.5]) cube([10, 15, 5]);
}

module extruder() {
    translate([-wall_thickness, 0, 0]) {
        // motor body
        color([0.5, 0.5, 0.5]) translate([-15, 0, 0]) cube([motor_length, 42, 42], center=true);        
        color([0.5, 0.5, 0.5]) rotate([0, 90, 0]) cylinder(d=22, h=2, $fn=64);
        
        // motor shaft
        color([0.8, 0.8, 0.8]) rotate([0, 90, 0]) cylinder(d=5, h=20, $fn=64);
    }
    
    // extruder body
    color([0, 0, 0, 0.5]) translate([0, -21, -21]) cube([26, 46, 44]);
    // heat sink + fan
    color([1,1,1,0.5]) translate([32, 2, 3]) rotate([45, 0, 0]) cube([21, 40, 40], center=true);    
    // large gear
    translate([7.5, 15.5, 15.5]) color([0,0,0,0.5]) rotate([0, 90, 0])  cylinder(d=34, h=4, center=true);
    // heat break
    color([1, 1, 1, 0.5]) translate([12, 12, -21 -25 ]) cylinder(d=8, h=25);
    translate([3,3,-42]) cube([25,18,12]); 
}



module carriage_bushings() {
    translate([ bushing_separation/2 + bushing_length/2,  rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
    translate([-bushing_separation/2 - bushing_length/2,  rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
    translate([ bushing_separation/2 + bushing_length/2, -rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
    translate([-bushing_separation/2 - bushing_length/2, -rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
}

module carriage_bushings_cutout() {
    hull() {
        translate([ bushing_separation/2 + bushing_length/2,  rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
        translate([ bushing_separation/2 + bushing_length/2,  rod_separation/2, -50]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
    }
    hull() {
        translate([-bushing_separation/2 - bushing_length/2,  rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
        translate([-bushing_separation/2 - bushing_length/2,  rod_separation/2, -50]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
    }
    hull() {
        translate([ bushing_separation/2 + bushing_length/2, -rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
        translate([ bushing_separation/2 + bushing_length/2, -rod_separation/2, -50]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
    }
    hull() {
        translate([-bushing_separation/2 - bushing_length/2, -rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
        translate([-bushing_separation/2 - bushing_length/2, -rod_separation/2, -50]) rotate([0, 90, 0]) cylinder(d=bushing_diameter, h=bushing_length, $fn=64, center=true);
    }    
}


module motor_plate() {
    difference() {
        cube([wall_thickness, 42 + wall_thickness * 2 + 2, 44], center=true);
        
        translate([0,  15.5 , 15.5]) rotate([0, 90, 0]) cylinder(d=3.8, h=wall_thickness, center=true, $fn=64);
        translate([0,  15.5, -15.5]) rotate([0, 90, 0]) cylinder(d=3.8, h=wall_thickness, center=true, $fn=64);
        translate([0, -15.5,  15.5]) rotate([0, 90, 0]) cylinder(d=3.8, h=wall_thickness, center=true, $fn=64);
        translate([0, -15.5, -15.5]) rotate([0, 90, 0]) cylinder(d=3.8, h=wall_thickness, center=true, $fn=64);
        rotate([0, 90, 0]) cylinder(d=22, h=wall_thickness, center=true, $fn=64);
    }
    hull() {
        translate([motor_length/2 + wall_thickness/2, 21 + wall_thickness/2 +1, -21 - 1.5]) cube([motor_length, wall_thickness, 1], center=true);
        translate([0, 21 + wall_thickness/2 + 1, 21.5]) cube([wall_thickness, wall_thickness, 1], center=true);
    }
    hull() {
        translate([motor_length/2 + wall_thickness/2, -21 - wall_thickness/2 -1, -21 - 1.5]) cube([motor_length, wall_thickness, 1], center=true);
        translate([0, -21 - wall_thickness/2 -1, 21.5]) cube([wall_thickness, wall_thickness, 1], center=true);
    }
}

module carriage() {

    
    platform_length = 33;
    platform_thickness = 3;
    platform_top_z = 8.5;
    
    platform2_length = 10;
    
    difference() {
        union() {
            // "katamaran" carriages
            translate([0, rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter + wall_thickness * 2, h=carriage_length, center=true, $fn=64);
            translate([0,-rod_separation/2, 0]) rotate([0, 90, 0]) cylinder(d=bushing_diameter + wall_thickness * 2, h=carriage_length, center=true, $fn=64);
            translate([0, rod_separation/2, -bushing_diameter/4 - wall_thickness/2]) cube([carriage_length, bushing_diameter + wall_thickness * 2, bushing_diameter/2 + wall_thickness], center=true);
            translate([0, -rod_separation/2, -bushing_diameter/4 - wall_thickness/2]) cube([carriage_length, bushing_diameter + wall_thickness * 2, bushing_diameter/2 + wall_thickness], center=true);           

            

            translate([carriage_length/2 - platform_length/2, rod_separation/2 - bushing_diameter / 4 - wall_thickness / 2, platform_top_z / 2]) cube([platform_length, bushing_diameter/2 + wall_thickness, platform_top_z], center=true);
            translate([carriage_length/2 - platform_length/2,-rod_separation/2 + bushing_diameter / 4 + wall_thickness / 2, platform_top_z / 2]) cube([platform_length, bushing_diameter/2 + wall_thickness, platform_top_z], center=true);

            
            // platforms
            translate([carriage_length/2 - platform_length/2, 0, platform_top_z - platform_thickness/2]) cube([platform_length, rod_separation, platform_thickness], center=true);
            //translate([-carriage_length/2 + platform2_length/2, 0, platform_top_z - platform_thickness/2]) cube([platform2_length, rod_separation, platform_thickness], center=true);
            
            // motor plate
            translate([carriage_length/2 - 30 - wall_thickness/2, motor_y_offset, motor_z_offset]) motor_plate();
            
            
        }
        
        // cutouts for bushings and rod clearance
        carriage_bushings_cutout();
        hull() {
            translate([0, rod_separation/2,   0]) rotate([0, 90, 0]) cylinder(d=rod_diameter + 3, h=rod_length, center=true, $fn=64);
            translate([0, rod_separation/2, -50]) rotate([0, 90, 0]) cylinder(d=rod_diameter + 3, h=rod_length, center=true, $fn=64);
        }
        hull() {
            translate([0, -rod_separation/2,   0]) rotate([0, 90, 0]) cylinder(d=rod_diameter + 3, h=rod_length, center=true, $fn=64);
            translate([0, -rod_separation/2, -50]) rotate([0, 90, 0]) cylinder(d=rod_diameter + 3, h=rod_length, center=true, $fn=64);
        }
        
        //cutouts for belt
        //translate([0, belt_y_offset, belt_separation/2 + belt_thickness/2]) cube([belt_length, belt_width * 1.5, belt_thickness], center=true);
        //translate([0, belt_y_offset - belt_width * 1.5, belt_separation/2 + belt_thickness -platform_thickness/2]) cube([belt_length, belt_width * 1.5, platform_thickness], center=true);
        
        #translate([5, belt_y_offset + belt_width, platform_top_z - wall_thickness]) cylinder(d=2.5, h=wall_thickness, $fn=64);
        #translate([5, belt_y_offset - belt_width, platform_top_z - wall_thickness]) cylinder(d=2.5, h=wall_thickness, $fn=64);
        
        
    }   
}


%printer();
translate([100, 0, 0]) {
    translate([carriage_length/2 - motor_length - wall_thickness, motor_y_offset, motor_z_offset]) rotate([0, 0, -180]) %extruder();
    carriage();
}