Main Content

在生成的代码中启用和重用本地块输出

此示例显示如何将块输出指定为局部变量。代码生成器可能会在生成的代码中潜在地重用这些局部变量。声明块输出作为局部变量节省ROM消耗。重用局部变量节省RAM消耗,减少数据副本,并提高执行速度。

Example Model

  1. UseInportOutportGain那and转变块以创建以下模型。在此示例中,该模型命名为local_variabiere_ex.

  2. 为了G2.,打开“增益块参数”对话框。输入价值2

  3. 为了G1.,输入一个值3.

  4. 为了转变block, open the Block Parameters dialog box. For the通过第一个输入的标准参数,选择U2> =阈值

Generate Code Without Optimization

  1. Open the Model Configuration Parameters dialog box. Select theSolver窗格。为了Type参数,选择固定步骤

  2. 清除配置参数>信号存储重用范围。

  3. 选择the代码>报告pane and select创建代码生成报告

  4. 选择the代码生成窗格。选择仅生成代码,然后,在模型窗口中,按Ctrl+B。When code generation is complete, an HTML code generation report appears.

  5. 在里面code generation report, select thelocal_variabiere_ex.。c部分并查看模型步函数。增益块输出是全局变量local_variabiere_ex_b.g2.andlocal_variabiere_ex_b.g1.

/ *模型步骤函数* / void local_variabies_ex_step(void){/ * switch:' / switch'包含:* inport:' / in1'* / if(local_variably_ex_u.in1> = 0.0){/ *增益:' / g2'* / local_variably_ex_b.g2 = 2.0 * local_variabiere_ex_u.in1;/ *出口:' / out1'* / local_variabiere_ex_y.out1 = local_variabiere_ex_b.g2;} else {/ * gain:' / g1'* / local_variabiere_ex_b.g1 = 3.0 * local_variable_ex_u.in1;/ *出口:' / out1'* / local_variabiere_ex_y.out1 = local_variabiere_ex_b.g1;} / *开关结束:' / switch'* /

启用本地块输出并生成代码

  1. 选择the配置参数>Signal Storage Reuse范围。TheSignal Storage Reuse启用以下优化参数:

    • 启用本地块输出

    • Reuse local block outputs

    • 消除多余的局部变量(表达式折叠)

  2. ClearReuse local block outputsand消除多余的局部变量(表达式折叠)

  3. 生成代码并查看模型步骤功能。模型步骤功能中有三个局部变量,因为您选择了优化参数Enable Local Block Outputs。The local variablesRTB_G2.andRTB_G1.hold the outputs of theGainblocks. The local variablertb_Switch持有输出转变堵塞。

/ *模型步骤功能* / void local_variabiere_ex_step(void){real_t rtb_switch;real_t rtb_g2;real_t rtb_g1;/ *切换:' / switch'包含:* inport:' / in1'* / if(local_variabiere_ex_u.in1> = 0.0){/ *增益:' / g2'* / RTB_G2 =2.0 * local_variabiere_ex_u.in1;RTB_Switch = RTB_G2;} else {/ *增益:' / g1'* / RTB_G1 = 3.0 * local_variabiere_ex_u.in1;RTB_Switch = RTB_G1;} / *开关结束:' / switch'* // *出口:' / out1'* / local_variabiere_ex_y.out1 = RTB_Switch;

Reuse Local Block Outputs and Generate Code

  1. 选择the配置参数>Reuse local block outputs范围。

  2. 生成代码。在里面local_variabiere_ex.。c部分,查看模型步函数。有一个局部变量,RTB_G2.,代码生成器使用三次。

/ *模型步函数* / void local_variabiese_ex_step(void){real_t rtb_g2;/ *切换:' / switch'包含:* inport:' / in1'* / if(local_variabiere_ex_u.in1> = 0.0){/ *增益:' / g2'* / RTB_G2 =2.0 * local_variabiere_ex_u.in1;} else {/ * gain:' / g1'* / RTB_G2 = 3.0 * local_variable_ex_u.in1;} / *开关结束:' / switch'* // *出口:' / out1'* / local_variabiere_ex_y.out1 = RTB_G2;

额外的临时变量rtb_Switchand the associated data copy is not in the generated code.

也可以看看

Related Topics