Integrate Generated Code with Custom Code
You can integrate generated Structured Text code with custom code by using the
ssMethodType enumerated state in the Structured Text function
block code. When you generate code for a top-level Subsystem block that has an internal
state, the function block which appears in the FUNCTION_BLOCK of the
generated code, contains the ssMethodType enumerated state.
When you set ssMethodType to the name of a function or an
equivalent integer, the function block executes the associated function. In the
generated code, you can see the functions available for each function block in the CASE
statement of the function block code. The code executes the associated function based on
the value you specify for ssMethodType.
This table lists the functions you can set ssMethodType to. Set
ssMethodType to the values in the Function
Name column. If the target IDE does not support symbolic constants, set
ssMethodType to values in the Enumerated
Value column.
| Enumerated Value | Function Name | Purpose of the Function |
|---|---|---|
| 0 | SS_INITIALIZE | Sets up initial conditions and parameters. |
| 1 | SS_STEP | Executes the logic inside the function at every time step in the generated code. |
| 2 | SS_START | Executes one-time setup tasks at the start of the generated code. |
| 3 | SS_OUTPUT | Calculates and updates the output values at each time step in the generated code. |
| 4 | SS_UPDATE | Updates values of the internal states. |
| 17 | SS_CONST_CODE | Contains generated code that remains constant when the code runs. |
The generated Structured Text for the top-level Subsystem block varies depending on whether the block has internal states.
| Has Internal State | Generated ssMethodType
Functions |
|---|---|
| Yes | The generated Structured Text code contains a function block
that represents the top-level subsystem and an
For example, open the
openExample('plccoder/GenerateStructuredTextCodeForASimpleSimulinkRSubsystemExample') open_system("plcdemo_simple_subsystem"); SimpleSubsystem block contains internal
states. In the generated code, to initialize the function block, set
ssMethodType to
SS_INITIALIZE. If the target IDE does not
support symbolic constants, set ssMethodType to
0.After you initialize the
function block, set FUNCTION_BLOCK SimpleSubsystem VAR_INPUT ssMethodType: SINT; U: LREAL; END_VAR VAR_OUTPUT Y: LREAL; END_VAR VAR UnitDelay_DSTATE: LREAL; END_VAR CASE ssMethodType OF SS_INITIALIZE: (* SystemInitialize for Atomic SubSystem: '<Root>/SimpleSubsystem' *) (* InitializeConditions for UnitDelay: '<S1>/Unit Delay' *) UnitDelay_DSTATE := 0.0; (* End of SystemInitialize for SubSystem: '<Root>/SimpleSubsystem' *) SS_STEP: (* Outputs for Atomic SubSystem: '<Root>/SimpleSubsystem' *) (* Gain: '<S1>/Gain' incorporates: * Sum: '<S1>/Sum' * UnitDelay: '<S1>/Unit Delay' *) Y := (U - UnitDelay_DSTATE) * 0.5; (* Update for UnitDelay: '<S1>/Unit Delay' *) UnitDelay_DSTATE := Y; (* End of Outputs for SubSystem: '<Root>/SimpleSubsystem' *) END_CASE; END_FUNCTION_BLOCK VAR_GLOBAL CONSTANT SS_INITIALIZE: SINT := 0; SS_STEP: SINT := 1; END_VAR If you select the Keep
top level ssMethod name same as non-top level model
configuration parameter, the generated code replaces
FUNCTION_BLOCK S1 VAR_INPUT ssMethodType: SINT; U: LREAL; END_VAR VAR_OUTPUT Y: LREAL; END_VAR VAR UnitDelay_DSTATE: LREAL; END_VAR CASE ssMethodType OF SS_INITIALIZE: (* InitializeConditions for UnitDelay: '<S2>/Unit Delay' *) UnitDelay_DSTATE := 0.0; SS_OUTPUT: (* Gain: '<S2>/Gain' incorporates: * Sum: '<S2>/Sum' * UnitDelay: '<S2>/Unit Delay' *) Y := (U - UnitDelay_DSTATE) * 0.5; (* Update for UnitDelay: '<S2>/Unit Delay' *) UnitDelay_DSTATE := Y; END_CASE; END_FUNCTION_BLOCK FUNCTION_BLOCK S2 VAR_INPUT ssMethodType: SINT; U: LREAL; END_VAR VAR_OUTPUT Y: LREAL; END_VAR VAR UnitDelay_DSTATE: LREAL; END_VAR CASE ssMethodType OF SS_INITIALIZE: (* InitializeConditions for UnitDelay: '<S3>/Unit Delay' *) UnitDelay_DSTATE := 0.0; SS_OUTPUT: (* Gain: '<S3>/Gain' incorporates: * Sum: '<S3>/Sum' * UnitDelay: '<S3>/Unit Delay' *) Y := (U - UnitDelay_DSTATE) * 0.75; (* Update for UnitDelay: '<S3>/Unit Delay' *) UnitDelay_DSTATE := Y; END_CASE; END_FUNCTION_BLOCK |
| No | The function block interface only has parameters mapped from
Simulink® block input and output ports. There is no
|
The generated Structured Text code for the non-top-level Subsystem block also varies depending on whether the block has internal states.
| Has Internal State | Generated ssMethodType
Functions |
|---|---|
| Yes | The function block interface has the
If non top-level subsystems
have blocks that have constant sample times, the generated code can
have For example, this code block shows the generated code for a non-top-level Subsystem block with internal states: FUNCTION_BLOCK SubSystem3 VAR_INPUT ssMethodType: SINT; U: LREAL; END_VAR VAR_OUTPUT Y: LREAL; END_VAR VAR UnitDelay_DSTATE: LREAL; END_VAR CASE ssMethodType OF SS_INITIALIZE: (* SystemInitialize for Atomic SubSystem: '<Root>/SubSystem3' *) (* InitializeConditions for UnitDelay: '<S1>/Unit Delay' *) UnitDelay_DSTATE := 0.0; (* End of SystemInitialize for SubSystem: '<Root>/SubSystem3' *) SS_OUTPUT: (* Outputs for Atomic SubSystem: '<Root>/SubSystem3' *) (* Gain: '<S1>/Gain' incorporates: * Sum: '<S1>/Sum' * UnitDelay: '<S1>/Unit Delay' *) Y := (U - UnitDelay_DSTATE) * 3.0; (* Update for UnitDelay: '<S1>/Unit Delay' *) UnitDelay_DSTATE := Y; (* End of Outputs for SubSystem: '<Root>/SubSystem3' *) END_CASE; END_FUNCTION_BLOCK VAR_GLOBAL CONSTANT SS_OUTPUT: SINT := 3; SS_INITIALIZE: SINT := 0; END_VAR |
| No | The function block interface only has parameters mapped from
Simulink block input and output ports. There is no
|