# Step 4 - Create Function Block Types

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

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

This page will show how to create a Basic, 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. And then 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

To create new Function Block Types you have three options

1. From the context menu in the System Explorer New → New Type
2. From the menu: File → New → New Type
3. From the menu: File → New → Other.. → 4DIAC → New Type

In the wizard page, select BlinkTest as the parent folder. The name and the type will be selected according to which type of Function Block we are doing.

## Create a Basic Function Block

In the wizard page, choose a name for the type (we use BasicTest) and select Basic.fbt as Type Name, and click Finish. The Type Editor view is open.

1. Your new created type appears in the System Explorer (press F5 to refresh).
2. The main view shows what you are working on. Initially it shows the interface of the Function Block.
3. The Palette shows all that you can add to the main view.
4. The tabs below the main view 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.

### Change the interface

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 output boolean Data for the state.

1. Select the INIT event, and delete it by pressing the Delete key, or right click → Delete. Do the same for QI and INITO.
2. Select the QO output, and change its name to STATE by either double cliking 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 boolean in Properties.
4. Check the lines between data and events which represent the WITH. They indicate wihch 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 differnt types of Data input and outputs to the interface to play around from the Palette. Then leave it again as the image.

### Change the ECC

Select the ECC tab, 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. Lila boxes are states (double border 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 beggining 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), excecept the START state, and delete them.
2. Add a State from the Palette on the right (Drag and Drop).
3. Change its name to REQUESTED by either double clicking on it, or selecting and then in Properties below.
4. Add an Action to the REQUESTED state by dragging it to it from the Palette.
5. Select nothing by clicking in the background of the main view, and select the Algorithms tab in Properties. 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. Select the name and change it to CHANGE. Depending on the selected algorithm type, different algorithm editors are provided. Currently the most used algorithm language is IEC 61131-3 structured text. For this also syntax highlighting is provided. 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).
7. In the Internal Vars tab, you can add variables that retain their values between events, and are unaccesible outside of the Function Block. For this example, we don't use them.
8. Select the CHANGE algorithm, and on the right side, you can write your code. This just need to negate the previous value. Using the name of the Data Output (or input or internal vairable), you access its value for reading and writing.
   STATE := NOT STATE;
9. Draw a transition from state START to REQUESTED. Select it, and in the Properties view, in the Condition field, select the REQ events. 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.
10. Draw a transition from state REQUESTED to START and leave it with condition 1.
11. Double click on the yellow box next to REQUESTED and select the CHANGE algorithm.
12. Double click on the green box next to REQUESTED and select the CNF event.

At the beginning, the FB is on the START state. When a REQ event arrives, it jumps to the REQUESTED state, execute algorithm CHANGE, triggers CNF output event, and then follows the 1 back to START. 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 Composite and Service Function Block

## Create a Composite Function Block

Create a new Type as shown before. In the wizard page, choose a name for the type (we use CompositeTest) and select Basic.fbt as Type Name, and click Finish. The Type Editor view is open.

1. Change the interface as before.
2. Move to the Composite Network tab. 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 main view, you can see events and data that correspond to the interface of the Composite. Connect them as the image below.

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

## Create a Service Function Block

Create a new Type as shown before. In the wizard page, choose a name for the type (we use ServiceTest) and select ServiceInterface.fbt as Type Name, and click Finish. The Type Editor view is open.

The defatult interface of the Function Block looks different, but change it again and leave similar to the Basic and Composite examples.

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 FORTE development process. In order to export a Function Block Type, select either

- File → Export → 4DIAC → 4DIAC Type Export
- Right click on the function block to export and select Export → 4DIAC → 4DIAC Type Export

Before exporting one or more Function Block Types, the corresponding types and the output directory have to be chosen. Furthermore the version of the FORTE C++ format have to be selected. We'll export the three just created.

After pressing the Fnish-Button a dialog window will inform when the export to the 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.

### Finish the Service Test

Now that you have exported the Service 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. You write there 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 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 Basic, Composite and Service Interface Function Block types as well as Adapter types is supported. The management of Device and Resource Types is currently not supported, Sub-applications are currently under development.

## Test Function Block

The FBTester tab 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, FORTE must know about these FBs. So, you need to compile forte using these Function Block just created. The guide to compile 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 FORTE Remote Tester in Test Configuration, and then set the IP and PORT for the 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 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.

# 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