-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbioprinter_arduino.ino
186 lines (157 loc) · 3.14 KB
/
bioprinter_arduino.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
#include <stdio.h>
const int X_STEPS_PER_MM = 10;
const int Y_STEPS_PER_MM = 10;
const int BAUD = 19200;
const int BUF_SIZE = 64;
char cmdBuf[BUF_SIZE];
int cmdLoc = 0;
// Current location of print head
float px, py;
// Syncronously moves the head to the new coords
void moveTo(float nx, float ny) {
Serial.print(F("Starting move to "));
Serial.print(nx);
Serial.print(F(","));
Serial.print(ny);
Serial.print(F("\r\n"));
// Linear interp
int dx = (nx - px) * X_STEPS_PER_MM;
int dirX = dx>0?1:-1;
int dy = (ny - py) * Y_STEPS_PER_MM;
int dirY = dy>0?1:-1;
dx = abs(dx);
dy = abs(dy);
int over = 0;
if(dx > dy) {
for(int i = 0; i < dx; i++) {
Serial.print("X");
over += dy;
if(over > dx) {
over -= dx;
Serial.print("Y");
}
}
}
else {
for(int i = 0; i < dy; i++) {
Serial.print("Y");
over += dx;
if(over > dy) {
over -= dy;
Serial.print("X");
}
}
}
px += float(dx) / X_STEPS_PER_MM * dirX;
py += float(dy) / Y_STEPS_PER_MM * dirY;
Serial.print(F("\r\nEnding at "));
Serial.print(px);
Serial.print(",");
Serial.print(py);
Serial.print("\r\n");
}
void spray(int head, int val) {
Serial.print(F("Spray "));
Serial.print(head);
Serial.print(F("Val "));
Serial.print(val);
Serial.print(F("\r\n"));
}
char * consumeWhitespace(char * buf) {
while(*buf != '\0' && (*buf == ' ' || *buf == '\t')) {
++buf;
}
return buf;
}
void processG1(char * buf) {
float nx;
float ny;
char * nbuf = strtok(buf, "XY");
nx = atof(nbuf);
nbuf = strtok(0, "XY");
ny = atof(nbuf);
moveTo(nx, ny);
}
void processGCommand(char * buf, int id) {
switch(id) {
case 0:
case 1:
// G0 and G1 are both the same here
processG1(buf);
break;
default:
error();
break;
}
}
void processM700(char *buf) {
char * b = strtok(buf, "PS");
int head = atoi(b);
b = strtok(0, "PS");
int val = atoi(b);
spray(head, val);
}
void processMCommand(char * buf, int id) {
switch(id) {
case 400:
//Finish movement command from image_to_gcod, since
//we are syncronous here, ignore it
break;
case 700:
//spray
processM700(buf);
break;
default:
error();
break;
}
}
void processCommand() {
char c;
int id, consumed;
sscanf(cmdBuf, "%c%d%n", &c, &id, &consumed);
char * args = consumeWhitespace(cmdBuf+consumed);
switch(c) {
case 'G':
processGCommand(args, id);
break;
case 'M':
processMCommand(args, id);
break;
default:
error();
}
}
// Tell host we are ready for another command
void ready() {
cmdLoc = 0;
Serial.print(F("\r\n>"));
}
void error() {
Serial.print(F("\r\nError bad command: "));
Serial.print(cmdBuf);
}
void setup() {
Serial.begin(BAUD);
px = 0;
py = 0;
ready();
}
void loop() {
while(Serial.available() > 0) {
char c = Serial.read();
Serial.print(c);
if(cmdLoc<BUF_SIZE) {
cmdBuf[cmdLoc++] = c;
}
if(cmdBuf[cmdLoc-1] ==';') {
Serial.print("\r\n");
break;
}
}
if(cmdLoc > 0 && cmdBuf[cmdLoc-1]==';') {
cmdBuf[cmdLoc] = 0;
processCommand();
ready();
}
}