Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Cbyrdtopper on April 21, 2015, 05:46:06 PM

Title: Having trouble with Lua Script
Post by: Cbyrdtopper on April 21, 2015, 05:46:06 PM
I am having trouble finding examples and syntax for LUA that goes with Mach 4.  Initially I need to know how to turn on outputs via macros.
here is an example from VB Script.  Can anyone help me translate this to LUA and if they have time explain it a little bit?
Thanks!!
-Chad

REM M1000 Clamp
DeActivateSignal (output4) 'Turns off the solenoid that unclamps.
ActivateSignal (output3)   'Turns on the solenoid that clamps.

REM Waits for the cylinder to reach the switch.
While Not IsActive (OEMTRIG2)
     Message ("I'm trying to clamp the tip")
     Sleep 10
Wend

REM Once the cylinder is forward it gives the "made it" message.
Message ("Made it")
Title: Re: Having trouble with Lua Script
Post by: dude1 on April 21, 2015, 07:49:30 PM
do a search through the M4 posts it in there
Title: Re: Having trouble with Lua Script
Post by: Gerber Baby on April 22, 2015, 06:00:09 PM
Sorry I don't have a lot of time, I am meeting some friends for pizza and will try to follow this up, but hopefully I can point you in the right direction.
You'll get a lot of help from the API documentation and the samples in the LuaExamples folder, also dig through the Mach4Toolbox subsection of this forum and look for a document called : "McLua mc Scripting Reference SpacedOut.pdf" it's really helpful.

This is how you put in a remark:
--This is the remark.  It works on only one line, and is preceded by two dash marks.

Message Boxes are written as follows:
wx.wxMessagebox("Your message goes here.");

Sleeping, where number of milliseconds to sleep goes between parenthesis.
wx.wxMilliSleep()

Activating signals is a little harder, first the controller instance must be captured:
local inst = mc.mcGetInstance()

and then for some reason the signal must be ported through a handler before it is used. 
this is the mcSignalGetHandle() command, see also mcSignalMap

the signals you are looking for are OSIG and ISIG, look in the API manual (C:\MachFolder\Docs\) under "Mach Signals" where you will also find the Get and Set commands

functions are called like this:

function NameOfFunction (arguments)
    -- comment so I can fill the function and show that bracket notation is not necessary for multi-line function
end
Title: Re: Having trouble with Lua Script
Post by: Cbyrdtopper on April 23, 2015, 11:22:35 AM
Thanks Gerber Baby.  I appreciate the help.  But I've looked in the examples and changed small parts of them to try and get them to work.  I can't even get simple G code movement in my macros.  I also don't understand the signal states.  I don't understand the syntax of LUA.  Just getting frustrated.  If anyone can help with a simple macro to do the following that would be great.

Move X to 5
De activate an output1
Wait for input2
Activate output2
Wait for input 3
Move X to position in a user DRO
Message:  Ready to go.

If anyone could help translate this to lua that would be greatly appreciated.  In the meantime I'll try it out some more.
Thanks!
-Chad



Title: Re: Having trouble with Lua Script
Post by: Gerber Baby on April 23, 2015, 08:01:33 PM
READ:
http://www.machsupport.com/forum/index.php/topic,27141.0.html (http://www.machsupport.com/forum/index.php/topic,27141.0.html)
http://www.machsupport.com/forum/index.php/topic,28484.0.html (http://www.machsupport.com/forum/index.php/topic,28484.0.html)

Snippets:

Always do this first
Code: [Select]
local mInst = 0;
local rc = 0;
local inst = mc.mcGetInstance(mInst);
--always start with the above code

move x to 5

Code: [Select]
mc.mcCntlMdiExecute(G00 X5);  --moves x to 5, and does basic Gcode anything.
Deactivate Output 1

Code: [Select]
hsig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1);  -- ports output 1
mc.mcSignalSetState(hsig, 0); --sets OUTPUT_1 to false

waiting for input, function really needs to go at top near variable declarations
Code: [Select]
function Input2IsFalse ()
    hsig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2);  --set hsig variable to handle input 2
    sigState = mc.mcSignalGetState(hsig);
    if sigState == 1 then
        return false;
    else
        return true;
    end
end --Input2IsFalse

while Input2IsFalse() do
--nothing
end  --waits until input2 is true

activate output 2
Code: [Select]
hsig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2);
mc.mcSignalSetState(hsig, 1);

