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 model
sf_car uses parallel state debouncer logic that
controls which gear state is active. For more information about how debouncers work in
Stateflow®, see Reduce Transient Signals by Using Debouncing Logic.
You can simplify the debouncer logic by using the
duration operator. You can see this simplification in the model
duration operator evaluates
a condition expression and outputs the length of time that the expression has been
true. 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 the
you can control oscillations with simpler Stateflow logic. The
duration operator is supported only in Stateflow charts in a Simulink® model.
Open the model
shift_logic is highlighted, select Diagram
> Mask > Look Under Mask.
The Stateflow chart
shift_logic controls which gear the car is in, given
the speed of the car and how much throttle is being applied. Within
shift_logic there are two parallel states:
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 event
UP must be sent from
gear_state. The event is sent when the speed crosses the threshold and
remains higher than the threshold for the length of time determined by
TWAIT. When the event
UP is sent,
gear_state transitions from
Open the model
Gear_Logic is highlighted, select
Diagram > Mask > Look
Gear_Logic there are four exclusive states for each gear. The
down guard the transitions
between each state.
In this Stateflow chart, for the car to move from first gear to second gear, the condition
up must be
true. The condition
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 by
down 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
TWAIT. The operator
duration keeps track
of the length of time that the speed has been above or below the threshold.
condition is met, the active state transitions from
By replacing the parallel state debouncer logic with the
operator, you can create a simpler Stateflow chart to model the gear shifting.