Main Content

Generate Reusable Code from Model Reference Hierarchy

A reusable subsystem function that has input or output connected to the root Inport or Outport block of a referenced model can impact code reuse. It can prevent you from reusing atomic subsystems in a reference model context in the same way you might reuse the subsystems in a standalone model.

This example shows how to generate reusable code when you have a reusable subsystem referenced inside Model block.

Generate Reusable Code for Subsystem

1. Convert these blocks into a subsystem. Drag a box to outline the subsystem that you want to create. From the Multiple tab on the Simulink Editor toolstrip, select Create Subsystem.

2. Make these changes to the Subsystem block parameters:

3. Create a model that includes three instances of the preceding subsystem.

4. Set the model configuration parameter Default parameter behavior to Inlined.

5. To build the model and generate code, press Ctrl+B.

The code generator optimizes the code by generating one copy of the function for the reused subsystem in the model.c file.

void ex_codeReuseThreeSubsys_Subsystem1(real_T rtu_In1,
  B_Subsystem1_ex_codeReuseThreeSubsys_T *localB)
{
  /* Gain: '<S1>/Gain' */
  localB->Gain = 3.0 * rtu_In1;
}

Generate Reusable Code for Subsystem in Referenced Model

1. Create a top model by using a Model block that references the preceding model that has three instances of the reusable subsystem.

2. Generate code for the top model. The code generator generates three different function signatures in the slprj folder.

/* Output and update for atomic system: '<Root>/Subsystem 1' */
void ex_codeReuseThreeSubsys_Subsystem1(const real_T *rtu_In1,
  B_Subsystem1_ex_codeReuseThreeSubsys_T *localB)
{
  /* Gain: '<S1>/Gain' */
  localB->Gain = 3.0 * *rtu_In1;
}

/* Output and update for atomic system: '<Root>/Subsystem 2' */
void ex_codeReuseThreeSubsys_Subsystem2(real_T rtu_In1,
  B_Subsystem2_ex_codeReuseThreeSubsys_T *localB)
{
  /* Gain: '<S2>/Gain' */
  localB->Gain = 3.0 * rtu_In1;
}

/* Output and update for atomic system: '<Root>/Subsystem 3' */
void ex_codeReuseThreeSubsys_Subsystem3(real_T rtu_In1, real_T *rty_Out1)
{
  /* Gain: '<S3>/Gain' */
  *rty_Out1 = 3.0 * rtu_In1;
}

3. To enable the subsystem functions for code reuse, insert Signal Conversion blocks into the referenced model. Place one block between the Inport and Subsystem 1 and another block between Subsystem 3 and the Outport.

If the subsystem has a Merge block with initial conditions, do not add a Signal Conversion block. Instead, add a Bias block to obtain a reusable function.

4. Select the model configuration parameter Pass fixed-size scalar root inputs by value for code generation.

5. Generate code for the top model. The code generator generates a single reusable function in the slprj folder.

void ex_codeReuseSubsysSigConvert_Subsystem1(real_T rtu_In1,
  B_Subsystem1_ex_codeReuseSubsysSigConvert_T *localB,
  P_Subsystem1_ex_codeReuseSubsysSigConvert_T *localP)
{
  /* Gain: '<S1>/Gain' */
  localB->Gain = localP->Gain_Gain * rtu_In1;
}

Related Topics