Hauptinhalt

Simulate Faults on a Real-Time Target by Using a Dashboard App

R2026b
Since R2026b

This example shows how you can use faults in a real-time simulation model. To use Simulink® Fault Analyzer™ faults in real-time applications or generate code from those models, you use fault-embedded models.

In this example, you embed faults into a model, configure the model for real-time simulation, generate the real-time application and dashboard instrument panel app, then use the dashboard instrument panel app to simulate the real-time application on a real-time target.

Open the Design Model

Open the design model, sf_car_discretized.slx.

open_system("sf_car_discretized")

sf_car_discretized.slx is a discretized version of the Stateflow® car transmission model, sf_car.slx, from the example Model a Car with Automatic Transmission (Stateflow). The model uses Simulink Fault Analyzer to model two faults on the engine output signal.

The sf_car_discretized model. The Passing_Maneuver Inputs block provides Brake and Throttle signals to the Engine, shift_logic, transmission, and Vehicle blocks. A fault badge appears on the engine RPM signal between the Engine block and the transmission block.

To view the faults, open the Fault Table pane:

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

  • In the Fault Table pane, expand the Engine/Outport/1 model element.

The model includes these faults:

  • engineRPM_noise_fault — Adds white noise to the RPM signal when the simulation time is greater than 50 seconds.

  • engineRPM_offset_fault — Adds a constant bias to the engine RPM signal when the simulation time is greater than 50 seconds.

The Fault Table pane with the Fault tab selected. The expanded Engine/Outport/1 model element lists two faults, engineRPM_noise_fault and engineRPM_offset_fault, in the Model Element/Fault Name column. The Active Fault column shows engineRPM_offset_fault selected, and the Trigger column shows Timed: 50 for both faults.

Simulink Fault Analyzer can inject only one fault on a given model element for a simulation. To change the active fault, change the selection in the Active Fault column.

To control when the simulation injects the fault, each fault uses a timed trigger. After injection occurs, Simulink Fault Analyzer adds the fault behavior to the signal. To see the behavior, right-click the fault and click Open fault behavior.

Create the Fault-Embedded Model

To use a model for real-time simulation, you must embed the faults directly into the model.

To create a fault-embedded model, in the Fault Analyzer tab, click Export Embedded Model. For more information on fault embedded models, see Create Fault-Embedded Models.

The Export Embedded Model dialog box. The Exported model field is set to sf_car_discretized_faultEmbedded, and the Exported files folder field has a Browse button. The dialog box has three check boxes: Delete existing folder content is cleared, and Embed all faults in variant subsystems and Include conditional logic are selected. OK and Cancel buttons appear at the bottom.

In this example:

  • Set Exported files folder to an empty folder on the file path.

  • Clear Delete existing folder content and Include conditional logic.

  • Select Embed all faults in variant subsystems.

Click OK. Simulink Fault Analyzer saves a new copy of the model in the folder that has embedded faults.

Inspect Fault-Embedded Model

This example includes a copy of the fault-embedded model, sf_car_discretized_faultEmbedded.slx. Open the fault-embedded model:

model = "sf_car_discretized_faultEmbedded";
open_system(model)

In the embedded model, the Engine_Outport1_Fault subsystem models the faults by using a Variant Subsystem block. The Variant Subsystem block uses the fault icon as the mask for the block.

A portion of the fault-embedded model. The engine RPM signal from the Engine block Ne output passes through the Engine_Outport1_Fault block, which displays a fault icon as its mask, before reaching the Ne input of the transmission block.

Open the Engine_Outport1_Fault subsystem to view the variant subsystems. The Variant Subsystem block separates the behaviors for each fault into subsystems.

The contents of the Engine_Outport1_Fault variant subsystem. An In1 input port and an Out1 output port connect to two variant subsystems stacked vertically, one labeled engineRPM_noise_fault and the other labeled engineRPM_offset_fault, each representing one fault behavior.

To select an active fault, the variant subsystem uses the variant control variable Engine_Outport1_Fault. When you load the fault-embedded model, Simulink Fault Analyzer defines this variable in the base workspace. These values determine the active fault:

  • 0 — No active fault

  • 1 — engineRPM_noise_fault is active

  • 2 — engineRPM_offset_fault is active

