Program Organization Unit (POU) Coming Soon
POU (Program Organization Unit) is a fundamental building block used to organize and structure the code. POUs are the primary means by which code is modularized, encapsulated, and reused. The concept of POUs is crucial for creating scalable, maintainable, and organized software.
This section will define all the supported POU features within Launchpad.
Types of POUs
There are three main types of POUs in IEC 61131-3:
Programs: These are large, independent blocks of code that typically represent the main control tasks or applications. A program POU can contain local variables and might call upon other POUs, such as functions and function blocks, to perform specific tasks.
Functions (FC): Functions are POUs that perform a specific calculation or logic operation and return a single output based on the input parameters. Functions are stateless, meaning they do not retain any internal state between calls. An example might be a function to calculate the average of two numbers.
Function Blocks (FB): Function blocks are similar to functions in that they are called with parameters and can return results. However, unlike functions, function blocks can maintain state information between calls. This means they can hold onto data, making them useful for implementing control algorithms, timers, counters, and more complex logic that relies on historical data or needs to maintain a state over time.
Characteristics of POUs
- Reusability: POUs can be written once and used multiple times throughout a program or across different projects, promoting code reuse and reducing redundancy.
- Encapsulation: Each POU encapsulates specific functionality, making the program structure clearer and more manageable by hiding its detailed implementation.
- Modularity: The use of POUs enables a modular approach to programming, where complex tasks are broken down into smaller, manageable pieces.
- Hierarchy and Organization: POUs can call other POUs, allowing for a hierarchical structure that organizes program logic in a logical and understandable manner.
Example
Here's a basic example to illustrate a Function POU in Structured Text:
FUNCTION AddNumbers : REAL
VAR_INPUT
A : REAL;
B : REAL;
END_VAR
AddNumbers := A + B; // Adds A and B and returns the result
END_FUNCTION
This example defines a simple function POU named AddNumbers
that takes two real numbers as input and returns their sum. Such a POU could be reused wherever addition of two numbers is required in the program.
Program
A Program is a type of POU (Program Organization Unit) that can contain variables, function calls, and logic to perform a specific task or control process.
Example
PROGRAM MyProgram
VAR
counter : INT := 0;
END_VAR
counter := counter + 1;
END_PROGRAM
Function
A Function is a POU that performs a specific calculation or operation and returns a single value. Functions are stateless and cannot retain values between calls.
Example
FUNCTION Add : INT
VAR_INPUT
A : INT;
B : INT;
END_VAR
Add := A + B;
END_FUNCTION
FunctionBlock
A Function Block is a POU that can contain its own variables and state, allowing it to retain information between calls. Useful for implementing complex logic that requires persistence of data.
Example
FUNCTION_BLOCK Counter
VAR
Count : INT := 0;
END_VAR
Count := Count + 1;
END_FUNCTION_BLOCK
Action
Actions are defined within Function Blocks and are blocks of code that execute based on certain conditions or triggers.
Example
FUNCTION_BLOCK MyFB
ACTION MyAction
MyFB.Count := MyFB.Count + 1;
END_ACTION
END_FUNCTION_BLOCK
Declarations
Declarations define the type and scope of variables, function blocks, functions, and other elements within a POU. They are essential for specifying the data and operations a program works with.
Sections below will define the different types of declarations.
VAR
Declares local variables that are accessible within the scope of a Program Organization Unit (POU) like a program, function block, or function.
Example
VAR
A : INT;
END_VAR
VAR_INPUT
Declares input parameters for functions and function blocks. These are read-only within the POU.
Example
FUNCTION Add : REAL
VAR_INPUT
A : REAL;
B : REAL;
END_VAR
Add := A + B;
END_FUNCTION
VAR_INPUT "{ref}"
Declares input parameters as references. This allows the function or function block to access the actual variable passed as an argument, not a copy of it.
Example
FUNCTION_BLOCK Increment
VAR_INPUT
Value : INT; {ref}
END_VAR
Value := Value + 1;
END_FUNCTION_BLOCK
VAR_OUTPUT
Declares output parameters for functions and function blocks. These variables are used to return values from the POU.
Example
FUNCTION_BLOCK Calculate
VAR_OUTPUT
Result : REAL;
END_VAR
END_FUNCTION_BLOCK
VAR_IN_OUT
Declares parameters that act as both input and output for a function or function block. These are variables that the POU can read and write to, allowing it to modify the original variable passed as an argument.
Example
FUNCTION_BLOCK Modify
VAR_IN_OUT
Parameter : REAL;
END_VAR
Parameter := Parameter * 2;
END_FUNCTION_BLOCK