Hello Guest it is April 19, 2024, 04:19:11 AM

Author Topic: Mach4 lua wait for input then turn off output  (Read 1072 times)

0 Members and 1 Guest are viewing this topic.

Mach4 lua wait for input then turn off output
« on: April 01, 2020, 01:56:09 PM »
FADAL 4020 tool changer carousel. I need help with MACH4 lua code.
How to write:

TURN ON OUTPUT 5 and when INPUT 8 is triggered turn OFF OUTPUT 5  -->Extend carriage turn of motor
Continue with the tool change cycle ….. (release tool raise head etc....)

TURN ON OUTPUT4 and when INPUT6 is triggered turn OFF OUTPUT4  -->Retract carriage turn off motor
Continue with the tool change cycle …..

I need help with the lua code to WAIT for the carriage input signals to close before going to the next line.

Thank you in advance.

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: Mach4 lua wait for input then turn off output
« Reply #1 on: April 01, 2020, 08:28:42 PM »
mc.mcSignalWait()  Look through the API manual in the Doc folder.  The section on Signals. 

Steve
Re: Mach4 lua wait for input then turn off output
« Reply #2 on: April 02, 2020, 12:21:18 AM »
Hi Steve
I am having trouble with what to put between the () in mc.mcSignalWait(). I attached the lua code I created. It works great until carousel out and carousel in. I need the motor to stop when it hits the out or in switch.
My confusion is at carousel out:Lines 18 to 25 and carousel in: Lines 52 to 59. Please let me know if you can help type something for me. I have been staring at this so long and cannot see what I'm missing.

Thanks

Don

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: Mach4 lua wait for input then turn off output
« Reply #3 on: April 02, 2020, 02:04:13 AM »
Notice the indentation in the code below.  It REALLY helps the readability of the code and it keeps you from starring so long.  :)
Also, although it is not required for a single expression, I like to wrap any expression in an if statement in parenthesis.

In a macro like M6, use mc.mcCntlGcodeExecuteWait() instead of the non wait function.  You don't want to ever use mc.mcCntlGcodeExecuteWait() in a screen or button script but here in a macro script, it is what you want. 

And remember (look at your line 22 "WAIT_MODE_Low"), all of the API functions and constants should be prefaced with mc.  And case is important.  It should be "mc.WAIT_MODE_LOW" instead of "WAIT_MODE_Low".

Line 3 improvement:
Code: [Select]
local inst = mc.mcGetInstance('M6 macro') -- This will allow logging the API calls with caller information.

Line 7 to 11 change.  You don't have use the the interpreter to turn the spindle off.  Use the API calls when you can in script!
If you have setup your ramp times in the spindle settings, they will be followed with the mc.mcSpindleSetDirectionWait().
Code: [Select]
locateSpindle, rc = mc.mcSignalGetHandle (inst, mc.OSIG_OUTPUT6) --Turn ON spindle lock.
rc = mc.mcSignalSetState(locateSpindle, 1) --Spindle lock ON
currentRPM, rc = mc.mcSpindleGetCommandRPM(inst) -- save the current RPM to restore later.
rc = mc.mcSpindleSetCommandRPM(inst, 15) -- use instead of execute M3 S15
--The possible values to use with the mc.mcSpindleSetDirectionWait() below
--mc.MC_SPINDLE_OFF
--mc.MC_SPINDLE_FWD
--mc.MC_SPINDLE_REV
rc = mc.mcSpindleSetDirectionWait(inst, mc.MC_SPINDLE_FWD)
-- Is there no input that tells you wen the keylock has engaged? 
wx.wxSleep (5) --Wait 5 seconds
rc = mc.mcSpindleSetDirectionWait(inst, mc.MC_SPINDLE_OFF) --Turn OFF spindle.

18 to 25 became:
Code: [Select]
-- Compound statements like your line 21 do not work well with LUA functions that return multiple items.  :( 
-- Break it up like so.  It makes the code easier to read and maintain in the future as well.
local hSig, state
hSig, rc = mc.mcSignalGetHandle (inst, mc.ISIG_OUTPUT5)
state, rc = mc.mcSignalGetState(hSig, mc.ISIG_OUTPUT5)
if (state == 1) then
rc = mcSignalWait(inst, mc.ISIG_INPUT5, mc.WAIT_MODE_LOW, 30) -- Wait on input 5 to go HIGH for at most 30 seconds.
-- Always put a timeout in you want to be able to catch errors instead of waiting indefinitely. 
-- If you want to wait on the input to go high, use mc.WAIT_MODE_HIGH.  Case is important!!! 
-- you really want to check the return code from EVERY API call for success/failure. 
-- Below will abort the script gracefully if there is an error.
if (rc ~= mc.MERROR_NOERROR) then
mc.mcCntlAlarm(inst, 100, "Timed out waiting for input 5 to go high.")
return(100) -- 100 here and above is just some number I dreamed up.  you could plan your own error number scheme.
end
mc.mcSignalSetState(CarouselOut, 0)
end

52 to 59 became:

Code: [Select]
--PNP input 4 switch activated to turn OFF OUTPUT4
--To turn output4 off:
hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT4);
state = mc.mcSignalGetState(hSig);
if (state == 1) then
mc.mcSignalSetState(CarouselRetract, 0)
else -- not 0
wx.wxMessageBox ("INPUT 4 not triggered OFF")
end
Re: Mach4 lua wait for input then turn off output
« Reply #4 on: April 02, 2020, 01:22:41 PM »

Steve
Thank you for the clear explanation. Mach4 is fun to customize when you know what your doing. I would like this to be a tool change BUTTON. I do not need to run this in the MDI or GCode. Another problem: For some reason when I change to M6 code in Edit/Debug Scripts or threw a button it keeps running old code. It wont update. I restart Mach4 and my computer but it hangs on to pervious scripts. I cannot try your update because it keeps running the last script.

This is my first time participating on this forum and your help is appreciated.


Don

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: Mach4 lua wait for input then turn off output
« Reply #5 on: April 02, 2020, 01:49:37 PM »
It keeps the old scripts because there is a syntax error somewhere in your new script.  In the Zero Brane editor, Choose the menu path Project -> Compile.  It will tell you where the problem is. 

Make it a M6 macro.  Then you can call it from a button with mcCntlGcodeExecute(inst, "M6")  Also, when the code is in a macro, it is easier to debug.  I'm not saying that this is the best for ALL situations.  But for this challenge, making it a macro is the best way forward.

Steve
Re: Mach4 lua wait for input then turn off output
« Reply #6 on: April 03, 2020, 08:26:46 PM »
Thanks so much Steve.

With a lot of your help I was able to get my tool change macro to work. I would not have been able to do this without your time and effort. This project is for my 1988 FADAL 4020 using a HiCON Integra PN 7766 controller.
I attached the final m6 lua code so that it might help other people on the forum.

Don