DRO... I haven't figured out how to edit screen elements yet.  



Message:
Code: [Select]
mc.mcCntlSetLastError(inst, "ready to go");

Title: Re: Having trouble with Lua Script
Post by: Cbyrdtopper on April 24, 2015, 09:41:26 AM
Thanks Gerber Baby.  I really appreciate the help.  I'm brand new to lua.  I'm still having problems though.
Here is the macro I am trying:

function M1002() end
local mInst = 0;
local rc = 0;
local inst = mc.mcGetInstance(mInst);
--always start with the above code

mc.mcCntlMdiExecute(G00 X5);
--This moves X to 5

if (mc.mcInEditor() == 1) then     
M1002(); end

When I debug this I get an error on the MDI Execute line.  Saying:  ')' expected near 'X5'

So then I change that line to look like this:
mc.mcCntlMdiExecute(G00 'X5');

At this point it will compile the macro and when I step into it I get to that line again and it says:  McLua ERROR: Lua:  Error while running chunk.

Any thoughts?
Title: Re: Having trouble with Lua Script
Post by: Gerber Baby on April 24, 2015, 10:14:35 AM
Yeah, the code I gave you is incorrect.  Somehow I managed to not include the controller instance.  I tested all that code before I sent it to you I guess I copied the wrong one.  Also the GCode command string needs to be in quotes.

Code: [Select]
mc.mcCntlMdiExecute(inst, "G00 X5");  --moves x to 5, and does basic Gcode anything.
Also, if you need to run a command with multiple lines of code, you would add the \n

Code: [Select]
mc.mcCntlMdiExecute(inst, "G00 G90 X0.0 Y0.0\nZ0.0");
Title: Re: Having trouble with Lua Script
Post by: Gerber Baby on April 24, 2015, 10:46:32 AM
Also not sure how your M1002 function is going to work unless it's supposed to do nothing.   The end  command directly follows the m1002 declaration
Title: Re: Having trouble with Lua Script
Post by: Cbyrdtopper on April 24, 2015, 11:56:00 AM
Okay.  Got something to work finally! Thanks!!  But I can only get it to work when I step through my macro. AND I have to have the 'end' right after the
 function M1002 () otherwise it skips to the end and does nothing.  I am having another problem with running the macro in my MDI line in Mach 4.  I put the 'end' at the end of the macro and where it works when I step through the macro and I get nothing in Mach 4.  Also, earlier I would hit cycle start on the MDI line and it was giving me errors in the message for something in a totally different macro than the one I was trying to run.  Any thoughts on these problems?
Thanks Again!!!
-Chad
Title: Re: Having trouble with Lua Script
Post by: Cbyrdtopper on April 24, 2015, 12:10:25 PM
Okay,  with this code I can get it to work when I hit 'Run' in the macro menu.  I don't have to step through it or anything.  I just hit run and it works.  However, when I switch over to Mach 4 and put it in G Code or MDI line it doesn't do anything.  Does the macro have to be saved a certain way?  It is saved as "m1000.mcs"  or in a certain folder.  It is in the macro folder.  Any Ideas?

function m1000()

local mInst = 0;
local rc = 0;
local inst = mc.mcGetInstance(mInst);

mc.mcCntlMdiExecute(inst, "G00 X5 Y5 \nZ5");

end

if (mc.mcInEditor() == 1) then
    m1000()
end
Title: Re: Having trouble with Lua Script
Post by: vindennl48 on April 24, 2015, 01:19:50 PM
I am running into the same issue with the MDI line not always running macros.  It will sometimes, but most of the time it won't work at all. 

Also, I've noticed when opening Mach4, it will run macros at startup.. If I have a message box pop up in, say, my tool change macro.. The message boxes pop up as soon as I open Mach, even before the screen is built. 

Title: Re: Having trouble with Lua Script
Post by: Gerber Baby on April 25, 2015, 11:19:55 AM
Yeah.  Didn't work on my machine either.  Looks like mc.mcCntlMdiExecute() doesn't work in macros.  It does work with buttons though.  Use mc.mcCntlGcodeExecute(); instead like so:

Code: [Select]
function m1000()

local mInst = 0;
local rc = 0;
local inst = mc.mcGetInstance(mInst);

mc.mcCntlGcodeExecute(inst, "G00 X5 Y5 \nZ5");

end

