question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to debug a macro

See original GitHub issue

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:closed
  • Created 5 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
chuyskywalkercommented, Mar 20, 2019
0reactions
chuyskywalkercommented, Mar 21, 2019

Hey, that example code looks familiar! Jokes aside, very cool and should help a lot in further runs of things, thanks 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debugging in Excel VBA (Easy Macros)
Debugging · 1. Empty the range A1:E2. · 2. Open the Visual Basic Editor and reduce the size of the screen so that...
Read more >
Excel Macros - Debugging a Code - Tutorialspoint
VBA Debugging ; Stepping Through Code; Using Breakpoints; Backing Up or Moving Forward in Code; Not Stepping Through Each Line of Code; Querying...
Read more >
Excel Macro Troubleshooting Tips Debug and F8 Key Problems
To stop the macro, click the End button. · To get information about the error, click Help. · To go to the VBE...
Read more >
Test a macro by using Single Step mode - Microsoft Support
To carry out the action shown in the Macro Single Step dialog box, click Step. · To stop the macro and close the...
Read more >
Debugging a Macro - Microsoft Excel Tips
The process of debugging a macro in VBA is the same as debugging in any other programming language. All you need to do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found