control oscillations by using the duration
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 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 .
you can simplify the debouncer logic by using the operator. you can see this simplification in the model
sf_car_using_duration
. the 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 duration
operator,
you can control oscillations with simpler stateflow logic. the duration
operator is supported only in stateflow charts in a simulink® model.
control oscillation with parallel state logic
open the model sf_car
.
openexample("stateflow/automatictransmissionwithactivestatedataexample")
select the chart shift_logic
and, in the state
chart tab, click 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
and selection_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 event
up
must be sent from selection_state
to
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 first
to
second
.
control oscillation with the duration
operator
open the model sf_car_using_duration
.
openexample("stateflow/automatictransmissionusingdurationoperatorexample")
select the chart gear_logic
and, in the state
chart tab, click look under mask.
within gear_logic
there are four exclusive states for each gear. the
local variables up
and 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 up
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 twait
.
the condition 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
specified by twait
. the operator duration
keeps track
of the length of time that the speed has been above or below the threshold.
when the up
condition is met, the active state transitions from first
to
second
.
by replacing the parallel state debouncer logic with the duration
operator, you can create a simpler stateflow chart to model the gear shifting.