As always reference the LinuxCNC manuals and the LinuxCNC Wiki for more information.

Subroutine Files

Writing subroutines and placing them in separate files is a very powerful tool to use. You can pass parameters to the subroutine so you don’t have to modify the file each time you use it. Just like we did in the previous tutorial on subroutines we pass the variables with the call. The syntax of the subroutine file is simple.

  1. The subroutine name must be the same as the file and in lower case.

  2. The subroutine must end with a stop program after the endsub.

  3. The file name ends with .ngc just like regular G code files.

circle.ngc
o<circle> sub
  F25
  (getting the radius from the diameter value)
  (that was passed by dividing it in 1/2)
  (then storing it in a named parameter)
  #<radius> = [#3 / 2]
  G0 X[#1 - #<radius>] Y[#2]
  (debug, X = #1 Y = #2 radius = #<radius>)
  G2 X[#1 - #<radius>] Y[#2] I[#<radius>] J0
  (msg, some useful code here)
o<circle> endsub
M2

Recalling our circle subroutine from the previous tutorial notice the call line is missing. Now you can call circle.ngc from another file or the MDI tab.

manycircles.ngc
(X center, Y center, Circle Diameter)
o<circle> call [1.5] [2.0] [3]
o<circle> call [1.5] [4.0] [3]
o<circle> call [3.5] [2.0] [3]
o<circle> call [3.5] [4.0] [3]
G28 (return to preset position)
M2

In the above example file manycircles.ngc we are calling the circle.ngc file and passing variables to the circle.ngc file. Each time we call the file with different variables to cut the circle in a different location.

Now I hope you see the power in subroutines…