plcLib (Arduino): Sequential Function Charts

A Sequential Function Chart (SFC), allows a PLC to move through a series of steps under program control. The transition from one step to another occurs if the transition condition(s) are met, and each step may optionally produce one or more outputs. As the name suggests, SFCs are normally represented in diagrammatic form, as shown in the following example.

Creating Sequences

SFCs allow a sequence-based system to be represented in a concise form. Each step in the sequence is shown in a rectangular box at the left, with the first step having a double border. Transition conditions are shown next to horizontal lines between adjacent steps in the sequence. An active step may optionally produce one or more outputs and these are shown in a box to the right of the associated step.

In the above case, the system automatically begins in the Start-up step. Progression to Step 1 occurs when input X0 is pressed, causing output Y0 to be displayed. With Y0 displayed, pressing input X1 causes the system to progress to Step 2, activating output Y1.

An equivalent sketch is given below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Simple Sequence - A Three step sequence with push button control
   
   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Input - switch connected to input X1 (Arduino pin A1)
   Output - LED connected to output Y0 (Arduino pin 3)
   Output - LED connected to output Y1 (Arduino pin 5)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/


   Sequential Function Chart
   
        |=========|
        |  START  |
        |=========|
             |
            -|-- X0
             |
        |----|----|    |------|
        |  STEP1  |----|  Y0  |
        |---------|    |------|
             |
            -|-- X1
             |
        |----|----|    |------|
        |  STEP2  |----|  Y1  |
        |---------|    |------|


   The Start step is active when the system is switched-on or reset
   Press X0 to activate Step 1, displaying Y0
   Next, press X1 to activate Step 2, displaying Y1
   
*/

                            // Define state names

unsigned int START = 1;     // Start-up state (START = 1 to automatically start here)
unsigned int STEP1 = 0;     // Step 1
unsigned int STEP2 = 0;     // Step 2

void setup() {
  setupPLC();               // Setup inputs and outputs
}

void loop() {
                            // Do state transitions

  in(START);                // Read Start-up state
  andBit(X0);               // AND with Step 1 transition input
  set(STEP1);               // Activate Step 1
  reset(START);             // Cancel Start-up state
 
  in(STEP1);                // Read Step 1
  andBit(X1);               // AND with Step 2 transition input
  set(STEP2);               // Activate Step 2
  reset(STEP1);             // Cancel Step 1

  
                            // Display current state
  
  in(STEP1);
  out(Y0);                  // Send to Output 0

  in(STEP2);
  out(Y1);                  // Send to Output 1

}
Source location: File > Examples > plcLib > SequentialFunctionChart > SimpleSwitchSequence

The above listing consists of three key sections:

1: Variables are defined to represent each step in the sequence.

                            // Define state names

unsigned int START = 1;     // Start-up state (START = 1 to automatically start here)
unsigned int STEP1 = 0;     // Step 1
unsigned int STEP2 = 0;     // Step 2

A step is active if its associated variable is equal to 1, and inactive when zero. Hence, initial conditions at power-up or reset are defined based on the above allocated variable values.

2: Steps are activated in a predefined sequence, as each transition condition occurs.

                            // Do state transitions

  in(START);                // Read Start-up state
  andBit(X0);               // AND with Step 1 transition input
  set(STEP1);               // Activate Step 1
  reset(START);             // Cancel Start-up state
 
  in(STEP1);                // Read Step 1
  andBit(X1);               // AND with Step 2 transition input
  set(STEP2);               // Activate Step 2
  reset(STEP1);             // Cancel Step 1

Notice that transition conditions are ANDed with the current state, which ensures a transition input is only accepted when its associated step is active. The program should also cancel the previous step when the next step is activated, and this is achieved using the set() and reset() latch commands, in the above extract.

3: Output(s) are produced based on the active step(s).

                            // Display current state
  
  in(STEP1);
  out(Y0);                  // Send to Output 0

  in(STEP2);
  out(Y1);                  // Send to Output 1

This simple program has a single unique output for each step. A more complex system, such as a traffic light controller (see later), may require a custom 'mapping' between steps and outputs – typically implemented with Boolean OR functions.

The above example may easily be extended to create a repeating sequence, as shown below.

Source location: File > Examples > plcLib > SequentialFunctionChart > RepeatingSwitchSequence

The system powers up in the start-up step with output Y0 active. Pressing X0 activates Step 1, then inputs X1, X2 and X3 cause a repeating progression through Steps 1 – 3, with outputs Y1Y3 activated, respectively.

Branching and Converging

A parallel branch allows several steps to be simultaneously activated, as shown in the following illustration.

Source location: File > Examples > plcLib > SequentialFunctionChart > ParallelSwitchBranch

The Start step is initially active with output Y0 enabled. Pressing input X0 simultaneously activates
Steps 1 – 3, displaying outputs Y1Y3.

A parallel branch may be followed by a simultaneous convergence, as shown below.

Source location: File > Examples > plcLib > SequentialFunctionChart > ParallelSwitchBranchConverge
Any previous steps must be cancelled following a simultaneous convergence.

A selective branch allows a multiple choice decision to be made in which a single step is activated from a range of possible options, as shown below.

Source location: File > Examples > plcLib > SequentialFunctionChart > SelectiveSwitchBranch

A selective branch may be followed by a convergence, as shown below.

Source location: File > Examples > plcLib > SequentialFunctionChart > SelectiveSwitchBranchConverge

The next section discusses the use of delays to produce time-based sequences, and the development of simple SFC-based applications.

Related Topics

top