CODESYS problems with edge detection (bounce)

Images
Related searches

I have a problem with my CODESYS program. I have three buttons, which are defined as input. For each button a number is stored. For example the number 1. I have now created a program, which recognizes an edge on the button and stores the numerical value (2) of the button in an array. If you now press another button with the value (3), the value is also stored in a variable again. The two variables should be added together. 2 + 3 = 23. In my program I have the problem that if I press the button tester with the value 2, I get 22. This is wrong. I think the problem is due to the bruise of the push button. Several edges are detected. So I wanted to solve this software technically with a delay. Do you have any idea how I could program that?

CODE:

IF (PLC_PRG.calls[5].gpio = TRUE) THEN // edge detection on gpio
    IF (counter = 0) THEN // counter for the first value
        floorstorage2[0] := PLC_PRG.calls[5].message.floorstore[5]; // save button value in the array to calculate the total
        counter := 1;
    ELSE
        floorstorage2[1] := PLC_PRG.calls[5].message.floorstore[5]; // save button value in the array to calculate the total
        counter := 0;
    END_IF
END_IF

IF (PLC_PRG.calls[6].gpio = TRUE) THEN // edge detection on gpio
    IF (counter = 0) THEN // counter for the first value
        floorstorage2[0] := PLC_PRG.calls[6].message.floorstore[6]; // save button value in the array to calculate the total
        counter := 1;
    ELSE
        floorstorage2[1] := PLC_PRG.calls[6].message.floorstore[6]; // save button value in the array to calculate the total
        counter := 0;
    END_IF
END_IF

IF (PLC_PRG.calls[7].gpio = TRUE) THEN // edge detection on gpio
    IF (counter = 0) THEN // counter for the first value
        floorstorage2[0] := PLC_PRG.calls[7].message.floorstore[7]; // save button value in the array to calculate the total
        counter := 1;
    ELSE
        floorstorage2[1] := PLC_PRG.calls[7].message.floorstore[7]; // save button value in the array to calculate the total
        counter := 0;
    END_IF
END_IF


GlobalVar.floorstorage := concat(floorstorage2[0],floorstorage2[1]); // Total of value 1 and value 2 (1 + 2 = 12)

You need to implement edge detection. Here is a code template you can use:

//  Generate Oneshot Signal 

VAR_INPUT
    SIGNAL : BOOL; // Input
END_VAR

VAR
    LATCH_SIGNAL : BOOL; // Latch
END_VAR

VAR_TEMP
    OS_P_SIGNAL : BOOL; // Oneshot - Rising edge detection
    OS_N_SIGNAL : BOOL; // Oneshot - Falling edge detection
END_VAR

//Code - Rising edge detection
OS_P_SIGNAL := SIGNAL AND NOT LATCH_SIGNAL;
LATCH_SIGNAL := SIGNAL;

//Code - Falling edge detection
OS_N_SIGNAL := NOT SIGNAL AND NOT LATCH_SIGNAL;
LATCH_SIGNAL := NOT SIGNAL;

