Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

使用 Stateflow 图的空燃比控制系统

为使用 Simulink® 和 Stateflow® 设计的空燃比控制系统生成代码。

图 1、2 和 3 显示了 sldemo_fuelsys 模型(包含被控对象和控制器的闭环系统)的相关部分。被控对象在设计周期早期阶段的仿真过程中验证控制器。在此示例中,您将为相关控制器子系统 fuel_rate_control 生成代码。图 1 显示顶层仿真模型。

使用命令 rtwdemo_fuelsys 打开模型 sldemo_fuelsys。然后,编译模型以查看信号数据类型。

rtwdemo_fuelsys
sldemo_fuelsys([],[],[],'compile');
sldemo_fuelsys([],[],[],'term');

图 1:被控对象和控制器的顶层模型

空燃比控制系统由 Simulink® 和 Stateflow® 组成。控制系统是指您为其生成代码的模型部分。

open_system('sldemo_fuelsys/fuel_rate_control');

图 2:空燃比控制器子系统

控制逻辑是指定不同操作模式的 Stateflow® 图。

open_system('sldemo_fuelsys/fuel_rate_control/control_logic');

图 3:空燃比控制器逻辑

关闭这些窗口。

close_system('sldemo_fuelsys/fuel_rate_control/airflow_calc');
close_system('sldemo_fuelsys/fuel_rate_control/fuel_calc');
close_system('sldemo_fuelsys/fuel_rate_control/control_logic');
hDemo.rt=sfroot;hDemo.m=hDemo.rt.find('-isa','Simulink.BlockDiagram');
hDemo.c=hDemo.m.find('-isa','Stateflow.Chart','-and','Name','control_logic');
hDemo.c.visible=false;
close_system('sldemo_fuelsys/fuel_rate_control');

使用 Embedded Coder® 配置并构建模型

要为模型配置和编译生产 ANSI® C/C++ 代码,请将模型配置参数 System target file 设置为 ert.tlc(嵌入式实时 (ERT))。您可以通过编程方式设置 System target file 参数。

rtwconfiguredemo('sldemo_fuelsys','ERT');

生成并检查代码。您可以使用 PreviousNext 按钮,以交互方式导航到相关的代码段。从图上下文菜单(右键点击 Stateflow® 模块)中,选择 C/C++ Code > Navigate to C/C++ Code。以编程方式使用 rtwtrace 实用工具。

slbuild('sldemo_fuelsys/fuel_rate_control');
rtwtrace('sldemo_fuelsys/fuel_rate_control/control_logic')
### Starting build procedure for: fuel_rate_control
### Successful completion of build procedure for: fuel_rate_control

Build Summary

Top model targets built:

Model              Action                       Rebuild Reason                                    
==================================================================================================
fuel_rate_control  Code generated and compiled  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 16.828s

ans =

    '&sid=sldemo_fuelsys:107&inputLocs={"data": [{"file": "fuel_rate_control.c", "line": ["21","168","331","435","440","443","444","445","448","449","450","453","454","455","458","461","464","467","470","473","476","479","482","485","488","491","492","493","494","495","499","500","501","502","503","504","505","506","507","508","511","512","513","519","520","522","523","524","525","526","529","530","536","537","539","540","548","549","550","551","552","553","554","555","558","559","564","565","566","567","573","574","575","576","577","578","581","582","586","592","593","594","595","596","597","598","599","602","603","608","609","610","616","617","619","620","621","622","625","626","630","636","637","638","639","640","641","642","643","646","647","653","654","655","656","659","660","661","662","664","672","673","675","676","677","678","681","682","686","692","693","696","697","701","853","855","858","859","860","861","862"]},{"file": "fuel_rate_control.h", "line": ["36","41","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","67"]}]}'

查看生成的代码中的空燃比控制逻辑。

rtwdemodbtype('fuel_rate_control_ert_rtw/fuel_rate_control.c',...
    '/* Function for Chart:','case IN_Warmup:',1,0);
/* Function for Chart: '<S1>/control_logic' */
static void Fueling_Mode(const int32_T *sfEvent)
{
  switch (rtDWork.bitsForTID0.is_Fueling_Mode) {
   case IN_Fuel_Disabled:
    rtDWork.fuel_mode = DISABLED;
    switch (rtDWork.bitsForTID0.is_Fuel_Disabled) {
     case IN_Overspeed:
      /* Inport: '<Root>/sensors' */
      if ((rtDWork.bitsForTID0.is_Speed == IN_normal) && (rtU.sensors.speed <
           603.0F)) {
        if (rtDWork.bitsForTID0.is_Fail != IN_Multi) {
          rtDWork.bitsForTID0.is_Fuel_Disabled = IN_NO_ACTIVE_CHILD;
          rtDWork.bitsForTID0.is_Fueling_Mode = IN_Running;

          /* The fuel is actively controlled while in this state. */
          switch (rtDWork.bitsForTID0.was_Running) {
           case IN_Low_Emissions:
            rtDWork.bitsForTID0.is_Running = IN_Low_Emissions;
            rtDWork.bitsForTID0.was_Running = IN_Low_Emissions;
            rtDWork.fuel_mode = LOW;
            switch (rtDWork.bitsForTID0.was_Low_Emissions) {
             case IN_Normal:
              rtDWork.bitsForTID0.is_Low_Emissions = IN_Normal;
              rtDWork.bitsForTID0.was_Low_Emissions = IN_Normal;

              /* All sensors are in correct operating modes, so effective closed-loop mixture control can be used. */
              break;

关闭模型和代码生成报告。

clear hDemo;
rtwdemoclean;
close_system('sldemo_fuelsys',0);

相关示例

有关使用 sldemo_fuelsys 的相关定点示例,请参阅: