Step 4 - Create Function Block Types

This page is part of a guide that gives a walkthrough over the major 4DIAC features.

  1. Overview
  2. Use 4DIAC locally
  3. Create a distributed application
  4. Deploy remotely
  5. Create your own Function Blocks (YOU ARE HERE!)
  6. Other basic features
  7. Advanced Features [optional]

In this tutorial, you will learn how to create your own Function Block. There are several different types of function blocks, we will show how to create a Basic, a Simple, a Composite and a Service Function Block. The new FBs are then available in the FB Palette for use in the Systems' Applications.
New function blocks need to be created when the needed functionality doesn't exist or multiple FBs should be combined into just one FB. First you have to define the interface with its input and output events and data. Afterwards define the FB internals, which are defined by the kind of FB.
We implement the Blinking functionality inside all three different FB types to introduce their creation process. All 4 types do the same thing in different ways. At an event input, the boolean data output will be toggled and the event output will send the message that the value has changed.

Create new Function Block by selecting:
File → New → New Type

Create a Basic Function Block

Follow the instructions in the creation wizard. For this tutorial select BlinkTest as the parent folder. Choose a name for the new FB, we name it BasicTest. Afterwards select the type of FB you want to create, in this case we choose Basic. The Function Block Type Editor (FBT Editor) is opening after clicking Finish.

New Type Wizard - Create a Basic Function Block
  1. Your new created type appears in your System's Type Library.
  2. The central editing area shows the FB part you are currently working on. Initially it shows the interface of the Function Block.
  3. The Palette shows all elements you can add to the FB interface.
  4. The tabs below the central editing area identify the current part of the Function Block you're editing. The picture shows the Interface tab selected.
  5. The properties below show the information about the Function Block.
General View after Creating a Basic Function Block

Change the interface

The Basic FB already has some inputs and outputs which are mostly used. If you need another just drag and drop it from the Palette into the FB or right clicking at the FB to set another input or output. Of course you can also delete them by pressing the Delete key or right click → Delete.

In this example, we are doing a Blinking Function Block. This should have then one input event that triggers the change, one output event as an indicator of a change, and a boolean data output for the state.
By default, the Boolean output is set to false and remembers the state until a new event comes in and converts the boolean to true until the next input arrives.
Right now the Basic FB has more inputs and outputs then we need.

  1. Select the INIT event, and delete it. Do the same for QI and INITO.
  2. Select the QO output, and change its name to STATE by either double clicking on it, or in the Properties below. You can change the Comment for each event and data also in the Properties.
  3. Check that the Type of STATE is BOOL in Properties.
  4. Check the lines between events and data which represent the WITH. They indicate which data outputs are refreshed when an Event occurs. Similarly occurs with event and data inputs, but in this case there's no data input to refresh. Since the STATE is changed when CNF is triggered, there's a WITH line between them.
  5. Try and add Events and different types of Data input and outputs to the interface to play around from the Palette. Then leave it again as the image.
Create Basic Type - Interface

Change the ECC

