Rapid Prototyping Model Functions - MATLAB & Simulink (original) (raw)

Rapid prototyping code defines the following functions that interface with the main program (main.c or main.cpp):

The contents of the above functions are directly related to the blocks in your model. A Simulink block can be generalized to the following set of equations.

Output y is a function of continuous state_x_c, discrete state_x_d, and input_u_. Each block writes its specific equation in a section ofMdlOutputs.

The discrete states _x_d are a function of the current state and input. Each block that has a discrete state updates its state in MdlUpdate.

The derivatives x are a function of the current input. Each block that has continuous states provides its derivatives to the solver (for example,ode5) in_`model`__Derivatives. The derivatives are used by the solver to integrate the continuous state to produce the next value.

The output, y, is generally written to the block I/O structure. Root-level Outport blocks write to the external outputs structure. The continuous and discrete states are stored in the states structure. The input, u, can originate from another block's output, which is located in the block I/O structure, an external input (located in the external inputs structure), or a state. These structures are defined in the_`model`_.h file that the Simulink Coder™ software generates.

The next example shows the general contents of the rapid prototyping style of C code written to the _`model`_.c file.

This figure shows a flow chart describing the execution of the rapid prototyping generated code.

Rapid Prototyping Execution Flow Chart

A schematic block diagram representing the rapid prototyping of generated code. The blocks are connected in a chain from Start Execution to End Execution. A directional Execution Loop connects the part of the chain just before the second-to-last step, MdlTerminate, to the part of the chain just after the second step, MdlStart.

Each block places code in specific Mdl routines according to the algorithm that it is implementing. Blocks have input, output, parameters, and states, as well as other general items. For example, in general, block inputs and outputs are written to a block I/O structure (_`model`__B). Block inputs can also come from the external input structure (_`model`__U) or the state structure when connected to a state port of an integrator (_`model`__X), or ground (rtGround) if unconnected or grounded. Block outputs can also go to the external output structure (_`model`__Y). This figure shows the general mapping between these items.

Data View of the Generated Code

Block diagram of the generated code.

The following list defines the structures shown in the preceding figure:

See Also

Topics