//B. Vandeportaele 2019
//TODO prévoir fixation batterie 18650+ chargeur (virer l'usb et cabler directement avec interrupteur )
//+ carte ESP + HX711,  + 1 ou 2 led WS2812 
//élargir un peu et trous pour vis


epaisseur=18.3;
largeur=21.5;
hauteur=26.2;

jeu=0.1;
eps=0.01;

ecart_doigt=10;
      
       
resolutionpart=60;//default value for the constant printerpart, it can be changed through command line using -D
//https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_OpenSCAD_in_a_command_line_environment
//use compile.sh to generate the stl files in stl/ subdirectory
resolution=resolutionpart;  //assign the constant value to a variable to be able to test it, even if the constant hasn't been defined in command line
 
      
      
      ///////////////////////////////////////// 
      module gripper(){
      
       difference(){
            union(){
          fixation_jauge(1);
translate([0,0,-43.4])  
cylinder(h = 14, d = 52, center = true,$fn=resolution*3);             
                }
           translate([0,0,-46])    
          #cylinder(h = 13.1, d = 31+10, center = true,$fn=resolution*3);  
       
       }
          
      }
      ///////////////////////////////////////// 
      module bouchon(){
               translate([0,0,-50])    
          cylinder(h = 10, d = 31, center = true,$fn=resolution);  
      }
  ///////////////////////////////////////// 
  position_hx711=[-45,18,-9]; //pour déplacer l'HX711
  ///////////////////////////////////////// 
module trou_HX711(){
        translate(position_hx711)
       for (i=[-1:2:1]) 
           for (j=[-1:2:1]) 
              translate([0,i*(17.5/2),j*25/2])
             rotate([0,90,0]) 
                    #cylinder(h = 20, d = 2.7, center = true,$fn=resolution);  
       }
  ///////////////////////////////////////// 
module HX711(istrou)      {
    difference()
    {
    translate(position_hx711)
        cube([4,24,31],center=true);
      trou_HX711();
       }             
}
///////////////////////////////////////// 
 position_esp=[0,-22,-9]; //pour déplacer l'esp et les trous
///////////////////////////////////////// 
module trou_esp(){
        translate(position_esp)
       for (i=[-1:2:1]) 
           for (j=[-1:2:1]) 
              translate([i*(51.5/2),0,j*25/2])
             rotate([90,0,0]) 
                    #cylinder(h = 20, d = 2.7, center = true,$fn=resolution);  
       }
///////////////////////////////////////// 
module esp(istrou)      {
    difference()
    {
    translate(position_esp)
        cube([58,4,31],center=true);
      trou_esp();
       }             
}
///////////////////////////////////////// 
module boost(istrou)      {
      translate([25.1,23.5,-19+(istrou*5)])    
      cube([25+istrou*2,18+istrou*1,8+istrou*10],center=true);
}
///////////////////////////////////////// 
module batterie(istrou)
//istrou=1 pour faire le trou avec jeu
//istrou=0 pour la vrai batterie
{
translate([0+istrou*5,23.5,-5])
  rotate([0,90,0]) 
cylinder(h = 68+istrou*10, d = 18.5+istrou*1.2, center = true,$fn=resolution);  
}
///////////////////////////////////////// 
   position_jauge=[0,0,-24-6]; //pour déplacer la jauge et les trous
///////////////////////////////////////// 
module trousjauge()
{
  translate(position_jauge)
      rotate([90,0,0]) 
          for (i=[-1:2:1]) {
              translate([i*(11.1),0,0])
                    #cylinder(h = 50, d = 3.1, center = true,$fn=resolution);  
               translate([i*(11.1+7.3),0,0])
                    #cylinder(h = 50, d = 3.1, center = true,$fn=resolution);  
          }
      }
