587,262 active members*
3,323 visitors online*
Register for free
Login
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2005
    Posts
    1237

    Question Gcode sub program example needed

    Could someone post an example of a nested sub program? I know that there are su programs for drilling and etc., but I'm interested in how the sub program is defined, labeled, called, and ended. It need not be a large program, just that it contains how to do sub programs in EMC2.

    Thanks

  2. #2
    Join Date
    Oct 2005
    Posts
    1237
    More information. People say that EMC2 uses the same commands as a Fanuc control, yet EMC2 doesn't recognize M98 and M99. I've read over and over about O100 or 0100 labeled subprograms, but still don't understand the full workings of how they relate to say a Fanuc6M post that uses M98 to call, and M99 to end subprogram. EMC2 G98 and G99 don't seem to be the same commands. The EMC2 manual and quick G-code guide do not give much in the way of examples, and my tech school book doesn't use EMC2's protocols for G-code writing.

    My Anilam M and Fanuc Posts in Dolphin Part Master does fine for none subprogram posts, but the minute I start getting involved with subprograms there are problems. I have the PP manual for Partmaster posts, but need subprogram example to make the changes needed.

  3. #3
    Join Date
    Feb 2006
    Posts
    338
    looking at the EMC manual it uses O### <-- any unique number as the sub label followed by "sub, endsub, call" instead of M98, M99 that fanuc uses.

    It looks like any O number can be used, not just 100 or 101 like in the examples. It also allows calling subs in separate files. In that case the O is followed by the file name. The sub program still has to have O### sub, and endsub lines.

  4. #4
    Join Date
    Oct 2005
    Posts
    1237
    Something I don't understand about the examples. When a notation is made at the start of program , () is used so Axis knows it isn't part of the program. In all but the first example they have constants(?) inside of (). Like;

    o200 sub (1=x ... 2=i ... )

    and when I look down at the rest of the program it seems that the () data IS being looked at by Axis. I would use the G-code basic tutorial in the WIKI, but it isn't more than an outline of possible chapters with no information and the last update was over two years ago.

    The G-code program I'm trying to make has two .64 dia pockets .76 deep 3" away from each other on Y axis, and a border (boss) around them (part of a dust boot). The boss is machined semi finish, and finish passes using separate depths as roughing clears all surrounding material and i don't want that. The loop would be three steps at deeper depth on the boss, and holes.

    My text book just assigns a name (number) to a sub g-code program for a call.

    move to position, call sub "whatever" which has an incremental modal g-coded program,

    finish the sub cut, end sub, move in absolute, and repeat.

    The examples in the advanced coding is well,... advanced and uses a different (macro?) format. So looking at what my book says is way different from what I'm seeing.

  5. #5
    Join Date
    Feb 2006
    Posts
    338
    The variables are set by the call line, not the beginning of the sub. Also use [] not ()

    O115 call [1] [2] [37] [3.2]

    inside:
    O115 sub
    (#1=1, #2=2, #3=3,7 #4=3.2)
    ....
    O115 endsub

    Note: the sub code needs to be before the call line in your program unless it is called from a separate file. It has to be read first, but it will not execute until you use the call command.

  6. #6
    Join Date
    May 2005
    Posts
    1662
    example:

    main program:
    Code:
    G00 x1 y1 z.25
    o123 call
    G00 X2
    o123 call
    m2
    subprogram:
    Code:
    o123 sub
    G91
    G01 z-.5 f10 
    G03 x.5 y0 r.25 f5
    x-1 y0 r.5
    x1 y0 r.5
    x-.5 y0 r.25
    G01 z.5
    g90
    o123 endsub
    main program can be what-ever-name.ngc
    subprogram is 123.ngc

    Save both programs in the prefix directory set in the .ini file. This is most likely to be home/you/emc2/nc_files

    Untested and not meant for actual milling
    Anyone who says "It only goes together one way" has no imagination.

  7. #7
    Join Date
    Mar 2010
    Posts
    0

    Sub Program example

    Hi

    Don't know if you have got to grips with this yet. Very often you don't actually need to put the subroutine in a separate file, only if the code is to be reusable to other code files.

    There are 2 examples attached that I use as a psuedo G71 template for linear turning.
    I have a different one for turning which involves arcs.

    The decrementing of the X value for each cut is calculated in a separate file '710.ngc' because I use it with other templates.

    It also shows the use of named parameters, passing parameters in the subroutine call and using the return value parameter.
    1 - 30 can be parameters to the call and 31-60 the return values.

    Named Parameters are a real plus for EMC gcode, make reading the code much easier 2 months later!

    Hope this is of some assistance.

    regards

    ArcEye
    Attached Files Attached Files

  8. #8
    Join Date
    Oct 2005
    Posts
    1237
    Quote Originally Posted by ArcEye View Post
    Hi

    Don't know if you have got to grips with this yet. Very often you don't actually need to put the subroutine in a separate file, only if the code is to be reusable to other code files.

    regards

    ArcEye
    WOW. My G-code text book is much too basic. Where might I find a book that tells and explains what each line. and word represents? I can write snfd proof G-code, but this is different than Gwords telling do this and do that. My bok defines a sub program as another g-code string of move.

    I like the way it seems to have addresses, but I haven't learned this style. Heck I didn't even know it existed, being self taught. Knowing would help trouble shoot an EMC2 post I'm playing with. As a work around, its all posted as long hand. Fanuc sub programming isn't the same as EMC2 in all respects.

  9. #9
    Join Date
    Feb 2006
    Posts
    338
    Sub calls can be a simple path, or complex macro commands. It all depends on the creativity of the programmer.

    The main reason for simple subs is you only need to change code in 1 place to change every use of the sub.

    Macros (fancy subs) allow the use of the same code to do varied operations. Again easily changed in the sub, or by the sub call parameters.

    Two samples might be:
    Simple g-code that is executed where ever the sub is called
    Code:
    o123 call (1 x 1 square)
    
    o123 sub (1" square sub)
    G91
    G1 X1.0 F10.0
    Y1.0
    X0.0
    Y0.0
    G90
    o123 subend
    A more advanced example that uses the two numbers in the call line to change the lengths of the rectangle each time it is called.
    Code:
    o123 call [2.0] [1.5] (2 x 1.5 rectangle)
    o123 call [1.0] [2.5] (1 x 2.5 rectangle)
    
    o123 sub (rectangle macro)
    G91
    G1 X#1 F10.0
    Y#2
    X0.0
    Y0.0
    G90
    o123 subend
    Remember EMC2 needs the sub to be BEFORE the call, or in a separate file like ArcEye's attachements

  10. #10
    Join Date
    Mar 2010
    Posts
    0

    Learning GCode

    Hi
    All the information needed is in the wikis and documentation, the trouble is that you have to know what it is you need to know, sometimes a chicken and egg situation.

    I have not found any really useful books, but I can recommend the articles in Digital Machinist magazine by Ed Nisley.

    He uses EMC and in particular uses named parameters, sub-routines etc that are specific to EMC.

    His series started some while back but back issues are available quite cheaply from Village Press.

    The only issue I have with Digital Machinist, is that the other articles tend to be hugely wordy and padded.
    I don't know if this is an American trait or just this magazine.
    I for one do not need 6 pages and a blow by blow description of drilling operations interspersed with workshop reminiscences, where 1 page and 2 photos would have made all abundantly clear.

    That said, there are a couple of writers who make the purchase worthwhile.

    Good Luck

    ArcEye

  11. #11
    Join Date
    Jan 2008
    Posts
    13
    fairly new to emc2 and newer still to making complex sub. but i hope this helps.

    %
    g17 g20 g40 g49 g54 g80 g90 g94
    (Mill Four Ports)
    #<_plunge> = 3
    #<_startfeed> = 3.5
    #<_regfeed> = 5
    #<_finfeed> = 8
    #<_fastfeed> = 12
    #<_safeheight> = .2
    #<_start> = .05
    #<_end> = .06
    #<_.69step> = -.69
    #<_.8depth> = -.8

    o100 sub (roughing top)
    g1 f#<_startfeed> x#<_start> y#<_start>
    y2.27
    x1.2
    y#<_start>
    x#<_start>

    x.03 y.03
    f#<_regfeed>
    y2.29
    x1.22
    y.03
    x.03
    x#<_end> y#<_end>
    o100 endsub
    o101 sub (top cleanup)
    f#<_regfeed> x.15 y .15
    x1.05
    y2.15
    x.15
    y.15

    x.3 y.3
    x.9
    y2
    x.3
    y.3

    x.45 y.45
    x.75
    y1.85
    x.45
    y.45

    x.6 y.6
    y1.7
    o101 endsub
    o102 sub (finishing top)
    g1 f#<_regfeed> x.022 y.022
    y2.293
    x1.228
    y.022
    x.022

    x.015 y.015
    y2.3
    x1.235
    y.015
    x.015

    x.01 y.01
    y2.305
    x1.24
    y.01
    x.01

    x.005 y.005
    y2.31
    x1.245
    y.005
    x.005

    x0 y0
    y2.315
    x1.25
    y0
    x0

    f#<_fastfeed> y2.315
    x1.25
    y0
    x0
    g0 x#<_end> y#<_end>
    o102 endsub
    o103 sub (roughing Step)
    g1 f#<_startfeed> x.02 y.02
    x.01 y.01
    y2.02
    x.91
    y2.305
    x1.24
    y1.76
    x.99
    y.09
    x1.24
    y.01
    x.01
    (step cleanup)
    x.15 y.15
    x.8
    y1.85
    x1.075
    y2.165
    y1.9
    x.15
    y.25
    x.65
    y1.7
    x.3
    y.35
    x.475
    y1.5
    o103 endsub
    o104 sub (finish step)
    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_regfeed> z#<_.8depth>
    x.005 y.005
    y2.025
    x.9
    y2.31
    x1.245
    y1.75
    x1
    y.1
    x1.245
    y.005
    x.005

    x0 y0
    f#<_finfeed> y2.03
    x.9
    y2.315
    x1.25
    y1.75
    x1
    y.1
    x1.25
    y0
    x0

    f#<_fastfeed> y2.03
    x.9
    y2.315
    x1.25
    y1.75
    x1
    y.1
    x1.25
    y0
    x0
    g0 x#<_end> y#<_end>
    o104 endsub
    o105 sub (tool change)
    g0 z2
    g49 g80
    g0 x3.8 y2
    o105 endsub
    o106 sub (0.0 run on top)
    g1 f#<_fastfeed> x.01 y.01 z-.68
    x0 y0
    y2.315
    x1.25
    y0
    x0
    g0 x#<_end> y#<_end>
    z#<_safeheight>
    o106 endsub
    ( o107 sub)
    (O102 if [#<_.69step> lt .68])
    ( o100 call [10])
    (O102 else)
    ( o101 call)
    (O102 endif)
    ( o107 endsub)


    (program start)
    G10 L2 P1 x0 y0 z0 (p1 = g54, L2? L1? L10? L20?)
    G10 L2 P2 x1.9 y0 z0 (p2 = g55 )
    G10 L2 P3 x3.8 y0 z0 (p3 = g56 )
    G10 L2 P4 x5.7 y0 z0 (p4 = g57,p5 = g58,etc...g59.3)

    o105 call
    g28.1
    (o107 call)

    g54 (g54)
    g64 p.005 q0
    g0 x#<_start> y#<_start>
    z#<_safeheight>
    m6 t4
    (msg, first port)
    (disable / to mill from blank stock)
    g1 f#<_regfeed> z-.1
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.2
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.3
    /o100 call)
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.4
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.5
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.6
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.675
    o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.69step>
    o100 call
    o101 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.69step>
    o102 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.78
    o103 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.8depth>
    o103 call
    o104 call
    o106 call
    g0 z#<_safeheight>

    g55 (g55)
    (msg, second port, s to continue)
    x#<_start> y#<_start>
    m1 (alternate stop)
    g1 f#<_regfeed> z-.1
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.2
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.3
    /o100 call)
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.4
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.5
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.6
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.675
    o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.69step>
    o100 call
    o101 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.69step>
    o102 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.78
    o103 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.8depth>
    o103 call
    o104 call
    o106 call
    g0 z#<_safeheight>

    g56 (g56)
    (msg,third port, s to continue)
    x#<_start> y#<_start>
    m1 (alternate stop)
    g1 f#<_regfeed> z-.1
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.2
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.3
    /o100 call)
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.4
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.5
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.6
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.675
    o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.69step>
    o100 call
    o101 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.69step>
    o102 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.78
    o103 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.8depth>
    o103 call
    o104 call
    o106 call
    g0 z#<_safeheight>

    g57 (g57)
    (msg,fourth port, s to continue)
    x#<_start> y#<_start>
    m1 (alternate stop)
    g1 f#<_regfeed> z-.1
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.2
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.3
    /o100 call)
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.4
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.5
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.6
    o100 call
    /o101 call
    g0 z#<_safeheight>

    x#<_start> y#<_start>
    g1 f#<_plunge> z-.675
    o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.69step>
    o100 call
    o101 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.69step>
    o102 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.78
    o103 call

    g0 z#<_safeheight>
    x#<_start> y#<_start>
    g1 f#<_plunge> z#<_.8depth>
    o103 call
    o104 call
    o106 call
    g0 z#<_safeheight>
    (msg,Complete)
    g40 g80 g54 g92.1
    m6 t0
    g28

    m2
    %

    o100b sub (roughing top)
    o101b sub (roughing Step)
    o102b sub (finish step)
    o103b sub (finishing top)
    o104b sub (top cleanup)
    o105b sub (safe position)
    o106b sub (0.0 run on top)

  12. #12
    Join Date
    Feb 2006
    Posts
    338
    A nice real world example.

    Because it is long though, I wanted to edit your example to show the different parts better. Note the order. Also note this example is using nicely named global variables, and not passing local only variables in the sub call command as I posted above.

    Start of program
    set variables before any use
    define sub programs
    Main program with sub calls

    Quote Originally Posted by piasdom View Post
    %
    g17 g20 g40 g49 g54 g80 g90 g94
    (Mill Four Ports)
    #<_plunge> = 3
    #<_startfeed> = 3.5
    #<_regfeed> = 5
    #<_finfeed> = 8
    #<_fastfeed> = 12
    #<_safeheight> = .2
    #<_start> = .05
    #<_end> = .06
    #<_.69step> = -.69
    #<_.8depth> = -.8


    o100 sub (roughing top)
    g1 f#<_startfeed> x#<_start> y#<_start>
    y2.27
    x1.2
    y#<_start>
    x#<_start>

    x.03 y.03
    f#<_regfeed>
    y2.29
    x1.22
    y.03
    x.03
    x#<_end> y#<_end>
    o100 endsub
    o101 sub (top cleanup)

    f#<_regfeed> x.15 y .15
    x1.05
    y2.15
    x.15
    y.15

    x.3 y.3
    x.9
    y2
    x.3
    y.3

    x.45 y.45
    x.75
    y1.85
    x.45
    y.45

    x.6 y.6
    y1.7
    o101 endsub


    ... snip

    (program start)
    G10 L2 P1 x0 y0 z0 (p1 = g54, L2? L1? L10? L20?)
    G10 L2 P2 x1.9 y0 z0 (p2 = g55 )
    G10 L2 P3 x3.8 y0 z0 (p3 = g56 )
    G10 L2 P4 x5.7 y0 z0 (p4 = g57,p5 = g58,etc...g59.3)

    o105 call
    g28.1
    (o107 call)

    g54 (g54)
    g64 p.005 q0
    g0 x#<_start> y#<_start>
    z#<_safeheight>
    m6 t4
    (msg, first port)
    (disable / to mill from blank stock)
    g1 f#<_regfeed> z-.1
    /o100 call
    x#<_start> y#<_start>
    g1 f#<_plunge> z-.2
    o100 call
    /o101 call

    g0 z#<_safeheight>

    ... snip

    m2
    %

  13. #13
    Join Date
    Oct 2005
    Posts
    1237
    Holy cow! I have a LOT of learning to do. That is not what the Post was putting out. (Fanuc sub program/macro post) I ended up turning off the macro option and the post kicks it all out long hand now. At least until I learn more about writing the proper post so it posts proper macros to EMC2.

  14. #14
    Join Date
    Feb 2006
    Posts
    338
    For as close a comparison to Fanuc subs and macros, I'd try changing the fanuc post to output for EMC2 like the following

    normal Fanuc main program output:
    Code:
    O100 (main program code)
    ...
    M98 P1001 (sub program call without parameters)
    (aka. just repeated code)
    ...
    G65 P1002 A1.0 B3.5 (macro call with parameters)
    (aka. Code that changes with parameters)
    ...
    M02
    Modified Fanuc main program output to work with EMC2:
    Code:
    O100 (main program code)
    ...
    O1001 CALL (sub program call without parameters)
    (aka. just repeated code)
    ...
    O1002 CALL [1.0] [3.5] (macro call with parameters)
    (aka. Code that changes with parameters)
    ...
    M02
    EMC2 calls do not label the variables when done this way, they are just ordered in the call line as #1 #2 #3 ect. that is why I chose A B... for the fanuc sample.

    normal Fanuc sub program output:
    Code:
    O1001 (sub program call without parameters)
    G91 ( incremental )
    ...
    (normal G-code)
    ...
    G90 ( absolute )
    M99
    modified Fanuc sub program output to work with EMC2:
    Code:
    O1001 SUB (sub program call without parameters)
    G91 ( incremental )
    ...
    (normal G-code)
    ...
    G90 ( absolute )
    O1001 SUBEND
    modified Fanuc macro program output to work with EMC2:
    Code:
    O1001 SUB (sub program call without parameters)
    G91 ( incremental )
    ...
    (G-code with #1 and #2 variables used)
    ...
    G90 ( absolute )
    O1001 SUBEND
    For EMC2, save the subs to separate named files like ArcEye's example.
    This is just one way to do this, but probably the closest to a Fanuc like way of doing so.

    Personally I like that way piasdom's example is done when using just sub type code, or a macro that is only used with one set of values. If using a macro with different values each time it is called, I prefer the above setup of setting the values on the sub call line.

  15. #15
    Join Date
    Jan 2008
    Posts
    13
    put whatever you want repeated in a sub. then call in when you need. in my example, everytime i set Z .1" deepeer, i called the subroutine to either cleanup the pocket or cut close to the edges. until i was to my depth.

    Quote Originally Posted by MrWild View Post
    Holy cow! I have a LOT of learning to do. That is not what the Post was putting out. (Fanuc sub program/macro post) I ended up turning off the macro option and the post kicks it all out long hand now. At least until I learn more about writing the proper post so it posts proper macros to EMC2.

Similar Threads

  1. Replies: 14
    Last Post: 06-25-2010, 08:12 PM
  2. Direction needed with BobCad Pro Art X to GCode
    By tamedia1 in forum BobCad-Cam
    Replies: 1
    Last Post: 03-23-2010, 09:47 AM
  3. Warmup Program Gcode
    By Phife in forum DIY CNC Router Table Machines
    Replies: 6
    Last Post: 01-09-2010, 02:45 AM
  4. What program builds 3D Model from GCode?
    By cjjonesarmory in forum Uncategorised CAM Discussion
    Replies: 2
    Last Post: 07-28-2009, 08:33 PM
  5. cheap bitmap to gcode program
    By balsaman in forum DIY CNC Router Table Machines
    Replies: 29
    Last Post: 07-28-2003, 07:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •