This repository was archived by the owner on Jul 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandWriting Code.txt
More file actions
410 lines (344 loc) · 9.54 KB
/
Copy pathHandWriting Code.txt
File metadata and controls
410 lines (344 loc) · 9.54 KB
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
#include <Servo.h>
Servo pen;
// Servo on PWM pin 15
const int penServoPin = 15;
const int penDelay = 150; //delay for servo to reach position
//servo angle for up and down
const int penUp = 90;
const int penDown = 125;
// defines pins numbers
const int xstepPin = 12;
const int xdirPin = 11;
const int ystepPin = 13;
const int ydirPin = 14;
const int Control = 24;
const int xlimit = 25;
const int ylimit = 26;
const float away = 1;
const float origin = 0;
int xDir, yDir;
// Motor steps to go 1 millimeter.
// Calculate steps per mm. Enter here.
const float StepsPerMillimeterX = 54.3478;
const float StepsPerMillimeterY = 54.3478;
//Makes 200 pulses for making one full cycle rotation - full-stepping
//if you need micro-stepping - 3200 pulses
const int stepsPerRevolution = 64;
// Drawing robot limits, in mm
const float Xmin = 0;
const float Xmax = 155;
const float Ymin = 0;
const float Ymax = 155;
//initialise the positions of x and y
float Xpos = Xmin;
float Ypos = Ymin;
char line[50]; //initialise an array to store received G codes
int lineIndex = 0;
char temp[15]; //temporary array to process G code
void xstepper_rev(int xdir)
{
//set the direction of rotation
digitalWrite(xdirPin, xdir);
//one pulse for the motor to take one step
digitalWrite(xstepPin, HIGH);
delayMicroseconds(800);
digitalWrite(xstepPin, LOW);
delayMicroseconds(800);
}
void ystepper_rev(int ydir)
{
//set the direction of rotation
digitalWrite(ydirPin, ydir);
//one pulse for the motor to take one step
digitalWrite(ystepPin, HIGH);
delayMicroseconds(800);
digitalWrite(ystepPin, LOW);
delayMicroseconds(800);
}
void not_writing()
{
pen.write(penUp); //pen up
delay(penDelay); //delay for theservo to reach position
Serial.println("pen is not writing");
}
void writing()
{
pen.write(penDown); //pen down
delay(penDelay); //delay for theservo to reach position
Serial.println("pen is writing now");
}
void setup()
{
//declare all outputs
pinMode(xstepPin, OUTPUT);
pinMode(xdirPin, OUTPUT);
pinMode(ystepPin, OUTPUT);
pinMode(ydirPin, OUTPUT);
//declare input for homing controls
pinMode(Control, INPUT);
pinMode(xlimit, INPUT);
pinMode(ylimit, INPUT);
//pullup input
digitalWrite(Control, HIGH);
//servo pin
pen.attach(penServoPin);
//initially set servo up
pen.write(penUp);
delay(penDelay);
Serial.begin( 9600 );
Serial.println("Mini CNC Plotter alive and kicking!");
Serial.print("X range is from ");
Serial.print(Xmin);
Serial.print(" to ");
Serial.print(Xmax);
Serial.println(" mm.");
Serial.print("Y range is from ");
Serial.print(Ymin);
Serial.print(" to ");
Serial.print(Ymax);
Serial.println(" mm.");
}
void loop()
{
//Manual homing of motors to (0,0)
if (digitalRead(Control) == LOW)
{
int c = digitalRead(xlimit);
int d = digitalRead(ylimit);
if ((c == 0) && (d == 0))
{
xstepper_rev(origin);
}
else if ((c == 0) && (d == 1))
{
xstepper_rev(away);
}
else if ((c == 1) && (d == 0))
{
ystepper_rev(origin);
}
else if ((c == 1) && (d == 1))
{
ystepper_rev(away);
}
}
//********** Handwriting **************
else if (digitalRead(Control) == HIGH)
{
// Serial reception
while ( Serial.available() > 0 ) //if data is received from the g code sender
{
char e = Serial.read(); //read the data
line[lineIndex] = e; //store the data in the array
//increment the index so that the next char is stored in the next position
lineIndex = lineIndex + 1;
//check if a new line or return catridge is received - indicates end of line
if ((e == '\n') || (e == '\r'))
{
line[lineIndex] = '\0'; //add a null character in th end to indicate end of char array
Serial.print("Received :");
Serial.println(line);
Serial.print("lineIndex =");
Serial.println(lineIndex);
//check if it is a g code or a M code
if ((line[0] == 'G') || (line[0] == 'M'))
{
//process the g code
gcode_processing(line);
//make the index to 0
lineIndex = 0;
//send "OK" to indicate that processing is done
//and the machine is ready to receive the next code
Serial.println("ok");
}
}
}
}
}
//**************** Processing Gcode**************
void gcode_processing(char* line)
{
//initialise the index to zero
int currentIndex = 0;
//temporary array
if (line[currentIndex] == 'G') // if it is a G code
{
//read the next two indices to check the code and copy it into temp array
temp[0] = line[currentIndex + 1];
temp[1] = line[currentIndex + 2];
//add null char to temp array
temp[2] = '\0';
Serial.print("G Command: ");
Serial.println(temp);
//convert the values of the temp array to integer
int code = atoi(temp);
Serial.print("code =");
Serial.println(code);
//check the code
if ((code == 1) || (code == 2) || (code == 3))
{
//if the code is 1 or 2 or 3
//find the position of X, Y, F in the array
char* xIndex = strchr(line, 'X');
char* yIndex = strchr(line, 'Y');
char* FIndex = strchr(line, 'F');
//convert the values after X,Y, F index to float to check the cordinates
float newXpos = atof(xIndex + 1);
float newYpos = atof(yIndex + 1);
float Fvalue = atof(FIndex + 1);
Serial.print("Fvalue =");
Serial.println(Fvalue);
Serial.print("xIndex =");
Serial.println(xIndex);
Serial.print("yIndex =");
Serial.println(yIndex);
Serial.print("newXpos, newYpos =");
Serial.print("(");
Serial.print(newXpos);
Serial.print(",");
Serial.print(newYpos);
Serial.println(")");
//if there is no F value,Draw
if (Fvalue == 0)
{
draw(newXpos, newYpos);
}
}
//else ingore the G code
else
{
//ignore other gcodes
}
}
// if it is a M code
else if (line[currentIndex] == 'M')
{
//read the next two indices to check the code and copy it into temp array
temp[0] = line[currentIndex + 1];
temp[1] = line[currentIndex + 2];
//add null char to temp array
temp[2] = '\0';
Serial.print("M Command: ");
Serial.println(temp);
//convert the values of the temp array to integer
int code = atoi(temp);
Serial.print("code =");
Serial.println(code);
//if the code is 3, the pen should write
if (code == 03)
{
writing();
}
//if the code is 5, the pen should not write
else
{
not_writing();
}
}
}
void draw(float x, float y)
{
Serial.println("entered draw");
if (x< Xmin)
{
x=Xmin;
}
if (x> Xmax)
{
x=Xmax;
}
if (y< Ymin)
{
y=Ymin;
}
if (y> Ymax)
{
y=Ymax;
}
//convert the millimeters into steps
x = float(x * StepsPerMillimeterX);
//convert the millimeters into steps
y = float(y * StepsPerMillimeterY);
//old positions -> initially zero
float x0 = Xpos;
float y0 = Ypos;
//initialize change
float ChangeInX = 0;
float ChangeInY = 0;
//if new x is greater than old x, move away from the origin
if (x > x0)
{
Serial.println("x>x0");
xDir = away;
Serial.print("X direction =");
Serial.println(xDir);
//calculate the change in x to find the number of steps
ChangeInX = x - x0;
}
else //if old x is greater than new x, move towards the origin
{
Serial.println("x0>x");
xDir = origin;
//calculate the change in x to find the number of steps
ChangeInX = x0 - x;
Serial.print("X direction =");
Serial.println(xDir);
}
//if new y is greater than old y, move away from the origin
if (y > y0)
{
Serial.println("y>y0");
yDir = away;
//calculate the change in y to find the number of steps
ChangeInY = y - y0;
Serial.print("Y direction =");
Serial.println(yDir);
}
else //if old y is greater than new y, move towards the origin
{
Serial.println("y0>y");
yDir = origin;
//calculate the change in y to find the number of steps
ChangeInY = y0 - y;
Serial.print("Y direction =");
Serial.println(yDir);
}
// print the number of steps
Serial.print("Change in X =");
Serial.println(ChangeInX);
Serial.print("Change in Y =");
Serial.println(ChangeInY);
//if the change in x is more than the change in Y then move xstepper first and theny stepper
if (ChangeInX >= ChangeInY)
{
for (float i = 0; i < ChangeInX; i++)
{
xstepper_rev(xDir);
ChangeInY ++;
if (ChangeInY > ChangeInX)
{
ChangeInY --;
ystepper_rev(yDir);
}
}
}
//if the change in Y is more than the change in X then move xstepper first and theny stepper
else if (ChangeInY > ChangeInX)
{
for (float i = 0; i < ChangeInY; i++)
{
ystepper_rev(yDir);
ChangeInX ++;
if (ChangeInX > ChangeInY)
{
ChangeInX --;
xstepper_rev(xDir);
}
}
}
Serial.println("Finished Drawing");
delay(150); //line delay
//set the current values to xpos and ypos to use for next calculations
Xpos = x;
Ypos = y;
}