banner



Computer Programming Macro Meaning

CNC programs use several techniques to allow the programmer to create complex pieces of code using CNC code building blocks. This article reviews the use of three common methods which are subprograms, macros, and parametric programming.

Subprograms

Subprograms (also known as subroutines), are specially designed programs that can be called on demand by another program and repeated as desired. Such repetition can take place in one program or in several programs. The content of standard subprograms is fixed and can be used only for the intended, specific purpose. For variable type subprograms - called macros - a special control option is required.

The main advantages of subprograms include transportability between programs (even machines), shorter programs, and easier editing. Any repetitive toolpath or machine activity is suitable to be developed as a subprogram.

Any subprogram has to have a number (for example O6001), in the same format as the main program (or other program of origin). The end of subprogram uses the M99 function, which transfers the processing put of the subprogram back to the program of origin. Another miscellaneous function - M98 - is used to call a previously stored subprogram, for example M98 P6001. The address O is used for storing the subprogram, address P is used for calling the subprogram.

If required, a subprogram can be repeated, up to 9999 times. That is achieved in three ways, depending on the control system:

Method 1: M98 P6001 L5 will repeat subprogram O6001 five times

Method 2: M98 P6001 K5 will repeat subprogram O6001 five times

Method 3: M98 P00056001 will repeat subprogram O6001 five times

Subprograms can be nested. Nesting is a feature that allows one subprogram to call another subprogram up to four levels deep.

In the example of a simple subprogram application Fig. 30, a complete toolpath is developed for one pocket, stored as a subprogram, and called at the equivalent location of the three remaining pockets. Typical programming methods will use an incremental method of dimensioning.

FullHD_typical-subprogram-application.jpg - a few seconds ago

Macros and Parametric Programming

In this general introduction to the subject of macros and parametric programming, the most common development tool is known as Fanuc Custom Macros B. In spite of the great advancement of CAD/CAM software, macros have their special place. In addition to manual or computer-assisted programming., macros are used to provide a special solution to a special requirement. Macro programming is a method using variable data is a special subprogram (now called a macro), with actual values defined in the main program. This method provides a single 'master' program that can be used many times with different numerical values. A typical example is a bolt circle, where only a few values change. The main purpose of a macro program is to save programming time.

Basic Macro Skills

Depending on the complexity of the macro to be developed, some basic knowledge and skills are required. The following list identifies the most important items:

a) Understanding various methods of data settings

b) CNC machines and controls - operation and programming

c) Machining skills - how to machine a part

d) Basic mathematics skills - calculations, formulas

e) Program structure development skills - convenience and consistency

f) Offsets and compensations applications skills - various adjustments

g) Fixed cycles in depth - how they work, in detail

h) Subprograms in depth, including multi nesting applications

i) System parameters, their purpose and functions

In addition, if a macro is written for a particular function of the machine, knowledge of that function as it relates to the machine is also very important. If the macro is developed for probing or in-process gauging, the programmer has to understand the full process as well as the objective of the macro development.

Confirming Macro Capability

A macro is a control option, and may not be automatically available to the user. There is a simple way to find out if a macro is active. Set the control to MDI mode (Manual Data Input) and enter this command:

#1=1

After pressing the "Cycle Start" button, the control will either accept the statement or reject it. The # symbol identifies the subsequent number as a variable number 1 with an assigned value of 1. A statement using the # symbol is unique to macros. If the control rejects it, or it is impossible to enter the symbols, that means the control does not support macro functions.

Common Features and Applications

Typical features found in macros are:

  1. Arithmetic and algebraic calculations
  2. Trigonometric calculations
  3. Variable data storage
  4. Logical operations
  5. Branching
  6. Looping
  7. Error detection
  8. Alarm generation
  9. Input and Output
  10. Miscellaneous features

A macro program looks like a regular CNC program, but includes many other features. A macro program is structured as a subprogram - it is stored under its own program number (O-), and it is usually called by the main program, using the G65 preparatory command. In a simple form, macro features can be used in the main program, without the macro call. The following list highlights some of the most common applications of macros:

  1. Groups of similar parts (parametric programming)
  2. Offset control
  3. Custom cycles
  4. Non-standard tool motions (simulating curves)
  5. Special G-codes and M-codes
  6. Alarm and message generation
  7. Hiding and protecting macro programs
  8. Probing and gauging
  9. Various shortcuts and utilities

