How to debug a macro
See original GitHub issueIssue Description
Description
Looking for information about how Macros are evaluated and how one could debug them when they are going wrong.
Version | cncjs desktop osx 1.19.17 |
---|---|
CNC Controller | Grbl |
Hardware | Laptop |
OS | OSX |
Details
I have a macro I’m trying to suss out that should be able to find the center for WCS setting by probing three points within a circle cutout. However, when I run this, I eventually get something like:
G0 XNaN YNaN
which, as I have some passing familiarity with Javascript, is JSpeak for “you did a bad thing with numbers and I’m confused”.
However, when I plug all these values into a JS console in my browser, it all maths out just fine.
So, while I’d love someone to say “Oy, line 54 has error X on it” to fix my macro, what I think might be more helpful is: How does one debug macros? How are they evaluated?
(Based on an answer in #250 it looks like the data in between brackets []
is just, like, eval()
ed? I couldn’t quite figure out where the codebase does this to understand it…)
Here’s the macro in question:
;Start with probe in hole, BELOW Z surface
; Wait until the planner queue is empty
%wait
; Set user-defined variables
%PROBE_DISTANCE = 20
%PROBE_FEEDRATE_A = 150
%PROBE_FEEDRATE_B = 50
; for restoration at the end
%UNITS=modal.units
%DISTANCE=modal.distance
G91 ; Relative positioning
G21 ; Use millimeters
G10 L20 X0 Y0 ; set current position as xy zero for now
; Probe rear, slight right
G38.2 X1 Y[PROBE_DISTANCE] F[PROBE_FEEDRATE_A]
G1 Y-2 F[PROBE_FEEDRATE_B] ; back off a bit
G38.2 Y3 F[PROBE_FEEDRATE_B] ; probe again, slowly
%p1x = [posx] ; set the x/y coords for later
%p1y = [posy]
; set back to absolute mode and return to original spot
G90
G0 X0 Y0
G91
; Probe 45deg
G38.2 X[PROBE_DISTANCE] Y[PROBE_DISTANCE] F[PROBE_FEEDRATE_A]
G1 X-2 Y-2 F[PROBE_FEEDRATE_B] ; back off a bit
G38.2 X3 Y3 F[PROBE_FEEDRATE_B] ; probe again, slowly
%p2x = [posx] ; set the x/y coords for later
%p2y = [posy]
; set back to absolute mode and return to original spot
G90
G0 X0 Y0
G91
; Probe slight rear, far right
G38.2 X[PROBE_DISTANCE] Y1 F[PROBE_FEEDRATE_A]
G1 X-2 F[PROBE_FEEDRATE_B] ; back off a bit
G38.2 X3 F[PROBE_FEEDRATE_B] ; probe again, slowly
%p3x = [posx] ; set the x/y coords for later
%p3y = [posy]
; Figure out proper center https://stackoverflow.com/a/30106470 and set it
%ma = [(p2y - p1y) / (p2x - p1x)]
%mb = [(p3y - p2y) / (p3x - p2x)]
%cx = [(ma * mb * (p1y - p3y) + mb * (p1x + p2x) - ma * (p2x + p3x)) / (2 * (mb - ma))]
%cy = [(-1 / ma) * (cx - (p1x + p2x) * 0.5) + (p1y + p2y) * 0.5]
; Return to origin, move to new center, set center
G90
G0 X0 Y0
G91
G0 X[cx] Y[cy]
%wait
G10 L20 X0 Y0
[UNITS] [DISTANCE] ;restore unit and distance modal state
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (2 by maintainers)
Worked like a charm, thanks!
Hey, that example code looks familiar! Jokes aside, very cool and should help a lot in further runs of things, thanks 😃