Control Oscillations by Using theduration
Operator
The following example focuses on the gear logic of a car as it shifts from first gear to fourth gear.
When modeling the gear changes of this system, it is important to control the oscillation that occur. The modelsf_car
uses parallel state debouncer logic that controls which gear state is active. For more information about how debouncers work in Stateflow®, seeReduce Transient Signals by Using Debouncing Logic.
You can simplify the debouncer logic by using theduration
operator. You can see this simplification in the modelsf_car_using_duration
. Theduration
operator evaluates a condition expression and outputs the length of time that the expression has beentrue
. When that length of time crosses a known time threshold, the state transitions to a higher or lower gear.
By removing the parallel state logic and using theduration
operator, you can control oscillations with simpler Stateflow logic. Theduration
operator is supported only in Stateflow charts in a Simulink®model.
Control Oscillation with Parallel State Logic
Open the modelsf_car
.
openExample("stateflow/AutomaticTransmissionWithActiveStateDataExample")
Select the chartshift_logic
and, in theState Charttab, clickLook Under Mask.
The Stateflow chartshift_logic
控制齿轮car is in, given the speed of the car and how much throttle is being applied. Withinshift_logic
there are two parallel states:gear_state
andselection_state
.gear_state
contains four exclusive states for each gear.selection_state
determines whether the car is downshifting, upshifting, or remaining in its current gear.
In this Stateflow chart, for the car to move from first gear to second gear, the eventUP
must be sent fromselection_state
togear_state
. The event is sent when the speed crosses the threshold and remains higher than the threshold for the length of time determined byTWAIT
. When the eventUP
is sent,gear_state
transitions fromfirst
tosecond
.
Control Oscillation with theduration
Operator
Open the modelsf_car_using_duration
.
openExample("stateflow/AutomaticTransmissionUsingDurationOperatorExample")
Select the chartGear_Logic
and, in theState Charttab, clickLook Under Mask.
WithinGear_Logic
there are four exclusive states for each gear. The local variablesup
anddown
guard the transitions between each state.
In this Stateflow chart, for the car to move from first gear to second gear, the conditionup
must betrue
. The conditionup
is defined as true if the length of time that the speed is greater than or equal to the threshold is greater than the length of time that is specified byTWAIT
. The conditiondown
is defined as true if the length of time that the speed is less than or equal to the threshold is greater than the length of time that is specified byTWAIT
. The operatorduration
keeps track of the length of time that the speed has been above or below the threshold. When theup
condition is met, the active state transitions fromfirst
tosecond
.
By replacing the parallel state debouncer logic with theduration
operator, you can create a simpler Stateflow chart to model the gear shifting.