Use Stateflow® charts to create test vectors that change dynamically during simulation, based on the state of the system you are modeling.
For example, suppose you want to test an automatic car transmission controller in the situation where a car is coasting. To achieve a coasting state, a driver accelerates until the transmission shifts into the highest gear, then eases up on the gas pedal. To test this scenario, you could generate a signal that represents this behavior, as in the following Signal Builder block.
However, this approach has limitations. The signal changes value based on time, but cannot respond dynamically to changes in the system that are not governed by time alone. For example, how does the signal know when the transmission shifts into the highest gear? In this case, the signal assumes that the shift always occurs at time 5 because it cannot test for other deterministic conditions such as the speed of the vehicle. Moreover, you cannot change the signal based on outputs from the model.
By contrast, you can use Stateflow charts to develop test vectors that use conditional logic to evaluate and respond to changes in system state as they occur. For example, to test the coasting scenario, the chart can evaluate an output that represents the gear range and reduce speed only after the transmission shifts to the highest gear. That is, the car slows down as a direct result of the gear shift and not at a predetermined time.
The following model of an automatic transmission controller uses a Stateflow chart to implement test vectors that represent brake, throttle, and gear shift dynamics. The chart, called Dynamic Test Vectors, interfaces with the rest of the model as shown.
The chart models the dynamic relationship between the brake and throttle to test four driving scenarios. Each scenario is represented by a state.
In some of these scenarios, the throttle changes in response to time; in other cases, it responds to gear selection, an output of the Stateflow chart Shift_logic. The Shift_logic chart determines the gear value based on the speed of the vehicle.
The key behaviors of the test vector chart and model are:
The Dynamic Test Vectors chart represents each test case as an exclusive (OR) state. Each state manipulates brake and throttle values in a unique way, based on the time and gear inputs to the chart.
The chart determines which test to execute from the value of a constant signal
case
, output from the Signal Builder block. Each test
case corresponds to a unique signal value.
The Dynamic Test Vectors chart uses conditions on transitions to test time and gear level, and then adjusts brake and throttle accordingly for each driving scenario. Stateflow charts provide many constructs for testing system state and responding to changes, including:
Conditional logic (see State Action Types and Transition Action Types)
Temporal logic (see Control Chart Execution by Using Temporal Logic)
Change detection operators (see Detect Changes in Data Values)
MATLAB® functions (see Access MATLAB Functions and Workspace Data in C Charts)
The model uses a Signal Builder block to provide an interface for selecting test scenarios to simulate.
Select and Run Test Cases. In the Signal Builder, select and run test cases as follows:
To Test: | Do This: |
---|---|
One case | Click the tab that corresponds to the driving scenario you want to test and click the Start simulation button: |
All cases and produce a model coverage report (requires a Simulink® Coverage™ software license) | Click the Run all and produce coverage button: |
The Signal Builder block sends to the Dynamic Test Vectors chart one or more constant signal values that correspond to the driving scenarios you select. The chart uses these values to activate the appropriate test cases.
Open the sf_test_vectors
model.
Open the Dynamic Test Vectors chart, the Signal Builder block, and the Scope block.
Select and simulate a driving scenario from the Signal Builder block, as described in Select and Run Test Cases.
The scope shows the interaction between speed and throttle for the selected scenario.
Driving Scenario | Scope Display | Description |
---|---|---|
Passing Maneuver |
| Driver accelerates rapidly. At t = 15 seconds, steps the throttle to 100. With continued heavy throttle, the vehicle accelerates to about 100 MPH and then shifts into overdrive at about t = 21 seconds. The vehicle cruises along in fourth gear for the remainder of the simulation. |
Gradual Acceleration |
| Driver maintains a slow but steady rate of acceleration. |
Hard Braking |
| Driver accelerates until the transmission shifts to third gear, then removes foot from the gas pedal. After a short delay, moves foot to the brake pedal and pushes hard. |
Coasting |
| Driver accelerates until transmission shifts to highest gear, then eases up on the gas. |