-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.c
More file actions
454 lines (416 loc) · 11.3 KB
/
plotter.c
File metadata and controls
454 lines (416 loc) · 11.3 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
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
#include "plotter.h"
#include "commands.h"
#include "dialog.h"
int serial_port;
struct termios tty;
int motor_status = 0;
void *readThread(){
char* buffer = malloc(50*sizeof(char));
while(1){
sleep(0.75);
serialRead(buffer);
printf("%s", buffer);
}
}
/*
ierialWrite(message);
* Function findPlotter
* --------------------------------------------------------
* brief: Searches /dev/ttyACM* for plotter.
*
* return: plotter_path (Path to plotter file)
*/
char * findPlotter(){
glob_t globbuff = {0};
printf("%s", FindingBoard);
glob("/dev/ttyACM*",0,NULL,&globbuff);
int i = 0;
FILE *fp;
char *plotter_path = (char*) malloc(20*sizeof(char));
for(i = 0; i < globbuff.gl_pathc; i++){
char *command = (char*) malloc(100*sizeof(char));
char *temp = (char*) malloc(100*sizeof(char));
size_t len1 = strlen("udevadm info -q path -n ");
memcpy(command, "udevadm info -q path -n ",len1);
memcpy(command+len1,globbuff.gl_pathv[i],strlen(globbuff.gl_pathv[i])+1);
fp = popen(command,"r");
if(fp == NULL){
printf("Failed to run command");
exit(1);
}
fgets(temp, 100, fp);
len1 = strlen("udevadm info -a -p ");
memcpy(command, "udevadm info -a -p ",len1);
memcpy(command+len1, temp,strlen(temp));
pclose(fp);
fp = popen(command,"r");
if(fp == NULL){
printf("Failed to run command");
exit(1);
}
while(fgets(temp, 10000, fp)){
if(strstr(temp, "EiBotBoard")!=NULL){
pclose(fp);
memcpy(plotter_path,globbuff.gl_pathv[i],strlen(globbuff.gl_pathv[i])+1);
globfree(&globbuff);
printf("%sPath:%s\n", BoardFound,plotter_path);
return plotter_path;
}
}
pclose(fp);
}
globfree(&globbuff);
return "";
}
/*
* Function init
* --------------------------------------------------------
* brief: Initiallizes serial connection with plotter.
*
*/
int init(){
char* path = findPlotter();
pthread_t thread_id_read;
pthread_create(&thread_id_read,NULL,readThread, NULL);
serial_port = open(path, O_RDWR);
sleep(3);
if(serial_port < 0){
printf("ERROR %i from open: %s\n", errno, strerror(errno));
return 1;
}
if(tcgetattr(serial_port, &tty) != 0){
printf("ERROR %i from tcgetattr: %s\n", errno, strerror(errno));
return 1;
}
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CREAD | CLOCAL;
tty.c_lflag &= ~ECHO;
tty.c_lflag &= ~ECHONL;
tty.c_lflag &= ~ICANON;
tty.c_lflag &= ~ISIG;
tty.c_lflag &= ~IEXTEN;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);
tty.c_oflag &= ~OPOST;
tty.c_oflag &= ~ONLCR;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 0;
cfsetispeed(&tty, B115200);
cfsetospeed(&tty, B115200);
if(tcsetattr(serial_port, TCSANOW, &tty)!=0){
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
return 1;
}
tcflush(serial_port, TCIFLUSH);
return 0;
}
/*
* Function lowerPen
* --------------------------------------------------------
* brief: Lowers pen to page using servo.
*
*/
int lowerPen(){
char* message = malloc(50*sizeof(char));
generateCommand(message,SET_PEN,"0");
printf("%s\n",message);
serialWrite(message);
sleep(0.1);
free(message);
}
int pixelToStep(int height, int width, int* x, int* y){
int ratioX = (int) MAX_X/height;
int ratioY = (int) MAX_Y/width;
*x = *x * ratioX;
*y = *y * ratioY;
return 0;
}
int plotterHome(){
char* message = malloc(50*sizeof(char));
generateCommand(message,HOME,"10000");
serialWrite(message);
free(message);
}
int drawImage(ContourNode* HEAD){
plotterXPos = 0;
plotterYPos = 0;
ContourNode * temp = HEAD;
int count = 0;
do{
int i = 0;
printf("ContourNumber: %d\n", count);
for(i = 0; i < temp->size;i++){
pixelToStep(temp->height,temp->width,&temp->x_positions[i],&temp->y_positions[i]);
}
drawContour(temp->x_positions,temp->y_positions,temp->size);
ContourNode * to_free = temp;
temp = temp->next;
free(to_free->x_positions);
free(to_free->y_positions);
free(to_free);
count++;
}while(!temp->end);
if(temp->next){
free(temp->next);
}
plotterHome();
return 0;
}
int importContours(ContourNode* contour, char* filename){
FILE *FP;
char buffer[255];
int * xpos = calloc(5000,sizeof(int));
int * ypos = calloc(5000,sizeof(int));
FP = fopen(filename,"r");
int points = 0;
int contour_count = 0;
int width = strtol(fgets(buffer,sizeof(buffer), FP),(char**)NULL,10);
int height = strtol(fgets(buffer,sizeof(buffer), FP),(char**)NULL,10);
while(fgets(buffer, sizeof(buffer), FP)){
if(strstr(buffer, "---")){
contour->height = height;
contour->width = width;
contour->x_positions = xpos;
contour->y_positions = ypos;
contour->size = points;
contour->next = (ContourNode *) malloc(sizeof(ContourNode));
contour->end = 0;
contour = (ContourNode *) contour->next;
points = 0;
contour->end = 1;
xpos = calloc(5000,sizeof(int));
ypos = calloc(5000,sizeof(int));
continue;
}
char *token = strtok(buffer, " ");
int count = 0;
while(token != NULL){
if (count == 0){
ypos[points] = strtol(token, (char **)NULL, 10);
}
if (count == 1){
xpos[points] = strtol(token, (char **)NULL, 10);
}
count++;
token = strtok(NULL," ");
}
points++;
}
}
int printList(ContourNode * HEAD){
ContourNode * temp = HEAD;
int count = 0;
do{
printf("%d\n", count++);
printf("-------\n");
int i = 0;
for(i = 0; i < temp->size; i++){
printf("x[1]: %d, ", temp->x_positions[i]);
printf("y[1]: %d\n", temp->y_positions[i]);
}
printf("Points: %d\n", temp->size);
printf("Points: %d\n", temp->end);
printf("-------\n\n");
temp = (ContourNode *)temp->next;
}while(!temp->end);
}
/*
* Function raisePen
* --------------------------------------------------------
* brief: Raises pen off of page using servo.
*
*/
int raisePen(){
char* message = malloc(50*sizeof(char));
generateCommand(message,SET_PEN,"1");
serialWrite(message);
sleep(0.1);
free(message);
}
/*
* Function drawContour
* --------------------------------------------------------
* brief: Moves writing head to specified position.
*
* x: Pointer to array of X positions.
*
* y: Pointer to array of y positions. Must be same length
* as X array.
*
* points: number of points to be connected
*
*/
int drawContour(int* x, int* y, int points){
movePen(x[0], y[0],0);
lowerPen();
sleep(1);
int i;
printf("points: %d\n", points);
for(i = 1; i < points; i++){
printf("point: %d\n",i);
movePen(x[i], y[i], 0);
}
movePen(x[0], y[0]);
raisePen();
sleep(1);
return 0;
}
/*
* Function movePen
* --------------------------------------------------------
* brief: Moves writing head /o specified position.
*
* x: X position in steps to be moved to.
*
* y: Y position in steps to be moved to.
*
* strictMode: If strict mode is enabled (1) the function
* will check if the motors are moving before
* and after each movment,
*
*/
int movePen(int x, int y, int strictMode){
printf("xpos: %d\n", x);
printf("ypos: %d\n\n", y);
char* move_message = malloc(50*sizeof(char));
int motorStatus = 0;
if(x < 0 || y < 0){
printf("%s", BoundsError);
printf("Desired X: %d, Desired Y: %d\n", x, y);
printf("Min X: %d, Min Y: %d\n", 0, 0);
free(move_message);
return 1;
}
if(x > MAX_X || y > MAX_Y){
printf("%s", BoundsError);
printf("Desired X: %d, Desired Y: %d\n", x, y);
printf("Max X: %d, Max Y: %d\n", MAX_X, MAX_Y);
free(move_message);
return 1;
}
//if (strictMode){
// int ms;
// while(ms = getMotorStatus()) sleep(0.25);
//}
int xSteps = x-plotterXPos;
int ySteps = y-plotterYPos;
int s1 = abs(xSteps + ySteps);
int s2 = abs(xSteps - ySteps);
int max_distance = (s1 >s2) ? s1 : s2;
int moveTime = max_distance*0.15;
if (moveTime < 1) moveTime = 1;
char *params;
size_t sz;
sz = snprintf(NULL, 0, "%d,%d,%d", moveTime, xSteps,ySteps);
printf("%d,%d,%d", moveTime, xSteps,ySteps);
params = (char *) malloc(sz+1);
sz = snprintf(params, sz+1, "%d,%d,%d", moveTime, xSteps,ySteps);
generateCommand(move_message,STEPPER_MOVE_MIXED,params);
serialWrite(move_message);
plotterXPos = x;
plotterYPos = y;
free(move_message);
usleep((useconds_t ) (moveTime)*1000);
}
/*
* Function getMotorStatus
* --------------------------------------------------------
* brief: Checks if motors are currently moving.
*
* return: status (0) if motors not moving (1) if motors
* moving
*
*/
int getMotorStatus(){
char* motor_buffer= malloc(50*sizeof(char));
char* message = malloc(50*sizeof(char));
int status = 0;
generateCommand(message,QUERY_MOTORS,"");
int count = 0;
do{
serialWrite(message);
sleep(0.5);
count = serialRead(motor_buffer);
}while(count < 11);
const char delim[2] = ",";
char *substring;
substring = strtok(motor_buffer, delim);
if(!strstr(substring, "0") && !strstr(substring, "Q"))
status = 1;
while(substring != NULL){
if(!strstr(substring, "0") && !strstr(substring, "Q"))
status = 1;
substring = strtok(NULL, delim);
}
free(motor_buffer);
free(message);
return status;
}
/*
* Function serialWrite
* --------------------------------------------------------
* brief: Writes data to connected plotter over serial.
* data is written from string pointed to by msg.
* Data is written until string terminator.
*
* msg: String pointer pointing to data to be written.
*
*/
int serialWrite(char * msg){
int n = write(serial_port, msg, strlen(msg));
printf("bytes sent:%d\n", n);
if(n == -1){
init();
}
return 0;
}
/*
* Function serialRead
* --------------------------------------------------------
* brief: Reads serial buffer from connected plotter until
* newline character, data is placed in buffer array.
*
* buffer: chaaracter array pointer pointing to array to
* hold data from the serial port.
*
*/
int serialRead(char* buffer){
memset(buffer, '\0', sizeof(buffer));
char temp;
int n = 0;
while(temp != '\n'){
sleep(0.05);
int size_read = 0;
n += read(serial_port, &temp, 1);
buffer[n-1] = temp;
}
return n;
}
//int main(){
// char* message = malloc(50*sizeof(char));
// printf("%s", Welcome);
// init();
// int* test_x = malloc(200*sizeof(int));
// int* test_y = malloc(200*sizeof(int));
// int count = 0;
// int i = 0;
// int x = 500;
// int y = 500;
// pixelToStep(850,1100, &x,&y);
// printf("%d\n", x);
// printf("%d\n", y);
// for(i = 0; i < 10000;i+=100){
// printf("%d", i);
// test_x[count] = i;
// test_y[count] = (int)1000*sin(0.005*i)+2000;
// printf("X: %d Y: %d\n",test_x[count],test_y[count]);
// count++;
// }
// drawContour(test_x,test_y,count);
// generateCommand(message,HOME,"3000");
// serialWrite(message);
//}