-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxy-plotter.ino
552 lines (480 loc) · 11.9 KB
/
xy-plotter.ino
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
/*
3200 steps = 2cm
1600 steps = 1cm
160 steps = 1mm
*/
#define DEFAULT_STEPS_PER_MM 160
#define DEBUG
#include "TimerOne.h"
#include "helpers.h"
#include "sin_table.h"
#include "workitem.h"
// Stepper 1 = z-axis = Motor 1 on shield
// See zAxis.ino
// Stepper 2 = x-axis = Motor 2 on shield
#define enPin1 5
#define stepPin1 6
#define dirPin1 7
// Stepper 3 = y-axis = motor 3 on shield
#define enPin2 11
#define stepPin2 12
#define dirPin2 13
// Servo
#define SERVO_PIN PIN_A1 // Logical pin , Servo port on motor 4 of Fabscan shield, pin 55 on mega
#define PEN_UP 10
#define PEN_DOWN 70
// LED pin
#define LED_PIN PIN_A3
// Tasks
#define TASK_MOVE 1
#define TASK_PEN_UP 2
#define TASK_PEN_DOWN 3
// bit mask is ....YYXX where x and y have swapped
// bits, as the direction setting is different for them.
#define X_LEFT_HIT 1
#define X_RIGHT_HIT 2
#define Y_RIGHT_HIT 4
#define Y_LEFT_HIT 8
workItem *workItems = new workItem[370];
int currentItem = 0;
#define END_MARKER { -1, -1, -1 , TASK_MOVE}
int stepsPerMM = DEFAULT_STEPS_PER_MM;
String command;
long stepCount;
int motor;
long stepsDone = 0 ;
boolean done;
long e2, err;
int pathPointer;
boolean continuousMode = false;
boolean compensating = false;
boolean homing = false;
int tokenCount;
args a; // for z-Axis
boolean dryRun = false;
volatile boolean xHit = false;
volatile boolean yHit = false;
volatile unsigned char hitmask = 0x0;
volatile char hitMsg = '\0';
boolean verbose = false;
boolean penIsUp;
char dirPins[] = {7, 13};
char stepPins[] = {6, 12};
/*
* One time setup routine called from the Arduino framework
*/
void setup() {
Serial.begin(115200);
// setup Motor 2 (x-axis)
pinMode(enPin1, OUTPUT);
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
digitalWrite(enPin1, LOW);
// setup Motor 3 (z-axis)
pinMode(enPin2, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
digitalWrite(enPin2, LOW);
// setupZAxis();
digitalWrite(dirPin1, HIGH);
digitalWrite(enPin1, HIGH); // Disable stepper
digitalWrite(dirPin2, HIGH);
digitalWrite(enPin2, HIGH); // Disable stepper
// Interrupt if the pin goes to GND
for (int i = 18 ; i <= 21 ; i++) {
pinMode(i, INPUT_PULLUP);
}
// TODO move Panic button to other pin as it clashes
// with stepper motor 1 (z-Axis)
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), panic, CHANGE);
// Attach to servo and raise pen
// TODO rewrite with z-Stepper
pinMode(SERVO_PIN, OUTPUT);
servoMove(SERVO_PIN, PEN_UP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.println();
Serial.flush();
Serial.println(F("OK Setup done"));
Serial.flush();
}
/*
* Interrupt callback for the 'panic' button.
* Stops the activity of the motors
*/
void panic() {
Timer1.stop();
println("STOPPED");
done = true;
xHit = true;
yHit = true;
continuousMode = false;
}
long dx, dy;
/*
* Does the work and is driven from the
* timer interrrupts.
* This function is called each time the Timer1
* fires.
* See #startWork() which initialises motors+interrupts.
*/
void oneStep() {
boolean doX, doY;
if (xHit || yHit) {
println(F("D One step, hit a switch"));
servoMove(SERVO_PIN, PEN_UP);
Timer1.stop();
Timer1.detachInterrupt();
// delete[] workItems;
done = true;
return;
}
workItem wItem = workItems[currentItem];
if (wItem.task == TASK_PEN_UP) {
servoMove(SERVO_PIN, PEN_UP);
currentItem++;
return;
}
if (wItem.task == TASK_PEN_DOWN) {
servoMove(SERVO_PIN, PEN_DOWN);
currentItem++;
return;
}
// From here on: TASK_MOVE
// wItem.steps <= 0 means END_MARKER, so we can stop with this
// set of work items
if (wItem.steps <= 0 ) {
Timer1.stop();
Timer1.detachInterrupt();
done = true;
return;
}
// No end maker? Then let's move
if (stepsDone == 0) {
if (verbose) {
printWorkItem(wItem);
}
setDirection(wItem);
dx = abs(wItem.x);
dy = -abs(wItem.y);
err = dx + dy;
}
if (!dryRun) {
// Bessenham algorithm from wikipedia
e2 = 2 * err;
if (e2 > dy && !xHit) {
digitalWrite(stepPins[0], HIGH);
err += dy;
doX = true;
}
if (e2 < dx && !yHit) {
digitalWrite(stepPins[1], HIGH);
err += dx;
doY = true;
}
delayMicroseconds(30);
if (doX) {
digitalWrite(stepPins[0], LOW);
}
if (doY) {
digitalWrite(stepPins[1], LOW);
}
}
stepsDone++;
if (stepsDone > abs(wItem.steps)) {
currentItem++;
stepsDone = 0;
}
}
void interruptOnX1() {
disableXInterrupts();
xHit = true;
hitMsg = 'A';
hitmask |= X_LEFT_HIT;
}
void interruptOnX2() {
disableXInterrupts();
xHit = true;
hitMsg = 'B';
hitmask |= X_RIGHT_HIT;
}
void interruptOnY1() {
disableYInterrupts();
yHit = true;
hitMsg = 'C';
hitmask |= Y_LEFT_HIT;
}
void interruptOnY2() {
disableYInterrupts();
yHit = true;
hitMsg = ('D');
hitmask |= Y_RIGHT_HIT;
}
void startWork() {
currentItem = 0;
xHit = yHit = false;
stepsDone = 0;
if (!dryRun) {
enableMotors();
enableEndSwitches();
}
done = false;
Serial.println("D Starting...");
Serial.flush();
// Start interrupts. Values are in micro-seconds
if (dryRun) {
Timer1.initialize(100); // No motor movement, so we can speed up
} else {
Timer1.initialize(200);
}
Timer1.attachInterrupt(oneStep);
}
/*
* Main loop as defined by Arduino. This
* basically reads input commands over serial
* and executes them in a row.
*/
void loop() {
if (Serial.available() > 0) {
command = Serial.readStringUntil('\n');
Serial.print("D ");
Serial.println(command.c_str());
Serial.flush();
if (continuousMode) {
// we read line by line from serial input and feed it to parser+worker
// until the line starts with -END
if (command.startsWith("-END")) {
done = true;
continuousMode = false;
dryRun = false;
Serial.println("D END found, continuous mode off");
Serial.print("D Tokens processed: ");
Serial.println(tokenCount, DEC);
Serial.flush();
}
else {
pathPointer = 0;
String path = preParse();
parsePath(path);
startWork();
}
}
else {
// we get commands on the command line - possibly with parameters
char c = command.charAt(0);
switch (c) {
case 'E':
enableMotors();
break;
case 'D':
disableMotors();
break;
case 's':
{
String in = command.substring(1);
in.replace('\n', '|');
pathPointer = 1; // comand[0] is 's', path starts at 1
String path = preParse();
parsePath(path);
startWork();
}
break;
case 'Q':
// Emergency shutdown for manual operation
panic();
break;
case 'C': {
continuousMode = true;
Serial.print("OK Continuous mode is on");
if (command.length() > 2) {
dryRun = true;
Serial.print(", dryRun is on ");
}
Serial.println();
Serial.flush();
break;
}
case 'H': {
// Home, requires interrupts and end-switches
println("D Homing");
homing = true;
String path = "X-300 Y-300";
parsePath(path);
startWork();
}
break;
case 'u': {
// Pen up
// penServo.write(PEN_UP);
servoMove(SERVO_PIN, PEN_UP);
Serial.println("OK pen up");
Serial.flush();
}
break;
case 'd': {
// Pen down
// penServo.write(PEN_DOWN);
servoMove(SERVO_PIN, PEN_DOWN);
Serial.println("OK pen down");
Serial.flush();
}
break;
case 'v': {
int val = command.substring(1).toInt();
servoMove(SERVO_PIN, val);
break;
}
case 'i':
enableEndSwitches();
break;
case 'V':
verbose = !verbose;
Serial.print(F("OK Verbose is "));
Serial.println(verbose ? "on" : "off");
Serial.flush();
break;
case 'R':
dryRun = !dryRun;
Serial.print(F("OK DryRun is "));
Serial.println(dryRun ? "on" : "off");
Serial.flush();
break;
case 'I': {
disableXInterrupts();
disableYInterrupts();
Serial.println("Interrupts detached");
Serial.flush();
break;
}
case 'r': { // resolution
int val = command.substring(1).toInt();
int tmp = stepsPerMM;
stepsPerMM = val;
Serial.print("OK set steps perMM to ");
Serial.print(val);
Serial.print(" previous value was ");
Serial.println(tmp);
Serial.flush();
break;
}
case 'z': {
args arg = optarg(command);
handleZ(arg);
break;
}
case 'a':
// Fall through intentional
case 'A':
handleArc(c,command);
break;
default: {
Serial.print("E Unknown command >>");
Serial.print(c);
Serial.println("<<");
Serial.flush();
}
} // switch
} // if (!continuous mode)
} // if serial
if (done) {
done = false;
boolean oldX = xHit;
boolean oldY = yHit;
println("D .. done reached ..");
if (xHit || yHit) {
// determine which was hit and then move into the
// opposite direction until we hit again
println("D.. x or y hit, compensating");
delay(5);
if (xHit) {
xHit = false;
compensate_move(0);
}
if (yHit) {
yHit = false;
compensate_move(1);
}
println("D.. compensating done");
pathPointer = 0; // Ignore remainder of input
if (homing) {
delay(5);
println("Homing now");
if (oldX) {
println(" on Y");
parsePath("Y-200");
startWork();
}
if (oldY) {
println(" on X ");
parsePath("X-200");
startWork();
}
homing = false;
}
} // xHit || yHit
// Works with the T or C command where the
// command string is very long, but we can only
// do so much at a time.
if (pathPointer > 1) {
String path = preParse();
Serial.println(path);
parsePath(path);
startWork();
}
else {
if (!continuousMode) {
Serial.println("D DONE, disabling motors");
disableMotors();
}
Serial.println("OK");
Serial.flush();
}
} // if done
if (xHit || yHit) { // TODO
if (hitMsg != '\0') {
Serial.print("D ");
Serial.println(hitMsg);
Serial.flush();
hitMsg = '\0';
}
}
} // loop()
void compensate_move(int motor) {
Serial.print("D motor = ");
Serial.print(motor, DEC);
Serial.print(" mask = ");
Serial.print(hitmask, BIN);
println(" Start crawling");
unsigned char mask = hitmask;
if (motor == 1) {
mask >>= 2;
}
mask &= 3;
if (mask & 1) {
digitalWrite(dirPins[motor], HIGH);
}
else {
digitalWrite(dirPins[motor], LOW);
}
delayMicroseconds(150);
if (motor == 1) {
enableYInterrupts(CHANGE);
} else {
enableXInterrupts(CHANGE);
}
delay(1);
// Now we move until the switch is released, which is signalled by an interrupt.
while (!xHit && !yHit) {
digitalWrite(stepPins[motor], HIGH);
delayMicroseconds(30);
digitalWrite(stepPins[motor], LOW);
delayMicroseconds(150);
}
println("D End crawling");
}
args optarg (String text) {
a.code = text.charAt(1);
a.value = text.substring(2).toInt();
return a;
}