Hello Guest it is March 28, 2024, 05:18:24 PM

Author Topic: Having trouble with Lua Script  (Read 6953 times)

0 Members and 1 Guest are viewing this topic.

Having trouble with Lua Script
« 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")
Chad Byrd

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Having trouble with Lua Script
« Reply #1 on: April 21, 2015, 07:49:30 PM »
do a search through the M4 posts it in there
Re: Having trouble with Lua Script
« Reply #2 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
Re: Having trouble with Lua Script
« Reply #3 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



Chad Byrd
Re: Having trouble with Lua Script
« Reply #4 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,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");

Re: Having trouble with Lua Script
« Reply #5 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?
Chad Byrd
Re: Having trouble with Lua Script
« Reply #6 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");
Re: Having trouble with Lua Script
« Reply #7 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
Re: Having trouble with Lua Script
« Reply #8 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
Chad Byrd
Re: Having trouble with Lua Script
« Reply #9 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
Chad Byrd