Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Cbyrdtopper on January 03, 2017, 06:02:52 PM

Title: Keyboard Inputs Enabled or Disabled Handle
Post by: Cbyrdtopper on January 03, 2017, 06:02:52 PM
I'm trying to get a handle on the Keyboard Inputs, whether it is enabled or disabled so I can set an output on or off. 

I've seen where there is GetHandle on Keyboard/Enable in the PLC First run but I'm not sure what it is getting a handle on. 

Is there a way to get the handle on whether or not the Keyboard is Enabled or Disabled?
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: Cbyrdtopper on January 03, 2017, 09:48:30 PM
I found this video by DazTheGas
https://www.youtube.com/watch?v=8KgZ1SYxEdg
Gonna check it out in the morning and see if I can use some of this code.
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: Cbyrdtopper on January 04, 2017, 09:45:19 AM
In the video from DazTheGas that I posted the buttons he was working with had code in them, now they have drop downs to determine the function. 

I tried to use the code form the video but it isn't working.  So I'm still stuck.   

Any ideas?
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: smurph on January 04, 2017, 02:12:28 PM
The keyboard plugin exports an output that can be used to enable/disable the keyboard plugin.  It is not necessarily a reflection of the enabled status as nothing in the keyboard plugin will actually set the value of this output because it is well...  an output.  It is a control mechanism that the GUI can use to enable/disable the keyboard functionality.

Code: [Select]
local inst = mc.mcGetInstance()
local hIo, rc = mc.mcIoGetHandle(inst, "Keyboard/Enable")
if (rc == mc.MERROR_NOERROR) then
    --the keyboard plugin exists and the plugin is enabled and we found the plugin output.
    mc.mcIoSetState(hIo, 1) -- Turn the keyboard enable output on.  The keyboard plugin will now accept keyboard input.
    mc.mcIoSetState(hIo, 0) -- Turn the keyboard enable output off.  The keyboard plugin will not accept keyboard input.
end

But currently, there is nothing that will get the status of the keyboard plugin.  :(  Other than looking the task bar icon.

I will add a status register that can be read to determine the status in a future build. 

Steve
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: DazTheGas on January 04, 2017, 03:30:54 PM
I tried to use the code form the video but it isn't working.  So I'm still stuck.   

Just tried the code myself and all is fine, make sure you have the plugin enabled as the keyboard icon will show in the taskbar wether the plugin is enabled or not.

DazTheGas
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: Cbyrdtopper on January 04, 2017, 03:39:43 PM
Thanks for the reply Steve.  It would be nice to have a status register to make things easier. 

DazTheGas,
If you don't mind, could you send me the code you used to test that to be sure I'm not making a mistake on my end?
Thanks!
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: DazTheGas on January 04, 2017, 03:46:56 PM
If you just want to get the state you can use

Code: [Select]
local inst = mc.mcGetInstance()
local hIo, rc = mc.mcIoGetHandle(inst, "Keyboard/Enable")
if (rc == 0) then
   local KbdState =  mc.mcIoGetState(hIo)
end

wx.wxMessageBox(tostring(KbdState)) -- 0 = Off, 1 = On   Just a test to show returned code

code for the example in video is as posted, nothing more or less, well apart from missing local inst = mc.mcGetInstance() at the beginning.

DazTheGas
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: DazTheGas on January 04, 2017, 04:04:23 PM
Scrap that last post?? local KbdState =  mc.mcIoGetState(hIo), sure that just worked but after cleaning up the code it doesnt??

DazTheGas   :o
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: smurph on January 04, 2017, 04:33:05 PM
Because getting the state of an output is not logical.  You set outputs.  If you get the state of an output, the result is undefined because there isn't really a backing store for the state at the device level.  Meaning if you get the state of an output, it may be different than the actual state of the output on the device.  

I'm adding a register that will allow both getting the status and controlling the keyboard plugin.  A register actually has a backing store for the value.  

Steve
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: Cbyrdtopper on January 04, 2017, 05:16:50 PM
I tried the code DazTheGas, I was getting the state of the Keyboard.  

Steve, That will be great when you get the register to control the keyboard plugin.  I am wanting to add a physical Button on the control panel that Lights up when the Keyboard Plugin is enabled and turns off when it's disabled, the button will toggle the keyboard plugin on and off.  This will make it easier for the operator to use, that way they don't have to keep going to the jogging tab to toggle the Keyboard Plugin.
Looking forward to it!!

Thanks for the time and insight guys!!
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: Cbyrdtopper on January 04, 2017, 05:18:39 PM
As of right now, I've got a physical button with the same code as the Keyboard Enable button in the jogging tab, and I've moved that Keyboard button to the file ops tab so the operator won't have to change tabs to see the change in color of the button.
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: DazTheGas on January 04, 2017, 05:42:37 PM
The code Steve posted earlier will do exactly that when combined with the SigLib, just assing to a input signal ie..

Code: [Select]
[mc.ISIG_INPUT10] = function (state) -- your choice of input
local hIo = mc.mcIoGetHandle(inst, "Keyboard/Enable")
    if (state == 1) then   
mc.mcIoSetState(hIo, 1) -- Turn the keyboard enable output on.  The keyboard plugin will now accept keyboard input.
    else
mc.mcIoSetState(hIo, 0) -- Turn the keyboard enable output off.  The keyboard plugin will not accept keyboard input.
    end
end,

DazTheGas
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: smurph on January 04, 2017, 07:30:15 PM
You can get the state of the keyboard plugin with the output.  However, since it may not reflect the state of the keyboard plugin at startup, you have to set it to something first.  This is why that call was made the FIRST time the PLC script ran.  Then, once the output is set to on or off, you can then get the output state and it will reflect the status of the keyboard plugin.

Now, there are two outputs that the keyboard plugin exports.  Keyboard/Enable and Keyboard/EnableKeyboardJog.  One enables/disables the entire keyboard key mapping and the other will just disable the jog key mappings but leave the rest of the key mapping working (keyboard must be enabled). 

So it can work.  But...  The register is the better way in case one ever just wants to check if the keyboard mappings are enabled without setting that status first.  In any case, there will be a register called "KeyboardControl/Enabled" that will reflect the state of the keyboard mappings.  If you set that register to 1, it will enable the mapping.  Setting it to 0 will disable all of the mappings.  Reading it will always reflect the status of the mappings. 

Steve
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: smurph on January 04, 2017, 07:32:45 PM
The Keyboard plugin was one of the first plugins we developed.  So it was made prior to the advent of the Mach Register concept.  Just a FYI as to why it is like the way it was.  :)
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: ThePaul on August 08, 2018, 06:36:00 PM
I just did this very thing recently by adding a record to the Keyboard Inputs plugin and using that to trigger the action through the screen PLC script.  My thanks to DazTheGas as I figured this out after watching his 'Mach4 Quicky #2 Keyboard Plugin' video: https://youtu.be/yzaf5pT0l50 (https://youtu.be/yzaf5pT0l50)

