Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: jderou on December 02, 2017, 06:44:55 PM
-
I am working on an automatic toolchange and I need the Lua code within the macro to wait for the spindle orient signal from the PLC to change from 0 to 1. I would have used a while wend in mach3, what is the best way to do this with Lua?
My input signal is Input 18.
I found the below example on the web but I am not quite sure how to use it in mach4. I don't understand what the a in a is.
local i = 1
while a do
if a == v then break end
i = i + 1
end
-
Hi,
I'm not sure that this will prove directly useful but I helped another bloke with a similar sort of situation with it:
LUA Syntax:
rc = mc.mcSignalWait(
number mInst,
number sigId,
number waitMode,
number timeoutSecs);
I'll try to find the original post but there is a couple of things that are worth highlighting:
1) You don't need the signal HANDLE, just its ID, in your case mc.ISIG_INPUT18
2) While there are nearly two hundred input signals defined in Mach4 only the first 64 signals work with this API...mc.ISIG_INPUT0 thru mc.ISIG_INPUT63
3) There are a number of return codes, it is very easy to miss the fact that under a particular circumstance an unexpected return code can be generated
and your code seems to fail for no reason. I suffered quite a few failures of this type while developing code which worked correctly. It is worthwhile
to code a 'trap and message' for those codes that while you don't expect them may well show up from time to time. It may well be that your completed code
is robust without such a precaution, but including such traps during development can save some hair pulling!
Craig
-
Hi,
rather longwinded post, its starts to make progress about page 3:
http://www.machsupport.com/forum/index.php/topic,35694.0.html (http://www.machsupport.com/forum/index.php/topic,35694.0.html)
Craig
-
Great! Thank you!
-
Ok, so that is working. What is the safest way to exit a macro on error? For instance, if the signalwait times out then I don't want to continue the macro, but I need to turn a couple outputs off. So I have:
if (rc==mc.MERROR_TIMED_OUT) then
hsig, rc=mc.mcSignalGetHandle(inst, loosendraw)
mc.mcSignalSetState(hsig, 0)
mc.mcCntlSetLastError(inst, "TIMED OUT")
end
This works, but does it stop the macro at this point? Does it exit the macro? I definitely would not want the next line of code to be run.
-
Hi,
not sure just yet.
You can use BREAK to exit a WHILE loop for instance.
I think the equivalent for breaking out of a Lua function is RETURN.
Will do a little research.
Craig
-
Hi,
have a look at function calls in:
https://www.lua.org/manual/5.3/ (https://www.lua.org/manual/5.3/)
Return is the correct statement to explicitly return from a Lua function.
Craig
-
I wish I could say I understand the terminology in that manual. I had to google syntactic sugar lol.
I will test out the below unless you can tell me I am making a mistake.
Thanks!
Joe
if (rc==mc.MERROR_TIMED_OUT) then
hsig, rc=mc.mcSignalGetHandle(inst, loosendraw)
mc.mcSignalSetState(hsig, 0)
mc.mcCntlSetLastError(inst, "TIMED OUT")
mc.mcCntlFeedHold(inst)
mc.mcCntlCycleStop(inst)
do return end
end
-
Hi,
I've never tried it so I don't know but its my understanding that what you've written will work.
Craig
-
Not sure this will work........
hsig, rc=mc.mcSignalGetHandle(inst, loosendraw)
loosendraw is not legit signal name
-
Hi,
signal 'loosendraw', missed that one!
I thought I saw it in a post...the same one that had:
mc.mcSignalGetHandle(inst,'GetMeACupOfCoffe')
mc.mcSignalGetHandle(inst,'AndPeelMeAGrape')
LOL.
Craig
-
;D
-
tightendraw is defined earlier as mc.OSIG_OUTPUT4.
Anyhow, it works. Stops the program and exits the macro as expected.
Will post the complete program once it is cleaned up.
-
OK, Good. I thought you may have defined it elsewhere. That's why I said not sure. I knew if you hadn't defined it elsewhere it would not work. I also knew there was a chance you had. Anyway, I felt like I needed to say something in case you did not understand that or others looked at it as an example.