Tutorial 2

<< Click to Display Table of Contents >>

Navigation:  Examples > Control programs >

Tutorial 2

Previous pageReturn to chapter overviewNext page

Show/Hide Hidden Text

This exercise shows how a control program with alternative branches is created. The complete program is located int the file examples/sfc/tutorial2.rvw2.

 

The complete control program looks as shown in figure 1.

examples_sfc_tutorial2_sfc_full

Fig. 1: the complete control program

 

In Step1 the value of a is changed. Thus in every cycle of the program one of the Steps Step2, Step3 and Step4 will be executed. Step5 compares the results produced by the previous steps. After the 6th execution of Step5 the program is stopped. Otherwise it continues with Step1.

 

Create a new project

Create a new project by

selecting FileNew
pressing Ctrl + N
selecting the symbol for creating  a new project in the tool bar newproject.

 

The main program contains the steps Init and Step1.

examples_sfc_tutorial2_sfc_init

 

Create global Variables

First, create the following global variables:

timer
a
b
step2count
step3count
step4count
step5count

 

Assign the initial value -1 to "a". All other variables keep their initial value 0.

 

Program Step1

In this sub-program the global variable "a" is incremented by 1. To make sure that the value of "a" is always between 0 and 2, "a" will be calculated Modulo 3 and rewritten to "a". The value of "b" will just be set 0.

examples_sfc_tutorial2_sfc_step1

 

Create steps Step2, Step3 and Step4

Now the steps will be created next to each other in alternative branches. To do that, select the transition condition below Step1.

 

You can see that the transition condition is selected by a dashed line round the condition.

Now click on the symbol to add an alternative branch on the right side alternativebranch_right.

examples_sfc_tutorial2_sfc_step2_a

Expand the branching you have just created by selecting the transition condition on the right and selecting the "Alternative branch right" alternativebranch_right symbol again.

examples_sfc_tutorial2_sfc_step2_b

Now create three Steps in those three alternative branches and call them Step2, Step3 and Step4. To do that, select the entering condition of a branch and click on the "insert step after" step_transition_after symbol. Then assign a sub-program of the same name to each step. To do that, double-click on the step and enter the name for the sub-program in the following dialog box. Alternatively, you can create a new sub-program with the tool bar button "Create new subprogram" sdfdocument and assign it to the step.

examples_sfc_tutorial2_sfc_step2_c

The entering and exit conditions of all three alternative branches are false at the moment. Change the entering conditions to a == 0, a == 1 and a == 2. Use timer == 10 as exit condition for all branches. Finally, change the final jump's destination from Init to Step1.

examples_sfc_tutorial2_sfc_step2_d

If you start the main program now, the program will hang in Step2 because "a" is 0 during the first cycle and the global variable "timer" is not altered.

 

Program Step2, Step3 and Step4

The sub-programs assigned to the steps Step2 to Step4 are empty at the moment. The sub-program Step2 is shown below.

examples_sfc_tutorial2_sfc_step2_program

Every 200ms the Arbitrary Waveform Generator creates a pulse of 100ms width and height 1. The settings for the Arbitrary Waveform Generator are shown below.

examples_sfc_tutorial2_sfc_step2_program_puls

I.e. every 200ms there is a rising edge from 0 to 1. With every rising edge the counter increments its value by 1. After 2s the value will be 10. When the value of the counter is 10, the result of the comparison of the constant and the counter's value will be added to the current value of step2Count. As long as the comparision results in false, 0 is added. As soon as the comparision results in true, 1 is added. At the end of every calculation step of the sub-program, the transition condition below the step in the main program is evaluated. When the global variable "timer" has the value 10, the sub-program will be left.

 

The sub-programs of Step3 and Step4 are built equivalently. Select all in Step2 (Ctrl+A) and copy it to Step3 and Step4. The only difference consists in the fact that step3count respectively step4count are read and written.

 

Once you start the main program, Step2, Step3 and Step4 will be executed cyclically for 2s each.

 

Create and program Step5

To add a new step after the alternative branching, select the final jump and click on the symbol to add a step before step_transition_before. Now create a sub-program named Step5 and assign it to Step5 just created. Change the transition condition below Step5 to b>0 && timer == 10.

examples_sfc_tutorial2_sfc_step5

The sub-program Step5 is similar to Step2 to Step4. Copy Step2 to Step5 and change step2count to step5count. Beyond setting the global variables "timer" and "step5count" also a check is performed if the condition step2count >= step3count >= step4count is valid. If this is the case, the global variable "b" is set to 1. Otherwise "b" is 0. The condition must always be true when in a correct program execution because Step2, Step3 and Step4 are executed one after another because Step1 increments "a" by 1 in every cycle.

examples_sfc_tutorial2_sfc_step5_prg

If the main program is started now, Step5 remains active for 2s if "b" is greater than 0.

 

Create program termination and jump to Step1

Now the program should be terminated when the value of "step5count" has reached 6. To achieve this, insert an alternative branch below Step5. Select the transition condition below Step5 and klick on the symbol to insert an alternative branch on the left alternativebranch_left.  Select the new branch's transition condition (at the moment it is false) and click on the symbol to create a new jump jump_after. Change the transition condition to step5count == 6 and select TERMINATE as new jump destination.

The main program now looks as it was shown at the beginning.

By the way, the alternative branch containing the jump to TERMINATE must be left of the branch with the condition b>0 && timer == 10 because the initial conditions of alternative branches are evaluated from left to right. In the first 6 cycles the condition step5count == 6 is not fulfilled. So the second branch's condition is evaluated.

One run of the main program lasts 24s now.