Refine, Test, and Debug a Subsystem
Test harnesses provide a development and testing environment that leaves the main model design intact. You can test a functional unit of your model in isolation without altering the main model. This example demonstrates refining and testing a controller subsystem using a test harness. The main model is a controller-plant model of an air conditioning/heat pump unit. The controller must operate according to several simple requirements.
Model and Requirements
Access the model. Enter
cd(fullfile(docroot,'toolbox','sltest','examples'))
Copy this model file and supporting files to a writable location on the MATLAB® path:
sltestHeatpumpExample.slx sltestHeatpumpBusPostLoadFcn.mat PumpDirection.m
Open the model.
open_system('sltestHeatpumpExample')
In the example model:
The controller accepts the room temperature and the set temperature inputs.
The controller output is a bus with signals controlling the fan, heat pump, and the direction of the heat pump (heat or cool).
The plant accepts the control bus. The heat pump and the fan signals are Boolean, and the heat pump direction is specified by
+1
for cooling and-1
for heating.
The test covers four temperature conditions. Each condition corresponds to one operating state with fan, pump, and pump direction signal outputs.
Temperature condition | System state | Fan command | Pump command | Pump direction |
---|---|---|---|---|
|Troom - Tset| < DeltaT_fan | idle | 0 | 0 | 0 |
DeltaT_fan <= |Troom - Tset| <
DeltaT_pump | fan only | 1 | 0 | 0 |
|Troom - Tset| >= DeltaT_pump and
Tset < Troom | cooling | 1 | 1 | -1 |
|Troom - Tset| >= DeltaT_pump and
Tset > Troom | heating | 1 | 1 | 1 |
Create a Harness for the Controller
Right-click the
Controller
subsystem and select Test Harness > Create for ‘Controller’.Set the harness properties:
In the Basic Properties tab:
Name:
devel_harness1
Clear Save test harness externally
Sources and Sinks: None and Scope
Clear Add separate assessment block
Select Open harness after creation
Click OK to create the test harness.
Inspect and Refine the Controller
In the test harness, double-click
Controller
to open the subsystem.Connect the chart to the Inport blocks.
In the test harness, click the Save button to save the test harness and model.
Add Test Inputs and Test the Controller
Navigate to the top level of
devel_harness1
.Create a test input for the harness with a constant
Tset
and a time-varyingTroom
. Connect a Constant block to theTset
input and set the value to75
.Add a Sine Wave block to the harness model to simulate a temperature signal. Connect the Sine Wave block to the conversion subsystem input
Troom_in
.Double-click the Sine Wave block and set the parameters:
Amplitude:
15
Bias:
75
Frequency:
2*pi/3600
Phase (rad): 0
Sample time:
1
Select Interpret vector parameters as 1–D.
Connect Inport blocks to the Data Store Write inputs.
In the Configuration Parameters dialog box, in the Data Import/Export pane, select Input and enter
u
.u
is an existing structure in the MATLAB base workspace.In the Solver pane, set Stop time to
3600
.Open the scope in the test harness and change the layout to show three plots.
Click Run to simulate.
Debug the Controller
Observe the controller output.
fan_cmd
is1
during the IDLE condition where|Troom - Tset| < DeltaT_fan
.This is a bug.
fan_cmd
should equal0
atIDLE
. Thefan_cmd
control output must be changed forIDLE
.In the harness model, open the
Controller
subsystem.Open
controller_chart
.In the
IDLE
state,fan_cmd
is set to return1
. Changefan_cmd
to return0
.IDLE
is now:IDLE entry: fan_cmd = 0; pump_cmd = 0; pump_dir = 0;
Simulate the harness model again and observe the outputs.
fan_cmd
now meets the requirement to equal0
atIDLE
.