Using o-words with PlanetCNC TNG software: Loop blocks

You can use loop o-words with PlanetCNC TNG.

Sometimes you need to execute line(s) of g-code in a loop while evaluating specific condition.  When condition evaluates to false, program will exit loop. This comes useful for e.g. repetitive motion sequences, parameter manipulation etc…

O-word loop commands consist of:
O-do – Begin loop block
O-while – End loop block [or condition evaluation]
O-endwhile – End loop block
O-break – Exit loop block immediately
O-continue – Skip to next condition evaluation of  the while condition

You can use two types of loops: While – Endwhile or Do – While.

While – Endwhile:
Evaluates condition on the beginning of loop and then executes the lines of code inside the loop.

Do – While:
First executes the lines of code inside the loop and then evaluates condition at the end of loop.

Example of Do – While loop:

%
 #1 = 1
 o100 do
 (print, #1)
 #1 = [#1+1]
 o100 while[#1 LE 10]
 (print,loop finished)
 %

The content of the loop will repeat for as long as while condition at the end will remain true. Which means that as soon as parameter #1 exceeds value of 10, program will exit the loop and print

loop finished

 

Example of While – Endwhile loop:

%
#1 = 1
o100 while [#1 LE 10]
 (print, #1)
 #1 = [#1+1]
o100 endwhile
 (print,loop finished)
%

The content of the loop will repeat for as long as while condition at the beginning will remain true. Which means that as soon as parameter #1 exceeds value of 10, program will exit the loop and print

loop finished

Example of break used in a Do – While loop:

%
#<A> = [2**0]
#<B> = 0
#1 = 0
o100 do
 (print, #1)
 #1 = [#1+1]
 G04 P1
 o200 if [AND[#<A>, #<_hw_input>]]
  #<B> = 1
  o100 break
 o200 endif
o100 while[#1 LE 10]

o300 if [#<B> EQ 0]
 (print,loop finished)
 o300 else 
 (print,preemptive loop break)
o300 endif
%

The content of the loop will repeat for as long as input 1 of controller is not active and while condition at the end remains true. Which means that as long as input 1 remains un-active, counter will count to 10 and program will exit the loop normally and print:

loop finished

As soon as input becomes active during counting, program will exit loop and print:

preemptive loop break