From cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 12 Nov 2026 01:28:13 +0300 Subject: publish --- src/diagnose.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/diagnose.c (limited to 'src/diagnose.c') diff --git a/src/diagnose.c b/src/diagnose.c new file mode 100644 index 0000000..041019a --- /dev/null +++ b/src/diagnose.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +#include "utils.h" +#include "rs274ngc_lexer.h" +#include "rs274ngc_cst.h" +#include "path.h" +#include "motion_planner.h" +#include "diagnostics.h" + +static void +usage (const char *progname) +{ + fprintf (stderr, "usage: %s INPUT.ngc OUTPUT.csv [dt]\n", progname); +} + +static double +parse_dt (const char *s) +{ + char *end = NULL; + errno = 0; + double dt = strtod (s, &end); + + if (errno || end == s || *end != '\0' || !(dt > 0.0) || !isfinite (dt)) { + fprintf (stderr, "invalid dt: %s\n", s); + exit (EXIT_FAILURE); + } + + return dt; +} + +static struct path +read_path_from_file (struct arena *a, + struct arena *t, + const char *filename) +{ + int fd = open (filename, O_RDONLY); + if (fd < 0) { + perror (filename); + exit (EXIT_FAILURE); + } + + struct sv rs274ngc = read_until_eof (a, fd); + close (fd); + + rs274ngc_lexer_compile_regexes (); + struct rs274ngc_lexer lexer = rs274ngc_lexer_from_sv (rs274ngc, filename); + + struct rs274ngc_cst_parser parser = { + .lexer = &lexer, + .output = a, + .temporary = t, + }; + + return build_path (parser); +} + +int +main (int argc, char **argv) +{ + progname = argv[0]; + + if (argc != 3 && argc != 4) { + usage (argv[0]); + return EXIT_FAILURE; + } + + const char *input_filename = argv[1]; + const char *output_filename = argv[2]; + double dt = argc == 4 ? parse_dt (argv[3]) : 0.001; + + struct arena a = {0}; + struct arena t = {0}; + + struct path path = read_path_from_file (&a, &t, input_filename); + if (path.count == 0u) { + fprintf (stderr, "%s: empty path\n", input_filename); + return EXIT_FAILURE; + } + + struct mp_limits limits = { + .vmax = 80, + .rapid_vmax = 125, + .amax = 800, + .jmax = 16000, + .corner_tol = .05, + .min_segment_len = 1e-6, + .default_feed = 50, + + .straight_angle_tol = 1e-5, + .stop_angle = M_PI/16, + .corner_speed_deadband = .02, + + .curvature_blend_len = 0.05, + }; + + struct motion_plan plan = {0}; + int rc = mp_plan_build (&path, 0.0, 0.0, &limits, &plan); + if (rc != MP_OK) { + fprintf (stderr, "%s: mp_plan_build failed: %d\n", input_filename, rc); + return EXIT_FAILURE; + } + + FILE *csv = fopen (output_filename, "w"); + if (!csv) { + perror (output_filename); + mp_plan_free (&plan); + return EXIT_FAILURE; + } + + motion_diag_write_csv (&plan, dt, csv, 1); + fclose (csv); + + fprintf (stderr, "%s: total_time=%g s, total_length=%g mm, dt=%g s\n", + output_filename, plan.total_time, plan.total_length, dt); + + mp_plan_free (&plan); + return EXIT_SUCCESS; +} -- cgit v1.3.1