Mitsubishi Electric
Published

Computer Programming-Related Features of Custom Macro

Custom macro is an interpreter-based language, meaning that all CNC G code and custom macro commands are executed as the CNC comes across them.

Share

Operator hand pushing a button

In last month’s column, we described five application categories for custom macro. Here, we dig into custom macro features.

The two categories of custom macro features are computer programming-related features and CNC-related features. This article examines the following computer programming-related features:

  • Variables
  • Arithmetic
  • Program flow control (logic)
  • Looping

These features can be found in any computer programming language. While most current computer programming languages are compiler-based, custom macro is an interpreter-based language. This means that all CNC G code and custom macro commands are executed as the CNC comes across them. This gives a custom macro the ability to make decisions in real time.

Variables are storage registers for values. They can be used to represent just about anything, including — among many other things — component dimensions, spindle speeds, feed rates, calculated values or “flags” that tell the custom macro how to behave. You can use variables in place of hard-and-fixed values or to help with calculations needed within the CNC program.

There are five kinds of variables in custom macro:

  1. Letter address arguments – One way to get data into the custom macro is to use a G65 or G66 command and specify a series of letter address arguments (input data). They resemble the various values placed in a hole machining canned cycle, like X, Y, R, Z and so on.
  2. Local variables – Ranging from #1-#33, the primary application for local variables is to represent the letter address arguments from the calling G65 or G66 command. Local variable #1, for instance, is used in the custom macro to represent the value of letter address “A” specified in the calling command. Local variables are reset to null at the completion of the custom macro’s execution (at M99).
  3. Common variables – Ranging from #100 through #149, these variables can be shared among programs, including multiple custom macros. They are set back to null at the completion of the program (at M30 or M02) or when the power is turned off (determined by parameter setting).
  4. Permanent common variables – Ranging from #500 through at least #549, these variables are retained after the power is turned off. In this regard, they are much like tool offset registers.
  5. System variables – Ranging over #1000, these variables provide access to many of the CNC-related features of custom macro.

Example that drills a hole:

Main program:

  • O0001
  • N005 T01 M06 (Drill)
  • N010 G54 G90 S500 M03
  • N015 G00 X0 Y0
  • N020 G43 H01 Z1.0 M08
  • N025 G65 P1000 X3.0 Y2.5 R0.1 Z-0.8 F10.0 (local variable assignments: X:#24, Y#25, R#18, Z#26, F:#9)
  • N030 G91 G28 Z0

Custom macro:

  • O1000
  • G00 X#24 Y#25
  • Z#18
  • G01 Z#26 F#9
  • G00 Z#18
  • M99

Arithmetic provides the ability to make calculations. Just about anything that can be done on a scientific calculator can be done with custom macro. This includes basic functions like add (+), subtract (-), multiply (*), and divide (/), as well as higher level functions like trig functions including sine (SIN), cosine (COS) and tangent (TAN), as well as other functions like square root (SQRT) and rounding (ROUND, FIX and FUP).

Program flow control (logic) provides decision-making capabilities. An IF statement is used to specify the test. If the test result is true, the custom macro will do what is to the right of the IF statement. If the test is false, the custom macro will execute the next command. Logical operators provide what it is you want to test against.

  • Equal to (EQ)
  • Not equal to (NE)
  • Greater than (GT)
  • Greater than or equal to (GE)
  • Less than (LT)
  • Less than or equal to (LE)

A GOTO statement is used to branch from one point in the custom macro to another. Sequence numbers are used as statement labels, branching points to which program execution will be sent.

Example that decides whether to use coolant:

  • O0001
  • N005 T01 M06 (Drill)
  • N010 G54 G90 S500 M03
  • N015 G00 X0 Y0
  • N020 G43 H01 Z1.0
  • N025 G65 P1000 X3.0 Y2.5 R0.1 Z-0.8 F10.0 C1.0 (C: #3)
  • N030 G91 G28 Z0

If letter address C (local variable #3) is set to 1.0, coolant will be turned on. If it is set to 0, coolant will be turned off.

  • .
  • IF [#3 EQ 0] GOTO 15 (Test)
  • M08 (Coolant comes on)
  • GOTO 20
  • N15 M09 (Coolant stays off)
  • N20 (Program continues)
  • .

Looping provides the ability to repeat a group of commands, while enabling certain things to change between executions. This can be a very powerful feature, generating many actions (like CNC motions or offset settings) based on only a few commands.

A WHILE statement can be used to specify a loop. As long as the condition within brackets is true, the loop will continue executing. When the condition is no longer true, the loop will end.

Example that counts to ten:

  • .
  • #100 = 1.0 (Counter)
  • WHILE [#100 LE 10.0] DO 1 (Test)
  • (Perform the action/s for each execution of the loop here)
  • #100 = #100+1 (Step counter)
  • END 1 (Go back to test)
  • N15 (Program continues)
  • .
Mitsubishi Electric
High Accuracy Linear Encoders
An ad for Formnext Chicago on April 8-10, 2025.
Hurco
SolidCAM
Koma Precision
MMS Made in the USA
EZ Access - Have it all with Ez - Mazak
Discover a variety of the best CNC machines
Techspex
Gardner Business Intelligence
BIMU 2024

Related Content

5 Tips for Running a Profitable Aerospace Shop

Aerospace machining is a demanding and competitive sector of manufacturing, but this shop demonstrates five ways to find aerospace success.

Read More

5 G-Code Tips for Increasing CNC Efficiency

Optimizing G code is a low-cost way to improve CNC efficiency without sacrificing usability and safety.

Read More

Generating a Digital Twin in the CNC

New control technology captures critical data about a machining process and uses it to create a 3D graphical representation of the finished workpiece. This new type of digital twin helps relate machining results to machine performance, leading to better decisions on the shop floor.

Read More
Five-Axis

6 Machine Shop Essentials to Stay Competitive

If you want to streamline production and be competitive in the industry, you will need far more than a standard three-axis CNC mill or two-axis CNC lathe and a few measuring tools.

Read More

Read Next

CNC & Machine Controls

5 Reasons Why You Should Know How to Write Custom Macros

Custom macros enhance what can be done in G-code programs, giving users the ability to code operations that were previously not possible.

Read More

3 Mistakes That Cause CNC Programs to Fail

Despite enhancements to manufacturing technology, there are still issues today that can cause programs to fail. These failures can cause lost time, scrapped parts, damaged machines and even injured operators.

Read More

Encountering Surface Finishes in the Everyday World

Surface measurement is becoming increasingly important to ensure proper performance of a manufactured product. Advanced surface measurement tools are not only beneficial in the manufacturing industry but also have unconventional applications.

Read More
Mitsubishi Electric