Digital Readout Kit for Mills, Lathes, & Grinding
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)
  • .
DN Solutions
Hurco
High Accuracy Linear Encoders
Norton Superabrasives Wheels  Paradigm Plus
Techspex
CHIRON Group, one stop solution for manufacturing.
World Machine Tool Survey
To any Measurement Question there is an Answer
Kennametal
Precision grinding & hard turning custom solutions
IMTS 2024
VERISURF

Related Content

SPONSORED

Swiss-Type Control Uses CNC Data to Improve Efficiency

Advanced controls for Swiss-type CNC lathes uses machine data to prevent tool collisions, saving setup time and scrap costs.

Read More
SPONSORED

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 More

Five Safety Considerations for CNC Machinists

Safety in CNC environments is essential for users – and for productivity. Consider these 5 points to avoid injury, part failure and downtime.

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

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
Large Part Machining

The Cut Scene: The Finer Details of Large-Format Machining

Small details and features can have an outsized impact on large parts, such as Barbco’s collapsible utility drill head.

Read More
CNC Turnkey Package for Knee Mills and Lathes