UltrasonicSensorServoMount

From BITPlan Wiki
Jump to navigation Jump to search

The preview below currently only works for older JSCad code using the CSG syntax. See OpenJsCad Issue 448 <jscad>//Parametric. //Oktay Gülmez, 23/2/2016. // converted to OpenJSCAD by Wolfgang Fahl 2019-06-09 //GNU_GPL_V3

// include("Bezier.jscad")

/**

* create front Plate
*/

function frontPlate(thickness, eyes_width, eyes_diameter) {

 safety = 0.6;
 radius = eyes_diameter+2;
 radius2 = 5;
 var parts = [];
 var holes = [];
 //Front plate:
 parts.push(linear_extrude({
   height: thickness,
   convexity: 10,
   fn: 60
 }, hull(
   translate([-eyes_width / 2, 0, 0], circle({
     r: radius,
     center: true
   })),
   translate([eyes_width / 2, 0, 0], circle({
     r: radius,
     center: true
   }))
 )));
 parts.push(linear_extrude({
   height: thickness,
   convexity: 10,
   fn: 60
 }, hull(
   translate([-eyes_width / 2, 0, 0], circle({
     r: radius,
     center: true
   })),
   translate([-8, -30, 0], circle({
     r: radius2,
     center: true
   }))
 )));
 parts.push(linear_extrude({
   height: thickness,
   convexity: 10,
   fn: 60
 }, hull(
   translate([eyes_width / 2, 0, 0], circle({
     r: radius,
     center: true
   })),
   translate([8, -30, 0], circle({
     r: radius2,
     center: true
   }))
 )));
 parts.push(translate([-8, -35, 0], cube([16, 35, thickness])));
 //Transducers holes:
 for (fx = -1; fx <= 1; fx += 2) {
   holes.push(translate([fx * eyes_width / 2, 0, thickness / 2], cylinder({
     h: thickness + 2,
     r: eyes_diameter / 2 + safety,
     fn: 50,
     center: true
   })));
 }
 return difference(union(parts), union(holes));

}

//Servo mounting bracket function servoBracket() {

 radius = 8;
 radius2 = 5;
 return translate([0, -35, 0], rotate([-90, -90, 0], linear_extrude({
   height: 6,
   convexity: 10
 }, hull(
   translate([0, 0, 0], circle({
     r: radius,
     center: true
   })),
   translate([23, 0, 0], circle({
     r: radius2,
     center: true
   }))
 ))));

}

function tabsPCB_Mounting(length,width) {

 //Sensor PCB mounting tabs:
 var parts = [];
 var holes = [];
 height = 3.5;
 outer = 3;
 inner = 1;
 for (fx = -1; fx <= 1; fx += 2) {
   for (fy = -1; fy <= 1; fy += 2) {
     parts.push(translate([fx *length / 2, fy * width / 2, 0], cylinder({
       h: thickness + height,
       r: outer
     })));
     //Sensor PCB mounting screws holes:
     holes.push(translate([fx * length / 2, fy * width / 2, 1], cylinder({
       h: thickness + height,
       r: inner
     })));
   }
 }
 return difference(union(parts), union(holes));

}

function servoHead() {

 var parts = [];
 //Servo head placement:
 parts.push(translate([0, -35, 8], rotate([-90, 0, 0], cylinder({
   h: 2.5,
   r: 3.8
 }))));
 parts.push(translate([0, -35 - 1, 8], rotate([-90, 0, 0], cylinder({
   h: 8,
   r: 1.25
 }))));
 parts.push(translate([0, -35 + 4.5, 8], rotate([-90, 0, 0], cylinder({
   h: 3,
   r: 3
 }))));
 parts.push(translate([0, -35 - 1, 23], rotate([-90, 0, 0], cylinder({
   h: 8,
   r: 1
 }))));
 parts.push(translate([0, -35, 8], rotate([-90, -90, 0], linear_extrude({
   height: 2.5,
   convexity: 10
 }, hull(
   translate([0, 0, 0], circle({
     r: 3.5,
     center: true
   })),
   translate([15, 0, 0], circle({
     r: 2,
     center: true
   }))
 )))));
 return union(parts);

}

/**

* create a smile
*/

function smile(thickness) {

 /*bezier = BezierFactory.create(0,0);
 bezier.append([
   [0,15],
   [15,15]
 ]);
 return rotate([0,0,135],bezier.spline(thickness)).translate([10,0,0]);
 */

}

/**

* create a parametric ultrasonic sensor
*/

function main() {

 BezierFactory();
 //Parameters:
 //The diameter of the transducer in mm.: 16mm. mostly.
 eyes_diameter = 16;
 //Distance between centers of transducers in mm.: 25.4mm for HC-SR04.
 width = 41.5
 eyes_width = 25.4;
 //Sensor PCB mounting holes in mm.: 41.4x17 for HC-SR04.
 mount_length = 42.35;
 mount_width = 17.4;
 //Desired thickness in mm.: Maximum 5mm.
 thickness = 2.5;
 //Decorations: "1" smile, "2" teeth, "3" vampire, "4" mustage, "5" O, other for none.
 face = 1;
 fn = 20;
 var parts = [];
 var holes = [];
 parts.push(frontPlate(thickness, eyes_width, eyes_diameter));
 parts.push(servoBracket());
 parts.push(tabsPCB_Mounting(mount_length,mount_width));
 // holes.push(smile(thickness).translate([0, -20, 0]));
 holes.push(servoHead())
 //Cleaning overhang:
 holes.push(translate([-10, -35, -10], cube([20, 10, 10])));
 return difference(union(parts), union(holes));

}</jscad>

