chiark / gitweb /
sewing-table: Tile11 seems good!
[reprap-play.git] / sewing-table.scad.m4
1 // -*- C -*-
2
3 include <funcs.scad>
4 include <commitid.scad>
5
6 ply_th = 18;
7 ply_hole_dia = 15;
8 ply_edge_min = 10;
9
10 tile_th = 3;
11 post_dia = 8;
12
13 post_shorter = 1;
14
15 screw_dia = 2.2;
16 screw_big_dia = 3.6;
17 screw_big_len = 4.0;
18
19 round_edge_rad = 2.0;
20
21 round_cnr_rad = 10;
22
23 interlock_dia = 10;
24 interlock_fine = 0.66;
25
26 interlock_fine_slope = 1.0;
27 interlock_fine_lenslop = 1.0;
28
29 demo_slop = 0.1;
30
31 // cutout
32
33 cutout_l_end_y_front_slop = 0.5;
34 cutout_l_end_y_rear_slop = 0.5;
35 cutout_l_end_x_slop = 0.25;
36
37 cutout_l_end_y = 85;
38 cutout_l_end_curve = 1;
39 cutout_l_end_y_total = cutout_l_end_y
40   + cutout_l_end_y_front_slop + cutout_l_end_y_rear_slop;
41
42 tile01_tr = [250, 0];
43
44 cutout_tile01_y = 170 - 147 + cutout_l_end_y_front_slop;
45 cutout_tile11_x = 22 + cutout_l_end_curve;
46 cutout_tile11_y = cutout_l_end_y_total - cutout_tile01_y;
47
48 // calculated
49
50 TEST = false;
51
52 ply_edge_hole_dist = ply_edge_min + ply_hole_dia/2;
53
54 echo(str("HOLES IN PLY ctr dist from PLY edge = ", ply_edge_hole_dist));
55
56 hole_slop = (ply_hole_dia - post_dia)/2;
57 tile_hard_edge_hole_dist = ply_edge_hole_dist + hole_slop;
58
59 echo(str("HOLES IN PLY ctr dist from TILE HARD edge = ",
60          tile_hard_edge_hole_dist));
61
62 echo(str("HOLES IN PLY ctr dist from TILE ROUND edge = ",
63          tile_hard_edge_hole_dist + round_edge_rad));
64
65 thehd = [ tile_hard_edge_hole_dist, tile_hard_edge_hole_dist ];
66 thehd_tr = thehd;
67 thehd_tl = [ -thehd_tr[0], thehd_tr[1] ];
68 thehd_bl = -thehd_tr;
69 thehd_br = -thehd_tl;
70
71 interlock_rad = interlock_dia/2;
72 interlock_negative_rad = interlock_rad + 0.125;
73
74 interlock_sq_adj = 0.2; // arbitrary
75
76 module Post(){
77   mirror([0,0,1]) {
78     difference(){
79       cylinder(r= post_dia/2, h= tile_th + ply_th - post_shorter);
80       translate([0,0, tile_th]) {
81         cylinder(r= screw_big_dia/2, h= screw_big_len);
82         cylinder(r= screw_dia/2, h= ply_th, $fn=20);
83       }
84     }
85     if (TEST) {
86       tsz = tile_hard_edge_hole_dist - test_edge + 1;
87       translate([0,0, tile_th/2]) {
88         cube([post_dia, tsz*2, tile_th], center=true);
89         cube([tsz*2, post_dia, tile_th], center=true);
90       }
91     }
92   }
93 }
94
95 module Posts(posts) {
96   for (p= posts) {
97     translate(concat(p, [0]))
98       Post();
99   }
100 }
101
102 module TileBase(botleft, topright){
103   size = topright - botleft;
104   botleft_post = botleft + thehd_tr;
105   topright_post = topright + thehd_bl;
106   difference(){
107     mirror([0,0,1])
108       translate(concat(botleft, [0]))
109       cube(concat(size, [tile_th]));
110     if (!TEST) {
111       translate( concat(botleft_post, [ -tile_th ])
112                  + 0.5 * [ post_dia, post_dia, 0 ] )
113         Commitid_BestCount_M( topright_post-botleft_post
114                               + [-post_dia,-post_dia]
115                               + [0, thehd[1]]);
116     }
117     if (TEST) {
118       translate( concat(botleft + [thehd[0], 0], [0]) )
119         Commitid_BestCount([ size[0] - thehd[0]*2, thehd[1] ]);
120       difference(){
121         mirror([0,0,1]) {
122           translate(concat(botleft + [test_edge,test_edge], [test_tile_th]))
123             cube(concat(size - [test_edge,test_edge]*2, [tile_th]));
124           translate(concat(botleft_post, [-1]))
125             cube(concat(topright_post-botleft_post, [tile_th+2]));
126         }
127         minkowski(){
128           Machine();
129           cube(max(test_edge, tile_hard_edge_hole_dist)*2, center=true);
130         }
131       }
132     }
133   }
134 }
135
136 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars) { body; }
137 m4_define(`INREFFRAME',`
138   length_vec = ($2) - ($1);
139   length = dist2d([0,0], length_vec);
140   length_uvec = length_vec / length;
141   ortho_uvec = [ -length_uvec[1], length_uvec[0] ];
142   m = [ [ length_uvec[0],  ortho_uvec[0], 0, ($1)[0], ],
143         [ length_uvec[1],  ortho_uvec[1], 0, ($1)[1], ],
144         [ 0,              0,              1,            0, ],
145         [ 0,              0,              0,            1, ] ];
146   $3
147   multmatrix(m)
148 ')
149
150 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars)
151 m4_dnl    INREFFRAME_EDGE { body; }
152 m4_define(`INREFFRAME_EDGE',`
153   translate([0,0, -round_edge_rad])
154 ')
155
156 module RoundEdge(left_cnr, right_cnr) {
157   INREFFRAME(left_cnr, right_cnr)
158     INREFFRAME_EDGE {
159     difference(){
160       rotate([0,90,0])
161         cylinder(r= round_edge_rad, h= length, $fn=50);
162       translate([-1, 0, -20])
163         cube([length+2, 20, 20]);
164     }
165   }
166 }
167
168 module RoundCornerCut(ci) {
169   // ci should be [this_cnr, right_cnr]
170   // where right_cnr is to the right (ie, anticlockwise)
171   this_cnr = ci[0];
172   right_cnr = ci[1];
173   offr= round_cnr_rad - round_edge_rad;
174   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
175     difference(){
176       cube(offr*2 - 0.1, center=true);
177       translate([offr, offr, 0])
178         cylinder(center=true, h=20, r= offr);
179     }
180   }
181 }
182
183 module RoundCornerAdd(ci) {
184   this_cnr = ci[0];
185   right_cnr = ci[1];
186   bigr = round_cnr_rad - round_edge_rad;
187   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
188     intersection(){
189       cube(bigr*2 + 0.1, center=true);
190       translate([bigr, bigr, 0])
191         rotate_extrude(convexity=10, $fn=50)
192         translate([bigr, 0])
193         difference(){
194           circle(r= round_edge_rad, $fn=50);
195           mirror([1,1])
196             square([20,20]);
197         }
198     }
199   }
200 }
201
202 module InterlockLobePlan(negative) {
203   r = negative ? interlock_negative_rad : interlock_rad;
204   ymir = negative ? 0 : 1;
205
206   dx = sqrt(3) * r;
207   $fn= 80;
208   translate([thehd[0], 0]){
209     mirror([0,ymir]){
210       circle(r=r);
211       difference(){
212         translate([-dx, -0.1])
213           square([ dx*2, r/2 + 0.1 ]);
214         for (xi = [-1, 1]) {
215           translate([ xi*dx, r ])
216             circle(r=r);
217         }
218       }
219     }
220   }
221 }
222
223 module InterlockEdgePlan(negative, nlobes, length, dosquare=true) {
224   for (lobei = [ 0 : nlobes-1 ]) {
225     lobex = (length - thehd[0]*2) * (lobei ? lobei / (nlobes-1) : 0);
226     translate([lobex, 0, 0]) {
227       InterlockLobePlan(negative);
228     }
229   }
230
231   if (dosquare) {
232     iadj = 0;
233     slotshorter = negative ? -0.1 : interlock_fine_lenslop;
234     mirror([0, negative])
235       translate([slotshorter, iadj])
236       square([length - slotshorter*2, interlock_fine + iadj*2]);
237   }
238 }
239
240 module InterlockEdge(left_cnr, right_cnr, negative=0, nlobes=2) {
241   plusth = negative * 1.0;
242   protr = interlock_fine + interlock_sq_adj;
243
244   z2 = -tile_th/2;
245   z1 = -tile_th/2 - protr / interlock_fine_slope;
246   z3 = -tile_th/2 + protr / interlock_fine_slope;
247
248   negsign = negative ? -1 : +1;
249   yprotr = negsign * protr;
250
251   INREFFRAME(left_cnr, right_cnr) {
252     for (vsect = [ // zs0            zs1      ys0,            ys1
253                   [ -tile_th-plusth, plusth,  0,              0],
254                   [ z1,              z2,      0, yprotr],
255                   [ z2,              z3,      yprotr, 0],
256                   ]) {
257       zs0 = vsect[0];
258       zs1 = vsect[1];
259       zsd = zs1-zs0;
260       ys0 = vsect[2];
261       ys1 = vsect[3];
262       ysd = ys1-ys0;
263       sl = ysd/zsd;
264       m = [ [ 1,0,   0,    0 ],
265             [ 0,1, -sl, -ys0 + negsign*interlock_sq_adj ],
266             [ 0,0,   1,  zs0 ],
267             [ 0,0,   0,    1 ] ];
268       multmatrix(m)
269         linear_extrude(height=zsd, convexity=10)
270         InterlockEdgePlan(negative, nlobes, length, !!ysd);
271     }
272   }
273 }
274
275 function TestPiece_holes2corners(holes) =
276   [ holes[0] + thehd_bl,
277     holes[1] + thehd_br,
278     holes[1] + thehd_tr,
279     holes[0] + thehd_tl ];
280
281 module TestPiece1(){ ////toplevel
282   holes = [ [-100, 0],
283             [   0, 0]
284             ];
285   corners = TestPiece_holes2corners(holes);
286   rcs = [corners[0], corners[1]];
287   difference(){
288     union(){
289       TileBase(corners[0], corners[2]);
290       Posts(holes);
291       RoundEdge(corners[0], corners[1]);
292       RoundEdge(corners[3], corners[0]);
293     }
294     InterlockEdge(corners[1], corners[2], 1, nlobes=1);
295     RoundCornerCut(rcs);
296   }
297   RoundCornerAdd(rcs);
298 }
299
300 module TestPiece2(){ ////toplevel
301   holes = [ [   0, 0],
302             [  50, 0]
303             ];
304   corners = TestPiece_holes2corners(holes);
305   TileBase(corners[0], corners[2]);
306   Posts(holes);
307   RoundEdge(corners[0], corners[1]);
308   InterlockEdge(corners[3], corners[0], 0, nlobes=1);
309 }
310
311 module TestDemo(){ ////toplevel
312   translate([ -thehd[0], 0 ])
313     color("blue")
314     TestPiece1();
315   translate([ +thehd[0] + demo_slop, 0 ])
316     TestPiece2();
317 }
318
319 module Machine_Arm(){
320   ysz = cutout_l_end_y_total;
321   // assume the round end is arc of a circle
322   chordlen = dist2d([0,0], [ cutout_l_end_y, cutout_l_end_curve ]);
323   endrad = cutout_l_end_y / cutout_l_end_curve * chordlen;
324   
325   translate([0,0,-30]) linear_extrude(height=60) {
326     translate(tile01_tr + [0, (-cutout_tile01_y + cutout_tile11_y)/2]) {
327       intersection(){
328         translate([-100, -ysz/2])
329           square([400, ysz]);
330         translate([ endrad - cutout_tile11_x - cutout_l_end_x_slop, 0 ])
331           circle(r=endrad, $fa=0.01,$fd=5);
332       }
333     }
334   }
335 }
336
337 module Machine(){
338   Machine_Arm();
339 }
340   
341 function Rectangle_corners(c0, sz) =
342   // returns the corners of a rectangle from c0 to c0+sz
343   // if sz is positive, the corners are anticlockwise starting with c0
344   [ c0 + [ 0,     0     ],
345     c0 + [ sz[0], 0     ],
346     c0 + [ sz[0], sz[1] ],
347     c0 + [ 0,     sz[1] ] ];
348
349 function Rectangle_corners2posts(c) =
350   [ c[0] + thehd_tr,
351     c[1] + thehd_tl,
352     c[2] + thehd_bl,
353     c[3] + thehd_br ];
354
355 module Rectangle_TileBase(c) { TileBase(c[0], c[2]); }
356
357 function Posts_interpolate_one(c0,c1) = [c0, (c0+c1)/2, c1];
358
359 m4_dnl   R_EDGE(c,ix)
360 m4_dnl        c is from Rectangle_corners and
361 m4_dnl        ix is a corner number
362 m4_dnl    expands to two comma-separated corners:
363 m4_dnl    that denoted by ix, and the next one anticlockwise
364 m4_define(`R_EDGE',`$1[$2],$1[(($2)+1)%4]')
365
366 module Tile02(){ ////toplevel
367   sz = [100,170];
368   c0 = -sz;
369   c = Rectangle_corners(c0, sz);
370   posts = Rectangle_corners2posts(c);
371   rcs = [R_EDGE(c,0)];
372   difference(){
373     union(){
374       Rectangle_TileBase(c);
375       Posts(posts);
376       RoundEdge(R_EDGE(c,0));
377       RoundEdge(R_EDGE(c,3));
378       InterlockEdge(R_EDGE(c,2), 0);
379     }
380     InterlockEdge(R_EDGE(c,1), 1);
381     RoundCornerCut(rcs);
382   }
383   RoundCornerAdd(rcs);
384 }
385
386 module Tile12(){ ////toplevel
387   sz = [100,250];
388   c0 = [-sz[0], 0];
389   c = Rectangle_corners(c0, sz);
390   posts = Rectangle_corners2posts(c);
391   rcs = [R_EDGE(c,3)];
392   difference(){
393     union(){
394       Rectangle_TileBase(c);
395       Posts(posts);
396       RoundEdge(R_EDGE(c,2));
397       RoundEdge(R_EDGE(c,3));
398     }
399     InterlockEdge(R_EDGE(c,0), 1);
400     InterlockEdge(R_EDGE(c,1), 1);
401     RoundCornerCut(rcs);
402   }
403   RoundCornerAdd(rcs);
404 }
405
406 tile_01_11_cnr = [250, 0] + [-cutout_tile11_x, 0];
407 tile_11_10_cnr = [250, 0] + [0, cutout_tile11_y];
408
409 module Tile11(){ ////toplevel
410   sz = [250,250];
411   c0 = [0,0];
412   c = Rectangle_corners(c0, sz);
413   cnr_posts = Rectangle_corners2posts(c);
414   posts = concat(
415                  Posts_interpolate_one(cnr_posts[0],
416                                        cnr_posts[1] - [cutout_tile11_x, 0]),
417                  [ cnr_posts[1] + [0, cutout_tile11_y],
418                    cnr_posts[2],
419                    cnr_posts[3]
420                    ]);
421   difference(){
422     union(){
423       Rectangle_TileBase(c);
424       Posts(posts);
425       RoundEdge(R_EDGE(c,2));
426       InterlockEdge(R_EDGE(c,3));
427     }
428     Machine();
429     InterlockEdge(c[0], tile_01_11_cnr, 1);
430     InterlockEdge(tile_11_10_cnr, c[2], 1);
431   }
432 }    
433
434 module Demo(){ ////toplevel
435   translate(demo_slop*[-2,1]) color("blue") Tile12();
436   translate(demo_slop*[-2,0]) color("red")  Tile02();
437   translate(demo_slop*[-2,1]) color("orange") Tile11();
438   %Machine();
439 }
440   
441 //TestPiece1();
442 //TestPiece2();
443 //Demo();