///////////////////////////////////////// 
dim_jauge=[45.2,6,9.5];
///////////////////////////////////////// 
module jauge()
{
   difference(){ 
        translate(position_jauge) 
            cube(dim_jauge,center=true);
        trousjauge();     
      }
}
///////////////////////////////////////// 
module texte()
{
    if (resolution>10){ // texte effacé si resolution<=10
    rotate([0,0,180])  {
    translate([-18,-33.5,-3])
    rotate([90,0,0])
    scale([0.7,0.7,1])
        linear_extrude(height =2) {       text( "IUT GEII", font = "Liberation Sans");
        }
    translate([-24,-33.5,-13])
    rotate([90,0,0])
    scale([0.7,0.7,1])        
            linear_extrude(height =2) {       text( "LPRO ROB", font = "Liberation Sans");
            }
    translate([-24,-33.5,-22])
    rotate([90,0,0])
    scale([0.7,0.7,1])        
            linear_extrude(height =2) {       text( "BVDP 2019", font = "Liberation Sans");
            }
        }
    }
}
///////////////////////////////////////// 
//régler jeu à 0 pour les doigts et à >0 pour faire des trous
module doigt(jeu)
{
    scale([epaisseur+(jeu*2),largeur+(jeu*2),hauteur+(jeu*2)])
        cube(1,center=true); 
}
///////////////////////////////////////// 
module paire_de_doigt(jeu)
{
    for (i=[-0.5:0.5])
    translate([i*(epaisseur+ecart_doigt),0,0])
        doigt(jeu);
}

///////////////////////////////       
module bord_arrondi(dim_bord_arrondi) //longueur + rayon
{
    difference(){  
     cube([dim_bord_arrondi[0],dim_bord_arrondi[1],dim_bord_arrondi[1]],center=false);  
     rotate(90,[0,1,0])      
        # cylinder(h = dim_bord_arrondi[0]+0.1, r = dim_bord_arrondi[1], center = false,$fn=resolution);    
    }
 }
///////////////////////////////       

module fixation_jauge(n){  //n=0 pour fixation au robot et 1 pour le gripper
    for  (i=[0,1]){  //pour les 2 trous de fixation
        difference(){  
       dim_cube_fix_jauge=[16,8,14.5];
            jeu_jauge=0.2;
            translate([-14.5+n*29,-7+(dim_jauge[1]+dim_cube_fix_jauge[1]+jeu_jauge)*i,-21.5-6-n*5]) 
        union(){
            cube(dim_cube_fix_jauge,center=true);    
            r_bords_arrondi=2;
            rotate(i*180,[0,0,1])   
              rotate(n*180,[0,1,0])    
            translate([-(dim_cube_fix_jauge[0]/2),-r_bords_arrondi-(dim_cube_fix_jauge[1]/2),-(3.1*r_bords_arrondi)+(dim_cube_fix_jauge[2]/2)+n])           
                bord_arrondi([dim_cube_fix_jauge[0],r_bords_arrondi]);
        }
        trousjauge();
          }
      }
} 
///////////////////////////////       
    module piece()
    {
        
fixation_jauge(0);
          {
  difference(){  
    translate([0,10,-6-3]) 
      
        cube([75,10*2+30,25+6],center=true); 
        #batterie(1);
        boost(1); 
      #trou_HX711();
      #trou_esp();     
    paire_de_doigt(jeu);    
    
      
    for (i=[0,1]){ 
         translate([0,0,2-10*i])        
         rotate(-90,[0,1,0])    
            {  
         for (j=[-1,1]){ 
               translate([0,0,23.2*j])    
 //translate([ecartentretroudefixation*j,0,0])  
             {
             //trou pour vis
		# cylinder(h = 35, r = 1.5+0.25, center = true,$fn=resolution);    
                //trou pour hexagones
                # cylinder(h = 7, r = 3.2, center = true,$fn=6);
         }  
     }
  } 
  }
  }
  texte();
  }
  }
/////////////////////////////// 
  

printerpart=0;//default value for the constant printerpart, it can be changed through command line using -D
//https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_OpenSCAD_in_a_command_line_environment
//use compile.sh to generate the stl files in stl/ subdirectory
type=printerpart;  //assign the constant value to a variable to be able to test it, even if the constant hasn't been defined in command line
//pour visualisation de l'assemblage
space=1; //free space between assembled parts
if (type==0)
 {
  piece();
  debug=true;
  //debug=false;
  if (debug)
  {
  color([0,1,0,1])  jauge();
  batterie(0);
  boost(0);     
  esp(0);
  HX711(0);
  //color([1,0,0,0.2]) paire_de_doigt(0);
      }
  
      gripper();
        if (debug)
  {
      //bouchon();
  }
  // mine_stylo();
  }
  
  ////////////////////////////////////
//pour impression via script compile.sh
if (type==1)
    rotate ([180,0,0])  piece();
if (type==2)
    translate([0,0,0])  gripper();
  