diff options
| author | 2026-11-11 22:28:13 +0000 | |
|---|---|---|
| committer | 2026-11-11 22:47:34 +0000 | |
| commit | cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch) | |
| tree | c7d90c0abe9533c87d51b5196871846855efa1ba /src/diagnose.c | |
| download | thesis-master.tar.gz | |
Diffstat (limited to 'src/diagnose.c')
| -rw-r--r-- | src/diagnose.c | 123 |
1 files changed, 123 insertions, 0 deletions
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 @@ | |||
| 1 | #include <errno.h> | ||
| 2 | #include <math.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <fcntl.h> | ||
| 6 | #include <unistd.h> | ||
| 7 | |||
| 8 | #include "utils.h" | ||
| 9 | #include "rs274ngc_lexer.h" | ||
| 10 | #include "rs274ngc_cst.h" | ||
| 11 | #include "path.h" | ||
| 12 | #include "motion_planner.h" | ||
| 13 | #include "diagnostics.h" | ||
| 14 | |||
| 15 | static void | ||
| 16 | usage (const char *progname) | ||
| 17 | { | ||
| 18 | fprintf (stderr, "usage: %s INPUT.ngc OUTPUT.csv [dt]\n", progname); | ||
| 19 | } | ||
| 20 | |||
| 21 | static double | ||
| 22 | parse_dt (const char *s) | ||
| 23 | { | ||
| 24 | char *end = NULL; | ||
| 25 | errno = 0; | ||
| 26 | double dt = strtod (s, &end); | ||
| 27 | |||
| 28 | if (errno || end == s || *end != '\0' || !(dt > 0.0) || !isfinite (dt)) { | ||
| 29 | fprintf (stderr, "invalid dt: %s\n", s); | ||
| 30 | exit (EXIT_FAILURE); | ||
| 31 | } | ||
| 32 | |||
| 33 | return dt; | ||
| 34 | } | ||
| 35 | |||
| 36 | static struct path | ||
| 37 | read_path_from_file (struct arena *a, | ||
| 38 | struct arena *t, | ||
| 39 | const char *filename) | ||
| 40 | { | ||
| 41 | int fd = open (filename, O_RDONLY); | ||
| 42 | if (fd < 0) { | ||
| 43 | perror (filename); | ||
| 44 | exit (EXIT_FAILURE); | ||
| 45 | } | ||
| 46 | |||
| 47 | struct sv rs274ngc = read_until_eof (a, fd); | ||
| 48 | close (fd); | ||
| 49 | |||
| 50 | rs274ngc_lexer_compile_regexes (); | ||
| 51 | struct rs274ngc_lexer lexer = rs274ngc_lexer_from_sv (rs274ngc, filename); | ||
| 52 | |||
| 53 | struct rs274ngc_cst_parser parser = { | ||
| 54 | .lexer = &lexer, | ||
| 55 | .output = a, | ||
| 56 | .temporary = t, | ||
| 57 | }; | ||
| 58 | |||
| 59 | return build_path (parser); | ||
| 60 | } | ||
| 61 | |||
| 62 | int | ||
| 63 | main (int argc, char **argv) | ||
| 64 | { | ||
| 65 | progname = argv[0]; | ||
| 66 | |||
| 67 | if (argc != 3 && argc != 4) { | ||
| 68 | usage (argv[0]); | ||
| 69 | return EXIT_FAILURE; | ||
| 70 | } | ||
| 71 | |||
| 72 | const char *input_filename = argv[1]; | ||
| 73 | const char *output_filename = argv[2]; | ||
| 74 | double dt = argc == 4 ? parse_dt (argv[3]) : 0.001; | ||
| 75 | |||
| 76 | struct arena a = {0}; | ||
| 77 | struct arena t = {0}; | ||
| 78 | |||
| 79 | struct path path = read_path_from_file (&a, &t, input_filename); | ||
| 80 | if (path.count == 0u) { | ||
| 81 | fprintf (stderr, "%s: empty path\n", input_filename); | ||
| 82 | return EXIT_FAILURE; | ||
| 83 | } | ||
| 84 | |||
| 85 | struct mp_limits limits = { | ||
| 86 | .vmax = 80, | ||
| 87 | .rapid_vmax = 125, | ||
| 88 | .amax = 800, | ||
| 89 | .jmax = 16000, | ||
| 90 | .corner_tol = .05, | ||
| 91 | .min_segment_len = 1e-6, | ||
| 92 | .default_feed = 50, | ||
| 93 | |||
| 94 | .straight_angle_tol = 1e-5, | ||
| 95 | .stop_angle = M_PI/16, | ||
| 96 | .corner_speed_deadband = .02, | ||
| 97 | |||
| 98 | .curvature_blend_len = 0.05, | ||
| 99 | }; | ||
| 100 | |||
| 101 | struct motion_plan plan = {0}; | ||
| 102 | int rc = mp_plan_build (&path, 0.0, 0.0, &limits, &plan); | ||
| 103 | if (rc != MP_OK) { | ||
| 104 | fprintf (stderr, "%s: mp_plan_build failed: %d\n", input_filename, rc); | ||
| 105 | return EXIT_FAILURE; | ||
| 106 | } | ||
| 107 | |||
| 108 | FILE *csv = fopen (output_filename, "w"); | ||
| 109 | if (!csv) { | ||
| 110 | perror (output_filename); | ||
| 111 | mp_plan_free (&plan); | ||
| 112 | return EXIT_FAILURE; | ||
| 113 | } | ||
| 114 | |||
| 115 | motion_diag_write_csv (&plan, dt, csv, 1); | ||
| 116 | fclose (csv); | ||
| 117 | |||
| 118 | fprintf (stderr, "%s: total_time=%g s, total_length=%g mm, dt=%g s\n", | ||
| 119 | output_filename, plan.total_time, plan.total_length, dt); | ||
| 120 | |||
| 121 | mp_plan_free (&plan); | ||
| 122 | return EXIT_SUCCESS; | ||
| 123 | } | ||
