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