-
Notifications
You must be signed in to change notification settings - Fork 18
/
2D-Processing.txt
221 lines (193 loc) · 6.75 KB
/
2D-Processing.txt
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
//import these libraries from libraries folder
import processing.serial.*;
import geomerative.*;
Serial arduino;
/*
* DIWire Bender
* 3D Wire Bender by Pensa - www.PensaNYC.com
* Written by Marco Perry. Call (718) 855 - 5354 for questions or support.
* Drives on 3 Stepper Motors to bender wire in 3D space
*
*/
/*the following program uploads a svg file, draws the 2D shape, allows user to set the print resolution,
calculates the feed and bend angles to print the 2D shape, and sends these values to the arduino*/
//Declaring the objects being used to print
RShape shp;
RPoint[] points;
int maxArray = 255;
float[] angles = new float [maxArray];
float[] feeds = new float [maxArray];
float res = 10; //default resolution
int interimSteps = 1; //a counter for the preview
int steps =1;
int lastpty = 0;
boolean chngRes = false;
boolean preview = false;
void setup() {
size (400, 400); //creates window
background (255);
println(Serial.list());
arduino = new Serial(this, Serial.list() [0], 9600); //sets arduino usb port
// VERY IMPORTANT: Allways initialize the library in the setup
RG.init(this);
shp = RG.loadShape("bendMe.svg"); //loads file from folder
steps = int(shp.getCurveLength()/res);
smooth();
frameRate(24);
println ();
println ("SET THE SYSTEM TO HOME POSITION");
println ("PRESS MOUSE BUTTON TO SET RESOLUTION");
println ("PRESS:");
println ("1. PROCESS SHAPE");
println ("2. PREVIEW SHAPE");
println ("3. SEND SHAPE TO ARDUINO");
println ();
println ("RESOLUTION IS CURRENTLY = " + res);
println ("NUMBER OF STEPS = " + steps);
}
void draw() {
translate(width/4, height/4); // Sets draw location and scale
noFill();
if (!preview) { //draws exact shape from file bendMe.svg
RG.setPolygonizer(RG.ADAPTATIVE);
stroke(0, 0, 200, 150);
shp.draw();
RG.setPolygonizer(RG.UNIFORMLENGTH);
if (chngRes) { //sets resolution if mouse is pressed
background(255);
float minDist = shp.getCurveLength()/maxArray; // minimum line distance(resolution) based on array size
res = map(float(mouseY), 0.0, float(height), minDist, 128); //resolution based on mouse location
shp.draw();
}
RG.setPolygonizerLength(res);
points = shp.getPoints(); //point resolution based on mouse location
// draw a shape of lines connecting the points
if (points != null) {
noFill();
stroke(0, 200, 0, 50);
strokeWeight (5);
beginShape();
for (int i=0; i<points.length; i++) {
vertex(points[i].x, points[i].y);
}
endShape();
strokeWeight (1);
fill(0);
stroke(0);
for (int i=0; i<points.length; i++) {
ellipse(points[i].x, points[i].y, 5, 5);
}
}
}
else { //draws preview shape based on set resolution
background (255);
stroke (random (255), random (255), random (255));
strokeWeight (5);
pushMatrix();
noFill();
beginShape();
for (int i=1; i<=steps+2; i++) { //preview bend by relative angles
line (0, 0, 0, feeds[i-1]);
translate (0, feeds [i-1]);
rotate (-radians(angles [i+1])); //bend
}
preview = !preview;
popMatrix ();
}
}
void mousePressed() { //mouse press sets resolution based on mouse location
println ("RESOLUTION SET TO = " + res + " mm");
steps = int(shp.getCurveLength()/res);
println ("NUMBER OF STEPS = " + steps);
chngRes = !chngRes;
}
void keyPressed() { //If keys are pressed run corresbonding subroutine
switch (key) {
case '1': //calculates bend angles and feed lengths
calcs();
break;
case '2': //shows a shape preview based on set resolution
preview = !preview;
break;
case '3': //sends bend angles and feed lengths to the arduino for printing
sendArd ();
break;
}
}
//this subroutine accepts feedback from the arduino. It is not necessary but sometimes needed to establish initial serial communication
/*void serialEvent (Serial p) {
String inString = arduino.readStringUntil ('\n');
if (inString !=null) {
println(inString);
}
}*/
void calcs () {
float lastAng=0;
float lastlastAng = 0;
println ( key );
println ();
println ( "Processing shape" );
println ();
println (points.length);
//calculate relative angles between points and assign to array angles[]
angles [0] = 0; // first angle is zero
for (int i = 1; i < points.length; i++) {
feeds[i-1] = dist (points[i-1].x, points[i-1].y, points[i].x, points[i].y); //feed legths are the distances between points
float deltaX = points[i].x - points[i-1].x; //change in x axis between points
float deltaY = points[i].y - points[i-1].y; //change in y axis between points
//the following equations calculate the bend angles between each point
//there are different equations for each coordinate quadrant
if ((deltaX >=0 && deltaY >=0)||(deltaX <=0 && deltaY >=0)) {
angles[i] =(degrees(atan(deltaX/deltaY)) - (lastAng + lastlastAng));
}
if (deltaX <= 0 && deltaY < 0) {
angles[i] = (180 + abs(degrees(atan(deltaX/deltaY)))) - (lastAng + lastlastAng);
}
if (deltaX >0 && deltaY <0) {
angles[i] = (180 - abs(degrees(atan(deltaX/deltaY)))) - (lastAng + lastlastAng);
}
if (angles[i]>720) {
angles[i] = angles[i]-360;
}
if (angles[i]<-180) {
angles[i] = 360+angles[i];
}
if (angles[i]>180) {
angles[i] = angles[i]-360;
}
//shows matrix of feeds and bend angles
println (round(1*(feeds[i-1]))+"\t"+round(angles[i])+"\t"+"0"); //change the 1 before feeds to change scale. z bend remains 0 because this is a 2D shape
if (abs(angles [i]) > 125) { //beacuse we are sending bytes to the arduino board no angle or feed length can exceed 125
println ("WARNING - ANGLE TOO BIG");
}
lastlastAng = lastAng+lastlastAng; //sets variables to know last 2 bend angles needed to calculate bends
lastAng = angles[i];
}
}
void sendArd () { //sends bend angles and feed lengths to the arduino board for printing
byte feedmotor = 126; //these byte values are markers so the arduino knows if a value is a bend, feed, or end it's the end of the array
byte bendmotor = 125;
byte end = 127;
println ( key );
println ();
println ( "Sending to Arduino" );
println ( "Printing" );
println ();
//send commands
for (int i = 2; i < steps+233; i++) {
//sends arduino feed marker followed by feed length
delay (100);
arduino.write (feedmotor);
delay (100);
arduino.write (byte(round (feeds[i-2])));
delay (100);
println (feeds[i-2]);
//sends arduino bend marker followed by bend angle
arduino.write (bendmotor);
delay (100);
arduino.write (byte(round (-angles[i])));
println (angles[i]);
}
println (end+128); //tells arduino the array of values in complete
arduino.write(end);
}