-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPlot2.pde
369 lines (277 loc) · 7.87 KB
/
Plot2.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
import processing.serial.*;
class Plot2 {
PApplet parent;
// variables for serial connection to EiBotBoard
Serial myPort;
int portnum; // Create object from Serial class
int val; // Data received from the serial port
int deltax=0;
int deltay=0;
boolean pen;
float oldx=0;
float oldy=0;
float checksumx=0;
float checksumy=0;
boolean refill;
float posx=0;
float decal=10;
float posy=0;
float drawspeed=1;
float seekspeed=2;
//test write output flags
boolean writeToConsole = false; //true will write only to console, not to EiBotBoard
PMatrix matrix;
Plot2(PApplet parenting) {
matrix = parenting.getMatrix();
// setup serial port
parent=parenting;
}
//used to convert between machine coordinates and paper coordinates
void openport(int portnum){
try {
String portName = Serial.list()[portnum];
println(portName);
myPort = new Serial(parent,portName, 9600);
//serial device found
println("found: " + portName);
println("Plotter Online");
println("ready to roll!");
connected=true;
if (!writeToConsole) myPort.write( "SC,4,10000 \r");
if (!writeToConsole) myPort.write( "SC,5," + 18000 + "\r");
// sendCommand( "SC,10," + min(_servoRateUp,_servoRateDown) + "\r");
if (!writeToConsole) myPort.write( "SC,11," +2000+ "\r");
if (!writeToConsole) myPort.write( "SC,12," + 2000+ "\r");
String outConsole = ("SM,"+500+","+0+","+(-500)+"\r"); //SP,<value>,<duration> : <value> is 0 (for up) or 1 (for down)
if (!writeToConsole) myPort.write(outConsole);
penUp();
}
catch (Exception e) {
//no serial device found
println("Didn't find Plotter");
println(e);
writeToConsole = true;
}
}
void goingto(float x, float y, float intensity) {
float[] out = new float[2];
float[] vec = new float[2];
vec[0] = x;
vec[1] = y;
getMatrix().mult(vec, out);
x=out[0];
y=out[1];
int pasx=int((x-oldx)*80.6);
int pasy=int((y-oldy)*45);
// println(checksumx/50.0-x);
// println(checksumy/25.0-y);
if (pen==true) {
deltax+=abs(pasx);
deltay+=abs(pasy);
}
println("Go to x "+pasx+" y "+pasy);
float distance=sqrt((pow(pasx, 2)+pow(pasy*2, 2)));
if (distance!=0) {
float vitesse=drawspeed;
if (pen==false ) {
vitesse=seekspeed;
}
if (vitesse>10 ) {
vitesse=10;
}
if (intensity!=0 ) {
vitesse=intensity;
}
int towait=int(distance/vitesse)+10;
// println(towait);
String outConsole = ("SM,"+towait+","+inverty*pasy+","+invertx*pasx+"\r"); //SP,<value>,<duration> : <value> is 0 (for up) or 1 (for down)
if (!writeToConsole) myPort.write(outConsole);
delay(int(towait));
}
oldx=oldx+float(pasx)/80.6;
oldy=oldy+float(pasy)/45;
if (deltax+deltay>10000) {
deltax=0;
deltay=0;
refill=true;
}
posx=x;
posy=y;
}
void goingtoabsolute(float x, float y, float intensity) {
x=x;
y=y;
int pasx=int((x-oldx)*80.6);
int pasy=int((y-oldy)*45);
checksumx+=pasx;
checksumy=pasy;
// println(checksumx/50.0-x);
// println(checksumy/25.0-y);
if (pen==true) {
deltax+=abs(pasx);
deltay+=abs(pasy);
}
println("Go to x "+pasx+" y "+pasy);
float distance=sqrt((pow(pasx, 2)+pow(pasy*2, 2)));
if (distance!=0) {
float vitesse=seekspeed;
if (vitesse>10 ) {
vitesse=10;
}
if (intensity!=0 ) {
vitesse=intensity;
}
int towait=int(distance/vitesse)+10;
// println(towait);
String outConsole = ("SM,"+towait+","+inverty*pasy+","+(invertx*pasx)+"\r"); //SP,<value>,<duration> : <value> is 0 (for up) or 1 (for down)
if (!writeToConsole) myPort.write(outConsole);
delay(int(towait));
}
oldx=oldx+float(pasx)/80.6;
oldy=oldy+float(pasy)/45;
if (deltax+deltay>10000) {
deltax=0;
deltay=0;
refill=true;
}
posx=x;
posy=y;
}
void gogetink(float ysec, float y, float xsec) {
goingto(0, y, 0);
penDown();
delay(500);
penUp();
goingto(xsec, ysec, 0);
}
void penDown() {
//pen down
pen=true;
println("pen down");
String outConsole = ("SP,1,100\r"); //SP,<value>,<duration> : <value> is 0 (for up) or 1 (for down)
if (!writeToConsole) myPort.write(outConsole);
delay(200);
}
void penUp() {
//pen up
pen=false;
println("pen up");
String outConsole = ("SP,0,100\r"); //SP,<value>,<duration> : <value> is 0 (for up) or 1 (for down)
if (!writeToConsole) myPort.write(outConsole);
delay(200);
}
void Toggle() {
if(pen==true){
penUp();
}else{
penDown();}
}
void shutDown() {
String outConsole = ("EM,0,0\r");
if (!writeToConsole) myPort.write(outConsole);
println("shut down");
}
void goHome() {
goingto(0, 0, 0);
}
void gotofirstpoint(float x, float y) {
// println("goy"+posy+"aim"+y);
// println("gox"+posx+"aim"+x);
float dist=sqrt(pow(x-posx, 2)+pow(y-posy, 2));
if (dist>0) {
penUp();
goingto(x, y, 0);
penDown();
}
if (pen==false) {
penDown();
}
}
void line(float x, float y, float xaim, float yaim) {
gotofirstpoint(x, y);
goingto(xaim, yaim, 0);
}
void point(float y, float x) {
gotofirstpoint(x, y);
}
void ellipse(float x, float y, float widthell, float heightell, int def) {
gotofirstpoint(x+widthell/2.0, y);
for (int i=0; i<=def;i++) {
goingto(x+ widthell/2.0*cos(i/float(def)*2*PI), y+heightell/2.0*sin(i/float(def)*2*PI), 0);
fill(255, 0, 0);
}
}
void rect(float y, float x, float widthrect, float heightrect) {
gotofirstpoint(x, y);
goingto(x+widthrect, y, 0);
goingto(x+widthrect, y+heightrect, 0);
goingto(x, y+heightrect, 0);
goingto(x, y, 0);
}
void setDrawspeed(float speed) {
drawspeed=speed;
println("drawspeed "+speed);
}
void setSeekspeed(float speed) {
seekspeed=speed;
println("seekspeed "+speed);
}
void onquit() {
penUp();
goingtoabsolute(0, 0, 0);
String outConsole = ("SM,"+500+","+0+","+(1600)+"\r"); //SP,<value>,<duration> : <value> is 0 (for up) or 1 (for down)
if (!writeToConsole) myPort.write(outConsole);
shutDown();
myPort.clear();
// Close the port
myPort.stop();
}
void coordrez(){
oldx=0;
oldy=0;
}
void moveX(float x) {
int pasx=int((x-oldx)*80.6);
println("Go to x "+pasx);
float distance=abs(pasx);
if (distance!=0) {
float vitesse=drawspeed;
if (pen==false ) {
vitesse=seekspeed;
}
if (vitesse>10 ) {
vitesse=10;
}
int towait=int(distance/vitesse)+10;
// println(towait);
String outConsole = ("SM,"+towait+","+0+","+invertx*pasx+"\r"); //SP,<value>,<duration> : <value> is 0 (for up) or 1 (for down)
if (!writeToConsole) myPort.write(outConsole);
delay(int(towait));
}
oldx=oldx+float(pasx)/80.6;
}
void moveY(float y) {
int pasy=int((y-oldy)*45);
println("Go to y "+pasy);
float distance=abs(pasy);
if (distance!=0) {
float vitesse=drawspeed;
if (pen==false ) {
vitesse=seekspeed;
}
if (vitesse>10 ) {
vitesse=10;
}
int towait=int(distance/vitesse)+10;
// println(towait);
String outConsole = ("SM,"+towait+","+inverty*pasy+","+0+"\r"); //SP,<value>,<duration> : <value> is 0 (for up) or 1 (for down)
if (!writeToConsole) myPort.write(outConsole);
delay(int(towait));
}
oldy=oldy+float(pasy)/45;
}
}
void serialEvent(Serial p) {
String inString = p.readString();
// println("received: " + inString, 10,50);
}