# Function Block Diagram SoftPLC

The integrated scripting environment supports the creation of Function Block Diagram (FBD) programs, which are executed by the integrated SoftPLC. The FBD is a graphical functional programming language for creating PLC programs.

You can create your own FBD programs inside the integrated script editor using the `.plc`

file extension.

## Contents

- Creating FBD Programs
- IO Signals
- Constants
- Memory
- Standard Function Blocks
- Logical
- Relational
- Math
- Timers
- Counters
- Controllers
- Utilities

## Creating FBD Programs

FBD programs are composed of a collection of connected function blocks. Input and output signals are connected to these function blocks via connection lines. Connections may only be created between function block ports with the same type (boolean, numeric, etc). A connection line can connect the following:

- An input signal to a function block input.
- A function block output to another function block input.
- A function block output to an output signal.

The output port of a block may be connected to multiple input ports, but an input port may only be connected to one output port. Cycles in the function block diagram are not permitted. The color of a block denotes the following:

**Green**- Input signal.**Red**- Output signal.**Blue**- Constants.**Purple**- Memory.**Gray**- Functions.

## IO Signals

All readable (input) signals and writable (output) signals provided by components in the model are listed in the block palette to the left. The input and output categories are not visible if there are no signals provided by any components in the model.

## Constants

The constant blocks that are supported are:

**BOOLEAN**- Takes the value of true or false.**NUMERICAL**- Takes the value of any real number, up to double floating-point precision.

## Memory

The memory blocks that are supported are:

**BOOLEAN**- Takes the value of true or false.**INTEGER**- Takes the value of a 32-bit signed integer.**FLOAT**- Takes the value of a double precision floating-point number.

The memory blocks store an address. The address is an integral value in the range [0, 512). Each type of memory block has a unique address range, so that a boolean block with address 0 does not correspond to a integer block with address 0.

## Standard Function Blocks

The supported function blocks are based on the IEC 61131-3 standard, providing a wide range of operations.

## Logical

All inputs and outputs for logical blocks are boolean typed.

### AND

The AND block has 2 inputs and 1 output.

`If both inputs are true, the output is also true.`

IN1 | IN2 | OUT |
---|---|---|

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

### OR

The OR block has 2 inputs and 1 output.

`If one of the inputs is true, the output is also true.`

IN1 | IN2 | OUT |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

### XOR

The XOR block has 2 inputs and 1 output.

`If one of the inputs is true, the output is also true. If both the inputs are true, the output is false.`

IN1 | IN2 | OUT |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

### NOT

The NOT block has 1 input and 1 output.

`If the input is true, the output is false. If the input is false, the output is true.`

IN | OUT |
---|---|

0 | 1 |

1 | 0 |

### NAND

The NAND block has 2 inputs and 1 output.

`The NAND block is an AND block connected to a NOT block.`

IN1 | IN2 | OUT |
---|---|---|

0 | 0 | 1 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

### NOR

The NOR block has 2 inputs and 1 output.

`The NOR block is an OR block connected to a NOT block.`

IN1 | IN2 | OUT |
---|---|---|

0 | 0 | 1 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 0 |

### SR (Set/Reset)

The SR (Set/Reset) block has 2 inputs and 1 output.

`The SR block is a memory block with set priority.`

The SR block is also sometimes called a flip-flop function block. OUT remembers that SET1 was set to true, until RESET is set to true. The SR block has set priority, meaning that if both inputs are true, then the output is set.

### RS (Reset/Set)

The RS (Reset/Set) block has 2 inputs and 1 output.

`The RS block is a memory block with reset priority.`

The RS block works similarly to the SR block. OUT remembers that SET was set to true, until RESET1 is set to true. The RS block has reset priority, meaning that if both inputs are true, then the output is reset.

### JK

The JK block has 5 inputs and 2 outputs.

```
- If the inputs J and K are different, the output Q takes the value of J.
- If both inputs are true, the output Q toggles its state.
- If both inputs are false, the output Q holds its state.
- It functions asynchronously (latch) when CLK input is disconnected.
- When CLK is connected, inputs are read on the CLK rising edge and ouputs are set on CLK falling edge.
```

INPUTS | |
---|---|

