use <stepper.scad>
use <arm.scad>
use <marker.scad>
use <electronics.scad>
use <sg90.scad>
//module arm(holes, width, thinkness, bore){


arm1 = 70;
arm2 = 55;
x = 55; //[15:85]
y = 70; //[35:85]
length = sqrt(pow(x,2) + pow(y,2));
arm1_sq = pow(arm1,2);
arm2_sq = pow(arm2,2);
length_sq = pow(length,2);
b = acos((arm1_sq + length_sq - arm2_sq)/(2 * arm1 * length));
c = acos((arm1_sq - length_sq + arm2_sq)/(2 * arm1 * arm2));
angle1 = b + atan2(y,x) - 90;
angle2 = angle1 - 90 + c;
stepper_spaceing = 47;
stepper_offset = 55;
arm_thickness = 3;
arm_width = 10;
arm_offset = 15;

bus_diameter = 3 + arm_width;
shaft_bore = 5.3;
joint_bore = 3.2;
joint_spacing = arm_thickness+1;
set_screw_bore = 2.5;

$fn = $preview? 32 : 72;

module arm3 (){
  difference(){
    union(){
      translate([0,0,-arm_thickness/2])cylinder(d=bus_diameter,h=10-arm_thickness/2);
      arm([0,25],arm_width,arm_thickness,joint_bore);
    }
    cylinder(d=shaft_bore,h=16,center=true);
    rotate([0,-90,0]){
      rotate([45,0,0]) translate([arm_thickness,0,0]) cylinder(d=set_screw_bore,h=20);
      rotate([-45,0,0]) translate([arm_thickness,0,0]) cylinder(d=set_screw_bore,h=20);
    }
  }
}

module arm0 (){
  difference(){
    union(){
      cylinder(d=bus_diameter,h=20,center=true);
      arm([0,25],arm_width,arm_thickness,joint_bore);
    }
    cylinder(d=shaft_bore,h=21,center=true);
    rotate([0,-90,0]){
      translate([5,0,0]) cylinder(d=set_screw_bore,h=20);
      translate([-5,0,0]) cylinder(d=set_screw_bore,h=20);
    }
  }
}
module arm5a(){
  rotate([90, 0, 90]) arm([25,0,50],arm_width,arm_thickness,joint_bore);
}

module arm5b(){
  arm5a();
  difference(){
    translate([-arm_thickness/2-2.5,35,-14]) rotate([0,0,180]) {sg90_bracket();
     sg90(-90);
    }
    translate([0,25,0]) rotate([0,90,0]) cylinder(d=7,h=10,center=true);
  }
}

module arm5(){
  translate([joint_spacing,0,0]) arm5a();
  translate([-joint_spacing,0,0]) arm5b();
  translate([0,50,0]) difference() {cube([arm_thickness+2*joint_spacing,10,arm_width], center=true);
  cylinder(d=joint_bore,h=40,center=true);
  }
}

module arm6a(){
  union(){
    rotate([0,0,90]) arm([0,30-arm_width/2],arm_width,arm_thickness,joint_bore);
    translate([0,30-arm_width/2,0]) cube([arm_width,arm_width,arm_thickness],center=true); //cylinder(h=arm_thickness,d=arm_width,center=true);
    }
}

module arm6b(){
    difference() {
    length = 29;
        hull() {
            cylinder(h=arm_thickness, d=arm_width,center=true);
            translate([0,length,0]) cylinder(h=arm_thickness, d=5, center=true);
            }
            cylinder(h=arm_thickness+1, d=joint_bore,center=true);
            translate([0,length,0]) cylinder(h=arm_thickness+1, d=2, center=true);
            }
}

module arm6(){
  arm_offset = 5+arm_thickness/2+1;
  difference(){
    union(){
      // joint arms
      translate([0,0,arm_offset]) arm6a();
      translate([0,0,-arm_offset]) arm6a();
      // servo arm
      translate([0,0,arm_offset]) rotate([0,0,90]) arm6b();
      // cylinder for marker holder
      translate([0,30,0]) {
       translate([-20+arm_width/2,0,0])  rotate([0,90,0]) cylinder(d=20,h=20);
        translate([-5,0,0]) {
          // added thinkness for setscrews
          rotate([-45,0,0]) cylinder(d=7,h=11);
          rotate([-135,0,0]) cylinder(d=7,h=11);
        }
      }
    }
    translate([0,30,0]){ 
      // hole for marker
      translate([-20+arm_width/2-1,0,0])rotate([0,90,0]) cylinder(d=16,h=22);
      translate([-5,0,0]) {
      // holes for setscrew
      rotate([-45,0,0]) cylinder(d=set_screw_bore,h=40);
      rotate([-135,0,0]) cylinder(d=set_screw_bore,h=40);
      }
    }
  }
}

//!arm6();

translate([stepper_offset,0,0]) {
  // stepper on joint C
  rotate([-90,0,-90]) stepper_103H5205();
  // stepper on joint A
  translate([0,stepper_spaceing,0]) rotate([90,0,90]) stepper_103H5205();

  translate([arm_offset,0,0]){

    //arm0
    translate([0,stepper_spaceing,0]) rotate([angle1,0,0]) rotate([0,90,0]) arm0();//([0,-25],arm_width,arm_thickness,5);
    //arm1
    rotate([angle1,0,0]) rotate([0,-90,0]) 
      difference(){
        arm([0,-25,arm1],arm_width,arm_thickness,joint_bore);
        cylinder(d=shaft_bore, h= 10, center=true);
        translate([45,10,0]) cylinder(d=16, h=10, center=true);
      }

    //arm2
    translate([3,0,0]) rotate([angle1,0,0]) translate([0,0,-25]) rotate([-angle1,0,0]) rotate([90,0,90]) arm([0,stepper_spaceing],arm_width,arm_thickness,joint_bore);
    translate([-3,0,0]) rotate([angle1,0,0]) translate([0,0,-25]) rotate([-angle1,0,0]) rotate([90,0,90]) arm([0,stepper_spaceing],arm_width,arm_thickness,joint_bore);

    //arm3
    rotate([angle2,0,0]){
      translate([3,0,0]) rotate([-90,0,-90]) arm3(); //([0,25],arm_width,arm_thickness,5);
      translate([-3,0,0]) rotate([90,0,-90]) arm3(); //arm([0,25],arm_width,arm_thickness,5);
      translate([0,-25,0]) rotate([angle1-angle2,0,0]) {
        // arm4
        rotate([0,-90,0]) arm([0,arm1],arm_width,arm_thickness,joint_bore);
        translate([0,0,arm1]) rotate([angle2-angle1,0,0]) rotate([0,0,0]) {
          arm5();
          translate([-stepper_offset-arm_offset,arm2+25]) rotate([0,90,0]) marker("red");
          translate([0,50,0]){
            arm6();
          }
        }
      }
    }
  }
}

// electronics
translate([40,0,-60]) rotate([-0,-90,0]){
  color("olive") translate([-12,-2,5]) cube([40,61,1]);
  translate([20,5,0])
    rotate([0,0,90]) {
      stepper_controller();
      translate([30.4,0,0]) stepper_controller();
    }
}

//carriage
translate([stepper_offset,0,0]){
  rotate([0,90,0]) {
    stepper_103H5205_bracket();
    translate([0,stepper_spaceing,0]) stepper_103H5205_bracket();
  }
  translate([-53,-25.5,-25.5])cube([40,98,4]);
}
