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