Machsupport Forum

Mach Discussion => Mach4 General Discussion => Mach4 Toolbox => Topic started by: swiftyscnc on September 09, 2015, 08:21:08 AM

Title: Lua coding for External Controls
Post by: swiftyscnc on September 09, 2015, 08:21:08 AM
Hi all,
This is the first post on the forum (so please go easy on me). ;D

I am currently building an external control panel (Buttons and lots of wire) to link in with Mach4 and have a question or two about the coding.

Below is a copy of the code for the first 3 inputs I have placed in wxMach screen set under the Screen Loan Script.

Code: [Select]
SigLib = {
--  Enable
    [mc.OSIG_MACHINE_ENABLED] = function(state)
        machEnabled = state
    end,

-- Enable Button
    [mc.ISIG_INPUT1] = function(state)
    mc.mcCntlSetLastError(mc.mcGetInstance(), "1 - M4 Enable NOT YET - " .. state)
        if (state == 1) then
            local inst = mc.mcGetInstance()
            local reg  = mc.mcSignalGetHandle(inst, mc.OSIG_MACHINE_ENABLED)
            machine_active = mc.mcSignalGetState(reg)
            if (machine_active == 1) then
                input_on_off = 0
                mc.mcCntlSetLastError(inst, "1 - M4 Enable Pressed (OFF)")
            else
                input_on_off = 1
                mc.mcCntlSetLastError(inst, "1 - M4 Enable Pressed (ON)")
            end
            mc.mcCntlEnable(inst, input_on_off)
        end
    end;
   
-- Cycle Start
    [mc.ISIG_INPUT2] = function(state)
    mc.mcCntlSetLastError(mc.mcGetInstance(), "2 - Cycle Start NOT YET - " .. state)
        if (state == 1) then
            local inst = mc.mcGetInstance()
            mc.mcCntlSetLastError(inst, "2 - Cycle Start Pressed - " .. state)
            mc.mcCntlCycleStart(inst)
        end
    end;

-- Feed Hold
    [mc.ISIG_INPUT3] = function(state)
    mc.mcCntlSetLastError(mc.mcGetInstance(), "3 - Feed Hold NOT YET - " .. state)
        if (state == 1) then
            local inst = mc.mcGetInstance()
            mc.mcCntlSetLastError(inst, "3 - Feed Hold Pressed")
            mc.mcCntlFeedHold(inst)
        end
    end;

As you can see I have placed display outputs in the code for debugging.
The output from the running code is as follows:

The layout of the output is
Button Number - Button Name - state


Code: [Select]
1 - M4 Enable NOT YET - 0
2 - Cycle Start NOT YET - 0
3 - Feed Hold NOT YET - 0

1 - M4 Enable NOT YET - 0
1 - M4 Enable NOT YET - 1
1 - M4 Enable Pressed (ON)
2 - Cycle Start NOT YET - 0
2 - Cycle Start NOT YET - 1
2 - Cycle Start Pressed - 1
3 - Feed Hold NOT YET - 0
3 - Feed Hold NOT YET - 1
3 - Feed Hold Pressed

1 - M4 Enable NOT YET - 0
2 - Cycle Start NOT YET - 0
3 - Feed Hold NOT YET - 0


In total I will have 19 switches, 2 panel mount Joysticks and other stuff on the panel.
My current PC setup is listed below



The code I have written is working once it comes to rest BUT....

Questions,

1. Why dose M4 magically change "state" to true, yet no button has been pressed?

2. Why dose it loop 4 times before coming to rest?

3. What is the use of the ";" at the end of some lines?

Cheers and thanks for all the fish
Greg
 ??? ??? ??? ???

PS. I have attached a full copy of the code and History files FYI
Title: Re: Lua coding for External Controls
Post by: poppabear on September 09, 2015, 09:23:14 AM
1. Why dose M4 magically change "state" to true, yet no button has been pressed?
>>state is controlled by M4 and deals with its status

2. Why dose it loop 4 times before coming to rest?
>>May depend on what motion plugin your using, but at any rate.
>>put in a "Loop Counter" that counts to "5" which will then set some custom
>>var to true, which then allows your code block(s) to run.
>>those 4 loops are going to happen REAL fast at init.

3. What is the use of the ";" at the end of some lines?
>>In lua, you can use them for a code line delimiter (like in C).
>>lua does not care if you use them or not.
>>I personally use them since I program in C as well, and don't wont
>>to have to think about using them or not..  :)

Cheers and thanks for all the fish
>>The answer is "42" because you where not very specific
Title: Re: Lua coding for External Controls
Post by: swiftyscnc on September 12, 2015, 07:58:31 PM
Thanks for your reply

the loop counter has fixed it
Title: Re: Lua coding for External Controls
Post by: dunnitagain on December 20, 2015, 08:16:35 AM
How are you coming on the Control Panel? I'm retrofitting a large Industrial Machine that has 32 Buttons and a MPG , and was wondering about your success.
Title: Re: Lua coding for External Controls
Post by: cncman172 on March 10, 2016, 11:42:58 AM
swiftyscnc,

When you setup your code for the POKEYS-56U, which is what I have did you use any matrix keyboard configuration or just direct inputs and outputs tied directly to pokey pins?  I can get the direct pins to work for inputs and outputs from the POKEY device and also the outputs via the MATRIX LED to work.  The keyboard matrix input to MACH4 shows but does not seem to see the key presses?

Russ