Gcode… The Stuff That Dreams Are Made Of
Welcome to another MachMotion minute! In this article we will be talking about the main difference between Gcode and Mcode, creating a Gcode file, catching mistakes and finally how to simplify and refine movements using Gcode's built in functions and routines. Let's make your day a little more automated!
We will be walking through the basic difference of a Gcode and Mcode, writing some Gcode and making mistakes so we can detect and correct them. I'll show you how to improve a Gcode file by removing unnecessary calls to simplify the process. We will explore all of the available Gcode built in functions and how to use them for advance movement and routines. We have a lot to go over so let's get started!
MachMotion provides industrial retrofits and controls, they also provide specialty solutions for application/processes that require unique control solutions.
Additionally, MachMotion provides top of the line consultation and support. They are dedicated to their customers and deliver high-end products and services.
For more information or to speak to a specialist please visit MachMotion: https://machmotion.com
Any Word that Starts with "G" is Good Enough for Me
How did RS-274 get the name G-Code?
Many people speculate that the "G" stands for "General" and that seems to make sense. The RS-274 language is based on lines of code, each line also known as a "Block" can contain code so that seem to be where we get "Code" in G-Code.
Typically each line of RS-274 consists of a line number followed by a "word". A word in RS-274 consists of a letter followed by a number and you guessed it, the most common words used in RS-274 is "G", we can then speculate this is where the "G" in G-code was derived from.
What is G-Code Good for?
G-Code has many different flavors depending on the platform you are using it on. To this day G-Code still tops the charts as the most used numerical control (NC) programming language. G-Code is commonly used for computer-aid manufacturing machines. At its roots people mostly use G-Code to tell the motors where to move (X,Y,Z etc.…) and how fast to go (Feed Rate), this path is typically the tool tip (zero point) and can clear, add or measure material through the workspace and along a path that is sometimes called the "toolpath".
*Bonus did you know: John T. Parsons filed the first patent for a "Motor Controlled Apparatus for Positioning Machine Tool" in 1952, 6 years later Fanuc was founded.
Introducing the Post Processor
G-Code Extensions and variations have been used over the years by many companies, to overcome all of these different type of G-Code. In the 2010's a post process operations needed to be developed to adapted to particular feature or functions for these specific machines. In came the post processor to solve each controller differences and incompatibilities and has been used for the last decade. Additionally added later were "Conversational" programming.
Conversational programming brought with it a high-level programming language that filled in the limitations of G-Code and made available complex loops, conditional operations and easy to read variables.
*Bonus did you know: The letter N is used for numbering lines or blocks of code and is ignored by CNC machines.
Main Difference Between G-Code and Mcode?
G-Code (Geometric/preparatory) is typically used for motion and positioning (Preparatory function), while Mcode (Miscellaneous Function) is used for machine functioning (Miscellaneous function).
For example the following would typically use G-Code:
- Rapid movement
- Controlled feed in a straight line or arc
- Set tool information such as offset
- Plain Selection
For example the following would typically use Mcode:
- Spindle ON/OFF
- Coolant ON/OFF
- Open workstation door
- Tool changing
- User created custom functions
Code, Group, Description and Modal
Each G-code command call can be summed up using 4 properties, this will help with tracking down the G-code that you want to use.
Call name that will be used to tell the interpreter what to do, these will always start with the letter G followed by a number.
Motion, Compensation, Coordinates, Canned or Other. Each category reference to a type of function. Collection of G codes that control the same function or mode, i.e. G90 and G91 positioning modes.
What it will do and what parameters if any, some include examples.
Active until a code from the same group is called. For example; G01 is modal so it is not necessary to put it in consecutive blocks. Once active every successive positioning block will be in the G1 mode unless another code from group one is called (G00, G02, G03, etc.). All G codes not in group 0 behave this way.
* Bonus did you know: Fanuc Siemens was founded in 1958 and dominated the CNC market in the 1970's. During this time they attempted to universally standardize G-Code.
Making Your Dreams Come True
In writing G-Code programs there are some rules to be aware of as well as some general formatting guidelines that should be followed or at least considered.
An important point to remember when reading this in describing motion of a machine it will always be described as tool movement relative to the work piece. In many machines the work piece will move in more axes than the tool; however the program will always define tool movement around the work piece. Axes directions follow the right hand rule, see below.
The first part of any program should be a safe start up block. This line of code is used to make sure that some modes are disabled and others are set to their most common setting.
An example safe start block would look like this: G00 G90 G17 G54 G40 G49 G80
- G00 – Rapid mode
- G90 – Absolute position mode
- G17 – XY plane select
- G54 – Fixture offset 1
- G40 – Cutter compensation (tool diameter) cancel
- G49 – Length offset cancel
- G80 – Canned cycle cancel
It is recommended that this safe start block be used at the start of the program and also before or immediately following every tool change. It is common to restart a program from a tool change, having the safe start line there can greatly reduce the chance of a machine not acting as expected, the results of which can be aggravating at best and a crash at worst. The safe start block shown here is just an example. Every machine and every programmer are a little different and each will have their own start up block.
*Bonus did you know: G-code is also called "preparatory code" and is any word that begins with the letter "G" in a NC program.
End of Program (EOF)
Jumping to the end of the program there is not a lot required. Typically there will be a couple blocks of code to return the Z axis to the home position and maybe move the work piece closer to the operator for easier loading and unloading of parts. Shutting off the spindle and coolant or any other accessories is also a good idea here. The final block in a program is a program end code, most commonly M30 but there are other options. Make sure this final block is followed by an end of block. It is easy to forget this last EOB in a program for Mach because it is just a carriage return and not always readily apparent. One way to make sure that there is always an EOB on your program end block is to follow it with %. Like this:
This percent sign is a familiar symbol to CNC programmers in industry; however any symbol or character can be used as it will not be read by the control because of the program end before it. If there is no EOB following the percent sign it will not show up in the program when loaded into Mach. In between the start and end is the body of the program. There are a few rules here. Each block of code will contain a combination of words. Multiple G codes can be specified in a single block, however if more than one from the same modal group is specified the last one in the block will be valid, with the exception of group 00. Modal G codes stay active until another from the same group is G & M Code called.
For example; G01 is modal so it is not necessary to put it in consecutive blocks. Once active every successive positioning block will be in the G1 mode unless another code from group one is called (G00, G02, G03, etc.). All G codes not in group 0 behave this way.
Only one M code can be specified in a single block. Same holds true for all other words.
Generally leading zeroes are not required in G Code. For example G01 and G1 are the same. The same holds true for M codes, position commands, feed rates, etc. When specifying values for position, feed rate, variables, etc., it is good practice to always use a decimal point and trailing zero, instead of X1 use X1.0. Although the decimal point is not required (in Mach X1 = X1.0) it is HIGHLY recommended.
For more G-Code example go here: https://machmotion.com/blog/g-code-examples
*Bonus did you know: G-Code was designed by Massachusetts Institute of Technology in 1950.
Best Practices To Minimize G-Code Mistakes
Although most of your G-code might be produced in a CAM software there is still a number of things that can go wrong. Suppose that you have a fixture or additional material you didn’t account for.
Here are some best practices to minimize G-code issue and break bad habits.
It’s a good habit to format your code in a way that it is easy to spot what is a command and what is commented out. Capitalize all G-code functions and use lower case for text comments.
- Cheat Sheet
Know matter how long you've been using G-code, no one is going to remember all of the details of each function. Keep a reference of all of the G-code and M-Code calls, it's important to add to these as you develop your own and this is a great way to document what they are for and how to use them.
It is so very important to simulate twice and cut once, this can save not only time but possible injury. Always take your time and step back to look at the big picture. This will not only make things more enjoyable and run smoother but also lower your blood pressure.
Most CAD software will give you a summary of the program. This is important as it’s a list of what it thinks you want to do and what it is your doing it to. Check this list over and make sure the safety zero, material dimensions and tool sizes are correct. It would be worth printing this out and hold it next to you while running your machine.
If an estimated time (ETA) seems too long or to short most of the time that is an indicator that something is off usually the tool size or feed rate.
*Bonus did you know: Verbose G-Code is an option is some software that allows to export all settings as comments and define each call.
Wizards to Simplify the Process
Some CNC machines use "Wizards" which is a quick way to create a job. Wizards are intended to bridge the gap between a G-Code editor and full featured CAM software package. Some examples of available wizards are CV Feed Rate, Lathe Canned Cycles, Probing etc.
* Bonus did you know: MachMotion has a plethora of information, examples and is available for free to download: https://machmotion.com/blog/knowledge-g-code
I hope this helps you get the most out of G-code and helps to a more productive experience. As always we appreciate a thumbs up and if you’re not already doing so, please subscribe to receive notifications for new videos and articles.
Please visit our website for more information: https://machmotion.com
Subscribe For More Articles: https://machmotion.com/newsletter-subscription
Subscribe For More Videos: https://www.youtube.com/user/MachMotionVideo
Follow Us On Twitter: https://twitter.com/machmotion?lang=en
Join Us On Facebook: https://www.facebook.com/MachMotion/
Share With Us On Instagram: https://www.instagram.com/explore/tags/machmotion/
Author Jason Blake
Contact Sales: [email protected] or call 573-368-7399
MachMotion CNC Mill Control Kit: https://machmotion.com/retrofits-main-mill
G-Code List: https://machmotion.com/blog/knowledge-support-gcode
Mcode List: https://machmotion.com/blog/knowledge-support-mcode
CNC Cook book: http://www.cnccookbook.com