Select the ECC (Execution Control Chart) tab of the bottom of the central editing area, and you will see a state-machine picture. This is the default one, and here's where you actually code the behavior of the Function Block. Purple boxes are states (double frame is the initial one), the yellow ones represent the Algorithms that are executed when the Function Block enter the state, and the green ones are the triggered events after the algorithm. The arrows between them represent Events that arrives to the Function Block. They can have conditions. A "1" means that it will always change state after the algorithm. Events are consumed only one at a time. The small number at the begining of the transition are the priorities, in case more than one transition is possible. Don't try to understand everything at once. With a little practice the terms and concepts become easier.

  1. Select everything (Click and Drag), except the START state, and delete them.
  2. Add a State from the Palette on the right by Drag and Drop from the Palette. Change its name to On by either double clicking on it, or selecting and then in Properties below.
  3. Add another State and name it Off.
  4. Add an Action to the states On by Drag and Drop from the Palette or right click → Add Action.
  5. Select nothing by clicking in the background of the central editing area, and select the Algorithms tab in Property view. Here you see the all the default Algorithms for the Function Block. Delete them all by selecting and clicking the read cross, since we don't use in this example.
  6. Add one algorithm by clicking on the green plus. Select the name and change it to turnOn.
    Depending on the selected algorithm type, different algorithm editors are provided. Currently the most used algorithm language is IEC 61131-3 Structured Text (ST). The ST Editor provides syntax highlighting and code completion. We'll see later that this code is converted to FORTE code. If you select AnyText, no conversion is done, so you should know the syntax for FORTE (or other Runtime).

    On the right side, you can write your code. As the name implies in the turnOn algorithm we set you STATE variable to TRUE. Using the name of the Data Output (or input or internal variable), you access its value for reading and writing.

    STATE := TRUE;

    Add an Algorithm to the Action
  7. Double-click on the yellow box of the action of the On state and select the turnOn algorithm. Alternatively, you can also select this in the Property view.
  8. Double-click on the green box of the action of the On state and select the CNF output event. Alternatively, you can also select this in the Property view.
  9. Add another algorithm by clicking on the green plus. Select the name and change it to turnOff. As the name implies in the turnOff algorithm we set you STATE variable to FALSE.
    STATE := FALSE;
  10. Double-click on the yellow box of the action of the Off state and select the turnOff algorithm.
  11. Double-click on the green box of the action of the Off state and select the CNF output event.
    Current score of the States And Actions
  12. Draw a transition by drag and drop from the START to the state On. Select it, and in the transition tab of the Properties view select the condition REQ. Next to it, you have the possibility to add a condition (for example, if STATE == TRUE or similar). In this example, no condition except the REQ event is needed.
  13. Draw a transition by drag and drop from the state On to the state Off. Select it, and in the transition tab of the Properties view select the condition REQ.
  14. Draw a transition by drag and drop from the state Off to the state On. Select it, and in the transition tab of the Properties view select the condition REQ.
Basic ECC example

At the beginning, the FB is on the START state. When a REQ event arrives, it jumps to the On state, execute algorithm turnOn and triggers the CNF output event. When the next event arrives, it jumps to Off state, execute algorithm turnOff and triggers the CNF output event. That's how the ECC works.

In case the transition with a "1" had also a REQ event, an infinite loop won't happen since the event is consumed only once. You would need to REQ events to go back.

Exporting the Function Block and testing are presented after the chapter Create a Composite and Service Function Block.

Create a Simple Function Block

The Simple FB has only one algorithm and no ECC.

Create a new Type follow the instructions in the creation wizard. For this tutorial select BlinkTest as the parent folder. Choose a name for the new FB, we name it SimpleTest. Afterwards select the type of FB you want to create, in this case we choose Simple. The Function Block Type Editor (FBT Editor) is opening after clicking Finish.

New Type Wizard - Create a Simple Function Block
  1. Change the interface as before.
    Create Simple Type - Interface
  2. Select the Algorithm tab of the bottom of the central editing area. In this view you can define a algorithm of the Simple FB.
    Simple FB Algorithm example

That's all you need to do for the Simple function block.

Create a Composite Function Block

Create a new Type follow the instructions in the creation wizard. For this tutorial select BlinkTest as the parent folder. Choose a name for the new FB, we name it CompositeTest. Afterwards select the type of FB you want to create, in this case we choose Composite. The Function Block Type Editor (FBT Editor) is opening after clicking Finish.

New Type Wizard - Create a Composite Function Block
  1. Change the interface as before.
    Create Composite Type - Interface
  2. Select the Composite Network tab of the bottom of the central editing area. In this view you can create your own network of FBs inside the Function Block. For this example, use the E_SWITCH and E_SR and connect them as the original Blink example.
  3. On the edges of the central editing area, you can see events and data that correspond to the interface of the Composite. Connect them as the image below.
    Composite Network example

That's all you need to do for the Composite function block.

Create a Service Function Block

