Hello Guest it is March 03, 2024, 10:13:11 AM

Author Topic: Can Mach4 use custom m-codes to trigger a user alert box?  (Read 1354 times)

0 Members and 1 Guest are viewing this topic.

Can Mach4 use custom m-codes to trigger a user alert box?
« on: December 06, 2019, 12:47:19 PM »
I would like to make custom m-codes in my cutting programs that trigger an alert box on the screen which has to be clicked on by the user before the program will continue.

I have two examples in mind:

1.  An alert box close to the start of the cutting program which reminds the user to set Z-zero to the top of the workpiece or to the table.

2.  An alert box which pauses cutting in order for the user to re-clamp the workpiece before continuing cutting.


I've tried searching for how to do this but I'm not finding any lua-scripting examples which trigger an alert box on the screen.

Is this possible?  And can anyone steer me in the direction of where to start?  I've read through the Mach4 scripting manual but there are no hints for how to make an alert box.


Any help would be much appreciated.

Leo
Re: Can Mach4 use custom m-codes to trigger a user alert box?
« Reply #1 on: December 06, 2019, 02:09:52 PM »
Hi,
yes, that is easy stuff for Mach4.

There is a steep learning curve to Mach4 with respect to customizing it, first there's Lua, then there is Machs API and then
Machs structure. Many feel that its too tough but those who have made the effort to climb the curve are rewarded with an
endlessly flexible CNC solution.

Writing macros in Mach4 is a breathe of fresh air after Mach3.

First open either the Edit/Debug Scripts OR Open Script Editor from the Operator tab.
I have chosen to open the script editor, called Zero Brane, because its more general and shows a comprehensive file tree
which in turn alludes to how Mach is structured.

The first thing to realize is that macros are stored in the Macros folder of EACH profile. If you have two profiles, lets say an
engraving profile, and another mill profile for when you want to cut brass say.....then EACH profile will have a distinct
Macros folder. If you write a macro for one profile but decide that its applicable to the other profile as well you'll have
to copy it to that profile Macros folder as well. There is a better way....called modules.....but lets not muddy the waters just yet!

You can see that I navigated the tree to my profile, C:\Mach4Hobby\Profiles\ESSMill\Macros...... and there is the list of my own
macros on my laptop.

You may note that the same macro m101 has several different versions, m101.lua, m101.mcs, and m101.mcc
The version we are interested in is the .mcs file, (macro code source). The .mcc is the compiled code and is gibberish to us,
its generated when the .mcs file is compiled. The .lua file is extraneous but Zero Brane generates .lua files by default where we
really want .mcs files to populate our macros folder.

With the Zero Brane editor now open you can write a macro at will. You can save it where you want by navigating the file tree,
but you'll naturally want to save it to your current profile Macros folder.

The Zero Brane editor allows you to write and debug ( by running the code) Lua. It has quite a few features that you would expect
for an editor designed for writing computer code, things like auto complete, various breakpoint devices plus all the usual Find
and Replace text editing commands.

Craig


'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Can Mach4 use custom m-codes to trigger a user alert box?
« Reply #2 on: December 06, 2019, 02:25:46 PM »
Hi,
Ok....so what to write?

All Mach4 macros start with an 'm'. Note here I use lowercase deliberately, uppercase works but fails occasionally which I will
explain later.

I follow the tradition set by Mach3 that any user macros should be numbered m100 or higher. Mach4 has some macros you don't
and should not overwrite, like m5 or m30. You can in fact overwrite them but that might also break your machine. Unless you
deliberately and especially wish to overwrite one of Machs core macros then chose to name your macros m100 or higher.
One small exception is m162 and m163. Those macros ship with Mach4 and are for laser ops, I would not by choice overwrite
them, despite me not having a laser.

All macros in Mach are functions and are called by a Gcode program by writing m101 for instance.

In your macros folder is where the function is stored:

function m101()
......
......
.....function body
.....
.....
end
if (mc.mcInEditor()==1) then
      m101()
end

So the function, in Lua is m101(). Then what is the deal with the:
Quote
if (mc.mcInEditor()==1) then
      m101()
end
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Can Mach4 use custom m-codes to trigger a user alert box?
« Reply #3 on: December 06, 2019, 02:58:17 PM »
Hi,
sorry misclicked and posted before I was ready....(my missus says that all the time????)

When you wish to run the code in the editor the function must be called......but what program or device is there to do the calling?
There isn't a parent program to do it and that's where that little bit of extra code at the end of just about every macro in Mach4
comes in.

It says that.....IF Mach is in Editor Mode......THEN call the function m101() (please...)
So that little bit of code formally calls you macro function code so you can run/debug it. The extra code has no revelance to
your Gcode program as Mach is not in Editor mode at that time so the call is ignored.

Ok....we now have a structure for your first macro, but what is your macro to do?
You want a text box that has to be cleared by the operator. That's easy. Mach4, or rather Lua which underpins it, makes
very liberal use of wxWidgets, which does amongst other things (a very VERY VERY GREAT number of other things)
a textbox called wx.MessageBox.

wxWidgets is one of those steep learning curves that you will encounter....its written and composed by computer geeks for computer
geeks, and from what my mother told me its a wonder that they are not all blind!

Fortunately wxMessage box is easy enough that it doesn't risk your eyesight.

Code: [Select]
function m102()
local inst=mc.mcGetInstance()
wx.wxMessageBox('OK, clear the bloody box you plonker!')
end
if (mc.mcInEditor()==1) then
m102()
end

Note that I called and save the macro as m102() as I already had an m101() which I did not wish to overwrite.
You might ask what is the:
Quote
local inst=mc.mcGetInstance()
line all about?
Firstly Lua is a self memory managed language. When a variable is created it will stay in memory until its 'garbage collected'
by Lua. When you specify 'local' it means that its safe for Lua to garbage collect the variable when it goes out of scope. If you
don't declare your variable as local then it will be global and clog up your PC. The rule in Lua is that all variables should be
local UNLESS there is some specific and overriding need to do otherwise.

Secondly, Mach is intended, not as yet implemented, but will be one day, to run in multiple instances on one PC, referring to different
machines or perhaps different sections of one large production machine. As such when addressing Mach you need to specify
which 'instance' you are referring to. While its not required here...I put it there so you get used to seeing it, you will need it in
times to come.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Can Mach4 use custom m-codes to trigger a user alert box?
« Reply #4 on: December 06, 2019, 10:10:52 PM »
Fantastic help Craig.

I really appreciate your detailed posts, perfect for my inexperience with Lua programming & Mach4!

I'll try this out and report back as soon as I can.

Cheers!
Leo
Re: Can Mach4 use custom m-codes to trigger a user alert box?
« Reply #5 on: December 06, 2019, 10:16:11 PM »
Hi,
your welcome.

You'll need this link open when you start coding for real:

https://www.lua.org/manual/5.3/

It lives on my desktop 27/7 when I'm coding.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'