91ÊÓÆµÍøÕ¾ÎÛ

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

Leaders-In background

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)
  • .
MMS Leaders in CNC Machining

Related Content

2 Secondary Coordinate Systems You Should Know

Coordinate systems tell a CNC machine where to position the cutting tool during the program’s execution for any purpose that requires the cutting tool to move.

Read More

4 Commonly Misapplied CNC Features

Misapplication of these important CNC features will result in wasted time, wasted or duplicated effort and/or wasted material.

Read More

Can AI Replace Programmers? Writers Face a Similar Question

The answer is the same in both cases. Artificial intelligence performs sophisticated tasks, but falls short of delivering on the fullness of what the work entails.

Read More
Sponsored

Continuous Improvement and New Functionality Are the Name of the Game

Mastercam 2025 incorporates big advancements and small — all based on customer feedback and the company’s commitment to keeping its signature product best in class.

Read More

Read Next

CNC Tech Talks

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
Automation

AMRs Are Moving Into Manufacturing: 4 Considerations for Implementation

AMRs can provide a flexible, easy-to-use automation platform so long as manufacturers choose a suitable task and prepare their facilities.

Read More
Top Shops

Machine Shop MBA

  Making Chips and 91ÊÓÆµÍøÕ¾ÎÛ are teaming up for a new podcast series called Machine Shop MBA—designed to help manufacturers measure their success against the industry’s best. Through the lens of the Top Shops benchmarking program, the series explores the KPIs that set high-performing shops apart, from machine utilization and first-pass yield to employee engagement and revenue per employee.  

Read More