Hello Guest it is May 06, 2021, 06:23:06 PM

Author Topic: Multiple inputs in LUA script  (Read 7945 times)

0 Members and 1 Guest are viewing this topic.

Offline Bob49

*
  •  57 57
    • View Profile
Multiple inputs in LUA script
« on: October 10, 2016, 06:40:51 PM »
Hello,

Been messing with Mach4 and trying to get my console switches to all work using LUA scripts.  These all operate through my modbus devise.  And that portion of things work fine, or appears to.  Let me be very clear here, I know pretty much zero about LUA.  But by using examples and lots of trial and error, I have 15 of my switches working now after a few weeks of effort.  One switch is kicking my butt and the other 4 are completely over my head.  The 4 in question are rotary switches that use various pin combinations for each position. 

I stumbled in to poppabear's script somewhere that seems to deal with multi inputs for a function.  It shows 2 inputs, but for two of my four switches, I have 3 pins each.  So me, being simple minded, figured if two were good, then adding a third should be no problem.  Here's the original I started from:
Code: [Select]
local Input14 = 0;
local Input15 = 0;
local Input17 = 0;
local hSig = 0;

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

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT15);
Input10 = mc.mcSignalGetState(hSig);

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT15);
Input10 = mc.mcSignalGetState(hSig);

if ((Input14 == 1) and (Input15 ==1) and (Input17 == 1)) then
    local inst = mc.mcGetInstance();
    mc.mcCntlSetRRO(inst, 100.0);
end

All the pins in the modbus reflect the values as I have them stated, but I get no response in Mach.  And the script debugs with no errors.  I'm guessing here that I'm missing a lot of statements to make this work.  This is just for one of the switch positions, but i figured that if I got one, I may be able to get the rest.  And this is really the easiest of the 4 rotary switches.  The others get more complex, one for jog control type and increment value, one for FRO and one for SRO. 

The one switch that has what seems like good script is the cycle start function.  Seems to me the scripts I'm using for Enable, single block and the like should work for cycle start.  But I get nothing when trying to operate the switch.  The others with that same scripting work fine.  I used 1 as the variable, did I guess wrong there?

Any thoughts on how to get over this hump?  I'd appreciate the help.

Thanks
Bob

Offline Bob49

*
  •  57 57
    • View Profile
Re: Multiple inputs in LUA script
« Reply #1 on: October 10, 2016, 08:47:02 PM »
I guess I need to back up on my stated problem with the cycle start function.  I had been testing it with the MDI.  And it doesn't start the g-code in the MDI.  But it will do a cycle start on a g-code program and restart either movement of that g-code or movement initiated via g-code in the MDI.  So I'm thinking this is designed in and different than how Mach3 worked.  Further guessing on my part that I'd need a different button to activate input I make in the MDI.  But I don't see a function for doing that.

Thanks
Bob

Offline Bob49

*
  •  57 57
    • View Profile
Re: Multiple inputs in LUA script
« Reply #2 on: October 11, 2016, 01:30:02 PM »
I realized earlier today that the script I inserted was the first capture and not completely correct as far as the inputs.  Here's the corrected script I've been testing.  I've tested the individual pins changing state with the Set RRO statement and it works fine.  And I've tried the below script with just two inputs and can't get it to work, so i'm thinking it's not the three inputs that's giving me a problem, I think there's something wrong in the wording of the script.

Code: [Select]
local Input14 = 0;
local Input15 = 0;
local Input17 = 0;
local hSig = 0;

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT14);
Input14 = mc.mcSignalGetState(hSig);

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT15);
Input15 = mc.mcSignalGetState(hSig);

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT17);
Input17 = mc.mcSignalGetState(hSig);

if ((Input14 == 1) and (Input15 ==1) and (Input17 == 1)) then
    local inst = mc.mcGetInstance();
    mc.mcCntlSetRRO(inst, 100.0);
end

Bob

Offline DazTheGas

*
  •  753 753
  • DazTheGas
    • View Profile
Re: Multiple inputs in LUA script
« Reply #3 on: October 12, 2016, 02:11:37 AM »
Well ive run the script on my machine and the code works fine, try a bit of debug info in the the script to see what part is not working on your screen.

DazTheGas
Theres always time for a choccy biccy

Offline Chaoticone