SFC EDGE DETECTION ISSUE, I'm running very simple test program and I'm having some issues with active and StopPB_ON is TRUE, causing that Stop_Mode jump (from upper with addition of R_TRIG next to edge detection contact (StopPB_ON)?. Command ‘Edge Detection’¶ Symbol FBD:, symbol LD:, shortcut: Ctrl + N. Function: This command inserts an edge detector before the selected box input or box output. Call: Main menu FBD/LD/IL, context menu. Requirements: The FBD or LD editor is active. A box input or box output is selected. See also. FBD/LD/IL Editor

I do not see any edge detection here

IF (PLC_PRG.calls[5].gpio = TRUE) THEN // edge detection on gpio
    IF (counter = 0) THEN // counter for the first value
        floorstorage2[0] := PLC_PRG.calls[5].message.floorstore[5]; // save button value in the array to calculate the total
        counter := 1;
    ELSE
        floorstorage2[1] := PLC_PRG.calls[5].message.floorstore[5]; // save button value in the array to calculate the total
        counter := 0;
    END_IF
END_IF

You check if the button is pressed. It means while you are holding this button this condition is true. It means that counter change it's state on every PLC cycle while you are holding the button. It means that if when you release button PLC made even number of cycles nothing will change and if odd number f cycles it will change. Here is how you detect a rising edge.

VAR
    xSignal, xSignalM: BOOL;
END_VAR

IF xSignal AND NOT xSignalM THEN
    // Raising edge is here
END_IF
xSignalM := xSignal;

This way condition will work only one PLC cycle and everything will be ok. So your code would look like this.

VAR
    M1, M1, M3: BOOL;
END_VAR

IF (PLC_PRG.calls[5].gpio AND NOT M1) THEN // edge detection on gpio
    IF (counter = 0) THEN // counter for the first value
        floorstorage2[0] := PLC_PRG.calls[5].message.floorstore[5]; // save button value in the array to calculate the total
        counter := 1;
    ELSE
        floorstorage2[1] := PLC_PRG.calls[5].message.floorstore[5]; // save button value in the array to calculate the total
        counter := 0;
    END_IF
END_IF
M1 = PLC_PRG.calls[5].gpio;

IF (PLC_PRG.calls[6].gpio  AND NOT M2) THEN // edge detection on gpio
    IF (counter = 0) THEN // counter for the first value
        floorstorage2[0] := PLC_PRG.calls[6].message.floorstore[6]; // save button value in the array to calculate the total
        counter := 1;
    ELSE
        floorstorage2[1] := PLC_PRG.calls[6].message.floorstore[6]; // save button value in the array to calculate the total
        counter := 0;
    END_IF
END_IF
M2 = PLC_PRG.calls[6].gpio;

IF (PLC_PRG.calls[7].gpio  AND NOT M3) THEN // edge detection on gpio
    IF (counter = 0) THEN // counter for the first value
        floorstorage2[0] := PLC_PRG.calls[7].message.floorstore[7]; // save button value in the array to calculate the total
        counter := 1;
    ELSE
        floorstorage2[1] := PLC_PRG.calls[7].message.floorstore[7]; // save button value in the array to calculate the total
        counter := 0;
    END_IF
END_IF
M3 = PLC_PRG.calls[7].gpio;

Or you can use R_TRIG

VAR
    RT1:R_TRIG;
END_VAR

R1(CLK := PLC_PRG.calls[5].gpio);
IF (R1.Q) THEN
    IF (counter = 0) THEN
        floorstorage2[0] := PLC_PRG.calls[5].message.floorstore[5];
        counter := 1;
    ELSE
        floorstorage2[1] := PLC_PRG.calls[5].message.floorstore[5];
        counter := 0;
    END_IF
END_IF

Codesys: R Trigger doesn't seem to be working properly? : PLC, Is there a problem with software/ function block or anything else? Thank you. EDIT: The issue was ScanTime. When I latched the output, Rising Edge was detected� Omron PLC E Learning Lesson 3 Rising and Falling Edge using Memory Bit - Duration: 12:27. Rajvir Singh 29,653 views

I would implement the whole logic in a more object oriented way.

First, we define a Button.

Declaration part:

FUNCTION_BLOCK FB_Button
VAR
    bSignal   AT%I* : BOOL;
    IButtonHandler : I_ButtonHandler;

    fbPushTimer : TON;
    fbTrig : R_TRIG;

    sValue : STRING;
END_VAR

Implementation part:

IF IButtonHandler = 0
THEN
    RETURN;
END_IF

fbPushTimer(IN:= bSignal, PT:=T#50MS);
fbTrig(CLK:=fbPushTimer.Q);

IF fbTrig.Q
THEN
    IButtonHandler.onPush(sValue);  
END_IF

The button has four properties:


1st Property:

Declaration part:

 PROPERTY getValue : String

Implementation part:

getValue := sValue;

2nd Property:

Declaration part:

PROPERTY isPushed : BOOL

Implementation part:

isPushed := bSignal;

3rd Property:

Declaration part:

PROPERTY setPushHandler : I_ButtonHandler

Implementation part:

IButtonHandler := setPushHandler;

4th Property:

Declaration part:

PROPERTY setValue : String

Implementation part:

sValue := setValue;

Then we define the interface.

INTERFACE I_ButtonHandler

and add the interface method:

METHOD onPush 
VAR_INPUT
    sValue : STRING;
END_VAR

At last we define the handler.

Declaration part:

FUNCTION_BLOCK FB_ButtonHandler IMPLEMENTS I_ButtonHandler
VAR_OUTPUT
    floorstorage : STRING;
END_VAR

The handler has two methods:


1st Method:

Declaration part:

METHOD onPush 
VAR_INPUT
    sValue : STRING;
END_VAR

Implementaion part:

floorstorage := concat(floorstorage,sValue);

2nd Method:

Declaration part:

METHOD reset 

Implementation part:

floorstorage := '';

Now we need to init the buttons and call them in main.

Main declaration part:

PROGRAM MAIN
VAR 
    aButtons : ARRAY[1..10] OF FB_Button;
    fbButtonHandler : FB_ButtonHandler;
    i : UINT;
    bInit : BOOL;
END_VAR

Implementation part:

IF NOT bInit
THEN
    FOR i := 1 TO 10 DO
        aButtons[i].setPushHandler := fbButtonHandler;
        aButtons[i].setValue := UINT_TO_STRING(i);
    END_FOR
    bInit := TRUE;
END_IF

FOR i := 1 TO 10 DO
    aButtons[i]();
END_FOR

You can choose your own button value depending on the button.

For simplicity, I assigned the loop index as the value of the button.

Each time the button is pushed, the method onPush gets called only once after 50ms.

When you want to access the value of floorstorage you simply call fbButtonHandler.floorstorage to assign it to another var.

By doing so you reach a stronger encapsulation and data protection for your variable than declaring it as global.

R_TRIG and F_TRIG (IEC 61131-3), The output value. The Rising Edge Detector is logically equivalent to: Function_Block R_TRIG Var_Input CLK : BOOL; End_Var Var_Output Q : BOOL; End_Var� The CODESYS Group is the manufacturer of CODESYS, the leading hardware-independent IEC 61131-3 automation software for developing and engineering controller applications. CODESYS GmbH A member of the CODESYS Group Memminger Straße 151, 87439 Kempten Germany Tel.: +49-831-54031-0 info@codesys.com

[PDF] The CoDeSys Visualization, The visualization integrated in CoDeSys does not need a tag list and it can directly access the Offset on the particular edges of an object (for Bar Display) <Tab> key and jump to the next by each further keystroke. path and this might cause problems in case you want to transfer the project to another� The CODESYS Edge Gateway enables the communication between the CODESYS Automation Server and the connected controllers. It can be operated on a controller or on a stand-alone device in the local network.</p>

[PDF] Programming Manual, EtherNet subnet. Non-compliance may result in connection problems between the CODESYS Safe trigger with falling-edge detection Max. bounce time. 250. CODESYS Store North America NOW ONLINE! CODESYS Automation Server – Now free of charge until end of 2020 The CODESYS Automation Server is the new cloud-based industry 4.0 platform for the centralized management of controller tasks and globally distributed controller networks.

If you need technical support, please purchase a Support Ticket in the CODESYS Store. Note that certain products in the CODESYS Store already include a Support Ticket. To report software bugs or if you have a question concerning the products in the CODESYS Store, click on the blue My question button in the CODESYS Store.

Comments