Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: zarsss on October 06, 2017, 11:28:48 AM

Title: G-code execution from screen button.
Post by: zarsss on October 06, 2017, 11:28:48 AM
Hi. Me again. Looking for any advice, how to create three buttons on the screen, and when i click any of them each one executes different g-code. Thank you.
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 06, 2017, 02:16:13 PM
Hi,
what sort of Gcode? Is it one  or two lines of code or is it a complete Gcode file? Does the code change?

If each piece of code you wish to execute is reasonably small and is fixed then
Quote
LUA Syntax:
rc = mc.mcCntlGcodeExecute(
      number mInst,
      string commands)
will be very useful to you. You can use it in the left-up button event script to execute small batches of code.

Craig

Title: Re: G-code execution from screen button.
Post by: zarsss on October 07, 2017, 04:47:17 AM
My target is to machine a part from 3 sides. The jig is coming up, to clamp three parts on the bed the same time. But each part is being machined at different stage. Ideally i would like to have three buttons on the screen: Do Stage 1, Do Stage 2, Do Stage 3. So  i dont know, can small batches of code classify 2000 lines. Also i have to admit, that i am hardware person, and absolutely noob in lua coding.
Title: Re: G-code execution from screen button.
Post by: zarsss on October 07, 2017, 04:51:57 AM
Here is a baby.

Title: Re: G-code execution from screen button.
Post by: joeaverage on October 07, 2017, 05:16:14 AM
Hi,
that API call is useful for Gcode snippets of two-three-four lines... not 2000.

The way to do that is to have a button call a Gcode file, three buttons, three files. To my knowledge the Gcode interpreter can only have one
active Gcode job. So if you hit button #2 the code which had been running will be closed and the file associated with button #2 loaded and then run.

Another possibility it to have one large file with three sections, one section associated with one button. This would mean that just the one Gcode file
would be loaded and it would not be necessary to unload between buttons. It might be possible to have yet other sections that control the jig and/or
clamping of the part.

Just exactly how you go about indentiying your part zero for the three different operations would require some thought.

Are the Gcode chunks likely to change often?

Craig
Title: Re: G-code execution from screen button.
Post by: zarsss on October 08, 2017, 09:08:38 AM

The way to do that is to have a button call a Gcode file, three buttons, three files. To my knowledge the Gcode interpreter can only have one
active Gcode job. So if you hit button #2 the code which had been running will be closed and the file associated with button #2 loaded and then run.

How this can be done, please? Gcode files will be changing at the beginning  till we happy with results. After that it will be semi mass production.
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 08, 2017, 12:17:05 PM
Hi,
two preliminary things to be done.

1) Set up a folder somewhere on your machine that will have the three Gcode files. Your buttons will be calling these files so they will
need a fixed filepath and the files themselves need a unique name.

2) You need to set up three buttons on your screen. Exactly where you put them is up to you. If you are not familiar with editing the screen
then you need this:
http://www.machsupport.com/wp-content/uploads/2014/05/Mach4%20Screen%20Editor%20V1.0.pdf (http://www.machsupport.com/wp-content/uploads/2014/05/Mach4%20Screen%20Editor%20V1.0.pdf)
For ease I would put them in the same 'container', means that you don't have to chase all around looking for the code. Given that you are
modifying the screen you should save it as a unique file so when you update Mach the standard screen set doesn't get overwritten.

Once you have these two items in hand then the event script of each button will be populated. Two instructions likely to be of use are:
Code: [Select]
rc = mc.mcCntlCloseGCodeFile(
number mInst)
and:
Code: [Select]
rc = mc.mcCntlLoadGcodeFile(
number mInst,
stringFileToLoad)

Craig
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 08, 2017, 11:51:57 PM
Hi zarass,
had a couple more thoughts that may help you along. I don't know what screenset you are using, my one is now somewhat personalised but is
still very close to wx4.set which is  one of the standard sets that ships with Mach. Given that it is personalised I've given it a new name.

