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