-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.c
282 lines (255 loc) · 7.28 KB
/
processor.c
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
/*
* processor.c
*
* Created on: Jan 22, 2018
* Author: william
*/
#include "processor.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
int InitContext(struct ParserContext* ctx, int argc, char* argv[])
{
unsigned int i, u_tmp;
float f_tmp;
//default values
ctx->options = 0;
ctx->drill_deepness = 5;
ctx->drill_feed = 1200;
ctx->feed = 1200;
ctx->move_height = 2;
ctx->precision = 1000;
//Reading Context
for(i=0; i<argc; i++)
{
if (strcmp(argv[i], "--help") == 0)
{
printf("Usage: EXC_GC <options>\n\nOptions:\n");
printf(" --help Show this text\n");
printf(" -i <filename> Input excellon file\n");
printf(" -o <filename> Output G-Code file\n");
printf(" -d <value> Drill deepness\n");
printf(" -df <value> Drill feed\n");
printf(" -f <value> XY-moving feed\n");
printf(" -u <value> precision\n");
printf(" -h <value> XY-moving height\n");
return 1;
}
if (strcmp(argv[i], "-i")==0){
//printf("Input file = %s\n", argv[++i]);
ctx->src_fileName = argv[++i];
}
if (strcmp(argv[i],"-o")==0){
//printf("Output file = %s\n", argv[++i]);
ctx->dst_fileName = argv[++i];
}
if (strcmp(argv[i],"-d")==0){
f_tmp = atof(argv[++i]);
//printf("Override drill deepness = %f\n", f_tmp);
ctx->drill_deepness = f_tmp;
}
if (strcmp(argv[i],"-df")==0){
u_tmp = atoi(argv[++i]);
//printf("Override drill feed = %u\n", u_tmp);
ctx->drill_feed = u_tmp;
}
if (strcmp(argv[i],"-f")==0){
u_tmp = atoi(argv[++i]);
//printf("Override drill moving feed = %u\n", u_tmp);
ctx->feed = u_tmp;
}
if (strcmp(argv[i],"-u")==0){
u_tmp = atoi(argv[++i]);
//printf("Override precision = %u\n", u_tmp);
ctx->precision = u_tmp;
}
if (strcmp(argv[i],"-h")==0){
f_tmp = atof(argv[++i]);
if (f_tmp<0)
{
fprintf(stderr, "Parameter -h can't be negative!\n");
return 1;
}
ctx->move_height = f_tmp;
}
}
return 0;
}
int Preprocess (struct ParserContext* ctx)
{
ctx->toolsCnt = 0;
ctx->pointsCnt = 0;
char buf[100];
unsigned int flag = 0;
//open source file
ctx->src_file = fopen(ctx->src_fileName, "r");
if (ctx->src_file == NULL)
{
fprintf(stderr, "Error opening source file for preprocessing!\n");
return 1;
}
//count number of tools and points
while(fgets(buf, 100, ctx->src_file) != NULL){
if (strcmp(buf, "M48\n") == 0) flag = 1; //встретили начало заголовка
switch (buf[0]) {
case '%':
flag = 0;
break;
case 'T':
if(flag&1) ctx->toolsCnt++;
break;
case 'X':
case 'Y':
ctx->pointsCnt++;
break;
default:
break;
}
}
fclose(ctx->src_file);
if (ctx->toolsCnt == 0 || ctx->pointsCnt == 0){
fprintf(stderr, "No points or/and tools defined!");
return 1;
}
return 0;
}
int Process (struct ParserContext* ctx)
{
char buf[100];
unsigned int u_tmp, flag = 0, cTool = 0, cPoint = 0;
float f1_tmp, f2_tmp;
//allocate memory for tools and points
ctx->tools = (float*)malloc(ctx->toolsCnt * sizeof(float));
ctx->points = (struct Point*)malloc(ctx->pointsCnt * sizeof(struct Point));
//open source file
ctx->src_file = fopen(ctx->src_fileName, "r");
if (ctx->src_file == NULL)
{
fprintf(stderr, "Can not open source file for processing!\n");
return 1;
}
while(fgets(buf, 100, ctx->src_file) != NULL && !(ctx->options & OPT_CTX_COMPLETED)){
switch (buf[0]) {
case '%':
flag = 0;
break;
case 'M':
sscanf(buf,"M%u", &u_tmp);
switch (u_tmp) {
case 48:
//header start
//printf("Start of header detected.\n");
flag = 1;
break;
case 71:
//printf("Switch to METRIC mode.\n");
ctx->options &= ~(OPT_CTX_IMPERIAL); //METRIC
break;
case 72:
//printf("Switch to IMPERIAL mode.\n");
ctx->options |= OPT_CTX_IMPERIAL; //INCHES
break;
case 30:
ctx->options |= OPT_CTX_COMPLETED; //set internal state to "completed"
//printf("Standard STOP command found.\n");
break;
default:
printf("Unknown M-Command\n");
break;
}
break;
case 'T':
if (flag)
{
//header area - add tool
sscanf(buf, "T%uC%f", &u_tmp, &f1_tmp);
//printf("Adding tool T%u, D = %f\n", u_tmp, f1_tmp);
ctx->tools[u_tmp-1] = f1_tmp;
}else{
//points area - change current numer of tool
sscanf(buf, "T%u", &u_tmp);
//printf("Selecting new tool number in toolset (start from 0): %u\n", u_tmp-1);
cTool = u_tmp-1;
}
break;
case 'X':
if (!flag)
{
//coord found
sscanf(buf, "X%fY%f", &f1_tmp, &f2_tmp);
//printf("Adding point for tool %u: %f, %f\n", cTool, f1_tmp, f2_tmp);
ctx->points[cPoint].tool_n = cTool;
ctx->points[cPoint].x = f1_tmp/ctx->precision;
ctx->points[cPoint].y = f2_tmp/ctx->precision;
cPoint++;
}
break;
default:
break;
}
}
fclose(ctx->src_file);
return 0;
}
int GenerateFile(struct ParserContext* ctx)
{
float x_min, y_min, x_max, y_max;
unsigned int i, cTool = 65535;
ctx->dst_file = fopen(ctx->dst_fileName, "w");
if (ctx->dst_file == NULL)
{
fprintf(stderr, "Error opening destination file.\n");
return 1;
}
fprintf(ctx->dst_file, "%s; set measuring mode\n", (ctx->options & OPT_CTX_IMPERIAL) ? "G20" : "G21");
fprintf(ctx->dst_file, "G90; set absolute positioning\n");
fprintf(ctx->dst_file, "G92 X0 Y0 Z0; reset coordinate to zero\n");
fprintf(ctx->dst_file, "G01 X0 Y0 Z%f F%u\n", (15 + ctx->move_height), ctx->feed); //lift drill
//find min/max
x_min = ctx->points[0].x;
x_max = x_min;
y_min = ctx->points[0].y;
y_max = y_min;
for (i=1; i<ctx->pointsCnt; i++)
{
if (ctx->points[i].x > x_max) x_max =ctx->points[i].x;
if (ctx->points[i].x < x_min) x_min =ctx->points[i].x;
if (ctx->points[i].y > y_max) y_max =ctx->points[i].y;
if (ctx->points[i].y < y_min) y_min =ctx->points[i].y;
}
//move across dimensions
fprintf(ctx->dst_file, "G01 X%f Y%f F%u\n", x_min, y_min, ctx->feed);
fprintf(ctx->dst_file, "G01 X%f Y%f F%u\n", x_min, y_max, ctx->feed);
fprintf(ctx->dst_file, "G01 X%f Y%f F%u\n", x_max, y_max, ctx->feed);
fprintf(ctx->dst_file, "G01 X%f Y%f F%u\n", x_max, y_min, ctx->feed);
fprintf(ctx->dst_file, "G01 X%f Y%f F%u\n", x_min, y_min, ctx->feed);
fprintf(ctx->dst_file, "G01 X0 Y0 F%u\n", ctx->feed);
for (i=0; i < ctx->pointsCnt; i++)
{
//move XY
fprintf(ctx->dst_file, "G01 X%f Y%f F%u\n", ctx->points[i].x, ctx->points[i].y, ctx->feed);
//do it when tool is new
if (ctx->points[i].tool_n != cTool)
{
cTool = ctx->points[i].tool_n;
//move tool up
fprintf(ctx->dst_file, "G01 Z50 F%u\n", ctx->feed);
//wait
fprintf(ctx->dst_file, "M0 Ch. T%u, D%f\n", cTool+1, ctx->tools[cTool]);
//move tool down to plate
fprintf(ctx->dst_file, "G01 Z%f F%u\n", 15.0, ctx->feed);
//wait for fix tool
fprintf(ctx->dst_file, "M0 Fix tool!\n");
//lift Z to h
fprintf(ctx->dst_file, "G01 Z%f F%u\n", (15 + ctx->move_height), ctx->feed);
//wait for spindle on
fprintf(ctx->dst_file, "M0 Turn on spindle!\n");
}
fprintf(ctx->dst_file, "G01 Z%f F%u\n", (15 - ctx->drill_deepness), ctx->drill_feed); //drilling
fprintf(ctx->dst_file, "G01 Z%f F%u\n", (15 + ctx->move_height), ctx->feed); //lift up
}
fprintf(ctx->dst_file, "M107 ; fan off\nG01 Z50 F%u\nG01 X0 Y0 ; home XY\n", ctx->feed);
fclose(ctx->dst_file);
return 0;
}