Hello Guest it is April 28, 2024, 12:49:05 PM

Author Topic: Mach 4 rising edge detection for key inputs in the PLC script  (Read 692 times)

0 Members and 1 Guest are viewing this topic.

Mach 4 rising edge detection for key inputs in the PLC script
« on: November 06, 2023, 04:58:21 AM »
Hello everyone,

I'm new to the forum and would like to introduce myself. I'm Thomas, 60 years old and live in Germany. After 10 years I converted my Router from stepper drives to servos. I also changed to Mach 4 at the same time. I mainly mill wood. In particular module boxes according to the FREMO standard for model railroads. The milling machine runs great after the conversion and there is nothing to complain about. I am also using a new handwheel, which I am very happy with. Without wanting to advertise, this is the DIY handwheel from cnc-handrad.
The handwheel has a freely programmable keypad. The pressed button is sent as an integer value via the Modbus. For example, the first button. If it is pressed, it returns 1 via the Modbus. If it is released, it returns a 0. Logical and simple up to this point. The entire handwheel is evaluated via the PLC script of the screen. The data exchange between Mach4 and the handwheel is bidirectional, so that all operating states and the important DROs are always up to date. It all works perfectly and functions flawlessly.
Now to my problem.
As the PLC script is run through cyclically, a problem arises if you want to switch a function On->Off->On->Off -> etc. with such a button. This is actually only possible with an edge evaluation on the rising edge. Without edge evaluation, the output always flickers when the button is not pressed but held down. Catching the right moment is then very difficult, especially as the script is run through every 25-50ms. The variable "val_input" supplies the key code.
   
The code with edge evaluation could look like this. (Looks like this, but does not work!)
Sorry for comments in German.

Code: [Select]
-- Absaugung EIN/AUS -------------------------
osigabs = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1) --Absaugung Start/Stop
Stateabs = mc.mcSignalGetState(osigabs)
if (val_input == 1) and (Flankenmerker== 0) then  --Taste 1 Trigger rising
   if (Stateabs == 0) then                                    -- Schalte Ausgang abhängig vom Status
       mc.SignalSetState(osigabs, 1)
   else
      mc.SignalSetState(osigabs, 0)
   end
   Flankenmerker = 1;                                         -- Der Flankenmerker verhindert jeden weiteren Durchlauf
elseif (val_input == 0) and (Flankenmerker == 1) then  -- Sobald die Taste losgelassen wird wartet der Code auf die nächste steigende Flanke. (Scharfschaltung auf gedrückte Taste warten)
      Flankenmerker = 0;
end

The whole thing would work perfectly, especially as I use this construct with all Siemens PLCs for edge evaluation in SCL. My problem is the static variable for the "edge flag" (Flankenmerker). The edge flag variable must be initialized somewhere in the script. But this leads to the fact that the value is not saved for the next cycle depending on the last keystroke, but the initialization overwrites the value.
My question: Does anyone know a way to solve this problem in LUA, or am I being too stupid with the initialization? In Mach 3 I had a hidden screen for such cases and used an LED as an edge marker. This was then evaluated by the macro pump and worked really well. It served its purpose, but I always felt it was a crutch. In Mach 4 this should surely be more elegant to solve.

I have also posted this post in the German forum, but I am hoping for an answer here.

Regards Thomas
Re: Mach 4 rising edge detection for key inputs in the PLC script
« Reply #1 on: November 06, 2023, 07:21:33 AM »
You can initialise variables at the top of the screen load script. Don’t make it a local variable though otherwise the PLC script won’t see it
Re: Mach 4 rising edge detection for key inputs in the PLC script
« Reply #2 on: November 06, 2023, 08:29:57 AM »
Hello SwiftyJ,
Thank you for your quick reply. However, this is not my problem. If I initialize the variable at the beginning of the script, then it will be initialized on every cycle. As a result, the variable does not have the value of the previous cycle. But that would be important to recognize the rising and falling edge. I will now try the whole thing once with the register and load the register at the beginning of the PLC script and save the value of "Flankenmerker" at the end of the script.
This is certainly inconvenient, but still better than the solution with the hidden LED and worth a try.

Regards Thomas
Re: Mach 4 rising edge detection for key inputs in the PLC script
« Reply #3 on: November 06, 2023, 09:21:44 AM »
Initialise it at the start of the Screen Load script, not the PLC script. The Screen Load Script runs once when Mach4 starts
Re: Mach 4 rising edge detection for key inputs in the PLC script
« Reply #4 on: November 06, 2023, 04:22:10 PM »
Thank you SwiftyJ.
That was the solution. I didn't know that all global variables in all screen scripts are known to each other. Learned a lot again.

Regards from Germany
Thomas