J (SET) | Set condition. |

CLK | Clock. |

K (RESET) | Reset condition. |

PRESET | Sets the output. |

CLEAR | Clears the output. |

OUTPUTS | |
---|---|

Q | Memory value. |

Q | Negated memory value. |

### TP (Pulse Timer)

The TP block has 2 inputs and 2 outputs. This block is called a Pulse Timer because it generates a pulse for a preset length of time.

```
- When IN is set to true, the output Q will be set for PT time.
- ET records the elapsed time Q has been active for.
```

INPUTS | |
---|---|

IN | Input value. |

PT | Preset time. It is the time you want the pulse at Q to be. |

OUTPUTS | |
---|---|

Q | Output value. |

ET | Elapsed time. It is the time Q has been active for. |

### RTRIG

The RTRIG block has 1 input and 1 output.

```
- The RTRIG block detects a transition of the CLK value from false to true.
- On the CLK rising edge, OUT is set to true.
- OUT is set to true for a single time step before being reset.
```

### FTRIG

The FTRIG block has 1 input and 1 output.

```
- The FTRIG block detects a transition of the CLK value from true to false.
- On the the CLK falling edge, OUT is set to true.
- OUT is set to true for a single time step before being reset.
```

### SEL (Binary Selection)

The SEL block has 3 inputs and 1 output. This block is sometimes called binary selection because it allows you to select between the two input values, which then get assigned to the output.

```
IF G == false THEN
OUT = IN0
ELSE
OUT = IN1
ENDIF
```

INPUTS | |
---|---|

G | The input condition. |

IN0 | 1st input value. |

IN1 | 2nd input value. |

OUTPUTS | |
---|---|

OUT | The output value. |

### MUX (8-Bit Multiplexer)

The MUX block has 9 inputs and 1 output.

`OUT = INK`

INPUTS | |
---|---|

K | The input condition. |

IN0 | 1st input value. |

IN1 | 2nd input value. |

IN2 | 3rd input value. |

IN3 | 4th input value. |

IN4 | 5th input value. |

IN5 | 6th input value. |

IN6 | 7th input value. |

IN7 | 8th input value. |

OUTPUTS | |
---|---|

OUT | The output value. |

## Relational

### < (Less Than)

The < block has 2 inputs and 1 output. OUT is true if IN1 is less than to IN2, false otherwise.

```
IF IN1 < IN2 THEN
OUT = true
ELSE
OUT = false
ENDIF
```

### ≤ (Less Than Or Equal To)

The ≤ block has 2 inputs and 1 output. OUT is true if IN1 is less than or equal to IN2, false otherwise.

```
IF IN1 <= IN2 THEN
OUT = true
ELSE
OUT = false
ENDIF
```

### = (Equality)

The = block has 2 inputs and 1 output. OUT is true if IN1 is equal to to IN2, false otherwise.

```
IF IN1 == IN2 THEN
OUT = true
ELSE
OUT = false
ENDIF
```

### ≠ (Inequality)

The ≠ block has 2 inputs and 1 output. OUT is true if IN1 is not equal to IN2, false otherwise.

```
IF IN1 != IN2 THEN
OUT = true
ELSE
OUT = false
ENDIF
```

### > (Greater Than)

The > block has 2 inputs and 1 output. OUT is true if IN1 is greater than to IN2, false otherwise.

```
IF IN1 > IN2 THEN
OUT = true
ELSE
OUT = false
ENDIF
```

### ≥ (Greater Than Or Equal To)

The ≥ block has 2 inputs and 1 output. OUT is true if IN1 is greater than or equal to IN2, false otherwise.

```
IF IN1 >= IN2 THEN
OUT = true
ELSE
OUT = false
ENDIF
```

## Arithmetic

### ADD

The ADD block has 2 inputs and 1 output.

`OUT = IN1 + IN2`

### SUBTRACT

The SUBTRACT block has 2 inputs and 1 output.

`OUT = IN1 - IN2`

### MULTIPLY

The MULTIPLY block has 2 inputs and 1 output.

`OUT = IN1 * IN2`

### DIVIDE

The DIVIDE block has 2 inputs and 1 output.

`OUT = IN1 / IN2`

