1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2025-01-09 00:40:15 +02:00

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
This commit is contained in:
Werner Almesberger 2010-12-16 04:47:00 -03:00
parent ed6bbff7f8
commit ffaded7f48

View File

@ -25,7 +25,7 @@
static double pos_x[MAX_POS]; static double pos_x[MAX_POS];
static double pos_y[MAX_POS]; static double pos_y[MAX_POS];
static double z0, height; static double z0, height;
static double x, y, z; static double cx, cy, cz;
#define UNITS(mm) ((mm)*40.0) #define UNITS(mm) ((mm)*40.0)
@ -33,20 +33,21 @@ static double x, y, z;
static void move(void) static void move(void)
{ {
serial_printf("!PZ%.1f,0;PD%.1f,%.1f\n", UNITS(z), UNITS(x), UNITS(y)); serial_printf("!PZ%.1f,0;PD%.1f,%.1f\n",
UNITS(cz), UNITS(cx), UNITS(cy));
} }
static void up(void) static void up(void)
{ {
z = z0+height; cz = z0+height;
move(); move();
} }
static void down(void) static void down(void)
{ {
z = z0; cz = z0;
move(); move();
} }
@ -62,22 +63,22 @@ static int do_key(char c)
break; break;
case 'h': case 'h':
up(); up();
x -= STEP; cx -= STEP;
move(); move();
break; break;
case 'j': case 'j':
up(); up();
y -= STEP; cy -= STEP;
move(); move();
break; break;
case 'k': case 'k':
up(); up();
y += STEP; cy += STEP;
move(); move();
break; break;
case 'l': case 'l':
up(); up();
x += STEP; cx += STEP;
move(); move();
break; break;
case 'q': case 'q':
@ -86,14 +87,54 @@ static int do_key(char c)
if (c < '0' || c > '9') if (c < '0' || c > '9')
break; break;
up(); up();
x = pos_x[c-'0']; cx = pos_x[c-'0'];
y = pos_y[c-'0']; cy = pos_y[c-'0'];
move(); move();
} }
return 0; return 0;
} }
static void gp_minmax(const char *name,
double *xa, double *ya, double *xb, double *yb)
{
FILE *file;
char buf[1024];
double x, y, z;
int n;
int first = 1;
file = fopen(name, "r");
if (!file) {
perror(name);
exit(1);
}
while (fgets(buf, sizeof(buf), file)) {
if (*buf == '#')
continue;
n = sscanf(buf, "%lf %lf %lf\n", &x, &y, &z);
switch (n) {
case 3:
/* fall through */
case 2:
if (first || x < *xa)
*xa = x;
if (first || x > *xb)
*xb = x;
if (first || y < *ya)
*ya = y;
if (first || y > *yb)
*yb = y;
first = 0;
break;
default:
break;
}
}
fclose(file);
}
static void usage(const char *name) static void usage(const char *name)
{ {
fprintf(stderr, "usage: %s z0 height (file | x y ...)\n", name); fprintf(stderr, "usage: %s z0 height (file | x y ...)\n", name);
@ -103,27 +144,39 @@ static void usage(const char *name)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
double xa = 0, ya = 0, xb = 0, yb = 0;
int i; int i;
char c; char c;
if (argc < 4) if (argc < 4)
usage(*argv); usage(*argv);
if (!(argc & 1))
usage(*argv);
z0 = atof(argv[1]); z0 = atof(argv[1]);
height = atof(argv[2]); height = atof(argv[2]);
for (i = 3; i != argc; i += 2) {
pos_x[(i-3)/2] = atof(argv[i]); if (argc & 1) {
pos_y[(i-3)/2] = atof(argv[i+1]); for (i = 3; i != argc; i += 2) {
pos_x[(i-3)/2] = atof(argv[i]);
pos_y[(i-3)/2] = atof(argv[i+1]);
}
} else {
if (argc != 4)
usage(*argv);
gp_minmax(argv[3], &xa, &ya, &xb, &yb);
pos_x[1] = pos_x[4] = pos_x[7] = xa;
pos_x[2] = pos_x[5] = pos_x[8] = (xa+xb)/2;
pos_x[3] = pos_x[6] = pos_x[9] = xb;
pos_y[1] = pos_y[2] = pos_y[3] = ya;
pos_y[4] = pos_y[5] = pos_y[6] = (ya+yb)/2;
pos_y[7] = pos_y[8] = pos_y[9] = yb;
} }
serial_open("/dev/ttyUSB0"); serial_open("/dev/ttyUSB0");
serial_printf("\nIN;!MC0\n"); serial_printf("\nIN;!MC0\n");
x = pos_x[0]; cx = pos_x[0];
y = pos_y[0]; cy = pos_y[0];
z = z0+height; cz = z0+height;
move(); move();
while ((c = getkey())) while ((c = getkey()))