Skip to content

Commit

Permalink
basic verification
Browse files Browse the repository at this point in the history
  • Loading branch information
fenrus75 committed Feb 16, 2020
1 parent 8817a64 commit f783f40
Showing 3 changed files with 199 additions and 20 deletions.
141 changes: 128 additions & 13 deletions gcodecheck/gcode.cpp
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@
#include <math.h>
#include <vector>

#include "gcodecheck.h"

struct line {
double X1, Y1, Z1;
double X2, Y2, Z2;
@@ -35,13 +37,18 @@ static double currentY;
static double currentZ;

static double minX = 1000, minY = 1000, minZ = 1000, maxX, maxY, maxZ;

static double maxspeed = 0, minspeed = 500000;

static double speed = 0;

double cuttersize = 2;

static char toollist[8192] = "";

static bool first_coord = true;
static bool spindle_running = false;

static bool need_homing_switches = false;

static double to_mm(double x)
{
@@ -60,6 +67,14 @@ static void record_motion_XYZ(double fX, double fY, double fZ, double tX, double
return;
}

if (!spindle_running)
error("Cutting without spindle on\n");

if (speed > maxspeed)
maxspeed = speed;
if (speed < minspeed)
minspeed = speed;

if (fX > maxX)
maxX = fX;
if (fX < minX)
@@ -76,6 +91,14 @@ static void record_motion_XYZ(double fX, double fY, double fZ, double tX, double
maxY = tY;
if (tY < minY)
minY = tY;
if (fZ > maxZ)
maxZ = fZ;
if (fZ < minZ)
minZ = fZ;
if (tZ > maxZ)
maxZ = tZ;
if (tZ < minZ)
minZ = tZ;

point = (struct line*)calloc(sizeof(struct line), 1);
point->X1 = fX;
@@ -94,6 +117,9 @@ static int xyzline(char *line)
char *c;
double X,Y,Z;

while (line[0] == ' ')
line++;

if (absolute == 1) {
X = currentX;
Y = currentY;
@@ -163,29 +189,30 @@ static int gline(char *line)
}

if (code == 20) {
printf("Switching to imperial\n");
vprintf("Switching to imperial\n");
metric = 0;
handled = 1;
}
if (code == 21) {
printf("Switching to metric\n");
vprintf("Switching to metric\n");
metric = 1;
handled = 1;
}
if (code == 90) {
printf("Switching to absolute mode\n");
vprintf("Switching to absolute mode\n");
absolute = 1;
handled = 1;
}
if (code == 91) {
printf("Switching to relative mode\n");
vprintf("Switching to relative mode\n");
absolute = 0;
handled = 1;
}

if (code == 53) { /* G53 is "absolute move once line" which is for bit changes/etc */
handled = 1;
first_coord = true;
need_homing_switches = true;
}


@@ -203,27 +230,33 @@ static int mline(char *line)
code = strtoull(&line[1], NULL, 10);

if (code == 2) {
printf("Program end\n");
vprintf("Program end\n");
handled = 1;
}
if (code == 3) {
printf("Spindle Start\n");
vprintf("Spindle Start\n");
spindle_running = true;
handled = 1;
}
if (code == 4) {
printf("Spindle Start\n");
vprintf("Spindle Start\n");
spindle_running = true;
handled = 1;
}
if (code == 5) {
printf("Spindle Stop\n");
vprintf("Spindle Stop\n");
spindle_running = false;
handled = 1;
}
if (code == 6) {
printf("Tool change: %s\n", line + 3);
if (spindle_running)
error("Tool change with spindle running\n");
vprintf("Tool change: %s\n", line + 3);
strcat(toollist, line+3);
handled = 1;
}
if (code == 30) {
printf("Program end\n");
vprintf("Program end\n");
handled = 1;
}

@@ -277,10 +310,10 @@ void read_gcode(const char *filename)
char *line;
FILE *file;

printf("Parsing %s\n", filename);
vprintf("Parsing %s\n", filename);
file = fopen(filename, "r");
if (!file) {
printf("Error opening file: %s\n", strerror(errno));
error("Error opening file: %s\n", strerror(errno));
}
while (!feof(file)) {
size_t ret;
@@ -293,3 +326,85 @@ void read_gcode(const char *filename)
}
fclose(file);
}

void print_state(FILE *output)
{
fprintf(output, "minX\t%5.4f\n", minX);
fprintf(output, "maxX\t%5.4f\n", maxX);
fprintf(output, "minY\t%5.4f\n", minY);
fprintf(output, "maxY\t%5.4f\n", maxY);
fprintf(output, "minZ\t%5.4f\n", minZ);
fprintf(output, "maxZ\t%5.4f\n", maxZ);
fprintf(output, "tools\t%s\n", toollist);
fprintf(output, "minspeed\t%5.4f\n", minspeed);
fprintf(output, "maxspeed\t%5.4f\n", maxspeed);
if (need_homing_switches)
fprintf(output, "homing\tyes\n");
else
fprintf(output, "homing\tno\n");
}


static void verify_line(const char *key, const char *value)
{
double valD = strtod(value, NULL);


if (strcmp(key, "minX") == 0) {
if (fabs(valD-minX) > 0.01)
error("min X deviates from reference %5.4f vs %5.4f\n", valD, minX);
return;
}
if (strcmp(key, "maxX") == 0) {
if (fabs(valD-maxX) > 0.01)
error("max X deviates from reference %5.4f vs %5.4f\n", valD, maxX);
return;
}
if (strcmp(key, "minY") == 0) {
if (fabs(valD-minY) > 0.01)
error("min Y deviates from reference %5.4f vs %5.4f\n", valD, minY);
return;
}
if (strcmp(key, "maxY") == 0) {
if (fabs(valD-maxY) > 0.01)
error("max Y deviates from reference %5.4f vs %5.4f\n", valD, maxY);
return;
}
if (strcmp(key, "minZ") == 0) {
if (fabs(valD-minZ) > 0.01)
error("min Z deviates from reference %5.4f vs %5.4f\n", valD, minZ);
return;
}
if (strcmp(key, "maxZ") == 0) {
if (fabs(valD-maxZ) > 0.01)
error("max Z deviates from reference %5.4f vs %5.4f\n", valD, maxZ);
return;
}


printf("Unhandled key %s \n", key);
}

void verify_fingerprint(const char *filename)
{
FILE *file;
file = fopen(filename, "r");
if (!file) {
error("Error opening reference file %s", filename);
return;
}

while (!feof(file)) {
char line[4096];
char *c1;
line[0] = 0;
fgets(line, 4096, file);
c1 = strchr(line, '\t');
if (!c1)
continue;
*c1 = 0;
c1++;
verify_line(line, c1);
}
fclose(file);
}
11 changes: 11 additions & 0 deletions gcodecheck/gcodecheck.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#pragma once

extern void read_gcode(const char *filename);
extern void print_state(FILE *output);

extern void verify_fingerprint(const char *filename);

extern int verbose;
extern int errorcode;

#define vprintf(...) do { if (verbose) printf(__VA_ARGS__); } while (0)

#define error(...) do { fprintf(stderr, __VA_ARGS__); errorcode++;} while (0)

67 changes: 60 additions & 7 deletions gcodecheck/main.cpp
Original file line number Diff line number Diff line change
@@ -8,18 +8,71 @@

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>


#include "gcodecheck.h"

int verbose = 0;
int errorcode = 0;


void usage(void)
{
printf("Usage:\n\tgcodecheck [options] <file.nc>\n");
printf("\t--verbose (-v) verbose output\n");
exit(EXIT_SUCCESS);
}

static struct option long_options[] =
{
/* These options set a flag. */
{"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0}
};

int main(int argc, char **argv)
{
int iter = 0;
if (argc < 2) {
printf("Usage:\n\tgcodecheck <filename>\n");
exit(0);
}
int opt;
int option_index;

while ((opt = getopt_long(argc, argv, "vh", long_options, &option_index)) != -1) {
switch (opt)
{
case 'v':
verbose = 1;
break;
case 'h':
default:
usage();
}
}

read_gcode(argv[1]);
if (optind == argc) {
usage();
}

for(; optind < argc; optind++) {
char filename[8192];
char *c;
read_gcode(argv[optind]);
strcpy(filename, argv[optind]);
c = strstr(filename, ".nc");
if (!c)
continue;
strcpy(c, ".fingerprint");

return 0;
if (access(filename, R_OK) == 0) {
verify_fingerprint(filename);
} else {
FILE *output;
output = fopen(filename, "w");
print_state(output);
fclose(output);
}

}
return errorcode;
}

0 comments on commit f783f40

Please sign in to comment.