## Math

### ABS

The ABS block has 1 input and 1 output.

`Calculates the absolute value of the input.`

### SIN

The SIN block has 1 input and 1 output.

`Calculates the sine of the input (in radians).`

### COS

The COS block has 1 input and 1 output.

`Calculates the cosine of the input (in radians).`

### TAN

The TAN block has 1 input and 1 output.

`Calculates the tangent of the input (in radians).`

### ASIN

The ASIN block has 1 input and 1 output.

```
Calculates the inverse sine of the input (in radians).
The domain of the input is [-1, 1].
The range of the output is [-π/2, π/2] or [-90°, 90°].
```

### ACOS

The ACOS block has 1 input and 1 output.

```
Calculates the inverse cosine of the input (in radians).
The domain of the input is [-1, 1].
The range of the output is [0, π] or [0, 180°].
```

### ATAN

The ATAN block has 1 input and 1 output.

```
Calculates the inverse tangent of the input (in radians).
The domain of the input is all real numbers.
The range of the output is [-π/2, π/2] or [-90°, 90°].
```

### ATAN2

The ATAN2 block has 2 inputs and 1 output.

`Calculates the angle in the plane (in radians) between the positive x-axis and the ray from (0,0) to (X,Y).`

INPUTS | |
---|---|

Y | 1st input value. |

X | 2nd input value. |

### SQRT

The SQRT block has 1 input and 1 output.

`Calculates the square root of the input. The domain of the input {x ∈ R ∣ x >= 0}.`

### EXP

The EXP block has 1 input and 1 output.

`Calculates e raised to the power of the input.`

### EXPT

The EXPT block has 2 inputs and 1 output.

`Calculates BASE raised to the power of EXP.`

INPUTS | |
---|---|

BASE | The base input value. |

EXP | The exponent input value. |

### LOG

The LOG block has 1 input and 1 output.

`Calculates the base e logarithm of the input.`

### LOG10

The LOG10 block has 1 input and 1 output.

`Calculates the base 10 logarithm of the input.`

### FLOOR

The FLOOR block has 1 input and 1 output.

`Calculates the largest integer smaller to or equal to the input.`

### CEIL

The CEIL block has 1 input and 1 output.

`Calculates the smallest integer greater to or equal to the input.`

### ROUND

The ROUND block has 1 input and 1 output.

`Rounds the input to the nearest integer.`

### TRUNC

The TRUNC block has 1 input and 1 output.

`Calculates the integer part of the input by removing any fractional digits.`

### SIGN

The SIGN block has 1 input and 1 output.

```
IF IN < 0 THEN
OUT = -1
ELSEIF IN > 0 THEN
OUT = 1
ELSE
OUT = 0
ENDIF
```

### MOD

The MOD block has 2 inputs and 1 output.

`Calculates the remainder left over when IN1 is divided by IN2.`

### MIN

The MIN block has 2 inputs and 1 output.

`Calculates the smaller of two inputs.`

### MAX

The MAX block has 2 inputs and 1 output.

`Calculates the larger of two inputs.`

### LIMIT

The LIMIT block has 3 inputs and 1 output.

```
IF IN < MIN THEN
OUT = MIN
ELSEIF IN > MAX THEN
OUT = MAX
ELSE
OUT = IN
ENDIF
```

### LERP

The LERP block has 3 inputs and 1 output. It performs a linear interpolation between two values (MIN and MAX). The input T ranges from 0 to 1.

`OUT = (1 - T) * MIN + T * MAX`

INPUTS | |
---|---|

T | The interpolation value in [0, 1]. |

MIN | The 1st input value. |

MAX | The 2nd input value. |

### SHL

The SHL block has 2 inputs and 1 output. It performs a bitwise shift left on the input.

`OUT = IN << SHIFT`

INPUTS | |
---|---|

IN | The input value. |

SHIFT | The number of bits to shift. |

### SHR

The SHR block has 2 inputs and 1 output. It performs a bitwise shift right on the input.

`OUT = IN >> SHIFT`

INPUTS | |
---|---|

IN | The input value. |

SHIFT | The number of bits to shift. |

### ROL

The ROL block has 2 inputs and 1 output. It performs a bitwise left rotation on the input value.

