주요 콘텐츠

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 ValueFunction NamePurpose of the Function
0SS_INITIALIZESets up initial conditions and parameters.
1SS_STEPExecutes the logic inside the function at every time step in the generated code.
2SS_STARTExecutes one-time setup tasks at the start of the generated code.
3SS_OUTPUTCalculates and updates the output values at each time step in the generated code.
4SS_UPDATEUpdates values of the internal states.
17SS_CONST_CODEContains 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 StateGenerated ssMethodType Functions
Yes

The generated Structured Text code contains a function block that represents the top-level subsystem and an ssMethodType enumerated state.

For example, open the plcdemo_simple_subsystem model.

openExample('plccoder/GenerateStructuredTextCodeForASimpleSimulinkRSubsystemExample')
open_system("plcdemo_simple_subsystem");
The 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 ssMethodType to SS_STEP to compute and return the output for each time step. If the target IDE does not support symbolic constants, set ssMethodType to 1.

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 SS_STEP with SS_OUTPUT. To update the output, set ssMethodType to SS_OUTPUT. If the target IDE does not support symbolic constants, set ssMethodType to 3.

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 ssMethodType parameter. To use the function block, call the function block with input and output arguments.

The generated Structured Text code for the non-top-level Subsystem block also varies depending on whether the block has internal states.

Has Internal StateGenerated ssMethodType Functions
Yes

The function block interface has the ssMethodType parameter. The generated code can have SS_INITIALIZE, SS_OUTPUT, or other ssMethodType enumerated values that implement Simulink semantics.

If non top-level subsystems have blocks that have constant sample times, the generated code can have SS_CONST_CODE constants that implement Simulink semantics.

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 ssMethodType parameter.

See Also

Topics