Create a new Type follow the instructions in the creation wizard. For this tutorial select BlinkTest as the parent folder. Choose a name for the new FB, we name it ServiceInterfaceTest. Afterwards select the type of FB you want to create, in this case we choose Service Interface. The Function Block Type Editor (FBT Editor) is opening after clicking Finish.

New Type Wizard - Create a Service Interface Function Block

Change the interface as before


Create Service Interface Type - Interface

That's it. The behavior of the Service Function Block must be implemented directly in the code that's generated from it. You will need to implement the functionality for each incomming event, manage the internal variables and send output events by yourself after exporting. Below you find the code for this example.

Export Function Block types

4diac IDE provides an export filter to generate C++ code which can be included in the 4diac FORTE development process. In order to export a Function Block Type, select either

Select Export Type

Select the FBs to be exported on the right side of the wizard. It makes sense to use your own folder, we choose ExportedFBs. Before exporting one or more Function Block Types, the Export Destination and the Exporter (FORTE 1.x)have to be chosen. Furthermore the version of the 4diac FORTE C++ format have to be selected. We'll export the three just created.

Attention: Make sure that not all FBs are checked, otherwise all existing FBs will be exported!

Export Type Wizard

After pressing the Finish-Button and no warning pop up everything went fine. Your exported FBs are in the folder you choose before.
If a dialog window will pop up, something went wrong and it will inform you that the export to the 4diac FORTE C++ format was not successful.

If the output directory already contains an older version of the exported Function Block Type it is possible to overwrite the old file or to open a Merge Editor where manual merges can be performed.

Export Merge Window

Finish the Service Interface Test

Now that you have exported the Service Interface Function Block, you need to edit it in order to get the desired behavior. Open the ServiceTest.cpp file that was recently exported, and take a look at the executeEvent function at the bottom.

void FORTE_ServiceInterfaceTest::executeEvent(int pa_nEIID){ switch(pa_nEIID){ case scm_nEventREQID: #error add code for REQ event! /* do not forget to send output event, calling e.g. sendOutputEvent(scm_nEventCNFID); */ break; } }

The comment reminds us to set an output event. replace #error add code for REQ event! with STATE() = !STATE(); for the toggling effect.

void FORTE_ServiceTest::executeEvent(int pa_nEIID){ switch(pa_nEIID){ case scm_nEventREQID: STATE() = !STATE(); sendOutputEvent(scm_nEventCNFID); break; } }

To get or set an internal variable or data input, in the 4diac FORTE template, you use the name and the parenthesis. The sendOutputEvent is the specific function to send output events.

Other Type Editors

Currently only the management (i.e. creation, deletion and modification) of the Basic, Simple, Composite, Service Interface and Sub Application (SubApp) Function Block types as well as Adapter types are supported. The management of Device and Resource Types is currently not supported.

Test Function Block

The FBTester tab of the bottom of the central editing area is available for all Function Blocks. It allows to test the functionality of a Function Block by executing it on a target device. But first, 4diac FORTE must know about these FBs. So, you need to compile 4diac FORTE using these Function Block just created. The guide to compile 4diac FORTE is here. See the External Module section to add the files for compilation. Also, the CMake option FORTE_SUPPORT_MONITORING must be marked, which is the default value.

Once compiled, you can go to the FBTester, select 4diac FORTE Remote Tester in Test Configuration, and then set the IP and PORT for the 4diac FORTE that was compiled with the Function Blocks to be tested. The default is locally, but you can test them on any reachable FORTE.

Press Start Testing FB, and the FB will be load in 4diac FORTE, and you can manually set input values of your FB, trigger input events and inspect the resulting output events and output data. Furthermore you can store input/output sequences and execute them to automatically test a certain functionality of your FB.

Functionblock Tester

Where to go from here?

In the next step you will see other basic features

Step 5 - Other basic features

If you want to go back to the distributed application running remotely, here's a link

Step 3 - Deploy remotely

If you want to go back to the Start Here page, we leave you here a fast access

Start Here page

Go to top