aboutsummaryrefslogtreecommitdiff
path: root/src/diagnose.c
blob: 041019a224a61bc28f4c78b1c9b25dde58f6c0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#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;
}