*
  • *
  •  5,626 5,626
  • Precision Chaos
    • View Profile
Re: Multiple inputs in LUA script
« Reply #4 on: October 12, 2016, 08:07:38 AM »
Where are you defining the inst = mc.mcGetInstance(); before getting the hSig's?
;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 Bob49

*
  •  57 57
    • View Profile
Re: Multiple inputs in LUA script
« Reply #5 on: October 12, 2016, 12:00:05 PM »
Well ive run the script on my machine and the code works fine, try a bit of debug info in the the script to see what part is not working on your screen.

DazTheGas

DazTheGas,

So it ran fine and actually caused the function to operate?  Wow!  On my mill, it just gets ignored it seems.

Debug info huh?  I've been trying to absorb some of thats stuff, but I'll admit LUA makes my mind go numb.  But I've mainly been focused on what makes up a correct working script.

Where are you defining the inst = mc.mcGetInstance(); before getting the hSig's?

I've been thinking some assignment wasn't getting made, I've tried various things.  But not that.  I struggle to follow any logic in some of these scripts.
But I gave it a try this morning adding it between the SignalGetHandle and SignalGetState statements for each of the three inputs.  It debugged fine, but had no bearing on the operation of the switch itself.  Still no responce  from the switch after restarting Mach.

If I take this RRO portion of the whole screen load script into a new McLUA window and do a debug on it, I get this error:
At Breakpoint line: 7 file: C:\Mach4Hobby\LuaExamples\RRO test script.mcs
mcLua ERROR: Lua: Error while running chunk
[string "C:\Mach4Hobby\LuaExamples\RRO test script.mcs..."]:7: wxLua: Expected a 'number' for parameter 1, but got a 'nil'.
Function called: 'mcSignalGetHandle(nil, number)'
01. mcSignalGetHandle(number, number, lightuserdata)
stack traceback:
   [C]: in function 'mcSignalGetHandle'
   [string "C:\Mach4Hobby\LuaExamples\RRO test script.mcs..."]:7: in main chunk


mcLua ERROR: Lua: Error while running chunk

Debug session finished.

That whole portion of only the RRO script I tested read as below, and yielded that noted error.  But if it's in the complete Screen Load Script, I get no errors.
Code: [Select]
--Set Rapid Over Ride Value, 0%, 25%, 50% and 100%
local Input18 = 0                                                           --Clear local Input18
local Input19 = 0                                                           --Clear local Input19
local Input20 = 0                                                           --Clear local Input20
local hSig = 0                                                              --Clear local hSig

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT18)
inst = mc.mcGetInstance()
Input18 = mc.mcSignalGetState(hSig) --Get state of input 18

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT19)
inst = mc.mcGetInstance()
Input19 = mc.mcSignalGetState(hSig) --Get state of input 19

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT20)
inst = mc.mcGetInstance()
Input20 = mc.mcSignalGetState(hSig) --Get state of input 20

if ((Input18 == 0) and (Input19 == 1) and (Input20 == 0)) then
    local inst = mc.mcGetInstance()
    mc.mcCntlSetRRO(inst, 100.0) --Set RRO to 100%
end
if ((Input18 == 1) and (Input19 == 0) and (Input20 == 1)) then
    local inst = mc.mcGetInstance()
    mc.mcCntlSetRRO(inst, 50.0) --Set RRO to 50%
end
if ((Input18 == 0) and (Input19 == 0) and (Input20 == 1)) then
    local inst = mc.mcGetInstance()
    mc.mcCntlSetRRO(inst, 25.0) --Set RRO to 25%
end
if ((Input18 == 1) and (Input19 == 0) and (Input20 == 0)) then
    local inst = mc.mcGetInstance()
    mc.mcCntlSetRRO(inst, 0.0) --Set RRO to 0%
end

Bob

Offline DazTheGas

*
  •  753 753
  • DazTheGas
    • View Profile
Re: Multiple inputs in LUA script
« Reply #6 on: October 12, 2016, 12:22:09 PM »
Quote
Where are you defining the inst = mc.mcGetInstance(); before getting the hSig's?

OOOppps - I tested the code in my personal editor and forgot the instance is declared automatically so I dont get errors..

try something on these lines

