17 ST Lab

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

17.

Using the Structured Text


Language in a Studio 5000 Logix
Designer Project

Before You Begin

About this Lab

The Studio 5000 Logix Designer application includes the ability to program logic in four different
programming languages, each of which is targeted at particular uses and applications. One of
these languages is the Structured Text (ST) programming language.
Structured Text (ST) includes many of the algorithmic structures which are well suited to
recursive mathematics or comparisons: FOR/NEXT, DO/WHILE, CASE, etc. It can also
represent mathematical expression in its native form as an equation. The similarity of Structured
Text to ‘C’ or ‘Basic’ makes it a good candidate for algorithmic processing that is specified from
‘Numerical Recipe’ texts or from pseudo-code specifications.
In this lab, you will:
• Create a Structured Text routine.
• Program a Structured Text routine.
• Test a Structured Text routine.

Duration
30 Minutes

Prerequisites
The following prerequisite knowledge is recommended in order to complete this lab:
• Level 1: Logix On Demand

© 2020 Rockwell Automation, Inc. All rights reserved. 17-1


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

Materials
This lab requires the following items. See the General Setup for more details.
☐ Hardware:
☐ PC-based programming workstation with access to the RAcbi learning
environment
☐ Software:
☐ Studio 5000 Logix Designer version 32

© 2020 Rockwell Automation, Inc. All rights reserved.


17-2 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

Create a Structured Text Routine

Scenario

We would like to create logic for sorting an array of values. We would also like the ability to
search for the location of a particular value within the array. In this section of the lab, you will
create a new Logix Designer project and add two ST routines into which you can program your
ST logic.

Practice
It’s your turn to try it:

1. Open the Studio 5000 environment.


2. Under the Create category, select New Project:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-3


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

3. Within the New Project dialog, select the Logix project type, the 1756-L85E controller,
name the project ST_Lab, and click the Next button:

4. Set the revision to 32, the chassis to 1756-A10, the controller slot number to 0, and click
the Finish button:

© 2020 Rockwell Automation, Inc. All rights reserved.


17-4 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

5. Within the Controller Organizer of the new project, right-click the MainProgram located
within the MainTask, expand Add, and select New Routine:

6. Within the New Routine dialog, Name the routine Sorting, select the Structured Text
Type, and click the OK button:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-5


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

7. Repeat steps 5 and 6 to create a second Structured Text routine named Binary_Search:

We now have a pair of ST routines into which we can program our logic.

© 2020 Rockwell Automation, Inc. All rights reserved.


17-6 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

Program a Structured Text Routine

Scenario

In this section of the lab, you will program an ST routine that sorts a 10-element array of double
integers into numerically ascending order. Furthermore, you will program a second ST routine
that will search the array for the location of a particular value.

Practice
It’s your turn to try it:
1. From within the Tasks folder of the Controller Organizer, open the Sorting routine of the
MainProgram located within the MainTask.

We are now ready to develop our sorting logic within the ST routine. The algorithm that we
will use is basically composed of two iteration loops that perform the sorting. In this case,
we will use FOR/DO loops.

2. On line 1 of the logic, type the keyword for and press the Tab key on your keyboard:

The Structured Text language editor includes blocks of preconfigured code that are
referred to as code snippets. Typing the keyword for a snippet and pressing the Tab key
causes the block of code to automatically appear. Once the snippet has been inserted, the
user may use the Tab key to cycle through the provided parameters, which are highlighted
in yellow. The currently selected parameter is highlighted in grey, and, when the user
begins typing, the entirety of the descriptive text will automatically be overwritten by the
user’s text.
In our case, the first parameter “count” is highlighted in grey. This is the indexing value for
the loop. Let’s use a tag named “i” to serve as our indexing tag.

3. To ease viewing of the text, use the zoom button in the upper left corner of the
language editor to zoom in on the logic as desired.

© 2020 Rockwell Automation, Inc. All rights reserved. 17-7


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

4. With count highlighted in grey, type i and press Tab on your keyboard:

Now we need to indicate the range through which we want the loop to iterate. In this case,
we will be sorting an array containing ten elements, so we will need for the loop to run from
an initial value of 0 through a final value of 9, for a total of ten iterations, one for each of the
array’s members.

5. With initial_value highlighted in grey, type 0 and press Tab on your keyboard:

6. With final_value highlighted in grey, type 9 and press Enter on your keyboard:

Notice that the cursor has automatically moved to inside of the loop so that we can begin
entering the code that will execute with each iteration of the loop. Furthermore, notice that
the cursor has been placed one tab to the right. Although these tabs do not have any effect
on how the logic functions, they do make the code easier to read.

