In this recipe, we will explain a typical-use case for Structured Text (ST) programming. In our exercise, we will develop a simple Structured Text program for calculating the flow rate based on our valve position.
First, we will need to declare our new routine. Right-click on MainProgram of the Controller Organizer and select New Routine.
In the New Routine form that appears, enter or select the following:
Name:
CALCULATIONS
Description:
Process Calculations
Type: Structured Text
Then click on OK.
In order for our newly created routine to be executed with each scan of the PLC, we will need to add a reference to it in MainRoutine that is executed with each Scan of the MainTask. Repeat Steps 3 to 7 of the Building Ladder Diagram programs recipe (or copy and paste the existing JSR), but use the routine name
CALCULATIONS
in the JSR element.We will use a simple linear equation to calculate the
Flow
value based on our Valve position. Enter in the following structured text comment and simple formula code as shown in the following screenshot://Linear flow calculation based on valve position FC1001_FLOW := FC1001_PV*0.83823;
The tag
FC1001_FLOW
must be added to our controller, so right-click on theFC1001_FLOW
tag in our structured text and select New Tag "FC1001_FLOW".On the New Tag form, enter the following values:
Name:
FC1001_FLOW
Description:
Flow estimated based on valve position
Type: REAL
Scope: FirstController
Then click on OK.
Structured Text (ST) is similar to traditional programming languages, such as Pascal, C, and BASIC, except that it will run continuously from start to finish as if it were contained within a loop. Structured Text is a great place to put complex formulas that would be difficult to implement using Ladder Logic.
More information on ST can be found in the Rockwell publication Logix5000 Controllers Structured Text available at http://literature.rockwellautomation.com/idc/groups/literature/documents/pm/1756-pm007_-en-p.pdf.