If you note the five buttons just below the DRO block <DeRef All Axes>,<GoTo Work Zero>,<blank>,<Machine Coords>,<Distance To Go>. I don' know about you
but I only use <GoTo Work Zero> and <Machine Coords>. Because the screen is that full of stuff already what I would do is reuse the unused buttons and
the one unassigned one for the three buttons you require. Cheap and Cheerful, Down and Dirty is my motto!

Actually adding buttons and LEDs and things is quite fun but you don't really have much room to play with. I've found you have to shrink a lot of existing
buttons and other elements just to squeeze in another button or whatever it is that you want.

If that sounds Like a reasonable idea to you then its just a case of using the screen editor to change two buttons to your preferred name and assigning a name
for the one unused button. You would need to clear any event script or functionality of the two buttons you are re-purposing.

Craig
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 08, 2017, 11:58:38 PM
Hi zarass,
just one other thing....your machine pics are great but because they are embedded in the text they open and download each and every time
the post is opened.

A better method for pictures, particularly detailed high resolution pictures is to add them as an attachment. They show up then as thumbnails and the
reader of the post can download and view as he wishes or not as the case may be.

When posting with the text window open you'll see 'Additional Options...' to the lower left of the panel, click on it and you can browse to any file
including jpegs that you wish to attach.

Craig
Title: Re: G-code execution from screen button.
Post by: zarsss on October 09, 2017, 08:44:45 AM
Hi. Sorry about pictures, not using forums a lot. Unfortunally i cannot edit my post to fix this.

