Hello Guest it is March 28, 2024, 02:52:49 PM

Author Topic: Lua While Loops  (Read 4446 times)

0 Members and 1 Guest are viewing this topic.

Lua While Loops
« on: October 21, 2015, 08:41:37 AM »
Here is some code that I have to move my tool carousel forward with a gear motor.  It moves forward until it hits a switch and then will turn off.  Likewise on the return.
I have no ability to test this macro in real time right now.  But could someone let me know if I have all of my syntax correct?  Thanks!!
-Chad

function m1003()

------
--Tool Carousel Forward
------
local Carousel = mc.OSIG_OUTPUT1 -- Carousel output, turning it on.
local hsig = mc.mcSignalGetHandlem (inst,Carousel)
mc.mcSignalSetState(hsig,1)

------
--Check for switch
------
local Input1 = 0; --Tool carousel is forward.
local hSig = 0;

hSig = mc.mcSignalGetHandle(inst,mc.ISIG_INPUT1);
Input1 = mc.mcSignalGetState(hSig);

while (Input1==0) do
     mc.mcCntlSetLastError(inst, "Carousel Moving Forward");
end

-----
--Turn off motor
-----
local Carousel = mc.OSIG_OUTPUT1 -- Carousel output, turning it off.
local hsig = mc.mcSignalGetHandlem (inst,Carousel)
mc.mcSignalSetState(hsig,0)

end

if (mc.mcInEditor() == 1) then
   m1003()
end
Chad Byrd

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Lua While Loops
« Reply #1 on: October 21, 2015, 09:27:11 AM »
The logic behind the code is correct, but with the loop you will receive thousands of Carousel Moving Forward into the log.

However with forward thinking from the mach team there is a command call mc.mcSignalWait so you can change your loop too mc.mcSignalWait(inst, mc.ISIG_INPUT1, mc.WAIT_MODE_HIGH, 0) or mc.mcSignalWait(inst, mc.ISIG_INPUT1, mc.WAIT_MODE_LOW, 0) - depends which way you need to go. The 0 on the end denotes wait indefinate, if you change this to 10 it will wait for the signal for 10 seconds then continue.

Apart from that all looks good, heres my version tested

Code: [Select]
local inst = mc.mcGetInstance()
local Carousel =  mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_OUTPUT1),1)
mc.mcCntlSetLastError(inst, "Carousel Moving Forward")
mc.mcSignalWait(inst, mc.ISIG_INPUT1, mc.WAIT_MODE_HIGH, 10)
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_OUTPUT1),0)

as you will notice you can join commands into 1 liners.

DazTheGas
« Last Edit: October 21, 2015, 09:29:25 AM by DazTheGas »
New For 2022 - Instagram: dazthegas
Re: Lua While Loops
« Reply #2 on: October 21, 2015, 09:49:56 AM »
Thanks DazTheGas!!
Thanks for taking the time to help out!! I really appreciate it!  I didn't realize you could join commands on 1 line either.  This command will help out a lot with Air Cylinders also I'm thinking.

on this line... "local Carousel =  mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_OUTPUT1),1)"  You have combined SetState with GetHandle just by using "( )"?  Is this correct?
Chad Byrd
Re: Lua While Loops
« Reply #3 on: October 21, 2015, 09:57:58 AM »
mc.mcSignalGetHandlem not sure why the "m" on the end and you used it twice

forgot to set up your inst
you don't need to keep declaring a variable

but here is another way that might work that follows your basic logic.
Re: Lua While Loops
« Reply #4 on: October 21, 2015, 10:13:36 AM »
Thank you Ya-Nvr-No!
I see what you mean by declaring variables multiple times, Didn't even realize I was doing that, I just copied and pasted top to bottom. 
When I get A signal handle I can name it whatever I want?  I do not have to use hsig, It is just a variable at that point, I noticed you used "hsigCar", just to show you that it is the handle on the carousel output.  Is that correct?
Chad Byrd

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Lua While Loops
« Reply #5 on: October 21, 2015, 10:16:06 AM »
on this line... "local Carousel =  mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_OUTPUT1),1)"  You have combined SetState with GetHandle just by using "( )"?  Is this correct?

yes thats correct if you dont need the rc from the function then this works just fine, just does all 3 things in one go.

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Lua While Loops
« Reply #6 on: October 21, 2015, 10:20:41 AM »
Yep, cause there all just numbers in the computer.

But it will make it easier to know what the variables represent, when the scripts start getting longer.
Re: Lua While Loops
« Reply #7 on: October 21, 2015, 10:35:16 AM »
Ya-Nvr-No
Good deal, so calling the declared output "hsigCar" means that later when you want to turn it on or off you just need to mc.mcSignalSetSate(hsigCar, 0 or 1). 

DazTheGas
I still don't know much about the RC stuff, I learned a little bit about it at the CNC workshop but just what it is not how to use it.  I don't know when you use it or what to use it for.
Chad Byrd