Macro Structure

There are three basic areas to understand for successful macro development. These three areas offer many special functions that are used within the macro.

Variables are the key element of all macros. They provide flexibility by easily storing changeable data. When a value is assigned to a variable, it is stored for future use. Stored values are called Defined Variables.

Functions and Constants are also an important part of macros. Functions calculate something - they solve a mathematical calculation or a formula. For example, a "+" (plus) function will sum two or more values together. In addition to functions, constants can be defined in a macro as well, for example the pi (π) with the value of 3.14159265359.

Logical Functions are used for branching and looping purposes. Looping and branching provide a change in the program flow that is based on a certain condition that has been previously defined. The given condition may be tested (evaluated), using the comparison functions, such as 'greater than' (), 'equal to', 'less than or equal to', and several others, used together with the IF function. They are called Boolean or logical operators. The given condition can be tested only once, using the IF checking function or it may be evaluated repeatedly, using the loop function WHILE - while the given condition is true, the loop is repeated. The result of the evaluation will determine further flow of the program.

Macro Definition and Call

Macros are defined in a similar way as subprograms, but they are called or invoked by the G65 command:

    O7001 (MACRO)
    ...
    ...
    M99
    %

Variable Definition (G65)

The G65 macro command accepts variable definitions, called arguments. Arguments are actual program values required for a particular macro only. They are passed to the macro. Variable data in the macro is then replaced with the supplied arguments. A typical program sample of a G65 macro using three arguments may have the following format:

G65 P7001 A6.0 B8.5 F10.0

G65… Macro call command

P7001… Program number containing the macro - stored as O7001