© 2020 Rockwell Automation, Inc. All rights reserved.


17-8 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

Now that we have a loop that will iterate through each of the array elements, we need an
inner loop that will again iterate through each of the array elements, swapping the current
inner loop element with any outer loop elements that are less than it. So, the outer loop will
execute 10 times, and the inner loop will execute 10 times for each iteration of the outer
loop, for a total of 100 iterations.

7. Type the keyword for and press Tab on your keyboard:

For this inner loop, we will require an indexing tag separate from the outer loop’s indexing
tag of i. So, we will use the letter j.

8. Using the Tab key, cycle through and configure the code snippet parameters as j, 0, and 9,
respectively:

We need to compare the j element of the array to the i element of the array, and, if it is
greater, we will swap the two. For this comparison, we can use an IF construct.

© 2020 Rockwell Automation, Inc. All rights reserved. 17-9


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

9. Type the keyword if and press Tab on your keyboard:

10. With bool_expression highlighted in grey, type array[j]>array[i] and i<>j and press Enter
on your keyboard:

© 2020 Rockwell Automation, Inc. All rights reserved.


17-10 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

Now we need to create logic that will swap the two values. To do this, we will need a tag
into which we can temporarily store one of the values while we are making the swap. We
will call this tag “buffer”.

11. Type the code shown below:

We should note the color-coded, wavy lines beneath some of our logic. The green wavy
line indicates that there is a syntax error somewhere in that line of code. The red wavy line
indicates that the underlined word is not defined. In this case, we have several tags that
have not yet been created. So, let’s do that now.

© 2020 Rockwell Automation, Inc. All rights reserved. 17-11


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

12. Right-click i and create it as a DINT Local Tag:

© 2020 Rockwell Automation, Inc. All rights reserved.


17-12 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

13. Right-click j and create it as a DINT Local Tag:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-13


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

14. Right-click array and create it as a DINT Local Tag array containing 10 elements:

© 2020 Rockwell Automation, Inc. All rights reserved.


17-14 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

15. Right-click buffer and create it as a DINT Local Tag:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-15


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

16. Verify that your logic contains no errors:

Now that we have completed our sorting logic, we can move on to our search logic.

© 2020 Rockwell Automation, Inc. All rights reserved.


17-16 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

17. Close the Sorting routine.


18. From within the Controller Organizer, open the Binary_Search routine of the
MainProgram located within the MainTask.

For our search algorithm, we will search though halves of the sorted array, narrowing down
our search until we have located the desired element. So, we will require tags that
represent the first and last elements of the portion of the array that we are searching. Let’s
initialize them such that we begin our search with the entire array.

19. Type the logic shown below:

In this case, P represents the first element of the portion of the array we are searching, and
U represents the last element of the portion of the array that we are searching.

© 2020 Rockwell Automation, Inc. All rights reserved. 17-17


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

20. Create P and U as DINT Local Tags:

For this algorithm, we are going to employ a WHILE/DO loop that will continue to execute
as long as P is less than or equal to U.

21. Type while and press Tab on your keyboard:

© 2020 Rockwell Automation, Inc. All rights reserved.


17-18 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

22. With bool_expression highlighted in grey, type P<=U and notice the contextual dropdown
menu that appears:

As we can see, the popup is providing suggestions based upon the letter U. So, we are
offered the existing tag buffer, the truncate function, and the existing tag U. Of course, we
want the existing tag U.

23. Press Enter on your keyboard to select U, and press Enter again:

In addition to the first and last elements of the portion of the array that we are searching,
we need to define the middle element so that we can compare the desired value to its
value and, if they do not match, we can divide the portion of the array that we are
searching in half and eliminate the half wherein the value does not lie.

© 2020 Rockwell Automation, Inc. All rights reserved. 17-19


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

24. Type M:=(P+U)/2; and press Enter on your keyboard:

25. Right-click M and Create it as a DINT Local Tag:

© 2020 Rockwell Automation, Inc. All rights reserved.


17-20 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

We may notice a yellow bar next to the line numbers of our code. This yellow bar indicates
a change to the code that has not yet been verified.

26. Click the Verify Routine button and note the change to the bar:

Let’s continue our logic. Now that we have identified the middle element of the portion of
the array that we are searching, we need to compare its value to the value we are seeking,
which we will store within a tag named Number. If they are equal, we have found our value
and the current value of M is written to Position, the tag wherein we want to store the
answer. So, we require an IF/THEN construct to perform the comparison.
27. Type if and press tab on your keyboard:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-21


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

