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