Merge pull request #57 from AxTheB/master
[clinton/prusa3.git] / box_frame / inc / functions.scad
1 // PRUSA iteration3
2 // Functions used in many files
3 // GNU GPL v3
4 // Josef Průša <josefprusa@me.com>
5 // Václav 'ax' Hůla <axtheb@gmail.com>
6 // Vlnofka <>
7 // http://www.reprap.org/wiki/Prusa_Mendel
8 // http://github.com/prusajr/PrusaMendel
9
10
11 module nut(d,h,horizontal=true){
12 cornerdiameter = (d / 2) / cos (180 / 6);
13 cylinder(h = h, r = cornerdiameter, $fn = 6);
14 if(horizontal){
15 for(i = [1:6]){
16 rotate([0,0,60*i]) translate([-cornerdiameter-0.2,0,0]) rotate([0,0,-45]) cube([2,2,h]);
17 }
18 }
19 }
20
21 // Based on nophead research
22 module polyhole(d, h, center=false) {
23 n = max(round(2 * d),3);
24 rotate([0,0,180])
25 cylinder(h = h, r = (d / 2) / cos (180 / n), $fn = n);
26 }
27
28 // make it interchangeable between this and cylinder
29 module cylinder_poly(r, h, center=false){
30 polyhole(d=r*2, h=h, center=center);
31 }
32
33 module fillet(radius, height=100, $fn=0) {
34 //this creates acutal fillet
35 translate([-radius, -radius, -height / 2 - 0.02]) difference() {
36 cube([radius * 2, radius * 2, height + 0.04]);
37 if ($fn == 0 && (radius == 2 || radius == 3 || radius == 4)) {
38 cylinder(r=radius, h=height + 0.04, $fn=4 * radius);
39 } else {
40 cylinder(r=radius, h=height + 0.04, $fn=$fn);
41 }
42
43 }
44 }
45
46 module cube_fillet(size, radius=-1, vertical=[3,3,3,3], top=[0,0,0,0], bottom=[0,0,0,0], center=false, $fn=0){
47 //
48 if (use_fillets == 1) {
49 if (center) {
50 cube_fillet_inside(size, radius, vertical, top, bottom, $fn);
51 } else {
52 translate([size[0]/2, size[1]/2, size[2]/2])
53 cube_fillet_inside(size, radius, vertical, top, bottom, $fn);
54 }
55 } else {
56 if (use_fillets == 2) {
57 if (center) {
58 cube_fillet_inside(size, radius, vertical, top, bottom, 4);
59 } else {
60 translate([size[0]/2, size[1]/2, size[2]/2])
61 cube_fillet_inside(size, radius, vertical, top, bottom, 4);
62 }
63
64 } else {
65 cube(size, center);
66 }
67 }
68
69 }
70
71 module cube_negative_fillet(size, radius=-1, vertical=[3,3,3,3], top=[0,0,0,0], bottom=[0,0,0,0], $fn=0){
72
73 j=[1,0,1,0];
74
75 for (i=[0:3]) {
76 if (radius > -1) {
77 rotate([0, 0, 90*i]) translate([size[1-j[i]]/2, size[j[i]]/2, 0]) fillet(radius, size[2], $fn=$fn);
78 } else {
79 rotate([0, 0, 90*i]) translate([size[1-j[i]]/2, size[j[i]]/2, 0]) fillet(vertical[i], size[2], $fn=$fn);
80 }
81 rotate([90*i, -90, 0]) translate([size[2]/2, size[j[i]]/2, 0 ]) fillet(top[i], size[1-j[i]], $fn=$fn);
82 rotate([90*(4-i), 90, 0]) translate([size[2]/2, size[j[i]]/2, 0]) fillet(bottom[i], size[1-j[i]], $fn=$fn);
83
84 }
85 }
86
87 module cube_fillet_inside(size, radius=-1, vertical=[3,3,3,3], top=[0,0,0,0], bottom=[0,0,0,0], $fn=0){
88 //makes CENTERED cube with round corners
89 // if you give it radius, it will fillet vertical corners.
90 //othervise use vertical, top, bottom arrays
91 //when viewed from top, it starts in upper right corner (+x,+y quadrant) , goes counterclockwise
92 //top/bottom fillet starts in direction of Y axis and goes CCW too
93
94
95 if (radius == 0) {
96 cube(size, center=true);
97 } else {
98 difference() {
99 cube(size, center=true);
100 cube_negative_fillet(size, radius, vertical, top, bottom, $fn);
101 }
102 }
103 }
104
105
106 module nema17(places=[1,1,1,1], size=15.5, h=10, holes=false, shadow=false, $fn=24){
107 for (i=[0:3]) {
108 if (places[i] == 1) {
109 rotate([0, 0, 90*i]) translate([size, size, 0]) {
110 if (holes) {
111 rotate([0, 0, -90*i]) translate([0,0,-10]) screw(r=1.7, slant=false, head_drop=13, $fn=$fn, h=h+12);
112 } else {
113 rotate([0, 0, -90*i]) cylinder(h=h, r=5.5, $fn=$fn);
114 }
115 }
116 }
117 }
118 if (shadow != false) {
119 %translate ([0, 0, shadow+21+3]) cube([42,42,42], center = true);
120 //flange
121 %translate ([0, 0, shadow+21+3-21-1]) cylinder(r=11,h=2, center = true, $fn=20);
122 //shaft
123 %translate ([0, 0, shadow+21+3-21-7]) cylinder(r=2.5,h=14, center = true);
124 }
125 }
126
127 module screw(h=20, r=2, r_head=3.5, head_drop=0, slant=i_am_box, poly=false, $fn=0){
128 //makes screw with head
129 //for substraction as screw hole
130 if (poly) {
131 cylinder_poly(h=h, r=r, $fn=$fn);
132 } else {
133 cylinder(h=h, r=r, $fn=$fn);
134 }
135 if (slant) {
136 translate([0, 0, head_drop-0.01]) cylinder(h=r_head, r2=0, r1=r_head, $fn=$fn);
137 }
138
139 if (head_drop > 0) {
140 translate([0, 0, -0.01]) cylinder(h=head_drop+0.01, r=r_head, $fn=$fn);
141 }
142 }
143
144 module plate_screw(long=0) {
145 if (i_am_box == 0) {
146 translate([0, 0, -long]) screw(head_drop=14 + long, h=30 + long, r_head=3.6, r=1.7, $fn=24, slant=false);
147 } else {
148 translate([0, 0, -2 - long]) screw(head_drop=14 + long, h=30 + long, r_head=4.5, r=2, $fn=24, slant=true);
149 }
150 }
151
152 //radius of the idler assembly (to surface that touches belt, ignoring guide walls)
153 function idler_assy_r_inner(idler_bearing) = (idler_bearing[0] / 2) + 4 * single_wall_width * idler_bearing[3];
154 //radius of the idler assembly (to smooth side of belt)
155 function idler_assy_r_outer(idler_bearing) = (idler_bearing[0] / 2) + (idler_bearing[3] ? (5.5 * idler_bearing[3]) : 3);
156
157
158 module idler_assy(idler_bearing = [22, 7, 8, 1]) {
159
160 translate([0,0,-1]) cylinder(h = 120, r=(idler_bearing[2] + 1) / 2, $fn=small_hole_segments, center=true);
161 //bearing shadow
162 %cylinder(h = idler_bearing[1], r=idler_bearing[0]/2, center=true);
163
164 cylinder(h = idler_width + 1, r=idler_assy_r_outer(idler_bearing) + 0.5, center=true);
165 }
166
167 module belt(len, side = 0){
168 //belt. To be substracted from model
169 //len is in +Z, smooth side in +X, Y centered
170 translate([-0.5, 0, 0]) maketeeth(len);
171 translate([0, -4.5, -0.01]) cube([belt_thickness, 9, len + 0.02]);
172 if (side != 0) {
173 translate([0, -4.5 + side, -0.01]) cube_fillet([belt_thickness, 9, len + 0.02], vertical = [3, 0, 0, 0]);
174 }
175 }
176
177
178 module maketeeth(len){
179 //Belt teeth.
180 for (i = [0 : len / belt_tooth_distance]) {
181 translate([0, 0, i * belt_tooth_distance]) cube([2, 9, belt_tooth_distance * belt_tooth_ratio], center = true);
182 }
183 }