PLECS 3.7 Online Help

C-Script

Purpose

Execute custom C code

Library

Control / Functions & Tables

Description

pict

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 or a vector of positive integers. The length of the vector specifies the number of input or output terminals, the elements in the vector specify the widths of the single terminals. For dynamic sizing set the width of one or more input terminals 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. All occurrences of -1 in the input and output widths and any other data vector will be expanded to the same width.
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 n × 2   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.

TypeValue 
Continuous[0, 0] or 0 
Semi-Continuous[0, -1] 
Discrete-Periodic[Tp, To] or TpTp: Sample period, Tp > 0
To: Sample offset, 0 ≤ To < Tp
Discrete-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). If the C-Script block has one input terminal, a flag in the vector is applied to the respective single element of the input signal; if the block has multiple input terminals, a flag is applied to the whole signal of the respective terminal. You can also specify a single scalar, which then applies to all input signals of all terminals.
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, 3d-arrays or strings.

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").

Probe Signals

Input i   
The i  th input signal.
Output i   
The i  th output signal.