chiark / gitweb /
d3915992fa8763eb622fab48166a06a4db5b38a5
[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_x = 22;
38 cutout_l_end_y = 85;
39 cutout_l_end_curve = 1;
40 cutout_l_end_y_total = cutout_l_end_y
41   + cutout_l_end_y_front_slop + cutout_l_end_y_rear_slop;
42
43 tile02_tr = [  0, 0];
44 tile01_tr = [250, 0];
45
46 cutout_tile01_y = 170 - 147 + cutout_l_end_y_front_slop;
47 cutout_tile11_x = cutout_l_end_x + cutout_l_end_curve;
48 cutout_tile11_y = cutout_l_end_y_total - cutout_tile01_y;
49
50 // rear curve
51
52 rearedge_len = 170;
53
54 rearcurve_rad = 25.4;
55 rearcurve_strt_len = 65;
56 rearcurve_total_len = 84;
57
58 rearcurve_rad_slop = 0.75;
59
60 rearcurve_avoid_y = rearcurve_rad + 10;
61
62 reartablet_z = 2.54;
63 reartablet_x = 5 + 1;
64 reartablet_y = 5;
65
66 // calculated
67
68 TEST = false;
69
70 ply_edge_hole_dist = ply_edge_min + ply_hole_dia/2;
71
72 echo(str("HOLES IN PLY ctr dist from PLY edge = ", ply_edge_hole_dist));
73
74 hole_slop = (ply_hole_dia - post_dia)/2;
75 tile_hard_edge_hole_dist = ply_edge_hole_dist + hole_slop;
76
77 echo(str("HOLES IN PLY ctr dist from TILE HARD edge = ",
78          tile_hard_edge_hole_dist));
79
80 echo(str("HOLES IN PLY ctr dist from TILE ROUND edge = ",
81          tile_hard_edge_hole_dist + round_edge_rad));
82
83 thehd = [ tile_hard_edge_hole_dist, tile_hard_edge_hole_dist ];
84 thehd_tr = thehd;
85 thehd_tl = [ -thehd_tr[0], thehd_tr[1] ];
86 thehd_bl = -thehd_tr;
87 thehd_br = -thehd_tl;
88
89 interlock_rad = interlock_dia/2;
90 interlock_negative_rad = interlock_rad + 0.125;
91
92 interlock_sq_adj = 0.2; // arbitrary
93
94 module Post(){
95   mirror([0,0,1]) {
96     difference(){
97       cylinder(r= post_dia/2, h= tile_th + ply_th - post_shorter);
98       translate([0,0, tile_th]) {
99         cylinder(r= screw_big_dia/2, h= screw_big_len);
100         cylinder(r= screw_dia/2, h= ply_th, $fn=20);
101       }
102     }
103     if (TEST) {
104       tsz = tile_hard_edge_hole_dist - test_edge + 1;
105       translate([0,0, tile_th/2]) {
106         cube([post_dia, tsz*2, tile_th], center=true);
107         cube([tsz*2, post_dia, tile_th], center=true);
108       }
109     }
110   }
111 }
112
113 module Posts(posts) {
114   for (p= posts) {
115     translate(concat(p, [0]))
116       Post();
117   }
118 }
119
120 module TileBase(botleft, topright){
121   size = topright - botleft;
122   botleft_post = botleft + thehd_tr;
123   topright_post = topright + thehd_bl;
124   difference(){
125     mirror([0,0,1])
126       translate(concat(botleft, [0]))
127       cube(concat(size, [tile_th]));
128     if (!TEST) {
129       translate( concat(botleft_post, [ -tile_th ])
130                  + 0.5 * [ post_dia, post_dia, 0 ] )
131         Commitid_BestCount_M( topright_post-botleft_post
132                               + [-post_dia,-post_dia]
133                               + [0, thehd[1]]);
134     }
135     if (TEST) {
136       translate( concat(botleft + [thehd[0], 0], [0]) )
137         Commitid_BestCount([ size[0] - thehd[0]*2, thehd[1] ]);
138       difference(){
139         mirror([0,0,1]) {
140           translate(concat(botleft + [test_edge,test_edge], [test_tile_th]))
141             cube(concat(size - [test_edge,test_edge]*2, [tile_th]));
142           translate(concat(botleft_post, [-1]))
143             cube(concat(topright_post-botleft_post, [tile_th+2]));
144         }
145         minkowski(){
146           Machine();
147           cube(max(test_edge, tile_hard_edge_hole_dist)*2, center=true);
148         }
149       }
150     }
151   }
152 }
153
154 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars) { body; }
155 m4_define(`INREFFRAME',`
156   length_vec = ($2) - ($1);
157   length = dist2d([0,0], length_vec);
158   length_uvec = length_vec / length;
159   ortho_uvec = [ -length_uvec[1], length_uvec[0] ];
160   m = [ [ length_uvec[0],  ortho_uvec[0], 0, ($1)[0], ],
161         [ length_uvec[1],  ortho_uvec[1], 0, ($1)[1], ],
162         [ 0,              0,              1,            0, ],
163         [ 0,              0,              0,            1, ] ];
164   $3
165   multmatrix(m)
166 ')
167
168 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars)
169 m4_dnl    INREFFRAME_EDGE { body; }
170 m4_define(`INREFFRAME_EDGE',`
171   translate([0,0, -round_edge_rad])
172 ')
173
174 module RoundEdge(left_cnr, right_cnr) {
175   INREFFRAME(left_cnr, right_cnr)
176     INREFFRAME_EDGE {
177     difference(){
178       rotate([0,90,0])
179         cylinder(r= round_edge_rad, h= length, $fn=50);
180       translate([-1, 0, -20])
181         cube([length+2, 20, 20]);
182     }
183   }
184 }
185
186 module RoundCornerCut(ci) {
187   // ci should be [this_cnr, right_cnr]
188   // where right_cnr is to the right (ie, anticlockwise)
189   this_cnr = ci[0];
190   right_cnr = ci[1];
191   offr= round_cnr_rad - round_edge_rad;
192   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
193     difference(){
194       cube(offr*2 - 0.1, center=true);
195       translate([offr, offr, 0])
196         cylinder(center=true, h=20, r= offr);
197     }
198   }
199 }
200
201 module RoundCornerAdd(ci) {
202   this_cnr = ci[0];
203   right_cnr = ci[1];
204   bigr = round_cnr_rad - round_edge_rad;
205   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
206     intersection(){
207       cube(bigr*2 + 0.1, center=true);
208       translate([bigr, bigr, 0])
209         rotate_extrude(convexity=10, $fn=50)
210         translate([bigr, 0])
211         difference(){
212           circle(r= round_edge_rad, $fn=50);
213           mirror([1,1])
214             square([20,20]);
215         }
216     }
217   }
218 }
219
220 module InterlockLobePlan(negative) {
221   r = negative ? interlock_negative_rad : interlock_rad;
222   ymir = negative ? 0 : 1;
223
224   dx = sqrt(3) * r;
225   $fn= 80;
226   translate([thehd[0], 0]){
227     mirror([0,ymir]){
228       circle(r=r);
229       difference(){
230         translate([-dx, -0.1])
231           square([ dx*2, r/2 + 0.1 ]);
232         for (xi = [-1, 1]) {
233           translate([ xi*dx, r ])
234             circle(r=r);
235         }
236       }
237     }
238   }
239 }
240
241 module InterlockEdgePlan(negative, nlobes, length, dosquare=true) {
242   for (lobei = [ 0 : nlobes-1 ]) {
243     lobex = (length - thehd[0]*2) * (lobei ? lobei / (nlobes-1) : 0);
244     translate([lobex, 0, 0]) {
245       InterlockLobePlan(negative);
246     }
247   }
248
249   if (dosquare) {
250     iadj = 0;
251     slotshorter = negative ? -0.1 : interlock_fine_lenslop;
252     mirror([0, negative])
253       translate([slotshorter, iadj])
254       square([length - slotshorter*2, interlock_fine + iadj*2]);
255   }
256 }
257
258 module InterlockEdge(left_cnr, right_cnr, negative=0, nlobes=2) {
259   plusth = negative * 1.0;
260   protr = interlock_fine + interlock_sq_adj;
261
262   z2 = -tile_th/2;
263   z1 = -tile_th/2 - protr / interlock_fine_slope;
264   z3 = -tile_th/2 + protr / interlock_fine_slope;
265
266   negsign = negative ? -1 : +1;
267   yprotr = negsign * protr;
268
269   INREFFRAME(left_cnr, right_cnr) {
270     for (vsect = [ // zs0            zs1      ys0,            ys1
271                   [ -tile_th-plusth, plusth,  0,              0],
272                   [ z1,              z2,      0, yprotr],
273                   [ z2,              z3,      yprotr, 0],
274                   ]) {
275       zs0 = vsect[0];
276       zs1 = vsect[1];
277       zsd = zs1-zs0;
278       ys0 = vsect[2];
279       ys1 = vsect[3];
280       ysd = ys1-ys0;
281       sl = ysd/zsd;
282       m = [ [ 1,0,   0,    0 ],
283             [ 0,1, -sl, -ys0 + negsign*interlock_sq_adj ],
284             [ 0,0,   1,  zs0 ],
285             [ 0,0,   0,    1 ] ];
286       multmatrix(m)
287         linear_extrude(height=zsd, convexity=10)
288         InterlockEdgePlan(negative, nlobes, length, !!ysd);
289     }
290   }
291 }
292
293 function TestPiece_holes2corners(holes) =
294   [ holes[0] + thehd_bl,
295     holes[1] + thehd_br,
296     holes[1] + thehd_tr,
297     holes[0] + thehd_tl ];
298
299 module TestPiece1(){ ////toplevel
300   holes = [ [-100, 0],
301             [   0, 0]
302             ];
303   corners = TestPiece_holes2corners(holes);
304   rcs = [corners[0], corners[1]];
305   difference(){
306     union(){
307       TileBase(corners[0], corners[2]);
308       Posts(holes);
309       RoundEdge(corners[0], corners[1]);
310       RoundEdge(corners[3], corners[0]);
311     }
312     InterlockEdge(corners[1], corners[2], 1, nlobes=1);
313     RoundCornerCut(rcs);
314   }
315   RoundCornerAdd(rcs);
316 }
317
318 module TestPiece2(){ ////toplevel
319   holes = [ [   0, 0],
320             [  50, 0]
321             ];
322   corners = TestPiece_holes2corners(holes);
323   TileBase(corners[0], corners[2]);
324   Posts(holes);
325   RoundEdge(corners[0], corners[1]);
326   InterlockEdge(corners[3], corners[0], 0, nlobes=1);
327 }
328
329 module TestDemo(){ ////toplevel
330   translate([ -thehd[0], 0 ])
331     color("blue")
332     TestPiece1();
333   translate([ +thehd[0] + demo_slop, 0 ])
334     TestPiece2();
335 }
336
337 module Machine_Arm(){
338   ysz = cutout_l_end_y_total;
339   // assume the round end is arc of a circle
340   chordlen = dist2d([0,0], [ cutout_l_end_y, cutout_l_end_curve ]);
341   endrad = cutout_l_end_y / cutout_l_end_curve * chordlen;
342   
343   translate([0,0,-30]) linear_extrude(height=60) {
344     translate(tile01_tr + [0, (-cutout_tile01_y + cutout_tile11_y)/2]) {
345       intersection(){
346         translate([-100, -ysz/2])
347           square([400, ysz]);
348         translate([ endrad - cutout_tile11_x - cutout_l_end_x_slop, 0 ])
349           circle(r=endrad, $fa=0.01,$fd=5);
350       }
351     }
352   }
353 }
354
355 module Machine_Rear(){ ////toplevel
356   big_rad = rearcurve_total_len - rearcurve_strt_len + rearcurve_rad;
357   small_rad = rearcurve_rad + rearcurve_rad_slop;
358   translate([ tile01_tr[0] - cutout_l_end_x + rearedge_len,
359               cutout_tile11_y,
360               0 ]){
361     //%cube([20,20,20]);
362     translate([ -reartablet_x,
363                 -1,
364                 -reartablet_z])
365       mirror([0,0,1])
366       cube([ reartablet_x+1,
367              reartablet_y+1,
368              20 ]);
369     translate([ rearcurve_strt_len,
370                 0,
371                 0 ]){
372       translate([ 0,
373                   0,
374                   -rearcurve_rad
375                   ]){
376         rotate([0,-90,0]){
377           rotate([0,0, 360/8/2])
378             cylinder(r = small_rad, h= rearcurve_strt_len);
379         }
380       }
381       translate([ 0,
382                   0,
383                   big_rad - rearcurve_rad ]) {
384         intersection(){
385           rotate([90,0,0]){
386             rotate_extrude(convexity=10) {
387               translate([ big_rad,
388                           0 ]) {
389                 hull(){
390                   circle(r= small_rad);
391                   translate([200,0])
392                     circle(r= small_rad);
393                 }
394               }
395             }
396           }
397           mirror([0,0,1])
398             cube([150,150,150]);
399         }
400       }
401     }
402   }
403 }
404
405 module Machine(){ ////toplevel
406   Machine_Arm();
407   Machine_Rear();
408 }
409   
410 function Rectangle_corners(c0, sz) =
411   // returns the corners of a rectangle from c0 to c0+sz
412   // if sz is positive, the corners are anticlockwise starting with c0
413   [ c0 + [ 0,     0     ],
414     c0 + [ sz[0], 0     ],
415     c0 + [ sz[0], sz[1] ],
416     c0 + [ 0,     sz[1] ] ];
417
418 function Rectangle_corners2posts(c) =
419   [ c[0] + thehd_tr,
420     c[1] + thehd_tl,
421     c[2] + thehd_bl,
422     c[3] + thehd_br ];
423
424 module Rectangle_TileBase(c) { TileBase(c[0], c[2]); }
425
426 function Posts_interpolate_one(c0,c1) = [c0, (c0+c1)/2, c1];
427
428 m4_dnl   R_EDGE(c,ix)
429 m4_dnl        c is from Rectangle_corners and
430 m4_dnl        ix is a corner number
431 m4_dnl    expands to two comma-separated corners:
432 m4_dnl    that denoted by ix, and the next one anticlockwise
433 m4_define(`R_EDGE',`$1[$2],$1[(($2)+1)%4]')
434
435 module Tile02(){ ////toplevel
436   sz = [100,170];
437   c0 = tile02_tr + -sz;
438   c = Rectangle_corners(c0, sz);
439   posts = Rectangle_corners2posts(c);
440   rcs = [R_EDGE(c,0)];
441   difference(){
442     union(){
443       Rectangle_TileBase(c);
444       Posts(posts);
445       RoundEdge(R_EDGE(c,0));
446       RoundEdge(R_EDGE(c,3));
447       InterlockEdge(R_EDGE(c,2), 0);
448     }
449     InterlockEdge(R_EDGE(c,1), 1);
450     RoundCornerCut(rcs);
451   }
452   RoundCornerAdd(rcs);
453 }
454
455 module Tile12(){ ////toplevel
456   sz = [100,250];
457   c0 = tile02_tr + [-sz[0], 0];
458   c = Rectangle_corners(c0, sz);
459   posts = Rectangle_corners2posts(c);
460   rcs = [R_EDGE(c,3)];
461   difference(){
462     union(){
463       Rectangle_TileBase(c);
464       Posts(posts);
465       RoundEdge(R_EDGE(c,2));
466       RoundEdge(R_EDGE(c,3));
467     }
468     InterlockEdge(R_EDGE(c,0), 1);
469     InterlockEdge(R_EDGE(c,1), 1);
470     RoundCornerCut(rcs);
471   }
472   RoundCornerAdd(rcs);
473 }
474
475 tile_01_11_cnr = tile01_tr + [-cutout_tile11_x, 0];
476 tile_11_10_cnr = tile01_tr + [0, cutout_tile11_y];
477
478 module Tile11(){ ////toplevel
479   sz = [250,250];
480   c0 = tile02_tr + [0,0];
481   c = Rectangle_corners(c0, sz);
482   cnr_posts = Rectangle_corners2posts(c);
483   posts = concat(
484                  Posts_interpolate_one(cnr_posts[0],
485                                        cnr_posts[1] - [cutout_tile11_x, 0]),
486                  [ cnr_posts[1] + [0, cutout_tile11_y],
487                    cnr_posts[2],
488                    cnr_posts[3]
489                    ]);
490   difference(){
491     union(){
492       Rectangle_TileBase(c);
493       Posts(posts);
494       RoundEdge(R_EDGE(c,2));
495       InterlockEdge(R_EDGE(c,3));
496     }
497     InterlockEdge(c[0], tile_01_11_cnr, 1);
498     InterlockEdge(tile_11_10_cnr, c[2], 1);
499     Machine();
500   }
501 }    
502
503 tile_01_00_cnr = tile01_tr + [0, -cutout_tile01_y];
504
505 module Tile01(){ ////toplevel
506   sz = [250,170];
507   c0 = tile02_tr + [0,-sz[1]];
508   c = Rectangle_corners(c0, sz);
509   cnr_posts = Rectangle_corners2posts(c);
510   posts = concat(
511                  Posts_interpolate_one(R_EDGE(cnr_posts,0)),
512                  [ cnr_posts[2] + [0, -cutout_tile01_y] ],
513                  Posts_interpolate_one(cnr_posts[2] - [cutout_tile11_x, 0],
514                                        cnr_posts[3])
515                  );
516   difference(){
517     union(){
518       Rectangle_TileBase(c);
519       Posts(posts);
520       RoundEdge(R_EDGE(c,0));
521       InterlockEdge(tile_01_11_cnr, c[3]);
522       InterlockEdge(R_EDGE(c,3));
523     }
524     InterlockEdge(c[1], tile_01_00_cnr, 1);
525     Machine();
526   }
527 }    
528
529 module Tile10(){ ////toplevel
530   sz = [250,250];
531   c0 = tile02_tr + [sz[0],0];
532   c = Rectangle_corners(c0, sz);
533   cnr_posts = Rectangle_corners2posts(c);
534   cty = cutout_tile11_y;
535   rcy = cty + rearcurve_avoid_y;
536   posts = [ cnr_posts[0] + [ 0,                             cty ],
537             cnr_posts[1] + [ -sz[1] + rearedge_len - cutout_l_end_x, cty ],
538             cnr_posts[1] + [ 0,                             rcy ],
539             cnr_posts[2],
540             cnr_posts[3] ];
541   rcs = [R_EDGE(c,2)];
542   difference(){
543     union(){
544       Rectangle_TileBase(c);
545       Posts(posts);
546       RoundEdge(R_EDGE(c,1));
547       RoundEdge(R_EDGE(c,2));
548       InterlockEdge(c[3], tile_11_10_cnr);
549     }
550     RoundCornerCut(rcs);
551     Machine();
552   }
553   RoundCornerAdd(rcs);
554 }
555
556 module Demo(){ ////toplevel
557   translate(demo_slop*[-2,1]) color("blue") Tile12();
558   translate(demo_slop*[-2,0]) color("red")  Tile02();
559   translate(demo_slop*[-2,1]) color("orange") Tile11();
560   translate(demo_slop*[-2,0]) color("purple") Tile01();
561   translate(demo_slop*[-3,1]) color("blue")   Tile10();
562   %Machine();
563 }
564   
565 //TestPiece1();
566 //TestPiece2();
567 //Demo();