A6.0… Assignment of local variable A (#1) argument to be passed to macro

B8.5… Assignment of local variable B (#2) argument to be passed to macro F10.0…

Assignment of a local variable F (#9) argument to be passed to macro

The illustration Fig. 31 shows graphically the flow between the main program and a macro, as well as the definition and use of the three variables (defined as arguments). In the example, a macro linear motion command will be replaced with the defined amounts:

G01 X#1 Y#2 F#9 = G01 X6.0 Y8.5 F10.0

FullHD_macro call from main program.jpg - a few seconds ago

Assignment of macro variables as arguments is one of the most important parts of developing any macro. Macro B has an arbitrary list of variables (called Argument List 1) and their meaning, shown in the following table:

Table 7. Macro B Argument List

FullHD_table 7.jpg - a minute ago

The above variables are LOCAL variables and can be used in arguments or in the macro.

Note that there are five letters missing (not assigned) - they are G N O P L. These letters are reserved and cannot be used as arguments. The letter L is used for the number of macro repetitions.

Variables #10, #12, #14, #15, #16, #27, #28, #29, #30, #31, #32 and #33 can only be used in a
macro, not as an argument.

Types of Variables

There are four types of variables available for macros:

Null Variable: Variable identified as #0 is a null variable. A null variable has no value – it is an empty variable, often called a vacant variable. This variable can be read by the macro program, but it cannot be assigned a value, which means data cannot be assigned to it.

Local Variables: Local variables are temporary only - they are used in macro body and hold certain data. When the macro is called, the local variables are set to their assigned values. When the macro is completed and exits (using the miscellaneous function M99), or the control power is turned off, all local variables are set to null values - they will no longer exist.

Common Variables: Common - or global - variables are still stored in the control memory when macro is completed. These variables are maintained by the system and they can be shared by several other macro programs. This group of variables is normally cleared by a specially designed macro program.

System Variables: System variables are used for setting and/or changing default conditions and can read and write different CNC data, for example, a current status of a G-code mode, the current work offset, etc. Their numbers are assigned by the control system manufacturer.

Variable Declarations and Expressions

Variable data is not only defined in the argument section of the G65 macro call, variables can also be defined - or declared - in the macro body itself. Compare the two following declarations:

#19 = 800 A value of 800 is assigned to variable number 19 - it can be spindle speed (r/min)
#9 = 15.0 A value of 15.0 is assigned to variable number 9 - it can be a feed rate (mm/min, m/min, ft/min, in/min, etc.)

Both declarations store values - the value of "800" is stored into the variable #19 and the value of "15.0" into the variable #9. Both values shown in the example are numbers, but they are two different types of a number. 800 is an integer, 15.0 is a real number. In macros, this distinction is important. Variables may also use special expressions. For example, the variable definition #9 = 15.0 can be redefined later, as #9 = #9 * 1.1, which returns 16.5. If #9 defines the cutting feed rate, the new feed rate will be F16.5. Expressions can also be nested, following the standard mathematical hierarchy of the order of calculations:

#1 = 5.0

#2 = 8.5

#3 = 10.0

#4 = [#3 * #2] / #1; which returns 17.0

Note that brackets are used for nesting, not parentheses. Parentheses indicate a program comment or a message:

#19 = 800 (SPINDLE SPEED DEFINED)

#9 = 15.0 (CUTTING FEEDRATE DEFINED)

If a macro feature is available, variables can be defined and used in the main program directly, for example:

#19 = 800

N4 G90 G54 G00 X24.0 Y13.5 S#19 M03

Many variations and applications are possible.

Macro Functions

The available macro functions can be divided into six groups:

  1. ARITHMETIC functions
  2. TRIGONOMETRIC functions
  3. ROUNDING functions
  4. MISCELLANEOUS functions
  5. LOGICAL functions and operations
  6. CONVERSION functions

Macro Arithmetic Functions: There are four arithmetic functions:

+ Addition * Multiplication
- Subtraction / Division

Example:

#1 = 3.5 3.5 Returned value of variable #1 is 3.5

#2 = 4.25 4.25 Returned value of variable #2 is 4.25

#3 = 2.0 + 5.0 7.0 Returned value of variable #3 is a sum of 2+5

#4 = #3 + 1 8.0 Add 1 to the result of variable #3

#5 = #2 - 0.8 3.45 Subtract 0.8 from the current value of variable #2

#6 = #1 - #3 -3.5 Subtract contents of #3 from the contents of #1

#7 = #2 * 6 25.5 Multiply contents of #2 by 6

#8 = 7.0 / 8.0 0.875 Divide 7 by 8 as real numbers

#9 = 7 / 8 0.875 Divide 7 by 8 as integer numbers

Trigonometric Functions: The following trigonometric functions are generally available for macros:

SIN Sine COS Cosine TAN Tangent
ASIN arcsine ACOS arccosine ATAN arctangent

All inputs for SIN, COS, and TAN are in degrees and the output of the inverse functions ATAN, ASIN and ACOS is also in degrees. Inverse functions are usually marked as tan-1, sin-1, and cos-1 on the calculator.

Example:

#1 = SIN[38] 0.6156615 Actual value must be in brackets
#2 = 23.7 23.7
#3 = COS[#2] 0.9156626 Reference to a variable must be in brackets
#4 = TAN[12.86] 0.2282959

The inverse trigonometric functions accept the length of two sides of a triangle, both enclosed in brackets, "[ ]", and separated by the slash symbol, "/", standing alone between them. The acceptable range is within 0 ≤ RESULT < 360:

Example:

#5 = ATAN[0.25]/[0.5] 26.5650512 Position of the slash symbol is important

ASIN and ACOS functions are not available on all controls.

Rounding Functions: Rounding functions control how decimal places are handled.

There are three rounding functions:

  1. ROUND
  2. FIX
  3. UP

(1) ROUND: The ROUND function rounds off a value to the nearest whole number. The function disregards fractions that are less than 0.5. For fractions that are equal to or greater than 0.5, the next whole number is the rounded value:

ROUND[0.00001] Returns 0.0
ROUND[0.5] Returns 1.0
ROUND[0.99999] Returns 1.0
ROUND[1.0] Returns 1.0

There is one very important difference between using the ROUND function directly in a program statement or in a variable definition. Consider the following two blocks:

#101 = 27/32

G91 G00 X[ROUND[#101]]

The incremental motion will be X0.8438 (U.S. customary) or X0.844 (metric). However, if the ROUND function is used in a variable definition, the result will be different:

#101 = [ROUND[27/32]]Returns 1.0

G91 G00 X[#101]

The incremental motion will be X1.0

Rounding can also be performed to a given number of decimal places, but it is a two-step process. In the first step, the given value must be multiplied by the factor of:

10... to round off to one decimal place

100... to round off to two decimal places

1000... to round off to three decimal places (typical for metric system)

10000... to round off to four decimal places (typical for U.S. customary system)

The second step is to use the ROUND function together with the multiplier. For example, to round 123.56789 to three decimal places:

#101 = 123.456789

#101 = #101*1000Returns 123456.789

#101 = ROUND[#101]Returns 123457.0 (nearest whole number)

#101 = #101/1000Returns 123.457

Note that only a single variable has been used for all four calculations. If the original value is not required later, this is a very efficient method of defining variables in the macro body.

The remaining two rounding functions FIX and FUP are used to round a given value up or down only, regardless of whether the decimal portion is over or under 0.5. The FIX and FUP functions are commonly used for counting rather than calculations.

(2) FIX: The FIX function is designed to round down the given value (discard fractions less than 1.0) i.e., strip all values after decimal point.

ROUND[0.00001] Returns 0.0
ROUND[0.5] Returns 0.0
ROUND[0.99999] Returns 0.0
ROUND[1.0] Returns 1.0

(3) FUP: The FUP function is designed to round up the given value (raise fractions less than 1.0).

ROUND[0.00001] Returns 1.0
ROUND[0.5] Returns 1.0
ROUND[0.99999] Returns 1.0
ROUND[1.0] Returns 1.0

Miscellaneous Functions: There are two common miscellaneous functions, SQRT and ABS. Several others exist but are not available on all control models.

(1) SQRT: The SQRT function calculates the square root of a number supplied between brackets:

(2) ABS: The ABS function (absolute function) always returns a positive value of a given number:

ABS[−1.3] Returns 1.3
ABS[1.3] Returns 1.3

Using the ABS function is very useful in many applications, for example, to guarantee a positive or a negative value:

#26 = 1.3 Positive input
G01 Z−[ABS[#26]] The motion will be equivalent to Z−1.3
#26 = −1.3 Negative input
G01 Z−[ABS[#26]] The motion will be equivalent to Z−1.3

Logical Functions: Logical functions consist of Boolean operators and bitwise or binary functions:

Boolean functions (operators) compare two values and return a true or false condition:

EQ Equal to GT Greater than GT Greater than or equal to
NE Not equal to LT Less than LE Less than or equal to

Bitwise functions provide a means of combining comparisons.

AND And OR Or XOR Exclusive Or

The AND, OR, and XOR functions compare two defined conditions simultaneously. The compared conditions are evaluated, and return either a TRUE value or a FALSE value. In English language, it is easy to understand the difference between the AND and the OR functions, because they follow the basic logic of everyday language. Function XOR is not used with the same frequency in machining macros. Examples of usage:

Example 1, (OR): #33 = [#1 EQ #2] OR [#3 EQ #4]

Variable #33 returns true (return value is 1) if #1 equals #2 OR #3 equals #4 - otherwise it returns zero (false).

Example 2, (AND) : #33 = [#1 EQ 1] AND [#2 EQ 2]

Variable #33 returns 1 (true) if #1 equals 1 AND #2 equals 2 - otherwise it returns 0 (false).

Example 3, (XOR):#33 = [#1 EQ 1] XOR [#2 EQ 2]

Variable #33 returns 0 (false) if both expressions are true or both expressions are false. If only one expression is true and the other is expression false, #33 returns 1 (true). XOR function is also known as the "exclusive or" function.

Branching and Looping

Both branches and loops (iterations) are based on the return value of an evaluated condition. There are three macro functions available: "IF", "GOTOn", and WHILE (also IF-THEN on some controls).

IF function: The "IF" function evaluates a given condition and branches to a specified block number providing the condition is true. If the condition is false, the macro processing continues in the next block. This function is commonly used when choosing between two alternatives. Effectively, the IF function bypasses one or more blocks if a certain condition is true. The programming format is:

IF [<this condition is true>] GOTOn

GOTOn Function: The "GOTOn" function is an unconditional branch and is programmed by itself in the block. the "n" is replaced by the block number to branch to. For example, GOTO901 will branch to block N901. The programming format is:

GOTO901

WHILE function: The "WHILE" function is for looping - it also requires a specified condition to be true for the loop to be processed. The word "while" can be interpreted as meaning "as long as". Its programming format is:

WHILE [<this condition is true>] DOn

The "ENDn" is the end of loop identification. There can be up to three levels of loop nesting. Each nested loop will have is own WHILE-ENDn. For a single loop, the ENDn is END1.

Note - there is also IF-THEN method available on some control systems. If available, the programming is much more streamlined. The programming format is:

IF [<this condition is true>] THEN [<argument>]

The 'THEN' argument is a statement, normally a definition based on the condition being true:

Example:IF [#1 EQ 1] THEN #33 = 0

All branching and looping functions are extremely powerful and are a fundamental part of effective use of macros.

Macro Example

A classic example of macro development is an equally spaced arrangement of holes along a circle - a bolt circle pattern, see Fig. 32. Many versions can be developed, depending on the actual needs

FullHD_bolt-circle-macro-sample.jpg - 22 minutes ago

(MAIN PROGRAM - CALLS MACRO O7051)

N1 G20

N2 G90 G00 G54 X0 Y0 S900 M03

N3 G43 Z1.0 H01 M08

N4 G99 G81 R0.1 Z-0.6 P250 F12.0 L0

N5 G65 P7051 X3.0 Y6.5 D4.5 H6 A5.0 S1

N6 G80 Z10.0 M09

N7 G28 Z10.0 M05

N8 M01

...

%

O7051 (BOLT HOLE CIRCLE MACRO)

#10 = #4003 (STORE CURRENT G90 OR G91)

IF [#7 LE 0] GOTO 9101

IF [#11 NE FUP[#11]] GOTO 9102

IF [#11 LE 0] GOTO 9103

IF [#19 EQ #0] THEN #19 = 1

IF [#19 NE FUP[#19]] GOTO 9102

IF [#19 LT 1] GOTO 9104

IF [#19 GT #11] GOTO 9105

#7 = #7/2 (BOLT CIRCLE RADIUS)

WHILE [#19 LE #11] DO1

#30 = [#19 - 1] * 360 / #11 + #1

X[COS[#30] * #7 + #24] Y[SIN[#30] * #7 + #25]

#19 = #19 + 1

END1

GOTO 9999

N9101 #3000=101 (DIA MUST BE POSITIVE)

N9102 #3000=102 (ONLY INTEGER NUMBER ALLOVED)

N9103 #3000=103 (ONLY POSITIVE NUMBER OF HOLES)

N9104 #3000=104 (START HOLE MUST BE AN INTEGER)

N9105 #3000=105 (START HOLE NUMBER IS TOO HIGH)

N9999 G#10 (RESTORE ORIGINAL G90 OR G91)

M99

%

The macro call in block N5 includes six arguments that will be passed to the macro:

X = #24 = Bolt circle center distance from X0 (X3.0)

Y = #25 = Bolt circle center distance from Y0 (Y6.5)

D = #7 = Bolt circle diameter (D4.5)

H = #11 = Number of EQSP holes (H6)

A = #1 = Bolt circle angular orientation (default is 0) (A5.0)

S = #19 = Start hole number (default is 1) (S1)

Summary

This article reviews the use of subprograms, macros, and parametric programming as applied to CNC machining code creation. Learn more about machining operations from the Machinery's Handbook, 30th Edition, which is published and available from Industrial Press on Amazon.

To locate sources of programming services, visit the Thomas Supplier Discovery Platform where you can locate potential sources of supply for over 70,000 different product and service categories.

Other CNC Programming Articles

  • An Introduction to G-Code and CNC Programming
  • List of Common G-Code Commands and What they Mean
  • An Introduction to M-Code and CNC Programming
  • Examples of G-Code for Common CNC Functions
  • G-Code Cheat Sheet of Common Commands

More from Custom Manufacturing & Fabricating

Computer Programming Macro Meaning

Source: https://www.thomasnet.com/articles/custom-manufacturing-fabricating/subprograms-macros-and-parametric-programming-for-cnc-machining/

0 Response to "Computer Programming Macro Meaning"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel