TLDW logo

Training - How to use the Structure Text (ST) in straton ?

By straton-plc

Summary

## Key takeaways - **ST Expressions Evaluate Left-to-Right**: Each statement describes an action and may include evaluation of complex expressions. An expression is evaluated from the left to the right according to the default priority order of operators. [00:27], [00:50] - **Operator Priority: Negation Highest**: Here you see the priority order of operators from the top priority to the lowest priority: first the negation, down to the logical XOR. [00:53], [01:12] - **Loops Risk Infinite Blocking**: Be careful of the infinite loop that blocks the target cycle. Do not test the state of an input in the condition as the input will not be refreshed before the next cycle. [01:48], [01:58] - **Function Blocks Need Instances**: To call a function block in ST language you have to declare an instance of the function block and use the instance name as instruction. The outputs of the function block are stored in the instance and you can access them as for the members of a structure, with the name of the instance dot the name of the output. [03:20], [03:54] - **MP3 Volume Uses R_TRIG + PLS**: We use R_TRIG for rising edge detection and PLS pulse generator so the level is increased or decreased only once when the corresponding button is pressed, to repeat every second if the button stays pressed. [04:29], [05:23] - **Mute Toggles with FLIPFLOP + SEL**: We use the FLIPFLOP function block for the mute button and SEL binary selector to choose between the output of the CTUD block or 0 depending on the mute state. [11:28], [13:12]

Topics Covered

  • Loops Risk Cycle-Blocking Infinite Loops
  • Function Blocks Need Instances
  • Rising Edges Drive Repeat Actions
  • Pulse Generators Enable Hold-to-Repeat

Full Transcript

Hello, in this video we will learn the Structured Text Language, also called the ST language.

I will first present the ST language: how it works, how it is written, and then we will make an example. The ST language is a textual language.

How do we use it? Each statement describes an action and may include evaluation of complex expressions. An expression is evaluated: - From the left to the right - According to the default priority order of operators - The default priority can be changed thanks to the use of parenthesis Arguments of an expression can be: - Declared variables - Constant expressions - Function calls

Here you see the priority order of operators from the top priority to the lowest priority: first the negation, down to the logical XOR. The ST language is perfect to use conditional statements IF, THEN, ELSE, ELSIF, END_IF. One or several ELSIF are allowed. The syntax is as

presented on this slide: first the IF followed by a Boolean expression and THEN, the statement, one or more ELSIF or an ELSE with their statements, and to close the condition END_IF.

The CASE instruction allows to switch between enumerated statements according to an expression.

The selector can be any integer, enumerated or string.

The WHILE, DO, END_WHILE instruction repeats a list of statements.

When using a While, the condition is evaluated before the statements.

So be careful of the infinite loop that blocks the target cycle. Do not test the state of an input in the condition as the input will not be refreshed before the next cycle.

The REPEAT, UNTIL, END_REPEAT instruction repeats a list of statements. In that case the condition is evaluated after the statement. As for the while instruction, be careful of the infinite loop as loop instructions may lead to infinite loops that block the target cycle.

The FOR, TO, BY, END_FOR instruction allows to make an iteration of statement execution.

The BY statement can be omitted, as the default value is 1. As for the While and Repeat instruction, the for instruction is a loop instruction and can lead to an infinite loop that block the target cycle.

Note that the FOR loop waits for a double integer in the condition.

How do we call a function in ST language? We enter its name, followed by the input parameters written between parenthesis and separated by comas. A function call may be inserted into any complex expression. A function call can be used as an input parameter of another function. Below is the example of the MAX function.

another function. Below is the example of the MAX function.

One of the inputs of this MAX function can be the result of the RAND function for instance.

The call of a function block is slightly different from the one of a function. To call

a function block in ST language you have to - declare an instance of the function block - use the instance name as instruction, followed by the input parameters written between parenthesis and separated by comas. - The outputs of the function block are stored in the instance and you can access them as for the members of a structure, with the name of

the instance dot the name of the output. For example, in the code presented here, we call the function block CTU, its instance is called MyCTU, we give its inputs into parenthesis.

To have access to its outputs we call them using the name of the instance, here MyCTU dot the name of the output, here Q or CV, so MyCTU.Q, and MyCTU.CV.

How do we add comments in ST? Comments begin with parenthesis-star and end with star-parenthesis.

You can put comments anywhere in the program, and they can be written on several lines. But they

cannot be nested. It is also possible to comment only the end of a line using double slash.

Let’s make an exercise to practice this ST language. We will take the same exercise as for the FBD language: the volume of an MP3 player. We will have 3 boolean variables: UP, DN and MUTE which are pushbuttons, and one double integer variable LEVEL the output level from 0 to 10.

The constraints are that the level is increased or decreased only once when the corresponding button is pressed, to repeat every second if the button stays pressed and the MUTE is a real one: I have to press it to mute and to press it again to unmute.

We will use the following functions to program this MP3 player: - R_TRIG: rising edge detection - PLS: pulse generator - CTUD: up and down counter - FLIPFLOP: change on rising edge - SEL: binary selector So first I open a Straton project. I add a new program,

so right-click, insert new program, I call it mp3_player_st, I select the ST language, and I click on OK. Then I open the program.

I will first deal with the behavior of the UP button. I can add a comment to explain what my program is doing. Parenthesis-star Up Button, star-parenthesis to end the comment.

We want that each time the button is pressed, the level increases, so we have to detect the rising edge of the button, so we use a R_TRIG function block. I go into the function library on the right and I look for the R_TRIG block. I drag-n-drop it into the program editor.

I see that straton has defined the name of the instance Inst_R_TRIG in the variable editor.

I set the input to the variable UP. I do not forget the semi-colon at the end of the line.

Then I press enter, and straton detects that the variable up does not exist, so it proposes to declare it. It is a Boolean varaible, then I click on Yes.

The exercise also asks to continue to increase the level every second as long as the UP button is pressed. To do this we will use the PLS block. I look for it in the block library,

is pressed. To do this we will use the PLS block. I look for it in the block library, I drag-n-drop it in my program. I see that there are two inputs, if I don’t know what the block does, or how it works, I can select it and press the F1 key to get the help.

I see that the first input is the enabling command, in our case the up variable, and the second input is the signal period, so 1 second in our example.

So I put the UP variable for the first input and a cycle time of 1 second.

And I add a semi-colon at the end of the line.

I do the same for the down variable. So I add a comment for the Down button.

I add an R_Trig function with this time the down variable.

Semi-colon, enter, I declare the down variable. Then I add a PLS function block with the down variable, still a cycle time of one second, semi-colon, enter.

Then I add the CTUD block that will increase or decrease an output variable, depending on its inputs. I will add counter for example as a comment. I add the CTUD function block.

its inputs. I will add counter for example as a comment. I add the CTUD function block.

The first input increases the output each time it is true, so I add the logical or combination of the output of the instances of the R_Trig and PLS blocks for the UP variable. So I enter the name of

the instance dot the output. Here I have a list of the variable I can select “.Q”. Or I can enter the instance of the PLS, I can click on Ctrl-space and I have a list of all the variable names

that start with “Inst_”. I can select the “Inst_PLS” and I select its output Q.

The second input of the CTUD decreases the output each time it is true, so I add the logical combination

of the R_TRIG of the down button or the PLS of the down button. So “Inst_R_Trig1.Q or Inst_PLS1.Q”.

The reset input resets the counter, as we don’t need it, I set it to false.

If the LOAD variable is true, it sets the counter to its maximum value. In our case we do not need it, so I put it to false. Finally the PV input is the maximum value of the counter. The exercise is to have a volume from 0 to 10, so I set the maximum to 10. And I

add the semi-colon at the end of the line. Now we will focus on the mute button.

We use the FLIPFLOP function block.

I drag-n-drop it. The first input is the swap command, so it is the MUTE variable in our case.

We don’t use the reset variable so I set it to false. Semi-colon, enter,

and the mute variable does not exist yet, so I declare it, it is a Boolean, Yes.

And finally to get the volume of the MP3, we will use the SEL function to choose between the output of the CTUD block or 0, depending on the Mute button. I look for the SEL function.

As SEL is a function, I write the variable Level, double point equal, to get the output of the SEL function. Then I enter the inputs of the function. The first input is the output of

the FlipFlop, so “Inst_FLIPFLOP.Q”. then it will switch between the output of the CTUD, .CV, the current value. Here I have more than one output in my block CTUD,

.CV, the current value. Here I have more than one output in my block CTUD, I select the one I want, in this case CV. And the other input is 0 because we will switch between the current value of the volume or 0. Semi-colon, enter, and the variable level

does not exist yet, and we said it is a double integer. I click on OK.

Then I build the project, I have no error, I start the simulation and start the project.

If I set the UP variable to true by pressing the space bar, I see that my level is increasing, if I stop pressing it, it stops. If I press the down button by switching it from false to true for example this time on double clicking on it and selecting True.

I see that the level here is decreasing, if I stop it stops. And if I click on the mute button, I see that my level is 0, and if I click again on the MUTE button, it is switched back to 2. So the expected behavior is correct.

That’s it for the ST module. Thank you for watching this video!

Loading...

Loading video analysis...