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
2525static double pos_x[MAX_POS];
2626static double pos_y[MAX_POS];
2727static double z0, height;
28static double x, y, z;
28static double cx, cy, cz;
2929
3030
3131#define UNITS(mm) ((mm)*40.0)
...... 
3333
3434static void move(void)
3535{
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));
3738}
3839
3940
4041static void up(void)
4142{
42    z = z0+height;
43    cz = z0+height;
4344    move();
4445}
4546
4647
4748static void down(void)
4849{
49    z = z0;
50    cz = z0;
5051    move();
5152}
5253
...... 
6263        break;
6364    case 'h':
6465        up();
65        x -= STEP;
66        cx -= STEP;
6667        move();
6768        break;
6869    case 'j':
6970        up();
70        y -= STEP;
71        cy -= STEP;
7172        move();
7273        break;
7374    case 'k':
7475        up();
75        y += STEP;
76        cy += STEP;
7677        move();
7778        break;
7879    case 'l':
7980        up();
80        x += STEP;
81        cx += STEP;
8182        move();
8283        break;
8384    case 'q':
...... 
8687        if (c < '0' || c > '9')
8788            break;
8889        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'];
9192        move();
9293    }
9394    return 0;
9495}
9596
9697
98static 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
97138static void usage(const char *name)
98139{
99140    fprintf(stderr, "usage: %s z0 height (file | x y ...)\n", name);
...... 
103144
104145int main(int argc, char **argv)
105146{
147    double xa = 0, ya = 0, xb = 0, yb = 0;
106148    int i;
107149    char c;
108150
109151    if (argc < 4)
110152        usage(*argv);
111    if (!(argc & 1))
112        usage(*argv);
113153
114154    z0 = atof(argv[1]);
115155    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;
119172    }
120173
121174    serial_open("/dev/ttyUSB0");
122175    serial_printf("\nIN;!MC0\n");
123176
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;
127180    move();
128181
129182    while ((c = getkey()))

Archive Download the corresponding diff file

Branches:
master



interactive