To adjust the active fault for simulation, change the value of this variable in the MATLAB Command Window, then simulate the model. You can also adjust the values by using the Variant Manager or by changing the block parameters of the Variant Subsystem block.

Generate Real-Time Application from Fault-Embedded Model

To run the model on a Simulink Real-Time target, you must:

First, retrieve the target and set the model system target file to the real-time target system target file.

tg = slrealtime;
connect(tg);
modelSTF = getSTFName(tg);
set_param(model,"SystemTargetFile",modelSTF);

Then, configure the model to support code generation. To support code generation in this model, set the solver to a fixed-step solver and disable MAT-file logging.

set_param(model,"SolverType","Fixed-step")
set_param(model,"Solver","ode4")
set_param(model,"MatFileLogging","off")

Finally, generate the real-time application from the model.

evalc("slbuild(model)");

Simulink saves the real-time application in the current folder as sf_car_discretized_faultEmbedded.mldatx. This example includes a copy of this executable.

Generate a Dashboard Application

To use the real-time application to control a real-time target, create a dashboard app by using the Simulink Real-Time App Generator. You can design the app to control the fault properties and display model signals. Because the Variant activation time parameter of the Variant Subsystem block is startup, the generated code identifies the active fault before executing the simulation. This setting allows you to choose the active fault before simulating in the app in the real-time environment.

In this example, create an app that controls the injected fault, specifies the fault properties, and displays the engine RPM signal.

  1. Open the Simulink Real-Time App Generator. In the Apps tab, click SLRT App Generator.

  2. Click New and select the real-time application of the fault-embedded model, sf_car_discretized_faultEmbedded.mldatx.

  3. Add the Engine_Outport1_Fault variable as a discrete knob. The app sets this variable before the simulation starts, and the model uses its value at startup to select the active fault. Set the values of Knob States to None, Noise Fault, and Offset Fault, and set their target values to 0, 1, and 2.

  4. In the Signals and Parameters pane, expand sf_car_discretized_faultEmbedded > Engine_Outport1_Fault > engineRPM_noise_fault > FaultBehavior, then add these parameters:

    • In the engineRPM_noise_fault list, add the FaultTrigger:Threshold block parameter as a numeric edit field.

    • Under FaultBehavior, add the White Noise:Mean and White Noise:StdDev block parameters as numeric edit fields.

  5. In the Signals and Parameters pane, expand engineRPM_offset_fault > FaultBehavior, then add these parameters:

    1. In the engineRPM_offset_fault list, add the FaultTrigger:Threshold block parameter as a numeric edit field.

    2. Under FaultBehavior, add the Offset_by_1:Bias block parameter as a numeric edit field.

  6. Click Generate App and save the app. The generated app includes a plot that displays the engine RPM, throttle, and vehicle speed signals.

For more information, see Create App Designer Instrument Panels by Using App Generator (Simulink Real-Time). To view this app, open model_fault_app.mlapp. The app opens in App Designer.

The generated dashboard app running in App Designer. The top toolbar has a target selector set to TargetPC1, a Connected status, and Load Application, Start, and Stop Time controls. The left side shows an Engine_Outport1_Fault knob with the states No_Faults, Add_Noise, and Add_Offset, along with numeric edit fields labeled FaultTrigger_Threshold, FaultTrigger_Threshold_2, Offset_by_1_Bias, White_Noise_Mean, and White_Noise_Variance. An empty Instrumented Signals plot of engine RPM versus time in seconds appears on the right.

Simulate the Model on the Target

To simulate the model from the app:

  1. In the App Designer, click Run. The app opens.

  2. In the app, in the Target Selector property, select the target. To run the model in Simulink without a target, select Simulink Normal Mode.

  3. Click Connect to connect to the target.

  4. Click Load Application to load the real-time application.

  5. Change the knob to specify a fault, and adjust the fault parameters by using the numeric edit fields.

  6. Click Start. The figure plots the engine RPM versus time in real time.

You cannot change the active fault during simulation. To change the fault, change the knob setting and rerun the simulation.

See Also

Topics