From cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 12 Nov 2026 01:28:13 +0300 Subject: publish --- src/path.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/path.c (limited to 'src/path.c') 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 @@ +/// src/path.c +// +// Copyright (C) 2026 by Martin Michalec +// +// 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 +#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 -- cgit v1.3.1