aboutsummaryrefslogtreecommitdiff
path: root/src/path.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/path.c b/src/path.c
new file mode 100644
index 0000000..7ded2d2
--- /dev/null
+++ b/src/path.c
@@ -0,0 +1,145 @@
1/// src/path.c
2//
3// Copyright (C) 2026 by Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7/// Commentary:
8//
9// Order of execution:
10// 1. comment (includes message).
11// 2. set feed rate mode (G93, G94 — inverse time or per minute).
12// 3. set feed rate (F).
13// 4. set spindle speed (S).
14// 5. select tool (T).
15// 6. change tool (M6).
16// 7. spindle on or off (M3, M4, M5).
17// 8. coolant on or off (M7, M8, M9).
18// 9. enable or disable overrides (M48, M49).
19// 10. dwell (G4).
20// 11. set active plane (G17, G18, G19).
21// 12. set length units (G20, G21).
22// 13. cutter radius compensation on or off (G40, G41, G42)
23// 14. cutter length compensation on or off (G43, G49)
24// 15. coordinate system selection (G54, G55, G56, G57, G58, G59,
25// G59.1, G59.2, G59.3).
26// 16. set path control mode (G61, G61.1, G64)
27// 17. set distance mode (G90, G91).
28// 18. set retract mode (G98, G99).
29// 19. home (G28, G30) or change coordinate system data (G10) or set
30// axis offsets (G92, G92.1, G92.2, G94).
31// 20. perform motion (G0 to G3, G80 to G89), as modified (possibly) by G53.
32// 21. stop (M0, M1, M2, M30, M60).
33//
34/// Code:
35
36#include <math.h>
37#include "path.h"
38#include "rs274ngc_state.h"
39#include "da.h"
40
41struct path
42build_path (struct rs274ngc_cst_parser parser)
43{
44 struct rs274ngc_state state = {
45 // optionally set defaults, but ZII seems to do the job
46 };
47
48 struct path path = {0};
49 double prev_x = 0;
50 double prev_y = 0;
51
52 while (rs274ngc_state_next (&state, parser)) {
53 // 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));
54 if (state.words_set & (X_MASK | Y_MASK)) {
55 struct path_element pe = { .line = state.line };
56 pe.line.end += 1; // HACK(cmmm): this should be fixed in parser
57 switch (state.distance_mode) {
58 case DISTANCE_MODE_ABSOLUTE: {
59 pe.x = state.x;
60 pe.y = state.y;
61 } break;
62 case DISTANCE_MODE_INCREMENTAL: {
63 pe.x = prev_x + state.x*((state.words_set & X_MASK) != 0);
64 pe.y = prev_y + state.y*((state.words_set & Y_MASK) != 0);
65 } break;
66 }
67
68 if (state.current_motion_mode == MOTION_RAPID_POSITIONING) {
69 pe.flags |= PEF_TRAVERSING;
70 pe.f = 100; // TODO(cmmm): take as arg
71 }
72 else
73 pe.f = state.f/60;
74
75 if (state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CW ||
76 state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CCW) {
77 pe.flags |= PEF_ARC;
78 if (state.words_set & R_MASK) {
79 // NOTE(cmmm): maybe we can use just one sqrt
80 double x0 = prev_x;
81 double y0 = prev_y;
82 double x1 = state.x;
83 double y1 = state.y;
84 double r = state.r;
85 double hdx = (x1 - x0)/2;
86 double hdy = (y1 - y0)/2;
87 double d = sqrt (hdx*hdx + hdy*hdy);
88 double nx = -hdy/d;
89 double ny = hdx/d;
90 double h = sqrt (r*r - d*d);
91 if (r > 0 && state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CW ||
92 r < 0 && state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CCW) {
93 h *= -1; // make center right
94 }
95 pe.i = prev_x + hdx + nx*h;
96 pe.j = prev_y + hdy + ny*h;
97 } else if (state.words_set & (I_MASK | J_MASK)) {
98 pe.i = prev_x + state.i;
99 pe.j = prev_y + state.j;
100 }
101 if (state.current_motion_mode == MOTION_CIRCULAR_INTERPOLATION_CCW)
102 pe.flags |= PEF_CCW;
103
104 // 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));
105 DA_APPEND (path, pe);
106 } else if (pe.x != prev_x || pe.y != prev_y) {
107 DA_APPEND (path, pe);
108 }
109
110 switch (state.distance_mode) {
111 case DISTANCE_MODE_ABSOLUTE: {
112 prev_x = state.x;
113 prev_y = state.y;
114 } break;
115 case DISTANCE_MODE_INCREMENTAL: {
116 prev_x += ((state.words_set & X_MASK) != 0)*state.x;
117 prev_y += ((state.words_set & Y_MASK) != 0)*state.y;
118 } break;
119 }
120 }
121
122 if (state.stopping == STOPPING_END)
123 break;
124 }
125
126 return path;
127}
128
129struct vec2
130path_element_start (const struct path *path, const struct path_element *pe)
131{
132 struct vec2 start;
133
134 if (pe == path->items) {
135 start.x = path->initial_x;
136 start.y = path->initial_y;
137 } else {
138 start.x = pe[-1].x;
139 start.y = pe[-1].y;
140 }
141
142 return start;
143}
144
145/// src/path.c ends here