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