-
Notifications
You must be signed in to change notification settings - Fork 18
/
CDMS.pde
685 lines (564 loc) · 16.9 KB
/
CDMS.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/* @pjs globalKeyEvents=true;
*/
// Simulation of Cycloid Drawing Machine - latest version is on https://github.com/jbum/CycloidDrawingMachine
//
// Physical machine designed by Joe Freedman kickstarter.com/projects/1765367532/cycloid-drawing-machine
// Processing simulation by Jim Bumgardner krazydad.com
//
static final float inchesToPoints = 72; // controls display scaling
static final float mmToInches = 1/25.4;
float seventyTwoScale = inchesToPoints / 72.0; // Don't change this
int[][] setupTeeth = {
{150,72},
{120,94,90,34},
{150,50,100,34,40},
{144, 100, 72},
{150, 98, 100},
{150, 100, 74},
{150,50,100,34,40,50,50},
};
float[][] setupMounts = { // mount point measurements
{0, 3.3838, 10.625},
{1.5, 4.4798, 10},
{0.8973, 1.5, 12},
{4, 4, 0.8, 2, 8.625},
{0.7, 2, 4, 8, 9},
{0.7, 3.3838, 4, 0.21, 12.75, 5.5, 5.25},
{2.5, 1.0, 14.0},
};
float[][] setupPens = {
{3.375,-55},
{4.5,90},
{7.5,-90},
{4.75,-65},
{4.5,-90},
{3.125,-65},
{6.5,-90},
};
Boolean[][] setupInversions = {
{true},
{false},
{false},
{false, false},
{false, false},
{false, false, false},
{false},
};
float bWidth = 18.14;
float bHeight = 11.51;
float pCenterX = 8.87;
float pCenterY = 6.61;
float toothRadius = 0.0956414*inchesToPoints;
float meshGap = 1.5*mmToInches*inchesToPoints; // 1.5 mm gap needed for meshing gears
PFont gFont, hFont, nFont;
PImage titlePic;
int setupMode = 0; // 0 = simple, 1 = moving pivot, 2 = orbiting gear, 3 = orbit gear + moving pivot
ArrayList<Gear> activeGears;
ArrayList<MountPoint> activeMountPoints;
ArrayList<Channel> rails;
ArrayList<ConnectingRod> activeConnectingRods;
Selectable selectedObject = null;
Gear crank, turnTable;
MountPoint slidePoint, anchorPoint, discPoint, penMount;
Channel crankRail, anchorRail, pivotRail;
ConnectingRod cRod;
PenRig penRig, selectPenRig = null;
PGraphics paper;
float paperScale = 1;
float paperWidth = 9*inchesToPoints*paperScale;
float crankSpeed = TWO_PI/720; // rotation per frame - 0.2 is nice.
int passesPerFrame = 1;
boolean hiresMode = false;
boolean isStarted = false;
boolean isMoving = false;
boolean penRaised = true;
float lastPX = -1, lastPY = -1;
int myFrameCount = 0;
int myLastFrame = -1;
int drawDirection = 1;
int recordCtr = 0;
color[] penColors = {color(0,0,0), color(192,0,0), color(0,128,0), color(0,0,128), color(192,0,192)};
color penColor = color(0,0,0);
int penColorIdx = 0;
float[] penWidths = {0.5, 1, 2, 3, 5, 7};
float penWidth = 1;
int penWidthIdx = 1;
int loadError = 0; // 1 = gears can't snug
void setup() {
// size(window.innerWidth, window.innerHeight);
size(1400, 828);
ellipseMode(RADIUS);
// mydebug("test");
gFont = createFont("Courier", int(32*seventyTwoScale));
hFont = createFont("Courier", int(18*seventyTwoScale));
nFont = createFont("Courier", int(11*seventyTwoScale)); // loadFont("Notch-Font.vlw");
titlePic = loadImage("title_dark.png");
gearInit();
activeGears = new ArrayList<Gear>();
activeMountPoints = new ArrayList<MountPoint>();
activeConnectingRods = new ArrayList<ConnectingRod>();
rails = new ArrayList<Channel>();
// Board Setup
paper = createGraphics(int(paperWidth), int(paperWidth));
discPoint = new MountPoint("DP", pCenterX, pCenterY);
rails.add(new LineRail(2.22, 10.21, .51, .6));
rails.add(new LineRail(3.1, 10.23, 3.1, .5));
rails.add(new LineRail(8.74, 2.41, 9.87, .47));
rails.add(new ArcRail(pCenterX, pCenterY, 6.54, radians(-68), radians(-5)));
rails.add(new ArcRail(8.91, 3.91, 7.79, radians(-25), radians(15)));
float[] rbegD = {
4.82, 4.96, 4.96, 4.96, 4.96, 4.96
};
float[] rendD = {
7.08, 6.94, 8.46, 7.70, 7.96, 8.48
};
float[] rang = {
radians(-120), radians(-60), radians(-40), radians(-20), 0, radians(20)
};
for (int i = 0; i < rbegD.length; ++i) {
float x1 = pCenterX + cos(rang[i])*rbegD[i];
float y1 = pCenterY + sin(rang[i])*rbegD[i];
float x2 = pCenterX + cos(rang[i])*rendD[i];
float y2 = pCenterY + sin(rang[i])*rendD[i];
rails.add(new LineRail(x1, y1, x2, y2));
}
setupButtons();
doLoadSetup();
drawingSetup(setupMode, true);
buttonFeedback();
}
Gear addGear(int setupIdx, String nom)
{
Gear g = new Gear(setupTeeth[setupMode][setupIdx], setupIdx, nom);
activeGears.add(g);
return g;
}
MountPoint addMP(int setupIdx, String nom, Channel chan)
{
MountPoint mp = new MountPoint(nom, chan, setupMounts[setupMode][setupIdx], setupIdx);
activeMountPoints.add(mp);
return mp;
}
ConnectingRod addCR(int rodNbr, MountPoint slide, MountPoint anchor)
{
ConnectingRod cr = new ConnectingRod(slide, anchor, rodNbr);
activeConnectingRods.add(cr);
return cr;
}
PenRig addPen(MountPoint penMount) {
return new PenRig(setupPens[setupMode][0], setupPens[setupMode][1], penMount);
}
void drawingSetup(int setupIdx, boolean resetPaper)
{
setupMode = setupIdx;
loadError = 0;
if (resetPaper) {
isStarted = false;
}
penRaised = true;
myFrameCount = 0;
activeGears = new ArrayList<Gear>();
activeMountPoints = new ArrayList<MountPoint>();
activeConnectingRods = new ArrayList<ConnectingRod>();
// Drawing Setup
switch (setupIdx) {
case 0: // simple set up with one gear for pen arm
turnTable = addGear(0,"Turntable");
crank = addGear(1,"Crank");
crankRail = rails.get(10);
pivotRail = rails.get(1);
crank.mount(crankRail,0);
turnTable.mount(discPoint, 0);
crank.snugTo(turnTable);
crank.meshTo(turnTable);
slidePoint = addMP(0, "SP", pivotRail);
anchorPoint = addMP(1, "AP", crank);
cRod = addCR(0, slidePoint, anchorPoint);
penMount = addMP(2, "EX", cRod);
penRig = addPen(penMount);
break;
case 1: // moving fulcrum & separate crank
turnTable = addGear(0,"Turntable");
crank = addGear(1,"Crank"); crank.contributesToCycle = false;
Gear anchor = addGear(2,"Anchor");
Gear fulcrumGear = addGear(3,"FulcrumGear");
crankRail = rails.get(1);
anchorRail = rails.get(10);
pivotRail = rails.get(0);
crank.mount(crankRail, 0); // will get fixed by snugto
anchor.mount(anchorRail,0);
fulcrumGear.mount(pivotRail, 0); // will get fixed by snugto
turnTable.mount(discPoint, 0);
crank.snugTo(turnTable);
anchor.snugTo(turnTable);
fulcrumGear.snugTo(crank);
crank.meshTo(turnTable);
anchor.meshTo(turnTable);
fulcrumGear.meshTo(crank);
slidePoint = addMP(0, "SP", fulcrumGear);
anchorPoint = addMP(1, "AP", anchor);
cRod = addCR(0, slidePoint, anchorPoint);
penMount = addMP(2, "EX", cRod);
penRig = addPen(penMount);
break;
case 2: // orbiting gear
crankRail = rails.get(9);
anchorRail = rails.get(4);
pivotRail = rails.get(1);
// Always need these...
turnTable = addGear(0,"Turntable");
crank = addGear(1,"Crank"); crank.contributesToCycle = false;
// These are optional
Gear anchorTable = addGear(2,"AnchorTable");
Gear anchorHub = addGear(3,"AnchorHub"); anchorHub.contributesToCycle = false;
Gear orbit = addGear(4,"Orbit");
orbit.isMoving = true;
// Setup gear relationships and mount points here...
crank.mount(crankRail, 0);
turnTable.mount(discPoint, 0);
crank.snugTo(turnTable);
crank.meshTo(turnTable);
anchorTable.mount(anchorRail, .315); // this is a hack - we need to allow the anchorTable to snug to the crank regardless of it's size...
anchorTable.snugTo(crank);
anchorTable.meshTo(crank);
anchorHub.stackTo(anchorTable);
anchorHub.isFixed = true;
orbit.mount(anchorTable,0);
orbit.snugTo(anchorHub);
orbit.meshTo(anchorHub);
// Setup Pen
slidePoint = addMP(0, "SP", pivotRail);
anchorPoint = addMP(1, "AP", orbit);
cRod = addCR(0, slidePoint, anchorPoint);
penMount = addMP(2, "EX", cRod);
penRig = addPen(penMount);
break;
case 3:// 2 pen rails, variation A
pivotRail = rails.get(1);
Channel aRail = rails.get(10);
Channel bRail = rails.get(7);
turnTable = addGear(0,"Turntable");
Gear aGear = addGear(1,"A");
Gear bGear = addGear(2,"B");
turnTable.mount(discPoint, 0);
aGear.mount(aRail, 0.5);
aGear.snugTo(turnTable);
aGear.meshTo(turnTable);
bGear.mount(bRail, 0.5);
bGear.snugTo(turnTable);
bGear.meshTo(turnTable);
slidePoint = addMP(0, "SP", aGear);
anchorPoint = addMP(1, "AP", bGear);
cRod = addCR(0, slidePoint, anchorPoint);
MountPoint slidePoint2 = addMP(2, "SP2", pivotRail);
MountPoint anchorPoint2 = addMP(3, "AP2", cRod);
ConnectingRod cRod2 = addCR(1, slidePoint2, anchorPoint2);
penMount = addMP(4,"EX",cRod2);
penRig = addPen(penMount);
break;
case 4: // 2 pen rails, variation B
pivotRail = rails.get(1);
aRail = rails.get(10);
bRail = rails.get(7);
turnTable = addGear(0,"TurnTable");
aGear = addGear(1,"A");
bGear = addGear(2,"B");
turnTable.mount(discPoint, 0);
aGear.mount(aRail, 0.5);
aGear.snugTo(turnTable);
aGear.meshTo(turnTable);
bGear.mount(bRail, 0.5);
bGear.snugTo(turnTable);
bGear.meshTo(turnTable);
slidePoint = addMP(0, "SP", pivotRail);
anchorPoint = addMP(1, "AP", bGear);
cRod = addCR(0, slidePoint, anchorPoint);
slidePoint2 = addMP(2, "SP2", aGear);
anchorPoint2 = addMP(3, "AP2", cRod);
cRod2 = addCR(1, anchorPoint2, slidePoint2);
penMount = addMP(4, "EX", cRod2);
penRig = addPen(penMount);
break;
case 5: // 3 pen rails
pivotRail = rails.get(1);
aRail = rails.get(10);
bRail = rails.get(7);
turnTable = addGear(0,"Turntable");
aGear = addGear(1,"A");
bGear = addGear(2,"B");
turnTable.mount(discPoint, 0);
aGear.mount(aRail, 0.5);
aGear.snugTo(turnTable);
aGear.meshTo(turnTable);
bGear.mount(bRail, 0.5);
bGear.snugTo(turnTable);
bGear.meshTo(turnTable);
slidePoint = addMP(0, "SP", pivotRail);
anchorPoint = addMP(1, "AP", bGear);
cRod = addCR(0, slidePoint, anchorPoint);
slidePoint2 = addMP(2, "SP2", aGear);
anchorPoint2 = addMP(3, "AP2", pivotRail);
cRod2 = addCR(1, slidePoint2, anchorPoint2);
MountPoint slidePoint3 = addMP(4, "SP3", cRod2);
MountPoint anchorPoint3 = addMP(5, "SA3", cRod);
ConnectingRod cRod3 = addCR(2, anchorPoint3, slidePoint3);
penMount = addMP(6, "EX", cRod3);
penRig = addPen(penMount);
break;
case 6: // orbiting gear with rotating fulcrum (#1 and #2 combined)
crankRail = rails.get(9);
anchorRail = rails.get(4);
// pivotRail = rails.get(1);
Channel fulcrumCrankRail = rails.get(1);
Channel fulcrumGearRail = rails.get(0);
// Always need these...
turnTable = addGear(0,"Turntable");
crank = addGear(1,"Crank"); crank.contributesToCycle = false;
// These are optional
anchorTable = addGear(2,"AnchorTable");
anchorHub = addGear(3,"AnchorHub"); anchorHub.contributesToCycle = false;
orbit = addGear(4,"Orbit");
Gear fulcrumCrank = addGear(5,"FulcrumCrank"); fulcrumCrank.contributesToCycle = false;
fulcrumGear = addGear(6,"FulcrumOrbit");
orbit.isMoving = true;
// Setup gear relationships and mount points here...
crank.mount(crankRail, 0);
turnTable.mount(discPoint, 0);
crank.snugTo(turnTable);
crank.meshTo(turnTable);
anchorTable.mount(anchorRail, .315);
anchorTable.snugTo(crank);
anchorTable.meshTo(crank);
anchorHub.stackTo(anchorTable);
anchorHub.isFixed = true;
orbit.mount(anchorTable,0);
orbit.snugTo(anchorHub);
orbit.meshTo(anchorHub);
fulcrumCrank.mount(fulcrumCrankRail, 0.735+.1);
fulcrumGear.mount(fulcrumGearRail, 0.29-.1);
fulcrumCrank.snugTo(turnTable);
fulcrumGear.snugTo(fulcrumCrank);
fulcrumCrank.meshTo(turnTable);
fulcrumGear.meshTo(fulcrumCrank);
// Setup Pen
slidePoint = addMP(0, "SP", fulcrumGear);
anchorPoint = addMP(1, "AP", orbit);
cRod = addCR(0, slidePoint, anchorPoint);
penMount = addMP(2, "EX", cRod);
penRig = addPen(penMount);
break;
}
turnTable.showMount = false;
}
void draw()
{
// Crank the machine a few times, based on current passesPerFrame - this generates new gear positions and drawing output
for (int p = 0; p < passesPerFrame; ++p) {
if (isMoving) {
myFrameCount += drawDirection;
turnTable.crank(myFrameCount*crankSpeed); // The turntable is always the root of the propulsion chain, since it is the only required gear.
// work out coords on unrotated paper
PVector nib = penRig.getPosition();
float dx = nib.x - pCenterX*inchesToPoints;
float dy = nib.y - pCenterY*inchesToPoints;
float a = atan2(dy, dx);
float l = sqrt(dx*dx + dy*dy);
float px = paperWidth/2 + cos(a-turnTable.rotation)*l*paperScale;
float py = paperWidth/2 + sin(a-turnTable.rotation)*l*paperScale;
paper.beginDraw();
if (!isStarted) {
// paper.clear();
paper.smooth(8);
paper.noFill();
paper.stroke(penColor);
paper.strokeJoin(ROUND);
paper.strokeCap(ROUND);
paper.strokeWeight(penWidth);
// paper.rect(10, 10, paperWidth-20, paperWidth-20);
isStarted = true;
} else if (!penRaised) {
paper.line(lastPX, lastPY, px, py);
}
paper.endDraw();
lastPX = px;
lastPY = py;
penRaised = false;
if (myLastFrame != -1 && myFrameCount >= myLastFrame) {
myLastFrame = -1;
passesPerFrame = 1;
isMoving = false;
buttonFeedback();
break;
}
}
}
// Draw the machine onscreen in it's current state
background(128);
pushMatrix();
image(titlePic, 0, height-titlePic.height);
drawFulcrumLabels();
fill(200);
noStroke();
float logoScale = inchesToPoints/72.0;
for (Channel ch : rails) {
ch.draw();
}
// discPoint.draw();
for (Gear g : activeGears) {
if (g != turnTable)
g.draw();
}
turnTable.draw(); // draw this last
penRig.draw();
pushMatrix();
translate(pCenterX*inchesToPoints, pCenterY*inchesToPoints);
rotate(turnTable.rotation);
image(paper, -paperWidth/(2*paperScale), -paperWidth/(2*paperScale), paperWidth/paperScale, paperWidth/paperScale);
popMatrix();
helpDraw(); // draw help if needed
popMatrix();
}
boolean isShifting = false;
void keyReleased() {
if (key == CODED) {
if (keyCode == SHIFT)
isShifting = false;
}
}
void keyPressed() {
switch (key) {
case ' ':
isMoving = !isMoving;
myLastFrame = -1;
// println("Current cycle length: " + myFrameCount / (TWO_PI/crankSpeed));
buttonFeedback();
break;
case '?':
toggleHelp();
break;
case '0':
isMoving = false;
passesPerFrame = 0;
myLastFrame = -1;
buttonFeedback();
// println("Current cycle length: " + myFrameCount / (TWO_PI/crankSpeed));
break;
case '1':
passesPerFrame = 1;
isMoving = true;
buttonFeedback();
break;
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
passesPerFrame = int(map((key-'0'),2,9,10,360));
isMoving = true;
buttonFeedback();
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
deselect();
drawingSetup(key - 'a', false);
doSaveSetup();
buttonFeedback();
break;
case 'X':
case 'x':
clearPaper();
break;
case 'p':
// Swap pen mounts - need visual feedback
break;
case '~':
case '`':
completeDrawing();
buttonFeedback();
break;
case '[':
advancePenColor(-1);
break;
case ']':
advancePenColor(1);
break;
case '<':
advancePenWidth(-1);
break;
case '>':
advancePenWidth(1);
break;
case '/':
invertConnectingRod();
break;
case '+':
case '-':
case '=':
int direction = (key == '+' || key == '='? 1 : -1);
nudge(direction, keyCode);
break;
case CODED:
case 65535:
default:
switch (keyCode) {
case UP:
case DOWN:
case LEFT:
case RIGHT:
direction = (keyCode == RIGHT || keyCode == UP? 1 : -1);
nudge(direction, keyCode);
break;
case SHIFT:
isShifting = true;
break;
default:
break;
}
break;
}
}
void mouseDragged()
{
drag();
}
void mouseReleased() {
isDragging = false;
}
void mousePressed()
{
deselect();
for (MountPoint mp : activeMountPoints) {
if (mp.isClicked(mouseX, mouseY)) {
mp.select();
selectedObject= mp;
return;
}
}
if (penRig.isClicked(mouseX, mouseY)) {
penRig.select();
selectedObject= penRig;
return;
}
for (ConnectingRod cr : activeConnectingRods) {
if (cr.isClicked(mouseX, mouseY)) {
cr.select();
selectedObject= cr;
return;
}
}
for (Gear g : activeGears) {
if (g.isClicked(mouseX, mouseY)) {
deselect();
g.select();
selectedObject = g;
}
}
}