Model Media Player by Using Enumerated Data

This example shows how to model a media player by using enumerated data in Stateflow®. The media player consists of a Simulink® model and a MATLAB® user interface (UI). The model has these components:

  • User Request is a Stateflow chart that reads and stores user inputs from UI.

  • Media Player Mode Manager is a Stateflow chart that determines whether the media player operates in AM radio, FM radio, or CD player mode.

  • CD Player Behavior Model is a Stateflow chart that describes the behavior of the CD player component.

These charts use enumerated data to group related values into separate data types, reduce the amount of data, and enhance readability. For more information, see Reference Values by Name by Using Enumerated Data.

Create Groups of Related Data Values

The model uses two enumerated data types to group the possible operating modes for the media player and for its CD player component. The Media Player Helper UI separates these modes into two groups of buttons.

The Radio Request section contains buttons for selecting an operating mode for the media player. The enumerated values for the data type RadioRequestMode correspond to these media player operating modes:

  • OFF(0)

  • CD(1)

  • FM(2)

  • AM(3)

The CD Request section contains buttons for selecting an operating mode for the CD player component. The Insert Disc and Eject Disc buttons also affect this operating mode. The enumerated values for the data type CdRequestMode correspond to these CD player operating modes:

  • EMPTY(-2)

  • DISCINSERT(-1)

  • STOP(0)

  • PLAY(1)

  • REW(3)

  • FF(4)

  • EJECT(5)

At the start of the model simulation, the Display blocks show the default settings of the media player.

To change the enumerated values in the Display blocks, use the Media Player Helper to select other operating modes. For example:

  1. In the Radio Request section, click CD. The Display blocks for enumerated data RR and CurrentRadioMode change from OFF to CD.

  2. Click Insert Disc. The Display block for enumerated data CdStatus changes from EMPTY to DISCINSERT to STOP.

  3. In the CD Request section, click PLAY. The Display blocks for enumerated data CR, MechCmd, and CdStatus change from STOP to PLAY.

Read Input from User Interface

The User Request chart reads requests from the Media Player Helper UI and stores the information as these outputs:

  • RR: Enumerated data representing a Radio Request button.

  • CR: Enumerated data representing a CD Request button.

  • DiscInsert: Boolean data representing the Insert Disc button.

  • DiscEject: Boolean data representing the Eject Disc button.

To read the input from the UI, the chart uses the ml namespace operator to call the function sfcdplayerhelper on the MATLAB path. For more information, see Access MATLAB Functions and Workspace Data in C Charts.

Select Mode Based on Changes in Enumerated Data

The Media Player Mode Manager chart activates a subcomponent of the media player depending on the input from the User Request chart.

At the start of the model simulation, the ModeManager state is active. If the Boolean input data DiscEject becomes true, a transition to the Eject state occurs, followed by a transition back to the ModeManager state.

When ModeManager is active, the previously active substate (Standby or ON, as recorded by the history junction) becomes active. Subsequent transitions between the Standby and ON substates depend on the enumerated input data RadioReq:

  • If RadioReq is OFF, the Standby substate is activated.

  • If RadioReq is not OFF, the ON substate is activated.

In the ON substate, three subcharts represent the operating modes of the media player: CD player, AM radio, and FM radio. Each subchart corresponds to a different value of enumerated input data RadioReq:

  • If RadioReq is CD, the CDMode subchart is activated. The subchart outputs PLAY, REW, FF, and STOP commands to the CD Player Behavior Model chart.

  • If RadioReq is AM, the AMMode subchart is activated. The subchart outputs a STOP command to the CD Player Behavior Model chart.

  • If RadioReq is FM, the FMMode subchart is activated. The subchart outputs a STOP command to the CD Player Behavior Model chart.

To scan for changes in the value of RadioReq, the inner transition inside the ON state calls the change detection operator hasChanged at every time step.

Control Timing of Transitions

The CD Player Behavior Model chart implements the behavior of the CD player mechanism depending on the input from the User Request and Media Player Mode Manager charts. To model the mechanical delays in the CD player, the chart uses the absolute-time temporal logic operator after. For instance:

  • At the start of the model simulation, the Empty state is activated. If the Boolean input data DiscInsert is true, a transition to the Inserting state occurs. After a one-second delay, a transition to the DiscPresent state occurs.

  • The DiscPresent state remains active until the input data CMD becomes EJECT. At that point, a transition to the Ejecting state occurs. After a one-second delay, a transition to the Empty state occurs.

Whenever a state transition occurs, the enumerated output data CdStatus changes value to reflect the status of the CD player:

  • Active substate is Empty (CD player is empty): CdStatus = EMPTY.

  • Active substate is Inserting (CD player is loading a disc): CdStatus = DISCINSERT.

  • Active substate is Ejecting (CD player is ejecting a disc): CdStatus = EJECT.

  • Active substate is DiscPresent.STOP (CD player is stopped): CdStatus = STOP.

  • Active substate is DiscPresent.PLAY (CD player is playing): CdStatus = PLAY.

  • Active substate is DiscPresent.REW (CD player is rewinding): CdStatus = REW.

  • Active substate is DiscPresent.FF (CD player is fast-forwarding): CdStatus = FF.

See Also

|

Related Topics