Author Topic: LUA, repeat until  (Read 200 times)

0 Members and 1 Guest are viewing this topic.

Offline sx3

  • Active Member
  • Posts: 31
    • View Profile
LUA, repeat until
« on: January 11, 2019, 04:48:47 PM »
Hi,

working on the next script and need some input from you guys.
Basically I have physical button for unclamping a tool collet. The Collet motor is a AC drive motor and there is a limit switch present.
I want to upon button press, drive the motor until the limit switch. I'm thinking of using the "repeat" and "until" strings, but I'm not sure if I'm doing it right or wrong.

Opinions?


This is the particular code snippet
Code: [Select]
hSigColletOpen = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1) --Assign the input for limit switch, collet open
ColletsState = mc.mcSignalGetState(hSigColletOpen)
if ColletsState == 1 then
wx.wxMessageBox('Collet is already unclamped!')
else
local UnclampOutput = mc.OSIG_OUTPUT6 --Set the output to control the Unclamp motor
repeat
hSigState = mc.mcSignalGetHandle(inst, UnclampOutput)
mc.mcSignalSetState(hSigState, true) --Start the unclamp motor
until ColletState == 1
mc.mcSignalSetState(hSigState, false) --Stop the unclamp motor
wx.wxMessageBox('Collet have now been unclamped!')
end

And this is the total code I've come up with for now.

Code: [Select]
-- This code is for unclamping the collet
[mc.ISIG_INPUT1] = function (state) --Assign the input for UNCLAMP button
if (state == 1) then
hSig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_RUNNING_GCODE)
if RC == 0 then
GC_State = mc.mcSignalGetState(hSig)
if GC_State == 1 then --Gcode is running, dont let the collet to be unclamped
wx.wxMessageBox('GCode is running')
else
hSigColletOpen = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1) --Assign the input for limit switch, collet open
ColletsState = mc.mcSignalGetState(hSigColletOpen)
if ColletsState == 1 then
wx.wxMessageBox('Collet is already unclamped!')
else
local UnclampOutput = mc.OSIG_OUTPUT6 --Set the output to control the Unclamp motor
repeat
hSigState = mc.mcSignalGetHandle(inst, UnclampOutput)
mc.mcSignalSetState(hSigState, true) --Start the unclamp motor
until ColletState == 1
mc.mcSignalSetState(hSigState, false) --Stop the unclamp motor
wx.wxMessageBox('Collet have now been unclamped!')
end
end
end
end
end,

Offline Cbyrdtopper

  • Active Member
  • Posts: 615
    • View Profile
    • Byrd Tool Corp.
Re: LUA, repeat until
« Reply #1 on: January 11, 2019, 05:31:31 PM »
You can use this:  mc.mcSignalWait

Look it up in the API.  I use this for a few things at the shop, a few of which are tool changers.

rc = mc.mcSignalWait(inst, mc.ISIG_INPUT1, 1, 0)

the call will look for the specified signal to go High or Low.  The "1" that is after the ISIG_INPUT1 means this line is waiting for the signal to go High, a "0" will mean that the call will wait for the signal to go low.

The "0" on the end is the timeout.  You can set how long you want it to wait before moving on in the script.  0 makes it wait indefinitely.
if you want to make sure it gets finished in a certain amount of time, then you can use the rc, one of them is for the timeout.  If it doesn't finish in the alotted time you specify then you can finish the script:
if rc == MERROR_TIMED_OUT then turn off your ac motor, error message, end macro.
Chad Byrd

Offline Cbyrdtopper

  • Active Member
  • Posts: 615
    • View Profile
    • Byrd Tool Corp.
Re: LUA, repeat until
« Reply #2 on: January 11, 2019, 05:41:41 PM »
Here is the code I'm using on a lathe at work.
This bit of code is the mc.mcSignalWait. 
I have it waiting 20 seconds, that's about 4 times as long as it takes to change a tool.  If it doesn't finish in that time, I want to shut the motor off on the tool changer so it doesn't burn up.  So, if rc does not equal 0 (which is no error) then message, error, stop my functions.   

--Wait for the signal from the CLick.         
         mc.mcCntlSetLastError(inst, "Wait for Click to complete tool change.")
         rc = mc.mcSignalWait(inst, mc.ISIG_INPUT0, mc.WAIT_MODE_HIGH, 20)--Time in seconds the Click has to complete the tool change.
         --Error check.  If the Click took too long.  Alarm out Mach4.
         if (rc~= 0) then
             wx.wxMessageBox("The Click did not complete the tool change in time.  Check for malfunction.")
             local Alarm = mc.mcSignalGetHandle(inst,mc.OSIG_ALARM)
             mc.mcSignalSetState(Alarm,1)
             mc.mcSignalSetState(TurretMotor, 0)
             CycleStop()
         else
             mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(Requested))
             mc.mcToolSetCurrent(inst, Requested)
             mc.mcCntlSetLastError(inst, "Tool Change Complete.")
         end


