G-code: Difference between revisions

Content deleted Content added
No edit summary
m Reverted 1 edit by Natalielaurend (talk) to last revision by Alexander Davronov
 
Line 1:
{{short description|Primary programming language used in CNC}}
'''G-code''' is a common name for the [[programming language]] that is used for [[numerical control|NC]] and [[CNC]] [[machine tool]]s. It is defined in [[Electronic_Industries_Alliance|EIA]] RS-274-D.
{{other uses|G-code (disambiguation)|G programming language (disambiguation)}}
{{redirect|RS-274|the photoplotter format|Gerber format}}
{{More footnotes needed|date=January 2025}}
{{Ambox
| name = G-code
| subst = <includeonly>{{subst:substcheck}}</includeonly>
| issue = This article may require restoring an older revision.
| talk = RfC:_Partially_Reversing_Thumperward's_deletions
| date = May 13, 2025
}}
 
{{Infobox programming language
G-code is also the name of any word in a CNC program that begins with the letter ''G'', and generally is a code telling the machine tool what type of action to perform, such as:
| name = G-code
*rapid move
| file ext = .gcode, .mpt, .mpf, .nc and several others
*controlled feed move in straight line or arc
| paradigm = [[Procedural programming|Procedural]], [[Imperative programming|imperative]]
*series of controlled feed moves that would result in a hole being bored, a workpiece cut (routed) to a specific dimension, or a decorative profile shape added to the edge of a workpiece.
| released = {{Start date|1963}} (RS-274)
*change a [[pallet]]
| designer = [[Massachusetts Institute of Technology]]
*Set tool information such as offset.
| developer = [[Electronic Industries Alliance|Electronic Industries Association]] (RS-274), [[International Organization for Standardization]] (ISO-6983)
| implementations = Numerous; mainly [[Siemens]] Sinumerik, [[FANUC]], [[Haas Automation|Haas]], [[Heidenhain]], [[Yamazaki Mazak Corporation|Mazak]], [[Okuma Corporation|Okuma]]
| dialects =
| influenced by =
| influenced =
| programming language =
| platform =
| operating system =
| license =
| website =
| wikibooks =
}}
 
