Date: | 2010-12-16 08:47:00 (13 years 3 months ago) |
---|---|
Author: | Werner Almesberger |
Commit: | ffaded7f4810ee34594af8a62de6c8c608e31d6e |
Message: | cngt: added support for setting default positions from toolpath file - cngt.c (x, y, z, move, up, down, do_key, main): renamed "x", "y", "z" to "cx", "cy", "cz", to avoid clash with common local "x", etc. - cngt.c (gp_minmax, main): populate default positions with bounding box of toolpath if given a file name instead of points |
Files: |
cngt/cngt.c (5 diffs) |
Change Details
cngt/cngt.c | ||
---|---|---|
25 | 25 | static double pos_x[MAX_POS]; |
26 | 26 | static double pos_y[MAX_POS]; |
27 | 27 | static double z0, height; |
28 | static double x, y, z; | |
28 | static double cx, cy, cz; | |
29 | 29 | |
30 | 30 | |
31 | 31 | #define UNITS(mm) ((mm)*40.0) |
... | ... | |
33 | 33 | |
34 | 34 | static void move(void) |
35 | 35 | { |
36 | serial_printf("!PZ%.1f,0;PD%.1f,%.1f\n", UNITS(z), UNITS(x), UNITS(y)); | |
36 | serial_printf("!PZ%.1f,0;PD%.1f,%.1f\n", | |
37 | UNITS(cz), UNITS(cx), UNITS(cy)); | |
37 | 38 | } |
38 | 39 | |
39 | 40 | |
40 | 41 | static void up(void) |
41 | 42 | { |
42 | z = z0+height; | |
43 | cz = z0+height; | |
43 | 44 | move(); |
44 | 45 | } |
45 | 46 | |
46 | 47 | |
47 | 48 | static void down(void) |
48 | 49 | { |
49 | z = z0; | |
50 | cz = z0; | |
50 | 51 | move(); |
51 | 52 | } |
52 | 53 | |
... | ... | |
62 | 63 | break; |
63 | 64 | case 'h': |
64 | 65 | up(); |
65 | x -= STEP; | |
66 | cx -= STEP; | |
66 | 67 | move(); |
67 | 68 | break; |
68 | 69 | case 'j': |
69 | 70 | up(); |
70 | y -= STEP; | |
71 | cy -= STEP; | |
71 | 72 | move(); |
72 | 73 | break; |
73 | 74 | case 'k': |
74 | 75 | up(); |
75 | y += STEP; | |
76 | cy += STEP; | |
76 | 77 | move(); |
77 | 78 | break; |
78 | 79 | case 'l': |
79 | 80 | up(); |
80 | x += STEP; | |
81 | cx += STEP; | |
81 | 82 | move(); |
82 | 83 | break; |
83 | 84 | case 'q': |
... | ... | |
86 | 87 | if (c < '0' || c > '9') |
87 | 88 | break; |
88 | 89 | up(); |
89 | x = pos_x[c-'0']; | |
90 | y = pos_y[c-'0']; | |
90 | cx = pos_x[c-'0']; | |
91 | cy = pos_y[c-'0']; | |
91 | 92 | move(); |
92 | 93 | } |
93 | 94 | return 0; |
94 | 95 | } |
95 | 96 | |
96 | 97 | |
98 | static void gp_minmax(const char *name, | |
99 | double *xa, double *ya, double *xb, double *yb) | |
100 | { | |
101 | FILE *file; | |
102 | char buf[1024]; | |
103 | double x, y, z; | |
104 | int n; | |
105 | int first = 1; | |
106 | ||
107 | file = fopen(name, "r"); | |
108 | if (!file) { | |
109 | perror(name); | |
110 | exit(1); | |
111 | } | |
112 | while (fgets(buf, sizeof(buf), file)) { | |
113 | if (*buf == '#') | |
114 | continue; | |
115 | n = sscanf(buf, "%lf %lf %lf\n", &x, &y, &z); | |
116 | switch (n) { | |
117 | case 3: | |
118 | /* fall through */ | |
119 | case 2: | |
120 | if (first || x < *xa) | |
121 | *xa = x; | |
122 | if (first || x > *xb) | |
123 | *xb = x; | |
124 | if (first || y < *ya) | |
125 | *ya = y; | |
126 | if (first || y > *yb) | |
127 | *yb = y; | |
128 | first = 0; | |
129 | break; | |
130 | default: | |
131 | break; | |
132 | } | |
133 | } | |
134 | fclose(file); | |
135 | } | |
136 | ||
137 | ||
97 | 138 | static void usage(const char *name) |
98 | 139 | { |
99 | 140 | fprintf(stderr, "usage: %s z0 height (file | x y ...)\n", name); |
... | ... | |
103 | 144 | |
104 | 145 | int main(int argc, char **argv) |
105 | 146 | { |
147 | double xa = 0, ya = 0, xb = 0, yb = 0; | |
106 | 148 | int i; |
107 | 149 | char c; |
108 | 150 | |
109 | 151 | if (argc < 4) |
110 | 152 | usage(*argv); |
111 | if (!(argc & 1)) | |
112 | usage(*argv); | |
113 | 153 | |
114 | 154 | z0 = atof(argv[1]); |
115 | 155 | height = atof(argv[2]); |
116 | for (i = 3; i != argc; i += 2) { | |
117 | pos_x[(i-3)/2] = atof(argv[i]); | |
118 | pos_y[(i-3)/2] = atof(argv[i+1]); | |
156 | ||
157 | if (argc & 1) { | |
158 | for (i = 3; i != argc; i += 2) { | |
159 | pos_x[(i-3)/2] = atof(argv[i]); | |
160 | pos_y[(i-3)/2] = atof(argv[i+1]); | |
161 | } | |
162 | } else { | |
163 | if (argc != 4) | |
164 | usage(*argv); | |
165 | gp_minmax(argv[3], &xa, &ya, &xb, &yb); | |
166 | pos_x[1] = pos_x[4] = pos_x[7] = xa; | |
167 | pos_x[2] = pos_x[5] = pos_x[8] = (xa+xb)/2; | |
168 | pos_x[3] = pos_x[6] = pos_x[9] = xb; | |
169 | pos_y[1] = pos_y[2] = pos_y[3] = ya; | |
170 | pos_y[4] = pos_y[5] = pos_y[6] = (ya+yb)/2; | |
171 | pos_y[7] = pos_y[8] = pos_y[9] = yb; | |
119 | 172 | } |
120 | 173 | |
121 | 174 | serial_open("/dev/ttyUSB0"); |
122 | 175 | serial_printf("\nIN;!MC0\n"); |
123 | 176 | |
124 | x = pos_x[0]; | |
125 | y = pos_y[0]; | |
126 | z = z0+height; | |
177 | cx = pos_x[0]; | |
178 | cy = pos_y[0]; | |
179 | cz = z0+height; | |
127 | 180 | move(); |
128 | 181 | |
129 | 182 | while ((c = getkey())) |
Branches:
master