NOTE:  This is just the middle of the code, I have more code above that gets the instance, sets output handles, etc... and more at the end to complete the tool change.
« Last Edit: January 11, 2019, 05:43:19 PM by Cbyrdtopper »
Chad Byrd

Offline sx3

  • Active Member
  • Posts: 31
    • View Profile
Re: LUA, repeat until
« Reply #3 on: February 04, 2019, 03:47:28 PM »
I've been using the mcSignalWait for some parts, thanks! :)
However, I wonder if I can use it to wait for a mcJogIncStart to finnish, before moving to the next part of the script?
As for now, the mcJogIncStart starts to jog the axis, but the script don't wait for it to complete before continuing with the script.
I'm looking for something similar as mcCntlGcodeExecuteWait, but for jogging, not Gcode.

Reading the API for mcSignalWait it says: sigId: A valid signal ID. (NOT a signal handle).
I'm not sure if mcAxisIsStill is considered to be a signal handle?

Code: [Select]
MotorIsStill = mc.mcAxisIsStill(mInst, 8)
mc.mcSignalWait(mInst, MotorIsStill, 1, 5)

Offline Cbyrdtopper

  • Active Member
  • Posts: 615
    • View Profile
    • Byrd Tool Corp.
Re: LUA, repeat until
« Reply #4 on: February 04, 2019, 06:08:00 PM »
Just curious, why wouldn't mcCntlGcodeExecuteWait work for you?
Chad Byrd

Offline sx3

  • Active Member
  • Posts: 31
    • View Profile
Re: LUA, repeat until
« Reply #5 on: February 05, 2019, 01:48:19 AM »
It's because of them out-of-band axises that can't be commanded with Gcode. Whish they could! :)
I can of course put in a MilliSleep(xx), but since the jog distance if different each time, I would need to calculate the number of seconds based on the distance.

That's one of solving it, but I thought there might be a more propriate way to get the same function?

Offline Chaoticone

  • Administrator
  • *
  • Posts: 5,433
  • Precision Chaos
    • View Profile
Re: LUA, repeat until
« Reply #6 on: February 05, 2019, 08:15:44 AM »
Once you send the jog command loop until you get motion. Right after that loop until motion stops. In each of the loops use a var to increment during the loop and set it to something reasonable so that you can exit the loops if the counter reaches some value.

Code: [Select]
local atcAxis = mc.AXIS6
local loopCount = 0
local loops = 200000

repeat --Until we start moving or time out
loopCount = (loopCount +1)
local ob1info = mc.mcAxisGetInfoStruct(inst, atcAxis)
local OB1Jogging = ob1info.Jogging
until --Jogging starts or we time out
(OB1Jogging == 1) or (loopCount >= loops)

if (loopCount >= loops) then
m6ErrorOut("Carousel took too long to start moving")
return
end

loopCount = 0

repeat --Until we stop moving or time out
loopCount = (loopCount +1)
local ob1info = mc.mcAxisGetInfoStruct(inst, atcAxis)
local OB1Jogging = ob1info.Jogging
until --Jogging stops or we time out
(OB1Jogging == 0) or (loopCount >= loops)

if (loopCount >= loops) then
m6ErrorOut("Carousel took too long to get into position")
return
end
« Last Edit: February 05, 2019, 08:20:35 AM by Chaoticone »
;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!

Offline Cbyrdtopper

  • Active Member
  • Posts: 615
    • View Profile
    • Byrd Tool Corp.
Re: LUA, repeat until
« Reply #7 on: February 05, 2019, 08:18:59 AM »
If you need it to wait for the axis to get to position before continuing with your script, you may as well turn that motor into A, B, or C axis.   The point of the OB axis is to be moved independently of the normal axis. 

If you do, however, use the OB axis outside of synced movement, then I'm not sure what the solution would be.   Maybe check the OB position requested position with the current position and use a wx. Millisleep until it reaches that point.   
while CurrentPosition~= RequestedPosition do
Wx. Millislepp (100)
end

I wonder if that would work?
Chad Byrd

Offline smurph

  • Administrator
  • *
  • Posts: 1,053
  • "That there... that's an RV."
    • View Profile
Re: LUA, repeat until
« Reply #8 on: February 07, 2019, 08:54:58 PM »
The reason the OOB axes can't be controlled by G code is because there are no letters left in the G code language left to control them!!!  Think about it.  :)  ABCDEFGHIJKLMNOPQRSTUVWXYZ are all taken. 

UVW are the linear variants of ABC, if you were thinking that they could be used...

Also, mcJogIsJogging() may be interesting for this discussion. 

Steve