2

What is the best way to make efficient and flexible state state machine(SM) in IEC61131-3 / CodeSyS v2.3? Are there any good examples of SM realizations on CodeSys V2.3?

As far as I understand, there are 3 different types of SM:

1.Case logic SM - simple, just change states based on if logic, not flexible at all, hard to maintain.

2.Table driven SM - better, but without function pointers there is need in additional conditional instructions to run some actions based on current state.

3.State Design Pattern SM - best option, flexible, no huge CASE statements, no duplication.

I am trying to adopt C State Design Patter by Adam Tornhill: http://www.adamtornhill.com/Patterns%20in%20C%202,%20STATE.pdf But it is quite hard to translate from plain C to Structural Text. CodeSys has support for Function Block pointers but I can't figure out how to declare incomplete pointer type in ST (is it even possible?):

/*C-style incomplite state pointer*/
typedef struct State* StatePtr;

struct State
{
   EventFunc exampleFunc1;
   EventFunc exampleFunc2;
}

{ST-style incomplite state pointer ???}
TYPE Sm_state_t :
STRUCT
State:POINTER TO ???;
END_STRUCT
END_TYPE
jaco0646
  • 15,303
  • 7
  • 59
  • 83
Andrew
  • 29
  • 1
  • 1
  • 4

1 Answers1

1

We just use simple CASE logic for our machines:

CASE iState OF
0:
    if x then
        iState := iState + 10;
    end_if

10:
    if y then
        iState := iState + 10;
    end_if

20:
etc..

Very simple, but it does the job.

It is not possible in CODESYS to use incomplete pointers. The only option you have is to create a POINTER TO BYTE Then when you need to use the pointer, make a new one that points to the datastructure that you want to use, and pass the address.