Source code

Just cut&paste the source code below to https://www.openjscad.org/

//Parametric.
//Oktay Gülmez, 23/2/2016.
// converted to OpenJSCAD by Wolfgang Fahl 2019-06-09
//GNU_GPL_V3

// include("Bezier.jscad")

/**
 * create front Plate
 */
function frontPlate(thickness, eyes_width, eyes_diameter) {
  safety = 0.6;
  radius = eyes_diameter+2;
  radius2 = 5;
  var parts = [];
  var holes = [];
  //Front plate:
  parts.push(linear_extrude({
    height: thickness,
    convexity: 10,
    fn: 60
  }, hull(
    translate([-eyes_width / 2, 0, 0], circle({
      r: radius,
      center: true
    })),
    translate([eyes_width / 2, 0, 0], circle({
      r: radius,
      center: true
    }))
  )));
  parts.push(linear_extrude({
    height: thickness,
    convexity: 10,
    fn: 60
  }, hull(
    translate([-eyes_width / 2, 0, 0], circle({
      r: radius,
      center: true
    })),
    translate([-8, -30, 0], circle({
      r: radius2,
      center: true
    }))
  )));
  parts.push(linear_extrude({
    height: thickness,
    convexity: 10,
    fn: 60
  }, hull(
    translate([eyes_width / 2, 0, 0], circle({
      r: radius,
      center: true
    })),
    translate([8, -30, 0], circle({
      r: radius2,
      center: true
    }))
  )));
  parts.push(translate([-8, -35, 0], cube([16, 35, thickness])));
  //Transducers holes:
  for (fx = -1; fx <= 1; fx += 2) {
    holes.push(translate([fx * eyes_width / 2, 0, thickness / 2], cylinder({
      h: thickness + 2,
      r: eyes_diameter / 2 + safety,
      fn: 50,
      center: true
    })));
  }
  return difference(union(parts), union(holes));
}

//Servo mounting bracket
function servoBracket() {
  radius = 8;
  radius2 = 5;
  return translate([0, -35, 0], rotate([-90, -90, 0], linear_extrude({
    height: 6,
    convexity: 10
  }, hull(
    translate([0, 0, 0], circle({
      r: radius,
      center: true
    })),
    translate([23, 0, 0], circle({
      r: radius2,
      center: true
    }))
  ))));
}

function tabsPCB_Mounting(length,width) {
  //Sensor PCB mounting tabs:
  var parts = [];
  var holes = [];
  height = 3.5;
  outer = 3;
  inner = 1;
  for (fx = -1; fx <= 1; fx += 2) {
    for (fy = -1; fy <= 1; fy += 2) {
      parts.push(translate([fx *length / 2, fy * width / 2, 0], cylinder({
        h: thickness + height,
        r: outer
      })));
      //Sensor PCB mounting screws holes:
      holes.push(translate([fx * length / 2, fy * width / 2, 1], cylinder({
        h: thickness + height,
        r: inner
      })));
    }
  }
  return difference(union(parts), union(holes));
}

function servoHead() {
  var parts = [];
  //Servo head placement:
  parts.push(translate([0, -35, 8], rotate([-90, 0, 0], cylinder({
    h: 2.5,
    r: 3.8
  }))));
  parts.push(translate([0, -35 - 1, 8], rotate([-90, 0, 0], cylinder({
    h: 8,
    r: 1.25
  }))));
  parts.push(translate([0, -35 + 4.5, 8], rotate([-90, 0, 0], cylinder({
    h: 3,
    r: 3
  }))));
  parts.push(translate([0, -35 - 1, 23], rotate([-90, 0, 0], cylinder({
    h: 8,
    r: 1
  }))));

  parts.push(translate([0, -35, 8], rotate([-90, -90, 0], linear_extrude({
    height: 2.5,
    convexity: 10
  }, hull(
    translate([0, 0, 0], circle({
      r: 3.5,
      center: true
    })),
    translate([15, 0, 0], circle({
      r: 2,
      center: true
    }))
  )))));
  return union(parts);
}

/**
 * create a smile
 */
function smile(thickness) {
  /*bezier = BezierFactory.create(0,0);
  bezier.append([
    [0,15],
    [15,15]
  ]);
  return rotate([0,0,135],bezier.spline(thickness)).translate([10,0,0]);
  */
}

/**
 * create a parametric ultrasonic sensor
 */
function main() {
  BezierFactory();
  //Parameters:
  //The diameter of the transducer in mm.: 16mm. mostly.
  eyes_diameter = 16;
  //Distance between centers of transducers in mm.: 25.4mm for HC-SR04.
  width = 41.5
  eyes_width = 25.4;

  //Sensor PCB mounting holes in mm.: 41.4x17 for HC-SR04.
  mount_length = 42.35;
  mount_width = 17.4;
  //Desired thickness in mm.: Maximum 5mm.
  thickness = 2.5;
  //Decorations: "1" smile, "2" teeth, "3" vampire, "4" mustage, "5" O, other for none.
  face = 1;

  fn = 20;
  var parts = [];
  var holes = [];
  parts.push(frontPlate(thickness, eyes_width, eyes_diameter));
  parts.push(servoBracket());
  parts.push(tabsPCB_Mounting(mount_length,mount_width));
  // holes.push(smile(thickness).translate([0, -20, 0]));
  holes.push(servoHead())
  //Cleaning overhang:
  holes.push(translate([-10, -35, -10], cube([20, 10, 10])));
  return difference(union(parts), union(holes));
}