Need Help! Using Variables


Results 1 to 15 of 15

Thread: Using Variables

  1. #1

    Default Using Variables

    Using Mach4 with ESS and I am trying to get familiar with Gcode before I start using CAM to create it. Just making a back and forth pass incrementing Y each time to surface the spoilboard for my wood router and I wanted to make it more flexible to work for surfacing anything. And now I am stuck. Where am I going wrong with the use variables?
    BTW Can I use text for variables or must they be numbers? What are the rules for that?


    The lines in parenthesis are what I used originally and it worked. Left there show show what my intention was.

    Code:
    #1001 = 46.5 (Width)#1002 = 36 (Depth)
    #1003 = 17 (Cycles of Loop)
    #1004 = 1 (Tool Diameter)
    #1005 = [#1004 - .1] (Step over)
    #1006 = -0.050 (Depth of Cut)
    G0 G90 G54 G17 G40 G49 G80
    (G0 X3.0 Y1.2 Z1.0 ;home, inside previous cut)
    G0 X0 Y0 Z.2 ;home, inside previous cut
    (G1 Z-0.050 F10 ; letting me know its cutting)
    G1 Z#1006 F10 ; letting me know its cutting
    M03 S18000; start spindle
    G04 P3000 ;Pause 3 seconds for VFD comes to speed
    M0 ;confirm the start
    ( M98 P1000 L17 ;Call Sub O1000, loop enough times to reach back side )
    M98 P1000 L#1003 ;Call Sub O1000, loop enough times to reach back side
    M04 ;stop spindle
    G0 G90 G54 G17 G40 G49 G80
    G0 Z5.0
    (G0 X1.5 Y1.95 )
    G0 X0.0 Y0.0 
    G28 M30 ;exit routine
    O1000 ;Beginning of loop
    G91 ;incremental
    (G01 Y0.95 F100.)
    G01 Y#1005 F100.
    G90 ;absolute
    ( X46.5 ;go to end)
    X#1001 ;go to end
    G91 ;incremental
    (Y0.95 ;by this much)
    Y#1005 ;by this much
    G90 ;absolute
    (G1 X3.0 ;back to beginning)
    G1 X0 ;back to beginning
    M99 ;End of loop



    Similar Threads:


  2. #2
    Member deadlykitten's Avatar
    Join Date
    Jun 2015
    Location
    Antarctica
    Posts
    4154
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    I am trying to get familiar with Gcode before I start using CAM to create it.
    BTW Can I use text for variables or must they be numbers? What are the rules for that?
    hy, if you wish to use the cam in the future, will you output variables from it ? kindly

    ps : i can't help with your code ...

    we are merely at the start of " Internet of Things / Industrial Revolution 4.0 " era : a mix of AI, plastics, human estrangement, powerful non-state actors ...


  3. #3
    Member
    Join Date
    Nov 2013
    Posts
    4361
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    Hi,
    using variables in that manner is certainly doable but is far from the easiest way to generate code.

    If you want good conversational programming I'd recommend Mill Wizard by NFS. It costs $75.00. I have it on my machine PC,
    its great for little jobs, maybe several operations strung together, like surface a part, mill a circular pocket and then drill a circular bolt
    hole pattern. Such a simple program does not require CAD/CAM just MillWizard. Download it and try it out, I know you'll be impressed
    with how simple it is.,

    Getting back to your use of variables, they are called pound variables. They are used by Mach to hold important data. You cannot come along
    and program #1001= 67.75, for instance because variable #1001 is already used by Mach for its own purpose and you have just screwed it up.

    If you have a look at my post:

    https://www.machsupport.com/forum/in...?topic=40051.0

    You will see that variables #100 to #199 are available for use, they are cleared on start-up, and #500 to #599 which are persistent across start-ups.
    Using pound variable outside those ranges risks very unpredictable results as Mach may be using them.

    Craig



  4. #4
    Member deadlykitten's Avatar
    Join Date
    Jun 2015
    Location
    Antarctica
    Posts
    4154
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    hy joe, yes, programing simple operations without using a solid, or 2d-contours, can be done by using simplified conversationals

    however, i believe that old-phart wishes to achieve an understanding of cnc capabilites, and maybe to customize his cam

    about variables, you are right, is not easy to handle them as the code size increases, but i like to use them to generate custom behaviours; some of this customizations may be implemented inside a cam, but so far, i have noticed that only a few persons show a desire to change how a cam works, and from them, only a fraction can customize the post or the cutting strategy / kindly

    we are merely at the start of " Internet of Things / Industrial Revolution 4.0 " era : a mix of AI, plastics, human estrangement, powerful non-state actors ...


  5. #5
    Member routalot's Avatar
    Join Date
    Dec 2003
    Posts
    1218
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    I can't help with the variables,and for operations like surfacing I don't think I would bother with them.I have found it easier to get the machine to the start point and to use the M98P call for a sub routine . Then to use the sub routine to add the values for travel and stepover with the L value at the end of the line to indicate how many times to perform the out and back sub routine.One thing does look a bit odd in your sample code; should that M04 be there? The controllers I am familiar with would read that as an instruction to start the spindle in the opposite direction.Your controller may be different but an M05 might be better in most instances.

    I do use a simpler way to deal with these operations now.In my own workshop I use Freecad for toolpaths and since I discovered the spreadsheet workbench it has become ridiculously easy to just input the values for length and width of the surface and watch the toolpath regenerate on screen.One click to post process and the file is sent directly to a folder from which I can load it into the controller.It is a good thing to have a sound grounding in G code but it is faster and more reliable to have a computer do the grunt work.You also get to see a simulation,which can be a valuable early warning system.Incidentally I mention Freecad and it costs ,as the name suggests, nothing as it is free.



  6. #6
    Member
    Join Date
    May 2005
    Location
    canada
    Posts
    1662
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    It looks like you're trying to do parametric programming in G&M code. Even if this example can be made to work it's like pounding a square peg in a round hole and the limitations will show soon.
    The answer is a real programming language or something like Macro B.
    For simple things like facing I use free software but I suspect the Wizards mentioned earlier will be less hassle.
    A link if you want to see what I'm using:
    https://github.com/linuxcnc/simple-gcode-generators

    Anyone who says "It only goes together one way" has no imagination.


  7. #7
    Member
    Join Date
    Nov 2013
    Posts
    4361
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    Hi,
    I like cyclestart's comment:

    It looks like you're trying to do parametric programming in G&M code.
    Parametric programming does have a place in CNC, particularly in high volume, highly repetitive operations. I'm dubious that OP's
    experience and fluency are going to be aided by delving this particular method.

    Just as a side note Mach4Industrial, as opposed to Mach4Hobby, has MacroB which allows the type of parametric programming used by the Pro's.

    There is a chappy, a regular correspondent on the Mach4 Discussion board, who has used Mach4Industrial and MacroB to control a grinding machine
    in his workshop. I would recommend to OP to reach out to him as his experience in parametric programming would be invaluable.

    Craig



  8. #8

    Default Re: Using Variables

    I'm dubious that OP's experience and fluency are going to be aided by delving this particular method.
    I disagree. I was trying to learn gcode syntax and flow from the beginning using simple projects like flattening and pocketing. I got my flattening and pocketing done with relatively few lines of code (around 20 as I recall) compared to what Vectric puts out for the same job.
    I learned that I need to spend additional $$ on a platform to enable the use of all parts of the language that would have made it easier and clearer to write. That was an interesting albeit disappointing find. That led me to understand why the code produced by programs don't include these operators.

    I didn't follow up on the variables yet, but I intend to just to make them work or find that I can't without something else in place.

    All knowledge is good. Especially helpful when the high level programs screw up.



  9. #9
    Member deadlykitten's Avatar
    Join Date
    Jun 2015
    Location
    Antarctica
    Posts
    4154
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    I got my flattening and pocketing done with relatively few lines of code (around 20 as I recall) compared to what Vectric puts out for the same job. I learned that I need to spend additional $$ on a platform to enable the use of all parts of the language that would have made it easier and clearer to write
    hy, please, the extra $$ is required to allow editing how code is written/generated ?

    That led me to understand why the code produced by programs don't include these operators.
    what operators are you reffering to ? kindly

    we are merely at the start of " Internet of Things / Industrial Revolution 4.0 " era : a mix of AI, plastics, human estrangement, powerful non-state actors ...


  10. #10

    Default Re: Using Variables

    Quote Originally Posted by deadlykitten View Post
    hy, please, the extra $$ is required to allow editing how code is written/generated ?

    what operators are you reffering to ? kindly
    Mach 4 Hobby ($100) does not allow While.Do or anything in the FanucB instruction set (While Do is part of that set). For that support I need Mach4 Industrial at $1400.



  11. #11
    Member deadlykitten's Avatar
    Join Date
    Jun 2015
    Location
    Antarctica
    Posts
    4154
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    let's supose that it would work ... please, will you share an example that uses while.do, or whatever else, that does what you wish ?

    i am asking this, because it seems that you wish to have 'logic' inside a code spitted by a cam, and i am curious about what you wish to achieve

    we are merely at the start of " Internet of Things / Industrial Revolution 4.0 " era : a mix of AI, plastics, human estrangement, powerful non-state actors ...


  12. #12

    Default Re: Using Variables

    I'm not trying to have the CAM software spit out logical code. I was simply trying to do it myself using logical constructs. Which is possible to do, just not with my version of Mach4.
    I was trying to surface a spoilboard by going back and forth, incrementing Y with each pass. I made it work using M98 but this is where I was trying to use both math in the vars and While...Do, neither of which is supported in Mach4 Hobby

    I'm just a hobbyist so this may not be the "BEST" way to do this in production, but it's what I needed to do to see how Gcode works and find some limitations.


    This is where I started
    #107 = 0 ; Starting (and Current) Y Position#101 = 46.5 ; X Max (Static Variable, Object width)
    #102 = 34.0 ; Y Max (Static Variable, Object height)
    #103 = 0.1 ; Step overlap (Static Variable)
    #104 = 1 ; Tool Diameter (Static Variable)
    #105 = #104-#103 ; Y Increment
    #106 = -0.010 (Depth of Cut)

    G0 G90 G54 G17 G40 G49 G80 ;Safety Line
    G0 X0 Y0 Z.2 ; Home
    ;M03 S18000; Start spindle
    G1 Z0.2 F10 ; Get close to surface
    G04 P5000 ; Pause 3 seconds for VFD comes to speed
    M0 ; Confirm the Cycle Start


    G01 Z#106 ; Z to Depth


    WHILE [#107 LE #102] DO1 ; Start Loop, run until Y value reached
    G01 Y#107 F10. ; move TO Y
    G01 X#101 ; Go To X Max
    #107=#107+#105 ; Increment Y variable by step over value
    G01 Y#107 F10. ; move Y to next step
    G01 X0 ; Move to X Min
    #107=#107+#105 ;increment Y by step over value


    END1 ; End Loop


    G0 Z5.0 ; Raise Spindle
    M04 ; stop spindle
    G0 G90 G54 G17 G40 G49 G80
    G0 X0.0 Y0.0 ; Go Home
    M30 ; exit routine




  13. #13
    Member
    Join Date
    Nov 2013
    Posts
    4361
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    Hi,

    I was trying to use both math in the vars and While...Do, neither of which is supported in Mach4 Hobby
    Mach3 and Mach4Hobby support the use of variables and mathematics on those variables but DO NOT support conditional constructs like While Do.

    Craig



  14. #14
    Member
    Join Date
    May 2005
    Location
    canada
    Posts
    1662
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    For that support I need Mach4 Industrial at $1400.
    OUCH ! Honestly you're not missing much if cnc is a hobby imo. I never much liked that style of programming back in in my industry days. Hard to edit and a pain if a tool needs replacing half-way through.
    Us old farts, (and I'm assuming you're one of us), need to tackle stuff like this occasionally to keep our brains from turning to porridge. In that spirit I tried to remember some python and managed to copy your program, and it works if the same values are plugged in. In all other ways it's a hot mess haha.
    Code:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    #  for_py_3.py
    #  
    
    def main(args):
        Ystart = float(input("(initial Y position        "))
        Xmax = float(input("(maximum x                 "))
        Ymax = float(input("(maximum y                 "))
        StepOver = float(input("(step overlap              "))
        Diam = float(input("(tool diameter             "))
        DepthCut = input("(depth of cut              ")
        Yincr = float(Diam - StepOver)
        print("(Y increment               " + str(Yincr))
        print("\nG0 G90 G54 G17 G40 G49 G80\nG0 X0 Y0 Z.2")
        print("M03 S18000\nG1 Z0.2 F10\nG04 P5000\nM0")
        print(str("G01 Z ") + str(DepthCut) + str(" F10"))
        
        while Ystart <= Ymax:
            print("Y " + str(Ystart))
            print("X " + str(Xmax))
            Ystart = round((Ystart + Yincr), 3)
            print("Y" + str(Ystart))
            print("X 0")
            Ystart = round((Ystart + Yincr), 3)
        print("G0 Z5\nM04\nG0 G90 G54 G17 G40 G49 G80")
        print("G0 X0.0 Y0.0\nM30")
               
    if __name__ == '__main__':
        import sys
        sys.exit(main(sys.argv))
    If the code tags are working it should be possible to paste in here and choose python 3 as interpreter:
    https://www.onlinegdb.com/online_python_interpreter
    (or simply use python 3 offlinei)

    Or simpler just read what was spit out:
    Code:
    (initial Y position        0        )
    (maximum x                 46.5     )
    (maximum y                 34.0     )
    (step overlap              0.1      )
    (tool diameter             1        )
    (depth of cut              -0.010   )
    (Y increment               0.9      )
    
    G0 G90 G54 G17 G40 G49 G80
    G0 X0 Y0 Z.2
    M03 S18000
    G1 Z0.2 F10
    G04 P5000
    M0
    G01 Z -0.010 F10
    Y 0.0
    X 46.5
    Y0.9
    X 0
    Y 1.8
    X 46.5
    Y2.7
    X 0
    Y 3.6
    X 46.5
    Y4.5
    X 0
    Y 5.4
    X 46.5
    Y6.3
    X 0
    Y 7.2
    X 46.5
    Y8.1
    X 0
    Y 9.0
    X 46.5
    Y9.9
    X 0
    Y 10.8
    X 46.5
    Y11.7
    X 0
    Y 12.6
    X 46.5
    Y13.5
    X 0
    Y 14.4
    X 46.5
    Y15.3
    X 0
    Y 16.2
    X 46.5
    Y17.1
    X 0
    Y 18.0
    X 46.5
    Y18.9
    X 0
    Y 19.8
    X 46.5
    Y20.7
    X 0
    Y 21.6
    X 46.5
    Y22.5
    X 0
    Y 23.4
    X 46.5
    Y24.3
    X 0
    Y 25.2
    X 46.5
    Y26.1
    X 0
    Y 27.0
    X 46.5
    Y27.9
    X 0
    Y 28.8
    X 46.5
    Y29.7
    X 0
    Y 30.6
    X 46.5
    Y31.5
    X 0
    Y 32.4
    X 46.5
    Y33.3
    X 0
    G0 Z5
    M04
    G0 G90 G54 G17 G40 G49 G80
    G0 X0.0 Y0.0
    M30
    And if nothing here is of interest at least I had some fun

    Last edited by cyclestart; 02-23-2020 at 03:20 AM.
    Anyone who says "It only goes together one way" has no imagination.


  15. #15
    Member deadlykitten's Avatar
    Join Date
    Jun 2015
    Location
    Antarctica
    Posts
    4154
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    I'm not trying to have the CAM software spit out logical code. I was simply trying to do it myself using logical constructs. Which is possible to do, just not with my version of Mach4
    hy i am sorry, but i don't get it : if you allready have this code, then why do you wish for the mach4 $1400 update ? is that $1400update capable of simulating toolpaths generated by parametric code, or is it capable of assisting you to develop parametric code ( for example by having a debbuger ) ? what exactly is doing that update ?

    most programmers that develop parametric code, don't need a cam to do it; they simply write it & test it on the machine

    do you wish to interface your code with the cam ? please, what exactly are you after ? onestly, i really don't understand what you wish to do ...

    And if nothing here is of interest at least I had some fun
    hy cyclestart, so you created a program that generates linear code, doing the same thing like the parameterized code shared by old-phart

    but is faster to edit the parameterized code inside the machine, instead of re-runing a pc based program

    please, will someone explain what am i missing ? i can't understand the idea behind all these ... kindly

    we are merely at the start of " Internet of Things / Industrial Revolution 4.0 " era : a mix of AI, plastics, human estrangement, powerful non-state actors ...


  16. #16
    Member
    Join Date
    May 2005
    Location
    canada
    Posts
    1662
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    deadlykitten:
    You're a professional and look at this from a practical point of view. That's what pros need to do. When this is a hobby we're free to do things for amusement and maybe learn a new skill.
    I wasn't sure if Old_Phart had a way to test his program so thought he might like some confirmation it works.
    I could have also run it (mostly) as he wrote it and shown a backplot.
    btw: Using linuxcnc calling a python program is easy and a common thing to do.

    summing up: it doesn't make much sense but it doesn't matter

    ps: if anyone plays with that python script be aware it waits for 'some_number' [enter]

    Anyone who says "It only goes together one way" has no imagination.


  17. #17
    Member deadlykitten's Avatar
    Join Date
    Jun 2015
    Location
    Antarctica
    Posts
    4154
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    I wasn't sure if Old_Phart had a way to test his program so thought he might like some confirmation it works
    hy cyclestart, i am also wondering why old_phart wishes for that $1400update, and if he can test the code on the machine:
    ... if he can't test the code on the machine, then why bother to develop it ?
    ... if he can test the code on the machine, then why bother about that $14 update ?

    summing up: it doesn't make much sense but it doesn't matter
    maybe you are right ... but maybe he will get used to a wrong way of programing; however, maybe he has a good idea, maybe he is after something : this is why i replied in this thread, maybe there is something

    btw: Using linuxcnc calling a python program is easy and a common thing to do.
    this is the 1st time when i hear such a thing : please, can you develop it a bit ? thus you can run phyton source-code on a cnc ? or you use phyton to output g-code ? kindly

    ps : i am not a professional, just hanging arround here ...

    we are merely at the start of " Internet of Things / Industrial Revolution 4.0 " era : a mix of AI, plastics, human estrangement, powerful non-state actors ...


  18. #18
    Member
    Join Date
    May 2005
    Location
    canada
    Posts
    1662
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    thus you can run phyton source-code on a cnc
    No. What I meant to say was the (Axis) User Interface allows python files to be selected from the ' file->open ' menu. A python interpreter does the work. Linuxcnc doesn't directly read python script though that would be an interesting feature.
    or you use phyton to output g-code ?
    Yes

    btw: Linuxcnc can do some of the Macro stuff. Old_Pharts program would need just a little change like this:
    Code:
    #107 = 0 ; Starting (and Current) Y Position
    #101 = 46.5 ; X Max (Static Variable, Object width)
    #102 = 34.0 ; Y Max (Static Variable, Object height)
    #103 = 0.1 ; Step overlap (Static Variable)
    #104 = 1 ; Tool Diameter (Static Variable)
    #105 = [#104-#103] ; Y Increment
    #106 = 0.010 (Depth of Cut)
    
    G0 G90 G54 G17 G40 G49 G80 ;Safety Line
    G0 X0 Y0 Z.2 ; Home
    M03 S18000; Start spindle
    G1 Z0.2 F10 ; Get close to surface
    G04 P5000 ; Pause 3 seconds for VFD comes to speed
    M0 ; Confirm the Cycle Start
    
    G01 Z [-1 * #106] ; Z to Depth
    
    o101 while [#107 LE #102] ; Start Loop, run until Y value reached
      G01 Y#107 F10. ; move TO Y
      G01 X#101 ; Go To X Max
      #107 = [#107+#105] ; Increment Y variable by step over value
      G01 Y#107 F10. ; move Y to next step
      G01 X0 ; Move to X Min
      #107 = [#107+#105] ;increment Y by step over value
    o101 endwhile
    
    G0 Z5.0 ; Raise Spindle
    M04 ; stop spindle
    G0 G90 G54 G17 G40 G49 G80
    G0 X0.0 Y0.0 ; Go Home
    M30 ; exit routine


    Anyone who says "It only goes together one way" has no imagination.


  19. #19

    Default Re: Using Variables

    I typed my stuff in notepad then simply ran it using Mach4. For parts that got me stuck I used the MDI panel. Thats how I figured it out and tested it. I tried the g-code simulator which was great - but alas it can use while do and so I was befuddled for a while till I was told about the differences. And you are correct - I'm just keeping my brain active.

    It was simply a way to learn. Thats it. In the end I'll use Aspire for the cad/cam. I always like to know what going on under the hood and this is how I did it.



  20. #20
    Member deadlykitten's Avatar
    Join Date
    Jun 2015
    Location
    Antarctica
    Posts
    4154
    Downloads
    0
    Uploads
    0

    Default Re: Using Variables

    hy cyclestart please, my english is not ok, and you seem to know a few things, and i am looking for advice

    i searched a bit on youtube, for linuxcnc&phyton, and after that i remembered that i have okuma.api, but i did not had time to look into it, being busy with regular stuff

    it seems that there are persons that try to test their g-codes inside a pc software, and some of them hit a wall, because is not ea$$y to find the software that can do it

    a few years ago i have assisted at a discussion, about someone saying that he is using a software that was, somehow, g-code friendly, helping him to test / debug, before sending the code to the machine; in that moment, it did not resonate for me, because i could test my codes very fast on the machine, having windows on the machine, fast file transfer, creating data logs, using codes with variables to output linear g-code, several kind of animation, etc, thus i was used with many "goodies"; in this way, i may test & develop pretty fast, improving a code in a relative short time

    but there are controllers that are not so capable, maybe they are as simple as they should be, and programers that are trying to test a code will look for other methods

    on a limited controller, programmers are mostly satisfied if their code works, while on a more performant controller, is possible to built custom codes doing some exotic stuff, that is not even possible to generate (or test) on a regular cam

    after a while, i had been re-thinking about this, because if you are specialized on a certain type of machine, having some custom macros, it may be hard to "shift" all your work to another machine, with a different controller, and from this perspective, i decided to change the way i create codes : i believe that all "logic" from macros has to be moved inside a software ( for example how you said, that you may use linux&phyton ), leaving inside the machine only those macros that are machine-specific ( like those ones that use real-time values, so to take decisions doing run-time, like probing, or checking plc signals, etc )

    please, what do you think about this, and how do you imagine programing ? for example, what would you like to be possible to do ? i don't know, sorry for the looong reply / kindly

    we are merely at the start of " Internet of Things / Industrial Revolution 4.0 " era : a mix of AI, plastics, human estrangement, powerful non-state actors ...


Tags for this Thread

Posting Permissions

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


About CNCzone.com

    We are the largest and most active discussion forum for manufacturing industry. The site is 100% free to join and use, so join today!

Follow us on


Our Brands

Using Variables

Using Variables