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.
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:
- 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.
- 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).
- 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).
- 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.
- 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)
- .
Related Content
How this Job Shop Grew Capacity Without Expanding Footprint
This shop relies on digital solutions to grow their manufacturing business. With this approach, W.A. Pfeiffer has achieved seamless end-to-end connectivity, shorter lead times and increased throughput.
Read MoreHow to Mitigate Chatter to Boost Machining Rates
There are usually better solutions to chatter than just reducing the feed rate. Through vibration analysis, the chatter problem can be solved, enabling much higher metal removal rates, better quality and longer tool life.
Read More5 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 MoreFearless Five-Axis Programming Fosters Shop Growth
Reinvestment in automation has spurred KCS Advanced Machining Service’s growth from prototyping to low-and mid-volume parts. The key to its success? A young staff of talented programmers.
Read MoreRead Next
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 MoreThe Future of High Feed Milling in Modern Manufacturing
Achieve higher metal removal rates and enhanced predictability with ISCAR’s advanced high-feed milling tools — optimized for today’s competitive global market.
Read MoreInside Machineosaurus: Unique Job Shop with Dinosaur-Named CNC Machines, Four-Day Workweek & High-Precision Machining
Take a tour of Machineosaurus, a Massachusetts machine shop where every CNC machine is named after a dinosaur!
Read More