aboutsummaryrefslogtreecommitdiff
path: root/src/path.c
blob: 7ded2d21b9bb6a81053929bb4b2531ea6e1259cf (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/// src/path.c
//
// Copyright (C) 2026 by Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
/// Commentary:
//
//  Order of execution:
//   1. comment (includes message).
//   2. set feed rate mode (G93, G94 — inverse time or per minute).
//   3. set feed rate (F).
//   4. set spindle speed (S).
//   5. select tool (T).
//   6. change tool (M6).
//   7. spindle on or off (M3, M4, M5).
//   8. coolant on or off (M7, M8, M9).
//   9. enable or disable overrides (M48, M49).
//  10. dwell (G4).
//  11. set active plane (G17, G18, G19).
//  12. set length units (G20, G21).
//  13. cutter radius compensation on or off (G40, G41, G42)
//  14. cutter length compensation on or off (G43, G49)
//  15. coordinate system selection (G54, G55, G56, G57, G58, G59,
//      G59.1, G59.2, G59.3).
//  16. set path control mode (G61, G61.1, G64)
//  17. set distance mode (G90, G91).
//  18. set retract mode (G98, G99).
//  19. home (G28, G30) or change coordinate system data (G10) or set
//      axis offsets (G92, G92.1, G92.2, G94).
//  20. perform motion (G0 to G3, G80 to G89), as modified (possibly) by G53.
//  21. stop (M0, M1, M2, M30, M60).
//
/// Code:

#include <math.h>
#include "path.h"
#include "rs274ngc_state.h"
#include "da.h"

struct path
build_path (struct rs274ngc_cst_parser parser)
{
  struct rs274ngc_state state = {
    // optionally set defaults, but ZII seems to do the job
  };

  struct path path = {0};
  double prev_x = 0;
  double prev_y = 0;

  while (rs274ngc_state_next (&state, parser)) {
    // printf ("move %2d | %10.4lf | %10.4lf | %10.4lf | %.*s\n", state.current_motion_mode, state.x, state.y, state.f, SV_FMTA (state.line));
    if (state.words_set & (X_MASK | Y_MASK)) {
      struct path_element pe = { .line = state.line };
      pe.line.end += 1;         // HACK(cmmm): this should be fixed in parser
      switch (state.distance_mode) {
      case DISTANCE_MODE_ABSOLUTE: {
        pe.x = state.x;
        pe.y = state.y;
      } break;
      case DISTANCE_MODE_INCREMENTAL: {
        pe.x = prev_x + state.x*((state.words_set & X_MASK) != 0);
        pe.y = prev_y + state.y*((state.words_set & Y_MASK) != 0);
      } break;
      }

      if (state.current_motion_mode == MOTION_RAPID_POSITIONING) {
        pe.flags |= PEF_TRAVERSING;
        pe.f = 100;             // TODO(cmmm): take as arg
      }
      else
        pe.f = state.f/60;

      if (state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CW ||
          state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CCW) {
        pe.flags |= PEF_ARC;
        if (state.words_set & R_MASK) {
          // NOTE(cmmm): maybe we can use just one sqrt
          double x0 = prev_x;
          double y0 = prev_y;
          double x1 = state.x;
          double y1 = state.y;
          double r = state.r;
          double hdx = (x1 - x0)/2;
          double hdy = (y1 - y0)/2;
          double d = sqrt (hdx*hdx + hdy*hdy);
          double nx = -hdy/d;
          double ny = hdx/d;
          double h = sqrt (r*r - d*d);
          if (r > 0 && state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CW ||
              r < 0 && state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CCW) {
            h *= -1;       // make center right
          }
          pe.i = prev_x + hdx + nx*h;
          pe.j = prev_y + hdy + ny*h;
        } else if (state.words_set & (I_MASK | J_MASK)) {
          pe.i = prev_x + state.i;
          pe.j = prev_y + state.j;
        }
        if (state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CCW)
          pe.flags |= PEF_CCW;

        // printf ("move %2d | %10.4lf | %10.4lf | %10.4lf | %10.4lf | %.*s\n", state.current_motion_mode, state.x, state.y, state.i, state.j, SV_FMTA (state.line));
        DA_APPEND (path, pe);
      } else if (pe.x != prev_x || pe.y != prev_y) {
        DA_APPEND (path, pe);
      }

      switch (state.distance_mode) {
      case DISTANCE_MODE_ABSOLUTE: {
        prev_x = state.x;
        prev_y = state.y;
      } break;
      case DISTANCE_MODE_INCREMENTAL: {
        prev_x += ((state.words_set & X_MASK) != 0)*state.x;
        prev_y += ((state.words_set & Y_MASK) != 0)*state.y;
      } break;
      }
    }

    if (state.stopping == STOPPING_END)
      break;
  }

  return path;
}

struct vec2
path_element_start (const struct path *path, const struct path_element *pe)
{
  struct vec2 start;

  if (pe == path->items) {
    start.x = path->initial_x;
    start.y = path->initial_y;
  } else {
    start.x = pe[-1].x;
    start.y = pe[-1].y;
  }

  return start;
}

/// src/path.c ends here