C Function

Description of the C function component in Schematic Editor, which enables importing functions from the C programing language.

The C Function allows you to implement a component with an arbitrary function using the C programming language.

Like any other Signal Processing component, the C Function consists of inputs, outputs and functions that define the functionality of the block. This section explains in details the options which are available for configuring the C Function in each of its tabs and sub-tabs.

General tab

In this tab, you define the general properties of the C Function which include its name, its execution rate, inputs, outputs, global variables and parameters. Figure 1 shows the user interface of the General tab.

Figure 1. General tab

Inputs and outputs

Input and output terminals are defined inside the Inputs and Outputs tabs, respectively.

To add a new terminal to the C Function component, simply click the green plus-sign button for adding a new element to the table (Figure 2).

Terminal name

Every terminal on the component must have a unique name. When a new terminal is added, its name is set automatically. To rename the terminal, double-click the name you want to change and type in a new name.

When naming a terminal, it is important to follow these two rules:

  • the Name column cannot be empty;
  • Name cannot contain spaces.
Figure 2. Button for adding a new element to the table

Type

You can use the Type column to define the terminal’s signal type. The signal type can be set to the following four values (Figure 3):

  • inherit,
  • real,
  • int, and
  • uint.
Figure 3. Changing the terminal’s signal type

The signal value type inherit has different meanings for input and output terminals.

If the signal type of an input terminal is set to inherit, then the terminal will inherit the signal type from the terminal that is connected to it.

If the signal type of an output terminal is set to inherit, then the signal type of the terminal will be determined by the internal rule.

The values real, int and uint are used to explicitly set a signal type to the terminal. If the signal type of the input terminal is set explicitly, only a terminal with the same signal type can be connected to it.

For further information about signal data types, please refer to the Signal types documentation.

Dimension

You can use the Dimension column to define the terminal’s signal dimension. The signal dimension can be either inherit or an integer number (e.g., 1). Similarly to the signal value type, the signal value dimension inherit has different meanings for input and output terminals.

If the signal dimension of an input terminal is set to inherit, then the terminal will inherit the signal dimension from the terminal that is connected to it.

If the signal dimension of an output terminal is set to inherit, then the terminal will inherit the signal dimension from an input terminal with the largest signal dimension.

Direct feedthrough

The Direct feedthrough column (Figure 4) contains a checkbox which determines whether the terminal is a direct feedthrough terminal or not.

If a terminal is defined as direct feedthrough, it means that its current value determines the current value of one of the component's outputs.

For further information about direct feedthrough terminals, please refer to the Component Sorting and Algebraic loops documentation.

Figure 4. Direct feedthrough column

Terminal removal

To remove a terminal, simply click the red minus-sign button (Figure 5) in the row of the terminal you want to remove and then click OK.

Figure 5. Button for terminal removal

Terminal reordering

To reorder terminals, first select rows of terminals you want to move and then simply click either the up-arrow-sign or the down-arrow-sign.

Figure 6. Terminal reordering

Global variables

Global variables are accessible from every component’s function (Figure 7 and Figure 11). Values of global variables are preserved among different function calls.

Variable name

Terminal naming rules defined in section Terminal name must be also applied for naming global variables.

Variable type

You can use the Type column to define a global variable’s signal type. The signal type can be set to (Figure 7): real, int and uint.

For further information about signal data types, please take a look at the section Signal types .

Vectors and matrices

Global variables can also be defined as a vectors and matrices. A size of a vector or a matrice must be defined statically, as shown in Figure 7 for var2 and var3 variables.

Figure 7. Changing the global variable’s signal type

Variable removal

Variables are removed in the same way as the terminals (please, see the section Terminal removal).

Parameters

Parameters allow you to pass external variables into the C Function. These external variables are propagated through the namespace into the C Function.

To make an external variable visible within the C Function, you must declare it inside the parameters table (Figure 7), otherwise an error will be raised (Figure 8).

Parameters can be accessed inside the component’s functions in the same way as the global variables are accessed.

Figure 8. Error raised because parameter param1 is not defined in the namespace

Parameter name

Terminal naming rules defined defined previously in the "Terminal name" section must also be applied

Parameter type

You can use the Type column to define a parameter’s signal type. The signal type can be set to: real, int and uint.

For further information about signal data types, please refer to the Signal types documentation.

Parameter removal

Variables are removed in the same way as the terminals (please, see the section Inputs and outputs).

Functions

The Functions tab allows you to define arbitrary init, output and update functions.

Once you implement the functions, you can check if the code is syntactically correct by pressing the Check syntax button (Figure 9).

Figure 9. Check syntax button

If the code is syntactically correct, you will get the No errors found message, otherwise the Code validator dialog will be shown (Figure 10). Every error message inside the Code validator dialog has a header and content. If you click on the header, you will be automatically redirected to the function that caused an error.

Figure 10. Dialog for showing errors in the C Function

C Function has a built-in support for mathematical functions and constants from the standard math.h library

Output function (output_fnc)

The output_fnc function is used to update the output signals of the component and is called at each simulation step.

Example of output_fnc function

Figure 11. Example of output_fnc function

Example of accessing global variables in init_fnc function

Init function (init_fnc)

The init_fnc function is used to initialize component state variables and is called at the beginning of a simulation

Figure 12. Example of init_fnc function

Update function (update_fnc)

The update_fnc function is used to update the state variables of the component and is called at each simulation step after the output_fnc function.

Figure 13. Example of update_fnc function