C-Script
Purpose
Execute custom C code
Library
Control / Functions & Tables
Description
The C-Script block allows for custom functionality to be implemented in the C programming language. For a detailed description of C-Scripts see chapter C-Scripts.
The C-Script dialog consists of two tabbed panes that are described below.
Setup
- Number of inputs, outputs
- A positive integer specifying the width of the input
port and output port. For dynamic sizing set the number of inputs to -1; the
width will then be determined at simulation start depending on the number of
elements in the signal that is connected to the input port. The output port and
any other data vector with a setting of -1 will be expanded to the same width.
Use the Signal Multiplexer to feed multiple signals into the block and the Signal Demultiplexer to split a vector output signal into individual scalar signals. - Number of cont. states, disc. states, zero-crossings
- A positive or zero integer specifying the sizes of the different data vectors (i.e. continuous and discrete state variables, and zero-crossing signals) that the C-Script registers with the solver.
- Sample time
- A scalar or an
matrix specifying the block sample time(s). The table below lists the valid parameter values for the different sample time types. For a detailed description of the sample time types see Sample Time.
Type Value Continuous [0, 0] or 0 Semi-Continuous [0, -1] Discrete-Periodic [Tp, To] or Tp Tp: Sample period, Tp > 0
To: Sample offset, 0 ≤ To < TpDiscrete-Variable [-2, 0] or -2 - Direct feedthrough
- A vector of zeros and ones specifying the direct feedthrough flags for the input signals. An input signal has direct feedthough if you need to access the current input signal value during the output function call. This has an influence on the block sorting order and the occurrence of algebraic loops (see Block Sorting). You can also specify a single scalar, which then applies to all input signals.
- Language standard
- The language standard used by the compiler. Possible values are C90 and C99. The default is C99.
- Enable runtime checks
- If this box is checked, protective code is added to guard
against access violations when working with block data (i.e. signal values, states,
zero-crossing signals etc.). The C-Script function calls are also wrapped with
protective code to prevent you from violating solver policies such as accessing
input signals in the output function without enabling direct feedthrough.
It is strongly recommended to leave the runtime checks enabled.
- Parameters
- A comma-separated list of expressions that are passed as external parameters into the C functions. The expressions can reference workspace variables and must evaluate to scalars, vectors, matrices or 3d-arrays.
Code
The Code pane consists of a combobox for selecting a particular code section and a text editor that lets you edit the currently selected code section. For details on the individual sections see C-Script Functions. The different macros that you need to use in order to access block data such as input/output signals and states are listed in C-Script Macros.
If you have made changes to the C code, it will be compiled when you click on Apply or OK. Any errors or warnings that occur during compilation are listed in a diagnostic window. Small badges next to the line numbers indicate the problematic code lines. If you move the mouse cursor near such a badge, a tooltip with the diagnostics for that line will appear.
A "Find" dialog for finding and optionally replacing certain text is available from the context menu or by pressing Ctrl-F. The dialog has an option to search the current code section only ("This section") or all code sections of the C-Script ("All sections").