1) Assign functions to keys:

Configure->Plugins->Keyboard_Inputs
Add input
InputName: EnableMach
Key: E (for Enable)
CTRL+ALT+SHIFT all checked so that this is not going to happen by accident
Lock key: none
Function: none

I also added similar Inputs for DisableMach (D key), StartCycle (S), HaltCycle (H, because S was already taken and I didn't want to use it as a toggle), and ResetCycle (R).

2)Map the keyboard inputs to Mach inputs:

Configure->Control->Inputs Signals
Input #6 (only because it was the next one available after several of my ESS inputs)
Mapping Enabled: checked
Device: keyboard (from dropdown)
Input Name: EnableMach (from dropdown)

I repeated above using Inputs #7-10 for DisableMach, StartCycle, HaltCycle, and ResetCycle respectively.


3) Add the following code to the screen PLC script immediately following the end of the 'PLC First Run' section:

Code: [Select]
-------------------------------------------------------
--PE3696 ENABLE Mach: CTRL+ALT+SHIFT+E[nable]
-------------------------------------------------------
if mc.mcSignalGetState (mc.mcSignalGetHandle (inst, mc.ISIG_INPUT6)) == 1 then
mc.mcCntlEnable(inst,1)
else
 --   Do something else
end

-------------------------------------------------------
--PE3696 DISABLE Mach: CTRL+ALT+SHIFT+D[isable]
-------------------------------------------------------
if mc.mcSignalGetState (mc.mcSignalGetHandle (inst, mc.ISIG_INPUT7)) == 1 then
mc.mcCntlEnable(inst,0)
else
 --   Do something else
end

-------------------------------------------------------
--PE3696 START CYCLE: CTRL+ALT+SHIFT+S[tart]
-------------------------------------------------------
if mc.mcSignalGetState (mc.mcSignalGetHandle (inst, mc.ISIG_INPUT8)) == 1 then
mc.mcCntlCycleStart (inst)
else
 --   Do something else
end

-------------------------------------------------------
--PE3696 STOP CYCLE: CTRL+ALT+SHIFT+H[alt]
-------------------------------------------------------
if mc.mcSignalGetState (mc.mcSignalGetHandle (inst, mc.ISIG_INPUT9)) == 1 then
mc.mcCntlCycleStop (inst)
else
 --   Do something else
end

-------------------------------------------------------
--PE3696 RESET CYCLE: CTRL+ALT+SHIFT+R[eset]
-------------------------------------------------------
if mc.mcSignalGetState (mc.mcSignalGetHandle (inst, mc.ISIG_INPUT10)) == 1 then
      mc.mcCntlCycleStop (inst)
  mc.mcCntlReset (inst)
else
 --   Do something else
end


You will have to enable Keyboard Inputs on the Jogging Tab.  You can do this automatically at Mach startup by adding the following code inside the end of the 'PLC First Run' section of the screen PLC script:
Code: [Select]
-------------------------------------------------------
--PE3696 Enable Keyboard on startup
-------------------------------------------------------
local hKeyboardState = mc.mcRegGetHandle(inst, "KeyboardControl/Enabled");
local KeyboardState = mc.mcRegGetValue(hKeyboardState);

if (KeyboardState == 0) then;
KeyboardInputsToggle()
else;
--Do something else
end;


This may be a primitive way of accomplishing all of these actions but it works for me.  I'm new to Lua and Mach4, so please tell me if this method is risky or if there is a vastly simpler way of doing it.

-Paul
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: smurph on August 08, 2018, 08:33:45 PM
Using the PMC is even easier.  Do the same creation of inputs in the the keyboard plugin and map them directly to the desired action in the PMC.  You don't have to map them to Mach signals this way. 

Steve
Title: Re: Keyboard Inputs Enabled or Disabled Handle
Post by: TOTALLYRC on September 09, 2018, 08:37:18 AM
+1 on the PMC.
No Lua required.
Great if you already have an understanding of ladder logic!

Mike