Main Content

Air-Fuel Ratio Control System with Stateflow Charts

Generate code for an air-fuel ratio control system designed with Simulink® and Stateflow®.

Figures 1, 2, and 3 show relevant portions of the sldemo_fuelsys model, a closed-loop system containing a plant and controller. The plant validates the controller in simulation early in the design cycle. In this example, you generate code for the relevant controller subsystem, "fuel_rate_control". Figure 1 shows the top-level simulation model.

Use the command rtwdemo_fuelsys to open model sldemo_fuelsys. Then, compile the model to see the signal data types.

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

Figure 1: Top-level model of the plant and controller

The air-fuel ratio control system is comprised of Simulink® and Stateflow®. The control system is the portion of the model for which you generate code.

open_system('sldemo_fuelsys/fuel_rate_control');

Figure 2: The air-fuel ratio controller subsystem

The control logic is a Stateflow® chart that specifies the different modes of operation.

open_system('sldemo_fuelsys/fuel_rate_control/control_logic');

Figure 3: Air-fuel rate controller logic

Close these windows.

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');

Configure and Build the Model with Embedded Coder®

To configure and build production ANSI® C/C++ code for the model, set model configuration parameter System target file to ert.tlc (Embedded Real-Time (ERT)). You can set the System target file parameter programmatically.

rtwconfiguredemo('sldemo_fuelsys','ERT');

Generate and inspect the code. You can navigate to the relevant code segments interactively by using Previous and Next buttons. From the chart context menu (right-click the Stateflow® block), select C/C++ Code > Navigate to C/C++ Code. Programmatically, use the rtwtrace utility.

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 22.415s

ans =

    '&sid=sldemo_fuelsys:107&inputLocs={"data": [{"file": "fuel_rate_control.c", "line": ["19","166","328","432","437","440","441","442","445","446","447","450","451","452","455","458","461","464","467","470","473","476","479","482","485","488","489","490","491","492","496","497","498","499","500","501","502","503","504","505","508","509","510","516","517","519","520","521","522","523","526","527","533","534","536","537","545","546","547","548","549","550","551","552","555","556","561","562","563","564","570","571","572","573","574","575","578","579","583","589","590","591","592","593","594","595","596","599","600","605","606","607","613","614","616","617","618","619","622","623","627","633","634","635","636","637","638","639","640","643","644","650","651","652","653","656","657","658","659","661","669","670","672","673","674","675","678","679","683","689","690","693","694","698","850","852","855","856","857","858","859"]},{"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"]}]}'

View the air-fuel ratio control logic in the generated code.

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 == 2) && (rtU.sensors.speed < 603.0F)) {
        if (rtDWork.bitsForTID0.is_Fail != 1) {
          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;

Close the model and code generation report.

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

Related Examples

For related fixed-point examples that use sldemo_fuelsys, see