Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: dude1 on May 18, 2015, 02:55:20 AM

Title: Lua Signal Table
Post by: dude1 on May 18, 2015, 02:55:20 AM
I have run into a problem with signal tables from the scripting manual in it it has the sig table like this

SignalTable = { -------sig table number goes here I think without {
     [mc.ISIG_INPUT0] = function (on_off)
        if (on_off == 1) then
            mc.mcCntlCycleStart(inst)
        end
    end

I keep get a error this

Compilation error on line number: 54
Lua: Syntax error during pre-compilation
[string "C:\Users\Daniel\AppData\Local\Temp\le6115.mcc..."]:54: unexpected symbol near '['



I have searched but cant find anything to help I used this and it work`s

if (sig == mc.ISIG_INPUT1) and (state == 1) then
local inst = mc.mcGetInstance()
mc.mcCntlCycleStart(inst)
end
Title: Re: Lua Signal Table
Post by: DazTheGas on May 18, 2015, 05:39:31 AM
Quote
I have run into a problem with signal tables from the scripting manual in it it has the sig table like this

You got a link to this manual..

DazTheGas
Title: Re: Lua Signal Table
Post by: dude1 on May 18, 2015, 05:45:13 AM
http://www.machsupport.com/help-learning/product-manuals/
Title: Re: Lua Signal Table
Post by: dude1 on May 18, 2015, 06:04:21 AM

changed to this seems to work
SignalTable = {001,    --sig table number goes here I think
     [mc.ISIG_INPUT0] = function (on_off)
        if (on_off == 1) then
            mc.mcCntlCycleStart(inst)
        end
    end

then for this bit
if (SignalTable[001] ~= nil) then
SignalTable[001](on_off)
end

it cant find the input

also the editing is a bit out in the manual
Title: Re: Lua Signal Table
Post by: poppabear on May 18, 2015, 07:16:10 AM
in your first post, you are missing the closing "}" for the sig table.

Scott
Title: Re: Lua Signal Table
Post by: dude1 on May 18, 2015, 03:45:51 PM
yep found that the call not working now

if (SignalTable[001] ~= nil) then
SignalTable[001](on_off)
end

it cant find the table
Title: Re: Lua Signal Table
Post by: dude1 on May 18, 2015, 07:02:45 PM
right it goes like this.

SignalTable = {001,                                  --your sig table number goes here it compiles and runs like this
     [mc.ISIG_INPUT0] = function (on_off)
        if (on_off == 1) then
            mc.mcCntlCycleStart(inst)             --name of button command
        end
    end
}

I think this is now correct if I am wrong someone comment please.

the only problem now when I run the other part of the code it cant find the signal table.

this is the other part

if (SignalTable[sig] ~= nil) then   --I think sig is the number of the table
SignalTable[sig](state)                --I think state is (on_off) or on_off == 1) like I have it below
end

if (SignalTable[001] ~= nil) then
SignalTable[001](on_off)
end
Title: Re: Lua Signal Table
Post by: MitchB on January 02, 2016, 12:28:00 PM
Could someone who understands Lua explain what is going on here?  This reference (http://www.lua.org/pil/3.6.html) describes Lua table constructors.  Based on that, it looks like this:

SignalTable = {001,
     [mc.ISIG_INPUT0] = function (on_off)
        if (on_off == 1) then
            mc.mcCntlCycleStart(inst)
        end
    end
}

would construct a new table stored in SignalTable with [1] = 001 and [mc.ISIG_INPUT0] = function...

1. Why is the 001 needed?
2. If I want to add additional signals and functions, it seems as though I would need to add them to this SignalTable constructor inside the "{}", but in another post it said that the way to do that was to add additional constructors of the form:

SignalTable = {00n,
     [mc.ISIG_INPUTn] = function (on_off)
        if (on_off == 1) then
            ....
        end
    end
}

Wouldn't this overwrite the previous SignalTable declarations?
Title: Re: Lua Signal Table
Post by: DazTheGas on January 02, 2016, 02:41:33 PM
If you want to use the table version of this then your best place to add it is within the startup script, it is declared only once there and there are examples already there, this creates a table of signal check and what to do with them, this is then activated from the signal script the same as a function would be in the PLC.

DazTheGas
Title: Re: Lua Signal Table
Post by: MitchB on January 02, 2016, 07:05:04 PM
I followed your example and have F5 working for feed hold using the PLC script, which is delightful - thanks.

I would also like to see how it works using the signal table.  What I have tried doesn't work and I don't see what is wrong.
At the end of the Screen Load script, I have:

SignalTable = {
    [mc.ISIG_INPUT63] = function (on_off)
        if (on_off == 1) then
            local inst = mc.mcGetInstance();
            mc.mcCntlFeedHold(inst);
        end
    end,
}

At the end of the Signal Script, I have:

if (SignalTable[mc.ISIG_INPUT63] ~= nil) then
    SignalTable[mc.ISIG_INPUT63](state);
end
Title: Re: Lua Signal Table
Post by: dude1 on January 02, 2016, 07:16:49 PM
your second end
Title: Re: Lua Signal Table
Post by: MitchB on January 02, 2016, 07:28:45 PM
your second end

No doubt I've been looking at it too long, but I'm not seeing the problem with any of the end's.
Title: Re: Lua Signal Table
Post by: dude1 on January 02, 2016, 09:53:05 PM
end, should be end
Title: Re: Lua Signal Table
Post by: MitchB on January 02, 2016, 11:28:28 PM
According to http://www.lua.org/pil/3.6.html, trailing commas are valid in Lua.  I tried deleting it anyway, but it didn't make a difference.
Title: Re: Lua Signal Table
Post by: dude1 on January 03, 2016, 12:39:57 AM
go back to how daz says to do it that example I posted was working when I tried it, they may have done some changes I can't get it to work properly.
Title: Re: Lua Signal Table
Post by: DazTheGas on January 03, 2016, 05:30:14 AM
You are trying to do it the old way, in your table your using the on_off but in your signal script your using state ,

Put your signal script back to how it was and use the already created table in the startup script and a signal like this

Code: [Select]
[mc.ISIG_INPUT63] = function (state)
   if (state == 1) then   
       mc.mcCntlFeedHold(inst)
    else
    --    do something else or not
    end
end,

DazTheGas
Title: Re: Lua Signal Table
Post by: MitchB on January 03, 2016, 10:52:18 AM
The name of the variable doesn't matter.  It's a function parameter and can be called anything.  But I tried changing it to "state" anyway.  No difference.