Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: jderou on December 02, 2017, 06:44:55 PM

Title: How to make Lua wait for signal state to change
Post 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
Title: Re: How to make Lua wait for signal state to change
Post by: joeaverage on December 02, 2017, 08:02:25 PM
Hi,
I'm not sure that this will prove directly useful but I helped another bloke with a similar sort of situation with it:

Code: [Select]
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
Title: Re: How to make Lua wait for signal state to change
Post by: joeaverage on December 02, 2017, 08:45:29 PM
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
Title: Re: How to make Lua wait for signal state to change
Post by: jderou on December 02, 2017, 09:32:07 PM
Great!  Thank you!
Title: Re: How to make Lua wait for signal state to change
Post by: jderou on December 07, 2017, 09:39:36 PM
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.
Title: Re: How to make Lua wait for signal state to change
Post by: joeaverage on December 07, 2017, 10:23:42 PM
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
Title: Re: How to make Lua wait for signal state to change
Post by: joeaverage on December 08, 2017, 12:32:52 AM
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
Title: Re: How to make Lua wait for signal state to change
Post by: jderou on December 08, 2017, 01:24:30 PM
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

Code: [Select]
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
Title: Re: How to make Lua wait for signal state to change
Post by: joeaverage on December 08, 2017, 01:33:40 PM
Hi,
I've never tried it so I don't know but its my understanding that what you've written will work.

Craig
Title: Re: How to make Lua wait for signal state to change
Post by: Chaoticone on December 08, 2017, 04:06:23 PM
Not sure this will work........

hsig, rc=mc.mcSignalGetHandle(inst, loosendraw)

loosendraw is not legit signal name
Title: Re: How to make Lua wait for signal state to change
Post by: joeaverage on December 08, 2017, 04:23:52 PM
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
Title: Re: How to make Lua wait for signal state to change
Post by: Chaoticone on December 08, 2017, 04:46:01 PM
 ;D
Title: Re: How to make Lua wait for signal state to change
Post by: jderou on December 09, 2017, 08:30:25 AM
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.
Title: Re: How to make Lua wait for signal state to change
Post by: Chaoticone on December 09, 2017, 11:21:52 AM
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.