'''G-code''' (abbreviation for '''geometric code'''; also called<ref>{{cite tech report |editor1-last=Barkmeyer |editor1-first=Edward J. |editor2-last=Hopp |editor2-first=Theodore H. |editor3-last=Michael J. |editor3-first=Pratt |editor4-last=Gaylen R. |editor4-first=Rinaudot |title=Background Study: Requisite Elements, Rationale, and Technology Overview for the Systems Integration for Manufacturing Applications (SIMA) Program |date=1995 |publisher=NIST Technical Series Publications |___location=Gaithersburg, MD, USA |pages=45 |edition=NIST Interagency/Internal Report (NISTIR) 5662 |url=https://nvlpubs.nist.gov/nistpubs/Legacy/IR/nistir5662.pdf}}</ref> '''RS-274''',<ref>{{cite book |title=EIA Standard RS-274-D Interchangeable Variable Block Data Format for Positioning, Contouring, and Contouring/Positioning Numerically Controlled Machines |date=February 1979 |publisher=Electronic Industries Association |___location=2001 Eye Street, NW, Washington, D.C. 20006 |url=https://search.worldcat.org/de/title/11135300 |ref=RS-274-D}}</ref> standardized today in '''ISO 6983-1'''<ref>{{cite tech report |editor1-last=Technical Committee ISO/TC 184/SC 1 |title=ISO 6983-1:2009 Automation systems and integration — Numerical control of machines — Program format and definitions of address words; Part 1: Data format for positioning, line motion and contouring control systems |date=December 2009 |publisher=International Standards Organization |___location=Geneva, Switzerland |url=https://www.iso.org/standard/34608.html |ref=ISO 6983:2009}}</ref>) is the most widely used [[computer numerical control]] (CNC) and [[3D printing]] [[programming language]]. It is used mainly in [[computer-aided manufacturing]] to control automated [[machine tool]]s, as well as for [[Slicer (3D printing)|3D-printer slicer applications]]. G-code has many variants.
There are other codes; the type codes can be thought of like [[processor register|registers]] in a computer
:X position
:Y position
:Z position
:M code (another "action" register or Machine code(*))
:F feed rate
:S [[cutting speed|spindle speed]]
:N line number
:R Radius
:T Tool selection
:I Arc data X axis
:J Arc data Y axis.
:K Arc data Z axis.
 
G-code instructions are provided to a [[Programmable logic controller|machine controller]] (industrial computer) that tells the motors where to move, how fast to move, and what path to follow. The two most common situations are that, within a machine tool such as a [[Metal lathe|lathe]] or [[Milling (machining)|mill]], a [[cutting tool (machining)|cutting tool]] is moved according to these instructions through a toolpath cutting away material to leave only the finished workpiece and/or an unfinished workpiece is precisely positioned in any of up to nine axes<ref>Karlo Apro (2008). ''[https://books.google.com/books?id=Ws228Aht0bcC Secrets of 5-Axis Machining]''. Industrial Press Inc. {{ISBN|0-8311-3375-9}}.</ref> around the three dimensions relative to a toolpath and, either or both can move relative to each other. The same concept also extends to noncutting tools such as [[Forming (metalworking)|forming]] or [[Burnishing (metal)|burnishing]] tools, [[Gerber format|photoplotting]], additive methods such as 3D printing, and measuring instruments.
(*) M Codes control the overall machine, causing it to stop, start, turn on coolant, etc. Whereas other codes pertain to the path traversed by cutting tools.
 
== History ==
Different machine tools, however, may use the same code to perform different functions: even machines that use the same CNC control. Some may have said that "[[Fanuc]]" G-code is standard, but that is a myth.
 
The first implementation of a numerical control programming language was developed at the [[MIT Servomechanisms Laboratory]] in the 1950s. In the decades that followed, many implementations were developed by numerous organizations, both commercial and noncommercial. Elements of G-code had often been used in these implementations.<ref>{{cite book | last=Xu | first=Xun | date=2009 | url=https://books.google.com/books?id=habcATPQWJ4C | title=Integrating Advanced Computer-aided Design, Manufacturing, and Numerical Control: Principles and Implementations | publisher=Information Science Reference | page=166 | isbn=978-1-59904-716-4 | via=Google Books}}</ref><ref>{{cite book | last=Harik | first=Ramy | author2=Thorsten Wuest | date=2019 | url=https://books.google.com/books?id=O3h0EAAAQBAJ | title=Introduction to Advanced Manufacturing | publisher=SAE International | page=116 | isbn=978-0-7680-9096-3 | via=Google Books}}</ref> The first [[Technical standard|standardized]] version of G-code used in the United States, ''RS-274'', was published in 1963 by the [[Electronic Industries Alliance]] (EIA; then known as Electronic Industries Association).<ref>{{cite book | last=Evans | first=John M. Jr. | date=1976 | url=https://www.govinfo.gov/content/pkg/GOVPUB-C13-2ef4aaa5a150eedcb85a1e6985e90bfa/pdf/GOVPUB-C13-2ef4aaa5a150eedcb85a1e6985e90bfa.pdf | title=National Bureau of Standards Information Report (NBSIR) 76-1094 (R): Standards for Computer Aided Manufacturing | publisher=National Bureau of Standards | page=43}}</ref> In 1974, EIA approved ''RS-274-C'', which merged ''RS-273'' (variable block for positioning and straight cut) and ''RS-274-B'' (variable block for contouring and contouring/positioning). A final revision of ''RS-274'' was approved in 1979, as ''RS-274-D''.<ref>{{cite journal | last=Schenck | first=John P. | date=January 1, 1998 | url=https://link.gale.com/apps/doc/A20429590/GPS?sid=wikipedia | title=Understanding common CNC protocols | journal=Wood & Wood Products | publisher=Vance Publishing | volume=103 | issue=1 | page=43 | via=Gale}}</ref><ref>{{citation| title = EIA Standard RS-274-D Interchangeable Variable Block Data Format for Positioning, Contouring, and Contouring/Positioning Numerically Controlled Machines |publisher = Electronic Industries Association |___location= Washington D.C. |date=February 1979}}</ref> In other countries, the standard ''[[International Organization for Standardization|ISO]] 6983'' (finalized in 1982) is often used, but many European countries use other standards.<ref>{{cite book | last=Stark | first=J. | author2=V.&nbsp;K. Nguyen | date=2009 | url=https://books.google.com/books?id=RIgLRe12RD4C | chapter=STEP-compliant CNC Systems, Present and Future Directions | title=Advanced Design and Manufacturing Based on STEP | editor-last=Xu | editor-first=Xun | editor2=Andrew Yeh Ching Nee | publisher=Springer London | page=216 | isbn=978-1-84882-739-4 | via=Google Books}}</ref> For example, ''[[Deutsches Institut für Normung|DIN]] 66025'' is used in Germany, and PN-73M-55256 and PN-93/M-55251 were formerly used in Poland.
 
During the 1970s through 1990s, many CNC machine tool builders attempted to overcome compatibility difficulties by standardizing on machine tool controllers built by [[Fanuc]]. [[Siemens]] was another market dominator in CNC controls, especially in Europe. In the 2010s, controller differences and incompatibility were mitigated with the widespread adoption of CAD/CAM applications that were capable of outputting machine operations in the appropriate G-code for a specific machine through a software tool called a post-processor (sometimes shortened to just a "post").
{|border="1" cellpadding="2"
|+ Common Fanuc G Codes
|-
|G00 || Fast positioning
|-
|G01 || Linear interpolation
|-
|G02 || CW circular interpolation
|-
|G03 || CCW circular interpolation
|-
|G17 || X-Y plan selection
|-
|G18 || X-Z plan selection
|-
|G19 || Y-Z plan selection
|-
|G20 || Programming in inches
|-
|G21 || Programming in mm
|-
|G33 || Constant pitch threading
|-
|G34 ||Variable pitch threading
|-
|G40/G41/G42 || Tool radius compensation
|-
|G90 || Absolute programming
|-
|G91 || Incremental programming
|-
|G94/G95 || Feed
|-
|G96/G97 || Constant cutting speed/Constant rotation speed
|}
 
== Syntax ==
A standardized version of G-code known as ''BCL'' is used, but only on very few machines.
 
G-code began as a limited language that lacked constructs such as loops, conditional operators, and programmer-declared variables with [[Natural language|natural]]-word-including names (or the expressions in which to use them). It was unable to encode logic but was just a way to "connect the dots" where the programmer figured out many of the dots' locations longhand. The latest implementations of G-code include macro language capabilities somewhat closer to a [[high-level programming language]]. Additionally, all primary manufacturers (e.g., Fanuc, Siemens, [[Heidenhain]]) provide access to [[programmable logic controller]] (PLC) data, such as axis positioning data and tool data,<ref>{{cite web |archive-date=2014-05-03 |url=http://www.machinetoolhelp.com/Applications/macro/system_variables.html |title=Fanuc macro system variables |access-date=2014-06-30 |archive-url=https://web.archive.org/web/20140503030834/http://www.machinetoolhelp.com/Applications/macro/system_variables.html }}</ref> via variables used by NC programs. These constructs make it easier to develop automation applications.
G-code is understood by [http://www.gerberscientific.com/ Gerber] photoplotters, machine tool controls, and some [[machinist]]s.
 
== Extensions and variations ==
G-code files are output by [[Computer-aided manufacturing|CAM]] software such as [[Smartcam]], [[Gibbscam]], [[Featurecam]], [[Edgecam]], [[Mastercam,]] etc. G-code is also output by specialized [[CAD]] systems used to design [[printed circuit board]]s. Such software must be customized for each type of machine tool that it will be used to program.
 
Extensions and variations have been added independently by control manufacturers and machine tool manufacturers, and operators of a specific controller must be aware of the differences between each manufacturer's product.
Some CNC machines use "Conversational" programming, which is a [[wizard_(software)|wizard]]-like programming mode that either hides G-code or completely bypasses the use of G-code.
 
One standardized version of G-code, known as ''BCL'' (Binary Cutter Language), is used only on very few machines. Developed at MIT, BCL was developed to control CNC machines in terms of straight lines and arcs.<ref>{{Cite book|url=https://books.google.com/books?id=GE8vBQAAQBAJ&q=binary+cutter+language+gcode&pg=PA321|title=Information Technology Standards: Quest for the Common Byte.|last=Martin.|first=Libicki|date=1995|publisher=Elsevier Science|isbn=978-1-4832-9248-9|___location=Burlington|page=321|oclc=895436474}}</ref>
 
Some CNC machines use "conversational" programming, which is a [[wizard (software)|wizard]]-like programming mode that either hides G-code or completely bypasses the use of G-code. Some popular examples are Okuma's Advanced One Touch (AOT), Southwestern Industries' ProtoTRAK, Mazak's Mazatrol, Hurco's Ultimax and Winmax, Haas' Intuitive Programming System (IPS), and Mori Seiki's CAPS conversational software.
== Example ==
 
== See also ==
This is a generic program that demonstrates the use of G-Code to turn a 1" diameter X 1" long part. Assume that a bar of material is in the machine and that the bar is slightly oversized in length and diameter and that the bar protrudes by more than 1" from the face of the chuck. (Caution: This is generic, it might not work on any real machine! Pay particular attention to point 5 below.)
* [[Canned cycle]]
* [[Direct Numerical Control]]
* [[LinuxCNC]]
* [[List of computer-aided manufacturing software]]
 
== References ==
[[Image:ToolPath.gif|thumb|right|200px|Tool Path for program]]
{{Reflist}}
{|border="1" cellpadding="2"
|+ Sample
|-
|N01 M216 || (Turn on load monitor)
|-
|N02 G00 X20 Z20 || (Rapid move away from the part, to ensure the starting position of the tool)
|-
|N03 G50 S2000 || (Set Maximum spindle speed)
|-
|N04 T03 || (Choose tool #3 from the carousel)
|-
|N05 G97 S854 M42 M03 M08 || (Variable speed cutting, 854 ft/min, High spindle gear, Start spindle CW rotation, Turn the coolant on)
|-
|N06 G00 X1.1 Z1.1 T0303 || (Rapid feed to a point 0.1" from the end of the bar and 0.05" from the side, Use offset values for tool #3)
|-
|N07 M01 || (Optional Stop)
|-
|N08 G01 Z1.0 F.05 || (Feed in horizontally until the tool is standing 1" from the datum)
|-
|N09 X0.0 || (Feed down until the tool is on center - Face the end of the bar)
|-
|N10 G00 Z1.1 || (Rapid feed 0.1" away from the end of the bar)
|-
|N11 X1.0 || (Rapid feed up until the tool is standing at the finished OD)
|-
|N12 G01 Z0.0 || (Feed in horizontally cutting the bar to 1" diameter all the way to the datum)
|-
|N13 G00 X1.1 || (Rapid feed 0.05" away from the surface of the part)
|-
|N14 G00 X20 Z20 || (Rapid feed back out away from the work)
|-
|N15 M05 M09 || (Stop the spindle, Turn off the coolant)
|-
|N16 M215 || (Turn the load monitor off)
|-
|N17 M02 || (End of program)
|}
 
== Bibliography ==
* {{MachinerysHandbook25e}}
* {{Smid2008}}
* {{Smid2010}}
* {{Citation |last=Smid |first=Peter |year=2004 |title=Fanuc CNC Custom Macros |publisher=Industrial Press |url=https://books.google.com/books?id=YKvH-zYd3VwC&pg=PR11 |isbn=978-0-8311-3157-9 |postscript=.}}
 
== External links ==
Several points to note:
* [http://carlsonmfg.com/cnc-g-code-m-code-programming CNC G-Code and M-Code Programming]
* {{Citation |last1=Kramer |first1=T. R. |last2=Proctor |first2=F. M. |last3=Messina |first3=E. R. |title=The NIST RS274NGC Interpreter – Version 3 |date=1 Aug 2000 |id=NISTIR 6556 |journal=[[NIST]] |url=https://www.nist.gov/manuscript-publication-search.cfm?pub_id=823374 |ref=none}}
* http://museum.mit.edu/150/86 {{Webarchive|url=https://web.archive.org/web/20160319102859/http://museum.mit.edu/150/86 |date=2016-03-19 }} Has several links (including history of MIT Servo Lab)
* [http://reprap.org/wiki/G-code Complete list of G-code used by most 3D printers] at reprap.org
* [http://www.cnccookbook.com/CCCNCGCodeList.html Fanuc and Haas G-code Reference]
* [http://www.cnccookbook.com/CCCNCGCodeCourse.htm Fanuc and Haas G-code Tutorial]
* [https://www.haascnc.com/content/dam/haascnc/en/service/manual/operator/english---mill-ngc---operator's-manual---2017.pdf Haas Milling Manual]
* [https://www.cncknowledge.in/2020/05/haas-cnc-g-code-list-for-lathe-milling.html G Code For Lathe & Milling]
* [https://www.cncknowledge.in/2020/05/haas-cnc-m-code-list-for-lathe-milling.html M Code for Lathe & Milling]
 
{{Metalworking navbox|machopen}}
1) There is room for some programming style, even in this short program. The grouping of codes in line N05 could have been put on multiple lines. Doing so may have made it easier to follow program execution.
 
[[Category:Computer-aided engineering]]
2) Many codes are "Modal" meaning that they stay in effect until they are cancelled or replaced by a contradictory code. For example, once variable speed cutting had been selected (G97), it stayed in effect until the end of the program. In operation, the spindle speed would increase as the tool neared the center of the work in order to maintain a constant cutting speed. Similarly, once rapid feed was selected (G00) all tool movements would be rapid until a feed rate code (G01, G02, G03) was selected.
 
3) It is common practice to use a load monitor with CNC machinery. The load monitor will stop the machine if the spindle or feed loads exceed a preset value that is set during the set-up operation. The job of the load monitor is to prevent damage in the event of tool breakage. Furthermore, to some extent it can warn of a tool that that is becoming dull and needs to be replaced or sharpened.
 
4) It is common practice to bring the tool in rapidly to a "safe" point that is close to the part - in this case 0.01" away - and then start feeding the tool. How close that "safe" distance is, depends on the skill and comfort level of the programmer.
 
5) If the program is wrong. There is a high probability that the machine will crash! That can be very costly. It is wise to intersperse the program with optional stops (M01 code) which allow the program to be run piecemeal. This gives the programmer a better view of the overall progress of the cutting cycle. The optional stops remain in the program but they are skipped during the normal running of the machine.
 
 
 
==See also==
*[[Direct Numerical Control]]
*[[Gerber file]]
 
==External links==
* [http://www.isd.mel.nist.gov/personnel/kramer/pubs/RS274NGC_3.web/RS274NGC_3TOC.html The NIST RS274NGC Interpreter]
 
 
{{Metalworking - CNC, CAD, and CAM}}
[[Category:CNC, CAD, and CAM]]
[[Category:CAD file formats]]
[[Category:Domain-specific programming languages]]
[[Category:Encodings]]
[[Category:ManufacturingMetalworking]]
 
[[pl:G code]]