3

For a motionPLC project written in ST, I use a lot of stepper structured functions like this:

stepNum := 1;

CASE stepNum OF
1: (* Move axis to upper positon *)
    axis.in.position := 0;
    axis.start := true;
    IF(axis.out.position = 0) THEN
        stepNum := 2;
    END_IF
2: (* Do something else *)

The goal behind this approach is to be able to wait for the drive to complete its task, before moving to the next task.

This works, but as programs grow more complex, and steps don't always need to be sequential (you can go from waiting state, to operating state, to fault state and back to waiting state), the code starts to look more like spaghetti code as written in the days of qBASIC.

Are there other styles to write this program that enable me to wait for the drive to finish its task, but that don't get so convoluted?

Please let me know if this question is 'too broad', I didn't know where else to ask.

DrDonut
  • 864
  • 14
  • 26
  • Sequential Function Charts (SFC) is a PLC programming language catered specifically for doing Sequencing, from very simple A then B then C, to even very complex sequencing including branching, diverging, and converging. ST is NOT good for doing sequencing (as you have correctly determined). – franji1 Mar 21 '17 at 17:05
  • @franji1: Thank you for your response. In SFC, how do you deal with having a lot of jumps between the different steps? (I have only seen it for very simple systems) – DrDonut Mar 22 '17 at 07:16
  • http://www.61131.com/sfchowto.htm shows some different branching examples, one where it is like a `case` statement, another where it is like parallel branching. Each branch can then have its own linear sequence. Convergence is not necessary. The SFC graphical picture should look a lot like your design documentation. – franji1 Mar 22 '17 at 12:45

2 Answers2

1

Honestly the case statement is the best way I have found to accomplish what you are talking about. However you can combine functionality in a function or function block to reduce the amount of code written in each step... or perhaps have mini case statements within function blocks to reduce the amount of code in one place to make it more "readable".

mrsargent
  • 2,267
  • 3
  • 19
  • 36
  • Thank you! Have you also seen other styles? And how do you keep oversight with all the jumps between steps? – DrDonut Mar 03 '17 at 07:47
  • 1
    There are other styles but requires a different plc language. You can use `SFC` which is essentially a case statement in more of a visual programming form. – mrsargent Mar 03 '17 at 16:12
1

I agree with mrsargent and the case statement constitutes a good way to implement a state machine in a program which is cyclically running.

I would suggest also using enumerations instead of numerical values for your states. In the example below, motorState is a variable of enumeration type and it makes the code much easier to read compared to having numerical values.

CASE motorState OF
    MOTOR_DISABLED:
    //do something here

    MOTOR_READY:
    //do something here

    MOTOR_RUNNING:
    //do something here
END_CASE
Grevster
  • 54
  • 6
  • Thanks! And yes, I found enumerations to help tremendously. Not only is it easier to read, it also forces you to think about what every step means. – DrDonut Oct 22 '20 at 06:40