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