Code: [Select]
--Set Rapid Over Ride Value, 0%, 25%, 50% and 100%
local Input18 = 0                                                           --Clear local Input18
local Input19 = 0                                                           --Clear local Input19
local Input20 = 0                                                           --Clear local Input20
local hSig = 0                                                              --Clear local hSig

local inst = mc.mcGetInstance()

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT18)
Input18 = mc.mcSignalGetState(hSig) --Get state of input 18

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT19)
Input19 = mc.mcSignalGetState(hSig) --Get state of input 19

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT20)
Input20 = mc.mcSignalGetState(hSig) --Get state of input 20

if ((Input18 == 0) and (Input19 == 1) and (Input20 == 0)) then
    mc.mcCntlSetRRO(inst, 100.0) --Set RRO to 100%
elseif ((Input18 == 1) and (Input19 == 0) and (Input20 == 1)) then
     mc.mcCntlSetRRO(inst, 50.0) --Set RRO to 50%
elseif ((Input18 == 0) and (Input19 == 0) and (Input20 == 1)) then
     mc.mcCntlSetRRO(inst, 25.0) --Set RRO to 25%
elseif ((Input18 == 1) and (Input19 == 0) and (Input20 == 0)) then
     mc.mcCntlSetRRO(inst, 0.0) --Set RRO to 0%
end

If I get time later I will look at some debugging for ya.

DazTheGas
Theres always time for a choccy biccy

Offline Bob49

*
  •  57 57
    • View Profile
Re: Multiple inputs in LUA script
« Reply #7 on: October 12, 2016, 12:53:41 PM »
Thanks for that, but still no love from the switch. :'(  But that version actually makes sense to me.  More sense than how it was before, but I'm LUA stupid, so take that with a grain of salt. :)

That would be fantastic for some help in debugging this.

Bob

Offline Chaoticone

*
  • *
  •  5,626 5,626
  • Precision Chaos
    • View Profile
Re: Multiple inputs in LUA script
« Reply #8 on: October 12, 2016, 12:56:11 PM »
Ok, here is the thing, in the call hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT18) inst is the first parameter required. What is inst? In your script it does not know because you have not defined it yet. You edited it to define it after it was needing defined. So any variable you are using for a parameter must be defined before using it.

The reason it works in the screen load script is because inst is defined at the very beginning of the screen load script.

inst as you can tell by the api call mc.mcGetInstance() is the instance of Mach. Because Mach4 can have multiple instances you need to know what instance you want to get or set. With the exception of a few, all api calls will use the instance parameter for the first parameter.

You only need to define it once within the scope of whatever it is your doing.

That error message is telling you exactly what is going on. It was looking for a number for the instance parameter (defined as mInst for Mach Instance in the api docs) but got a nil because the variable you were using for the instance ("inst") was not defined. All variables in lua are given the value of nil until you overwrite it with something else. So it got a nil when it was needing a number.

But listen, don't feel bad about this. I did this jut a week or so ago myself.  :-[  Tunnel vision may be the death of me. Thankfully another fella loaned me his eyes for a bit and caught it right off.

;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 Chaoticone

*
  • *
  •  5,626 5,626
  • Precision Chaos
    • View Profile
Re: Multiple inputs in LUA script
« Reply #9 on: October 12, 2016, 01:03:32 PM »
I realized earlier today that the script I inserted was the first capture and not completely correct as far as the inputs.  Here's the corrected script I've been testing.  I've tested the individual pins changing state with the Set RRO statement and it works fine.  And I've tried the below script with just two inputs and can't get it to work, so i'm thinking it's not the three inputs that's giving me a problem, I think there's something wrong in the wording of the script.

Code: [Select]
local Input14 = 0;
local Input15 = 0;
local Input17 = 0;
local hSig = 0;

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT14);
Input14 = mc.mcSignalGetState(hSig);

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT15);
Input15 = mc.mcSignalGetState(hSig);

hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT17);
Input17 = mc.mcSignalGetState(hSig);

if ((Input14 == 1) and (Input15 ==1) and (Input17 == 1)) then
    local inst = mc.mcGetInstance();
    mc.mcCntlSetRRO(inst, 100.0);
end

Bob

So what inputs are you using? 14,15,17 or 18,19,20?

Daz's script should work a treat but if your using different inputs edit the script so its looking at the right ones.
;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!