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