Sherline CNC G-Code; subroutines

I had a lot of trouble finding information about using subroutines on my Sherline CNC equipment. The Sherline controller does not recognize the M98 or M99 commands. These would make it a lot easier to run subroutines. You can still use subroutines on the Sherline controller. So I decided to pass on what is working for me.

Some of it was here, but still had to figure out how it works on the Sherline. http://gnipsel.com/linuxcnc/g-code/gen02.html

These subroutine line numbers begin with the letter “o”. So you can specify any legitimate line number prefaced with the “o”.

You need to add a couple of spaces before each command line. WordPress code boxes do not show empty spaces before characters. So remember to put them in if you paste any of this code!


o100 sub
command line
command line
command line
o100 endsub

To use the subroutine you need to call it.
o100 call

So we want to cut out a circle on the CNC mill we want to cut each step at .0150 (z-.015)
first we are going to set up a parameters list. Labeling your parameters is a very good idea.

EDIT: forgot equal signs in parameters.
%
g20 g40 g49 g90 x0 y0 z0 f10

#1001 = 0.9000 (x start point)
#1002 = -0.9000 (y start point)
#1003 = 0.1000 (z safe height)
#1004 = -0.0150 (z step cut depth)

o100 sub
X[#1001] y[#1001] (start point is left .9 and down .9)
G2 X[#1001] y[#1001] I-0.0000 J[#1002](Clockwise cut -.9 radius for 1.8″ circle on center)
o100 endsub  (actual circle size is 1.625″ without cutter compensation with 1/8″ endmill)

g1 z[1 * #1004] f6
o100 call

g1 z[2 * #1004] f6
o100 call

g1 z[3 * #1004] f6
o100 call

g1 z[4 * #1004] f6
o100 call

g1 z[5 * #1004] f6
o100 call

g1 z[6 * #1004] f6
o100 call

g1 z[6.7 * #1004] f6 (cut 1/10″ total)
o100 call

G90
M2
%

Each successive call is processed then the next call is processed until all of the calls are done.

This really simplifies the code that you need to cut out that circle. It is much cleaner than just cutting and pasting the code for the circle over and over and changing the z depth by hand for each. The parameters table allows you to change the z depth of cut in just the one place. You may have to add or subtract the cut depth code for the thickness of the piece you are cutting. But still simpler than having to refigure every depth by hand.

I will continue to try to improve this page and the code.

To be honest I am not finding a lot of information about the EMC2/LinuxCNC subroutines. I do not know why there is not more information out there. I have tried a lot of different  combinations to get the machine to lower the Headstock (Z axis) in steps. may of the articles I have found just do not seem to work on my particular version (EMC2 ver.2.4.4) from Sherline. I have found that there are newer versions available. I am testing 2.6.3 on my netbook right now. This is Ubuntu 12.04 LTS with LinuxCNC 2.6.3.

I finally have been able to get the Z to move down with a subroutine. It is below.

%
G20 gG0 G49 G90 X0 Y0 Z0 F10
G1 Z0.1 f20 (my safe Z height)

#1 = -.04
#2 = 0
#3 = 0.1

o100 repeat
#2 = [#2 - #3]
G1 Z#2 F8
X0.9000 y0.9000 (start point is left .9 and down .9)
G2 X0.9000 y0.9000 I-0.0000 J-0.9000 (Clockwise cut -.9 radius for 1.8" circle on center)
O100 ENDREPEAT

o100 CALL
M2
%

Another problem I just found is that EMC2 needs to be closed and reopened, after changes to code, before it will run the code properly. I will have to go back and try out some of the earlier code sample now that I know this.

With a restart this code now works. It stalls after the last circle so I am going to have to figure out what is need to exit the subroutine. This is the same problem with the code above using repeat!


%
G20 gG0 G49 G90 X0 Y0 Z0 F10
G1 Z0.1 f20 (my safe Z height)

#1 = -.4
#2 = 0
#3 = 0.1

o100 do
#2 = [#2 - #3]
G1 Z#2 F20
X0.9000 y0.9000 (start point is left .9 and down .9)
G2 X0.9000 y0.9000 I-0.0000 J-0.9000 (Clockwise cut -.9 radius for 1.8" circle on center)
o100 while [#2 gt #1]

o100 call