`OUT = (IN << ROT) | (IN >> (32 - ROT))`

INPUTS | |
---|---|

IN | The input value. |

ROT | The number of bits to rotate. |

### ROR

The ROR block has 2 inputs and 1 output. It performs a bitwise right rotation on the input value.

`OUT = (IN >> ROT) | (IN << (32 - ROT))`

INPUTS | |
---|---|

IN | The input value. |

ROT | The number of bits to rotate. |

## Timers

### TON (On Timer Delay)

The TON block has 2 inputs and 2 outputs.

```
- The TON block sets the output Q to true after the preset time PT.
- It starts on the rising edge (false to true) transition of IN.
- It resets on the falling edge (true to false) transition of IN.
- ET records the elapsed time since the timer was started.
```

INPUTS | |
---|---|

IN | Start/reset signal. |

PT | Preset time. It is the time after which Q is set to true. |

OUTPUTS | |
---|---|

Q | Output value. |

ET | Elapsed time. |

Compared to the Pulse Timer (PT), instead of setting the time for the pulse, it is used to set a delay for the pulse.

### TOF (Off Timer Delay)

The TOF block has 2 inputs and 2 outputs.

```
- The TOF block sets the output Q to false after the preset time PT.
- It starts on the falling edge (true to false) transition of IN.
- It resets on the rising edge (false to true) transition of IN.
- ET records the elapsed time since the timer was started.
```

INPUTS | |
---|---|

IN | Start/reset signal. |

PT | Preset time. It is the time after which Q is set to false. |

OUTPUTS | |
---|---|

Q | Output value. |

ET | Elapsed time. |

When the input IN is set to true, the output Q will be set. If the input IN stays true, the output Q stays true until the preset time PT has elapsed. After that time, the output Q will be turned off.

## Counters

### CTU (Counter Up)

The CTU block has 3 inputs and 2 outputs.

```
- When CU is set to true, it counts up a number.
- The output Q is set to true when the counter reaches the preset value PV.
- The RESET input resets the counter to 0.
- The CV output gets the current value of the counter.
```

INPUTS | |
---|---|

CU | Counts up. |

RESET | Resets the counter, setting CV to 0. |

PV | Preset value. |

OUTPUTS | |
---|---|

Q | Q is true when CV equals PV, false otherwise. |

CV | Current value. |

### CTD (Counter Down)

The CTD block has 3 inputs and 2 outputs.

```
- When CD is set to true, it counts down a number.
- The output Q is set to true when the counter reaches 0.
- The LOAD input loads the counter to the preset value PV.
- The CV output gets the current value of the counter.
```

INPUTS | |
---|---|

CD | Counts down. |

LOAD | Loads the counter, setting CV to PV. |

PV | Preset value. |

OUTPUTS | |
---|---|

Q | Q is true when CV equals 0, false otherwise. |

CV | Current value. |

### CTUD (Counter Up/Down)

The CTUD block has 5 inputs and 3 outputs.

`The CTUD block implements both the CTU and CTD counters.`

INPUTS | |
---|---|

CU | Counts up. |

CD | Counts down. |

RESET | Resets the counter, setting CV to 0. |

LOAD | Loads the counter, setting CV to PV. |

PV | Preset value. |

OUTPUTS | |
---|---|

QU | QU is true when CV equals PV, false otherwise. |

QD | QD is true when CV equals 0, false otherwise. |

CV | Current value. |

## Controllers

### PID

The PID block has 5 inputs and 1 output.

```
The PID block implements a proportional-integral-derivative controller.
The default values of the inputs PV, SP, KP, KI, KD is 0.
```

INPUTS | |
---|---|

PV | Measured process variable. |

SP | Setpoint. |

KP | Proportional gain. |

KI | Integral gain. |

KD | Derivative gain. |

OUTPUTS | |
---|---|

CV | Control variable. |

## Utilities

### DEGREES

The DEGREES block has 1 input and 1 output. It converts the input value from radians to degrees.

`OUT = (180 / π) * IN`

### RADIANS

The RADIANS block has 1 input and 1 output. It converts the input value from degrees to radians.

`OUT = (π / 180) * IN`