An Introduction to Logic in a Machining Macro

G83 peck drilling cycle for a lathe that does not have the While [ ] Do option.

In the last posting, we created a macro to peck drill holes on a CNC lathe in a manner similar to the standard G83 peck drilling cycle for a milling machine. In this posting, we will re-write the same macro but use the IF [ ] GOTO logic.

Understanding the Logic

Nesting is a programming term that means placing one routine inside of another. When a CNC programmer says a subprogram is nested, it means he or she has called a subprogram from a subprogram. Typically, subprograms may be nested up to 99 times. While [ ] Do statements may also be nested, but only up to three times. When nesting While [ ] Do statements, it is critical that the most recently executed (third) While [ ] Do statement is ended first and then the second is followed by the first While [ ] Do statement. Each nested While [Condition] Do statement must also have a unique number.

If you have successive While [Condition] Do statements that are not nested, then they may all be While [Condition] Do1

While [Condition] Do statements execute the code found between the While [Condition] Do and the End statement until the condition is no longer true. IF [Condition] GOTO statements will jump to the line number following the GOTO if the condition is true, otherwise they will not execute the GOTO but will instead continue on to the next line of code in the program. An important point about an IF [Condition] GOTO statement is that the GOTO may be omitted. In other words, the following two statements are equal:

IF [#100 LT 0] GOTO 12 Equals IF [#100 LT 0] 12.

Since it is possible to have a CNC machine that does not have the ability to execute a While [Condition] Do statement, it is sometimes necessary to perform the same logic using an IF [Condition] GOTO statement. However, the logic will need to be reversed. In order to create the same macro using IF [Condition] GOTO logic, lines N5 and N11 will need to be changed as shown below.

On some machine controllers, line N8 will need to be “N8 Z[#101+0.03].”

Figure 1. Haas HL-2 used to test the IF [ ] GOTO logic for the G83 drilling macro. (Image courtesy of Conestoga College Institute of Technology and Advanced Learning.)

Figure 1. Haas HL-2 used to test the IF [ ] GOTO logic for the G83 drilling macro. (Image courtesy of Conestoga College Institute of Technology and Advanced Learning.)

In order to keep the drill pecking and the program looping until the desired depth is reached, you must remember how the IF statement logic works. The IF statement on line N5 will only be executed once the value stored in variable #100 is less than zero. The GOTO 5 will keep the program looping by sending the controller back to line N5. The GOTO 12 is required on line N5 in order to enable the controller to exit the loop by forcing the controller to jump to line N12.

All comments and alarms should always be in all capital letters because many older machine controllers will load an empty space in place of a small letter. Hence I recommend that you program with your caps lock key enabled on your keyboard. (If you happen to send an email while writing a program, you can always explain later that you were not yelling, but rather simply forgot to take the caps lock off on your keyboard.)                                                                                  

The IF statement is a very simple yet very powerful programming tool. Besides being used to cause the program to jump to a specified line number, it can also be used to set other variables, check for a certain condition, initiate a user programmed alarm, track tool life and many other useful purposes. Both macro programs will produce the same results, but they rely on a competent user to program the G65 line and not miss any data or make any errors, such as a decimal place error.

Macros should always be tested on the machine controller prior to use, however; your machine time is very valuable. When you wish to test your macro, but cannot afford the machine down time, a good simulator can save you time and money. There are many simulators out there, but they are not all good.  NCPlot is an excellent CNC simulator that will run all of your macro codes correctly, and show you the value for all of your variables, making it easier to follow the logic.

Figure 2. NCPlot window showing the program, simulator and variable values windows. (Image courtesy of the author.)

Figure 2. NCPlot window showing the program, simulator and variable values windows. (Image courtesy of the author.)

To see this macro being run on a Haas HL-2, watch the video below.

This is Part 3 of a 3-part series. For more information, read Part 1 and Part 2.

About the Author






Fred Fulkerson is a graduate of the Faculty of Education, University of Western Ontario, and of the general machining program at Conestoga College in Ontario. He is a Canadian Red Seal certified general machinist and CNC programmer and a certified Mastercam and SOLIDWORKS instructor.