Hello Guest it is March 28, 2024, 09:29:16 AM

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

0 Members and 1 Guest are viewing this topic.

Re: Having trouble with Lua Script
« Reply #10 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. 

Re: Having trouble with Lua Script
« Reply #11 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();
« Last Edit: April 25, 2015, 11:24:37 AM by Gerber Baby »
Re: Having trouble with Lua Script
« Reply #12 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
Chad Byrd
Re: Having trouble with Lua Script
« Reply #13 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?
Chad Byrd
Re: Having trouble with Lua Script
« Reply #14 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
Chad Byrd
Re: Having trouble with Lua Script
« Reply #15 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
Chad Byrd
Re: Having trouble with Lua Script
« Reply #16 on: October 09, 2019, 02:27:33 AM »
HI
May you please reply yo the topic above to help the readers to learn also

Offline Bill_O

*
  •  562 562
    • View Profile
Re: Having trouble with Lua Script
« Reply #17 on: October 09, 2019, 09:50:47 AM »