Also thank you for button advice.  I just don`t know, how use those scripts. Where i should put filenames and file path in? Actually i know nothing about it. I was struggling with M6 script until i found one in samples.
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 09, 2017, 01:39:17 PM
Hi,
set up the buttons and a folder and the rest I can help with.

Craig
Title: Re: G-code execution from screen button.
Post by: zarsss on October 10, 2017, 03:36:20 AM
Good Morning. Created three buttons, attachment  below. Also have a folder at C:\Mach4Hobby\Button Codes

Thank you for your help, much appreciated.
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 10, 2017, 04:15:59 AM
Hi,
very low temperature Musa acuminate (kool bananas).

I'm going to suggest a wee work-around which will allow some flexibility and yet still remain fairly simple.
As it turns out initialising a string variable in Lua is not that easy because \ is an escape character. So the Lua interpreter is reading a line of text,
it encounters a \ and then treats the remainder of the line of text as an escape sequence. You can get around it by having two \\ in a row, its a bit
confusing and cumbersome.

Instead I'm going to suggest using Machs registers, means its easy to add a line of text with \ in it and also means its a simple edit if you wish to
change the name and/or file path to the Gcode files.

Fire up Mach go to Configure/Plugins/Regfile and click the wee green cross to add a new register. Give it a name like Button1
and as its initial value put C:\Mach4Hobby\Button Codes\Button1.tap You can expand the width of the column to see it if it makes it clearer. Ensure that the 'persistent'
option is checked. Likewise add two new registers Button2 and Button3 with the path and file as initial value. The description is optional and for your use. Hit <apply>

These registers now hold the path and filename, easy. Check now Diagnostic/Regfile and expand iRegs0 and your new registers should be there and if you
click on them you can edit them right there without delving into the GUI event scripts, nice.

I'm dreaming this up as I go...I would normally have a cigarette and a cup of coffee now while I thought some more....but I given up cigarettes...they were
damn handy at times like this.

Craig
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 10, 2017, 04:42:13 AM
Hi,
think I've got some code, I haven't tested it...maybe you can do it, I cant really be bothered cluttering up my screen with more buttons to test your
code...

For the Left Up Script for Button1:
Code: [Select]
local inst=mc.mcGetInstance();
mc.mcCntlCloseGCodeFile(inst);
local hreg=mc.mcRegGetHandle(inst,"iRegs0/Button1");
local filename=mc.mcRegGetValueString(hreg);
mc.mcCntlLoadGcodeFile(inst,filename);

Pretty simple and straight forward. Where it might come unstuck is if the file is not where it supposed to be or is null or corrupt etc.
If you have a look at the API for CntlLoadGcodeFile you will find 9 exceptions as return code. If you wish you could test for each
of the return codes to trap errors rather the crash Mach because you left the file empty for instance.

In the mean time go and put a legitimate Gcode file called Button1.tap in your folder and try out the button.

Haven't added any operation codes like <cycle start> yet but we'll get there.

Craig

Title: Re: G-code execution from screen button.
Post by: zarsss on October 10, 2017, 04:54:43 AM
WOW! This is so cool. It works! I am so thankful. I cannot wish it to be better. Thank you, thank you!
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 10, 2017, 05:11:09 AM
Hi,
kool, I didn't expect it to work straight off. I'm almost disappointed, it could've thrown a few challenges!

I think some error trapping would be a good idea, if its to operate in a production or semi production environment its highly desirable
that a small mistake or oversight not crash Mach.

Also do you wish to have execution automatic? Do you wish to have some 'safe' moves when a file is closed so the tool doesn't run into the jig
or a clamp on its way to the next lot of code?

Craig
Title: Re: G-code execution from screen button.
Post by: zarsss on October 10, 2017, 05:30:18 AM
Yeah, safe moves always are good, especially as for first time we made so powerful machine. Spindle power is enormous.

Oh, and some visual stuff would be great next to buttons, to see witch one is pressed.
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 10, 2017, 05:47:38 AM
Hi,
yes I thought they might be useful. I would suggest something like:
1) <feedhold>
2) Retract spindle to safe height
3) move to an XY location such that any subsequent move by the first Gcode in any of the three files is safe
4) close old file
5) load new file
6) <cycle start>

The few lines of code to enact 2) and 3) could be done  with CntlGcodeExecuteWait
1) and 5) could just be regular function calls to the functions in the screen load script.

As far as error trapping goes maybe it sufficient just to ensure an MEROR_NOERROR as a return code and call all other return codes as faults
and cease WITHOUT crashing Mach. Good trick if you can do it, my exception trapping and handling skills in Lua are very so so...

Craig
Title: Re: G-code execution from screen button.
Post by: zarsss on October 10, 2017, 06:43:56 AM
So i made up some toggle buttons with visual display, witch one is pressed. Each button loads a gcode at on stage, closes gcode at off stage. Is there a way to turn off other buttons while one is at on stage?
Title: Re: G-code execution from screen button.
Post by: zarsss on October 10, 2017, 07:28:19 AM
.... an ran into a problem...

error 1006: The volume for a file has been externally altered so that the opened file is no longer valid. 

Some buttons showing this.
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 10, 2017, 01:20:09 PM
Hi,
that's a new one. I think that one or more of the files associated with each button is invalid.

All of those buttons point to code where? Are all of the files fixed in size? Are all of the files there? Are all of the files non-empty?

How do these buttons differ from the first ones that you formed and apparently worked? These new buttons all appear to be bounded by a box.
What are the properties of the box?

Craig
Title: Re: G-code execution from screen button.
Post by: zarsss on October 11, 2017, 02:54:37 AM
Good morning. Yesterday was tough day. Sorted out buttons, but while i was doing that, had a lots of different problems. Mach4 itself was crashing a lot of times. Today, turned machine on, and it works first time. So my guess is, Mach4 is using some of the laptops resources, and when insufficient does a crash. Will see how it goes today, as we planning couple of dry runs. Still, thank you for button solution.
Title: Re: G-code execution from screen button.
Post by: joeaverage on October 11, 2017, 03:37:19 AM
Hi,
I think you may need to give some more consideration to error trapping and handling. Exception handling is one of the most difficult
areas of programming to get right and yet has a major bearing on the useful life of the software.

Craig

PS Its good night here!
Title: Re: G-code execution from screen button.
Post by: zarsss on October 11, 2017, 05:00:00 AM
First test run. Spot on.

https://www.youtube.com/watch?v=VbzZW_7xC_8&t=5s (https://www.youtube.com/watch?v=VbzZW_7xC_8&t=5s)