6 months by cncdivi
Accessing Modal Data in G-Code
CNCCookbook’s G-Code Tutorial
Introduction to Modal Data
If there is one thing I hope you’ve learned by now about g-code, it is that g-codes and CNC controllars are very modal. They remember certain states they’re in and continue to act on those states. For example, when you issue the G01 code for line motion, the controller takes all of the coordinates you give it as instructions to move in a straight line until you switch to another motion mode (perhaps arcs, using G02 or G03). When you’re writing g-code subprograms, it is therefore helpful if the subprogram can access or manipulate the modes.
Let’s consider what happens if you can’t manipulate the modes. For example, suppose you want to create a subprogram that makes a circular pocket at the current location whose radius is based on the “R” word. That’d be a pretty handy subprogram to have (although you could also quickly generate the g-code for such a pocket using our Conversational CNC Hole Wizard). Such a routine might be considerably easier to create if you could use relative motions by setting G91. But what happens when you go to return to the main program?
Without some ability to access the mode information, you just have to leave things in relative mode, or perhaps gratuitously assume the main program is using absolute motion and execute a G91 at the end if your subprogram. The truth is, you don’t really know what to do, unless you can determine what mode was in use–absolute or relative–when the subprogram was entered. If you can find that out, you can save that information in a #-variable and then restore the mode just before the subprogram returns. Now you have a subprogram that knows how to clean up after itself.
Using System Variables to Access Modal Data
There’s good news–many controllers will let you access the Modal Data using System Variables. This is typically done with System Variables in the #4xxx range. Here is a brief list of some of the more commonly used System Variables when it comes to accessing Modal Data:
#4001: G00 G01 G02 G03
#4002: G17 G18 G19
#4006: G20 G21
#4007: G40 G41 G42
#4109: F
#4119: S
#4120: T
Not every controller supports the same modes, these are just the ones most widely supported. For example, the G90/G91 modes we were just talking about aren’t here. On most Fanuc controls, you can access G90/G91 using #4003.
Now, if you wanted to save, for example, the Feedrate that was in effect, you could write something like #100 = #4109. Now you’ve got the Feedrate stored in the #100 variables.
Setting Modes With System Variables
Now having gotten the Feedrate into #100, how do we restore it when the subprogram returns via M99?
It’s simple:
F#100 (Reset Feedrate to the value in #100)
Okay, now here is a little bit trickier variation. Let’s save the motion mode:
#100 = #4001 (Save the motion mode to #100)
If we look at the value in #100, it will be one of the following:
00 = G00
01 = G01
02 = G02
03 = G03
That’s pretty clever, now here is how we set the control to use the motion mode stored in #100:
G#100
I remember that one really boggled my mind a bit when I first saw it. After all, #100 could contain almost anything. But, it works and it gives us exactly what we’re looking for in terms of setting up our modes.
The G-Wizard CNC Editor Can Help You Use and Debug System Variables That Access Modal Information
G-Wizard Editor implements exactly the #4xxx variables described as being the most common above. Over time, we’ll add more variables, and they’ll be dependent on which canned post you use (since each controller offers a little different set of variables), but that list above is what you can really count on. As you step through your program with the simulator/debugger, you’ll see the mode information in the little window right under the backplot, and you’ll also see the information that gets stored or accessed from your #-variables. In this way, you can use GW Editor to help you debug your use of Modal Data in your g-code programs.
Mode Snippets
G-Wizard Editor also has a feature called Code Snippets that lets you save snippets of g-code for use in programming. Even better, there are two special snippets, “Save Modes” and “Restore Modes” aimed at using #4xxx variables in your programming. After reading this chapter, their use should be self-explanatory. Here’s what they look like:
Exercises
1. Look up exactly which #4xxx variables your control supports so you can see which Modal Data you have access to. It’s probably more than the few common ones we mention here.
2. Write a subprogram that uses the #4xxx variables to do something useful.
3. Use G-Wizard Editor to debug some #4xxx programming. You’ll have to stick to those variables it has actually implemented, which are described above.
4. Set up your GWE “Save Modes” and “Restore Modes” snippets to reflect which modes you want to make sure are saved and restored for your macro programming.
Try the Free Trial Version of G-Wizard CNC Program Editor…
No credit card required–just your name and email.
Next Article: Modal Macro Calls
Like what you read on CNCCookbook?
Join 100,000+ CNC'ers! Get our latest blog posts delivered straight to your email inbox once a week for free. Plus, we’ll give you access to some great CNC reference materials including:
- Our Big List of over 200 CNC Tips and Techniques
- Our Free GCode Programming Basics Course
- And more!
Just enter your name and email address below:
100% Privacy: We will never Spam you!
Recently updated on March 23rd, 2024 at 06:35 am
Bob is responsible for the development and implementation of the popular G-Wizard CNC Software. Bob is also the founder of CNCCookbook, the largest CNC-related blog on the Internet.