Main Content

Add and Configure Faults Programmatically

This example models a flight control algorithm for the longitudinal motion of an aircraft. Under normal operating conditions, this model works without issues. However, faults in the controller hardware or software can prevent functional behavior and create hazardous conditions. By modeling faults, you can investigate their impact, and prevent or mitigate fault effects on your design if necessary.

You can create and manage faults programmatically. In this example, you programmatically open a model, add faults with behaviors, and simulate. For more information on how to define and model faults interactively, see Define and Model Faults.

Open the slexAircraftExampleFaulted example model.

mdl = "slexAircraftFaultedExample";
open_system(mdl);

This model is a discretized version of the model in the Aircraft Longitudinal Flight Control example.

Add Faults to the Model

In this example, the Controller subsystem represents the controller for the longitudinal flight control. Inside Controller, the alpha (rad) Inport block models the pitch angle sensor and the input Stick, in represents the control stick sensor input. Use the Simulink.fault.addFault function to add two faults to the output port of the Sum block, Sum1, and add one fault to the output of the Gain block, Gain3.

Simulink.fault.addFault(mdl + "/Sum1/Outport/1",Name="faultA");
Simulink.fault.addFault(mdl + "/Sum1/Outport/1",Name="faultB");
Simulink.fault.addFault(...
mdl + "/Controller/Gain3/Outport/1",Name="faultC");

Each fault you add has an associated Fault object. To modify the fault properties, retrieve the Fault objects as an array by using the Simulink.fault.findFaults function. The function returns the Fault objects indexed alphabetically by name.

faults = Simulink.fault.findFaults(mdl);

Add Behaviors to the Faults and Specify Fault Triggers

In addition to adding faults to the model, you must assign behaviors to each fault in order to simulate their effects on the model. Use the addBehavior function to assign stuck-at-0 behavior to faultA and add noise behavior to faultB and faultC. Add the behaviors to a fault model named myBehaviors in the current directory.

addBehavior(faults(1),"myBehaviors",...
FaultBehavior="mwfaultlib/Stuck-at-Ground")
addBehavior(faults(2),"myBehaviors",...
FaultBehavior="mwfaultlib/Add Noise")
addBehavior(faults(3),"myBehaviors",...
FaultBehavior="mwfaultlib/Add Noise")

If you accidentally assign fault behavior, delete the fault behavior by using the deleteBehavior function on the Fault object.

Faults are injected when specified conditions in the model occur. By default, faults are injected throughout simulation. However, you can specify different injection conditions, known as triggers. Specify the first and last faults as timed faults that are injected when the simulation time is equal to 30. Specify the trigger type and the trigger time by specifying the TriggerType and StartTime properties.

faults(1).TriggerType = "Timed";
faults(1).StartTime = 30;
faults(3).TriggerType = "Timed";
faults(3).StartTime = 30;

Create a Conditional

Specify the faultB trigger type as a conditional.

faults(2).TriggerType = "Conditional";

Conditionals have associated Conditional objects that you can create and customize. In this example, use the Simulink.fault.addConditional function to create a conditional that triggers when the controller reads a value of the stick that is greater than 0.4.

myConditional = Simulink.fault.addConditional(...
mdl,"myConditional","u1 > 0.4");

Assign the conditional to faultB.

faults(2).Conditional = "myConditional";

You need to assign the symbol u1 to an expression or a model element. Retrieve the symbol by using the getSymbols function and assign it to the value of the stick that the controller reads, which is determined by the output of the Transfer Fnc block Stick Prefilter.

mySymbol = getSymbols(myConditional);
mySymbol.Type = "Model Element";
mySymbol.Path = mdl + "/Controller/Stick Prefilter/Outport/1";

With this assignment, the conditional triggers when the stick signal value is greater than 0.8, and only then does the fault add noise to the value of the signal sent to the controller.

Enable Faults and Select the Active Fault

After defining the fault properties, enable faults on the specified model element, select the active fault, and turn on fault modeling. Enable faults on the Sum block output and disable faults on the signal from alpha (rad) with the Simulink.fault.enable function.

Simulink.fault.enable(mdl + "/Sum1/Outport/1",true);
Simulink.fault.enable(mdl + "/Controller/Gain3/Outport/1",false);

Set FaultA as the active fault.

activate(faults(1))

Simulate and Inspect Results

To see the effects of the fault, simulate the model.

sim(mdl);

To view the status of the fault injection and the simulation results, open the Simulation Data Inspector.

Simulink.sdi.view

You cannot simulate faults on continuous signals unless their destinations are discrete. Consequentially, you can only simulate faultA or faultB.

See Also

| | | |

Related Topics