Main Content

Generate Code from Fault-Embedded Models

Since R2025a

This example shows how to generate C/C++ code from a fault-embedded model. You add a fault to a model of an aircraft controller, create the fault-embedded model, configure the model to support code generation, and generate C code from the model. For more information on how to create fault-embedded models, see Create Fault-Embedded Models.

Create Fault-Embedded Model

Open the slexAircraftFaultedExample model to view the model.

This model does not contain faults. In this example, add a fault to the Sum block output port that connects to the Controller subsystem, and assign noise behavior to the fault that triggers when the simulation time is 30.

  1. Open the Fault Analyzer app. In the Apps tab, click Fault Analyzer.

  2. Select the output signal of the Sum1 block.

  3. In the Prepare Faults section, click Add Fault.

  4. Set Fault behavior to Add Noise.

  5. Set Trigger type to Timed, and set Trigger fault at time to 30.

  6. Click OK.

When you create the fault-embedded model, Simulink® Fault Analyzer™ only exports the enabled model elements and the assigned active fault. To include this fault in the fault-embedded model, activate the fault. In the Fault Table pane, click the check box in the Enable column to enable the Sum1/Outport/1 model element and activate the fault.

Create the fault-embedded model, and save it in a different directory than the folder that contains this model. You can also use the Simulink.fault.exportEmbeddedModel function to programmatically create the fault-embedded model.

  1. In the Fault Analyzer tab, in the Share section, click Export Embedded Model.

  2. Specify the name and location of the new model.

  3. The folder you select must be empty. If the folder is not empty, manually delete the folder files or select Delete existing folder content.

  4. Click OK.

After the export process completes, open the fault-embedded model. The model represents the fault as a subsystem between the output port of the Sum1 block and the Stick Input port of the Controller subsystem.

Prepare for Code Generation

If you have a Simulink Coder™ or Embedded Coder® license, you can generate code from fault-embedded models. This example contains a copy of the fault-embedded model that you created in the previous section. Open the slexAircraftFaultedExample_faultEmbedded model.

Fault-embedded models may not support code generation when you first create them. You must configure them to conform to MATLAB® and Simulink code standards and guidelines. See Code Interface Configuration (Simulink Coder). In this example, if you attempt to generate code from the fault-embedded model, the model returns an error because the model uses a variable-step size solver and the SimulationTime block in the myFault subsystem inherits its sample time.

To update the solver type to a fixed-step solver:

  1. In the Modeling tab, click Model Settings.

  2. In the left pane, select Solver.

  3. In the right pane, in the Solver selection section, set Type to Fixed-Step.

  4. Click OK.

To make the SimulationTime block support code generation, discretize the block sample time. In this example, set the sample time to 0.1:

  1. Open the embedded fault subsystem, named myFault.

  2. Double-click the SimulationTime block.

  3. In the Block Parameters window, set Sample time to 0.1.

  4. Click OK.

Generate Code

Generate code by using Simulink Coder. In the Apps tab, click Simulink Coder. In the Generate Code section, click Build. To open the code generation report, in the Results section, click Open Report. To see the generated code for the myFault subsystem, search in the slexAircraftFaultedExample_faultEmbedded.c file:

   /* Outputs for Atomic SubSystem: '<Root>/myFault' */
   /* Outputs for Atomic SubSystem: '<S4>/FaultBehavior' */
   /* RandomNumber: '<S61>/White Noise' */
   slexAircraftFaultedExample_fa_B.WhiteNoise_k =
     slexAircraftFaultedExample_f_DW.NextOutput_d;
   /* Sum: '<S61>/Sum' */
   slexAircraftFaultedExample_fa_B.Sum_l =
     slexAircraftFaultedExample_fa_B.Sum1_m +
     slexAircraftFaultedExample_fa_B.WhiteNoise_k;
   /* End of Outputs for SubSystem: '<S4>/FaultBehavior' */
   /* DigitalClock: '<S4>/SimulationTime' */
   slexAircraftFaultedExample_fa_B.SimulationTime =
     slexAircraftFaultedExample_f_M->Timing.t[1];
   /* Switch: '<S4>/FaultTrigger' */
   if (slexAircraftFaultedExample_fa_B.SimulationTime >=
       slexAircraftFaultedExample_fa_P.FaultTrigger_Threshold) {
     /* Switch: '<S4>/FaultTrigger' */
     slexAircraftFaultedExample_fa_B.FaultTrigger =
       slexAircraftFaultedExample_fa_B.Sum_l;
   } else {
     /* Switch: '<S4>/FaultTrigger' */
     slexAircraftFaultedExample_fa_B.FaultTrigger =
       slexAircraftFaultedExample_fa_B.Sum1_m;
   }
   /* End of Switch: '<S4>/FaultTrigger' */
   /* End of Outputs for SubSystem: '<Root>/myFault' */

See Also

Topics