Hello Guest it is March 28, 2024, 09:09:14 AM

Author Topic: How to make Lua wait for signal state to change  (Read 3291 times)

0 Members and 1 Guest are viewing this topic.

How to make Lua wait for signal state to change
« 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
Re: How to make Lua wait for signal state to change
« Reply #1 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: How to make Lua wait for signal state to change
« Reply #2 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

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: How to make Lua wait for signal state to change
« Reply #3 on: December 02, 2017, 09:32:07 PM »
Great!  Thank you!
Re: How to make Lua wait for signal state to change
« Reply #4 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.
Re: How to make Lua wait for signal state to change
« Reply #5 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: How to make Lua wait for signal state to change
« Reply #6 on: December 08, 2017, 12:32:52 AM »
Hi,
have a look at function calls in:
https://www.lua.org/manual/5.3/

Return is the correct statement to explicitly return from a Lua function.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: How to make Lua wait for signal state to change
« Reply #7 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
Re: How to make Lua wait for signal state to change
« Reply #8 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: How to make Lua wait for signal state to change
« Reply #9 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
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!