if (mc.mcInEditor() == 1) then
    m1000()
end

If you need it to wait for the Gcode without moving on (sometimes Gcode begins to run the next line without finishing the previous) try mc.mcCntlGcodeExecuteWait();
Title: Re: Having trouble with Lua Script
Post by: Cbyrdtopper on April 27, 2015, 08:11:03 AM
Okay Gerber Baby. That works great!! Thanks!!  ;D  Gonna try the input and output states now!!
-Chad
Title: Re: Having trouble with Lua Script
Post by: Cbyrdtopper on April 29, 2015, 08:25:59 AM
Okay.  So I'm working on the inputs/outputs portion of what you've helped me with.  I have them written in the macro and have a general understanding of the syntax.  But how to I get Mach 4 and the ESS on the same page as far as what outputs and inputs there are.  I am getting drop downs for motor jogging, coolant,  homes, over travel, and etc; I'm not seeing anything as far as Input 1, 2, 3 .... and Output 1,2,3....  Do I have to name them something?
Title: Re: Having trouble with Lua Script
Post by: Cbyrdtopper on April 29, 2015, 08:33:08 AM
Sorry.  I just figured it out.  It has a drop down to Standard Names and then you can put in a custom name, then in the Mach config. choose the ESS and it will have the drop down with your custom name in it.  Works great!! Thanks again for your help.
If I have any more questions I really hope you don't mind me asking on this thread! 
Again, Thanks for all the help!!!
-Chad
Title: Re: Having trouble with Lua Script
Post by: Cbyrdtopper on April 29, 2015, 03:55:28 PM

Here is my code that I am trying to do.  Like it says below.  Turning off a solenoid.  Waiting for it to move back and hit the switch.  Then turn on output 2 which will be a solenoid.  Wait for the switch and then display a message. 
This code works when I step through it, however when I try and run it Via MDI it locks up Mach 4 and closes it.  ANY IDEAS???
-Chad

--TEST CODE INPUTS AND OUTPUTS!!
--Example of a solenoid shutting off and waiting for the switch.
--Turn on output 1 and wait for switch then display message.

function m1002()

local rc = 0;
local inst = 0; -- mc.mcGetInstance(mInst)

-- Turns off output 1
hsig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1); 
mc.mcSignalSetState(hsig, 0); --sets OUTPUT_1 to False

-- Turns off output 2: SOLENOID 1
hsig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0); 
mc.mcSignalSetState(hsig, 0); --sets OUTPUT_0 to False
--Messages are written like this.
mc.mcCntlSetLastError(inst, "Output 2 Off");

function Input1IsFalse () --Solenoid 1 back switch.
    hsig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1);  --set hsig variable to handle input 1
    sigState = mc.mcSignalGetState(hsig);
    if sigState == 0 then
        return true; --Not active yet.
    else
        return false; --Input is active.
    end
end --Input1IsFalse

while Input1IsFalse() do
mc.mcCntlSetLastError(inst, "Solenoid 1 is Moving.");
end  --waits until input1 is true

mc.mcCntlSetLastError(inst, "Solenoid 1 is back.");

-- Turns on output 1
hsig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1); 
mc.mcSignalSetState(hsig, 1); --sets OUTPUT_1 to True

function Input2IsFalse () --Output 1 is active switch.
    hsig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2);  --set hsig variable to handle input 2
    sigState = mc.mcSignalGetState(hsig);
    if sigState == 0 then
        return true; --Not active yet.
    else
        return false; --Input is active.
    end
end --Input2IsFalse

while Input2IsFalse() do
mc.mcCntlSetLastError(inst, "Output1 is turing on.");
end  --waits until input2 is true


mc.mcCntlSetLastError(inst, "Output 1 ON");

end --This is for the end of the function m1002()

--This next part does not have to be in here
--it is for testing and steping through macros
--it runs the macro in Mach 4 fine without it.
if (mc.mcInEditor() == 1) then
   m1002()
end
Title: Re: Having trouble with Lua Script
Post by: shandseimela@gmail.com on October 09, 2019, 02:27:33 AM
HI
May you please reply yo the topic above to help the readers to learn also
Title: Re: Having trouble with Lua Script
Post by: Bill_O on October 09, 2019, 09:50:47 AM
Cbyrdtopper,

Try this.
Hope it helps.
https://www.machsupport.com/forum/index.php?topic=39763.msg266718#msg266718


Bill