28. With bool_expression highlighted in grey, type array[M]=Number and press Enter on
your keyboard:

29. Right-click Number and Create it as a DINT Local Tag:

© 2020 Rockwell Automation, Inc. All rights reserved.


17-22 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

30. Type Position:=M; within the if statement:

31. Right-click Position and Create it as a DINT Local Tag:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-23


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

Now, if the value in the M element of the array does not equal the value we are seeking,
we need to divide the portion of the array that we are searching in half and discard the half
of that portion where the desired value cannot exist.

32. Add the logic shown below:

So, the loop continues dividing the portion of the array that we are searching in half until
the middle element’s value equals the value we are seeking, at which point, the middle
element’s location is provided as the solution.
Our algorithms are complete. However, as a last step, we need to create a structured text
main routine from where the Sorting and Binary_Search routines can be selected using a
CASE statement.

© 2020 Rockwell Automation, Inc. All rights reserved.


17-24 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

33. Close the Binary_Search routine.


34. Within the Controller Organizer, right-click the MainRoutine of the MainProgram and
select Delete.
35. Right-click the MainProgram, expand Add, and select New Routine:

36. Within the New Routine dialog, Name the routine Main, select the Structured Text Type,
select the Main Assignment, and click the OK button:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-25


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

37. Open the new Main routine.

We would like to create logic that will allow the user to select which routine to run by
entering a value. To do this, we will use a CASE/OF construct.

38. Type case and press Tab on your keyboard:

39. With numeric_expression highlighted in grey, type routine_choice and press Tab on
your keyboard:

The tag routine_choice must be created before we specify our selectors, or an error will
occur. So, let’s create it now.

© 2020 Rockwell Automation, Inc. All rights reserved.


17-26 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

40. Right-click routine_choice and Create it as a DINT Local Tag:

41. For the first selector, type 1: JSR(Sorting); :

© 2020 Rockwell Automation, Inc. All rights reserved. 17-27


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

42. For the second selector, type 2: JSR(Binary_Search); :

43. Delete the else statement:

Now, if the user writes a value of 1 to the routine_choice tag, then the Sorting routine will
run. If they write a value of 2 to the routine_choice tag, then the Binary_Search routine will
run. This completes our logic.

44. Close the Main routine.


45. Verify your project (there should be no errors).
46. Save your project.

© 2020 Rockwell Automation, Inc. All rights reserved.


17-28 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

Test a Function Block Diagram Routine

Scenario

In this section of the lab, you will test the operation of your Structured Text logic.

Practice
It’s your turn to try it:
1. In the top ribbon menu, expand Communications and select Who Active.
2. Expand the EtherNet driver, download to the 1756-L85E controller, and place the
controller into the Remote Run mode:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-29


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

To test the operation of our sorting algorithm, we need to fill the array tag with random
values.
3. From within the Controller Organizer, open the Parameters and Local Tags of the
MainProgram located within the MainTask.
4. Go to the Monitor Tags tab and expand array:

5. Enter the values shown below into the elements of the array:

© 2020 Rockwell Automation, Inc. All rights reserved.


17-30 L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

We are now ready to run our logic. First, we must sort the array.
6. Type 1 into the Value for the routine_choice tag and press Enter on your keyboard.
7. Type 0 into the Value for the routine_choice tag and press Enter on your keyboard.
8. Verify that array has indeed been sorted:

Now that our array has been sorted, we can search for the location of a particular value.
Let’s see if we can find the location of 983.
9. Type 983 into the Value for the Number tag and press Enter on your keyboard.
10. Type 2 into the Value for the routine_choice tag and press Enter on your keyboard.
11. Type 0 into the Value for the routine_choice tag and press Enter on your keyboard.
12. Verify that 7 has been returned as the Position:

© 2020 Rockwell Automation, Inc. All rights reserved. 17-31


L2LST - Rev. July 2020
Using the Structured Text Language in a Studio 5000 Logix Designer Project

This is just some very simple logic, and not very robust. For example, what would happen
if we searched for a value that did not exist within our array?
Nevertheless, it does demonstrate the value of the Structured Text programming language.
At this point, we would simply need to expand our logic to provide for the above-mentioned
eventuality. However, we will stop here.

13. Take your project offline.


14. Save your project.
15. Close your project.

© 2020 Rockwell Automation, Inc. All rights reserved.


17-32 L2LST - Rev. July 2020

You might also like