Stateflow And Stateflow Coder User's Guide

Transcription

Stateflow andStateflow Coder For Complex Logic and State Diagram ModelingModelingSimulationImplementationUser’s GuideVersion 5

How to Contact The Newsgroupinfo@mathworks.comTechnical supportProduct enhancement suggestionsBug reportsDocumentation error reportsOrder status, license renewals, passcodesSales, pricing, and general information508-647-7000Phone508-647-7001FaxThe MathWorks, Inc.3 Apple Hill DriveNatick, MA athworks.comFor contact information about worldwide offices, see the MathWorks Web site.Stateflow and Stateflow Coder User’s Guide COPYRIGHT 1997 - 2003 by The MathWorks, Inc.The software described in this document is furnished under a license agreement. The software may be usedor copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc.FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation byor for the federal government of the United States. By accepting delivery of the Program, the governmenthereby agrees that this software qualifies as "commercial" computer software within the meaning of FARPart 12.212, DFARS Part 227.7202-1, DFARS Part 227.7202-3, DFARS Part 252.227-7013, and DFARS Part252.227-7014. The terms and conditions of The MathWorks, Inc. Software License Agreement shall pertainto the government’s use and disclosure of the Program and Documentation, and shall supersede anyconflicting contractual terms or conditions. If this license fails to meet the government’s minimum needs oris inconsistent in any respect with federal procurement law, the government agrees to return the Programand Documentation, unused, to MathWorks.MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered trademarks, andTargetBox is a trademark of The MathWorks, Inc.Other product or brand names are trademarks or registered trademarks of their respective holders.Printing History: May 1997January 1999September 2000June 2001October 2001July 2002January 2003First printingSecond printingThird printingFourth printingOnline onlyFifth printingOnline onlyRevised for Stateflow 2.0 (Release 11)Revised for Stateflow 4.0 (Release 12)Revised for Stateflow 4.1 (Release 12.1)Revised for Stateflow 4.2 (Release 12.1 )Revised for Stateflow 5.0 (Release 13)Revised for Stateflow 5.1 (Release 13.SP1)Renamed from Stateflow User’s Guide

ContentsPrefaceSystem Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxUsing Stateflow on a Laptop Computer . . . . . . . . . . . . . . . . xxxiRelated Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiiUsing This Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiiiTypographical Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvInstalling Stateflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxviIntroduction to Stateflow1What Is Stateflow? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Stateflow Is Part of Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . .Stateflow Is a Finite State Machine . . . . . . . . . . . . . . . . . . . . . .Stateflow Adds Flow Diagrams to the State Machine . . . . . . . .Stateflow Simulates its State Machine . . . . . . . . . . . . . . . . . . .Stateflow Generates Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-21-21-31-41-51-7Build a Stateflow Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8Creating a Simulink Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8Creating States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10Creating Transitions and Junctions . . . . . . . . . . . . . . . . . . . . . 1-11Define Input Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15Define Input Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-17Define the Stateflow Interface . . . . . . . . . . . . . . . . . . . . . . . . . 1-18Define Simulink Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20i

Parse the Stateflow Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20Run a Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21Debug the Model During Simulation . . . . . . . . . . . . . . . . . . . . 1-24More About Stateflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Examples of Stateflow Applications . . . . . . . . . . . . . . . . . . . . .Stateflow Works with Simulink and Real-Time Workshop . . .Stateflow and Simulink Design Approaches . . . . . . . . . . . . . .1-261-261-261-27How Stateflow Works2Finite State Machine Concepts . . . . . . . . . . . . . . . . . . . . . . . . .What Is a Finite State Machine? . . . . . . . . . . . . . . . . . . . . . . . . .Finite State Machine Representations . . . . . . . . . . . . . . . . . . . .Stateflow Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-22-22-22-32-32-42-4Stateflow and Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Simulink Model and the Stateflow Machine . . . . . . . . . . . .Stateflow Data Dictionary of Objects . . . . . . . . . . . . . . . . . . . . .Defining Stateflow Interfaces to Simulink . . . . . . . . . . . . . . . . .2-52-52-62-7Stateflow Graphical Components . . . . . . . . . . . . . . . . . . . . . . . 2-9Stateflow Diagram Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .Graphical Objects Example Diagram . . . . . . . . . . . . . . . . . . . .States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Default Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .History Junction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Connective Junctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-19Stateflow Hierarchy of Objects . . . . . . . . . . . . . . . . . . . . . . . . 2-21Exploring a Real-World Stateflow Application . . . . . . . . . .Overview of the “fuel rate controller” Model . . . . . . . . . . . . . .Control Logic of the “fuel rate controller” Model . . . . . . . . . . .Simulating the “fuel rate controller” Model . . . . . . . . . . . . . . .2-222-222-252-28Stateflow Notation3Overview of Stateflow Objects . . . . . . . . . . . . . . . . . . . . . . . . . .Graphical Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Nongraphical Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Data Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .How Hierarchy Is Represented . . . . . . . . . . . . . . . . . . . . . . . . . .3-23-23-33-43-4States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Is a State? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .State Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .State Label Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-73-73-83-9Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Is a Transition? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Transition Label Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Valid Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-133-133-143-16Transition Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Transitions to and from Exclusive (OR) States . . . . . . . . . . . .Transitions to and from Junctions . . . . . . . . . . . . . . . . . . . . . .Transitions to and from Exclusive (OR) Superstates . . . . . . . .Transitions to and from Substates . . . . . . . . . . . . . . . . . . . . . .Self-Loop Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Inner Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-173-173-183-183-193-213-21Default Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25iii

What Is a Default Transition?Drawing Default TransitionsLabeling Default TransitionsDefault Transition Examples.3-253-253-263-26Connective Junctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30What Is a Connective Junction? . . . . . . . . . . . . . . . . . . . . . . . . 3-30Flow Diagram Notation with Connective Junctions . . . . . . . . 3-31History Junctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-37What Is a History Junction? . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-37History Junctions and Inner Transitions . . . . . . . . . . . . . . . . . 3-38Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-39Graphical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-40Stateflow Semantics4Executing an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3Sources for Stateflow Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3Processing Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4Executing a Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5Executing an Inactive Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5Executing an Active Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5ivContentsExecuting a Transition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Transition Flow Graph Types . . . . . . . . . . . . . . . . . . . . . . . . . . .Executing a Set of Flow Graphs . . . . . . . . . . . . . . . . . . . . . . . . .Ordering Single Source Transitions . . . . . . . . . . . . . . . . . . . . . .4-64-64-74-8Executing a State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Entering a State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Executing an Active State . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exiting an Active State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-134-134-154-15

State Execution Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16Early Return Logic for Event Broadcasts . . . . . . . . . . . . . . . 4-18Semantic Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21Transitions to and from Exclusive (OR) States ExamplesLabel Format for a State-to-State Transition Example . . . . . .Transitioning from State to State with Events Example . . . .Transitioning from a Substate to a Substatewith Events Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-234-234-24Condition Action Examples . . . . . . . . . . . . . . . . . . . . . . . . . . .Condition Action Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Condition and Transition Actions Example . . . . . . . . . . . . . . .Condition Actions in For Loop Construct Example . . . . . . . . .Condition Actions to Broadcast Eventsto Parallel (AND) States Example . . . . . . . . . . . . . . . . . . . . . .Cyclic Behavior to Avoid with Condition Actions Example . . .4-294-294-314-32Default Transition Examples . . . . . . . . . . . . . . . . . . . . . . . . . .Default Transition in Exclusive (OR) Decomposition ExampleDefault Transition to a Junction Example . . . . . . . . . . . . . . . .Default Transition and a History Junction Example . . . . . . . .Labeled Default Transitions Example . . . . . . . . . . . . . . . . . . .4-354-354-364-374-39Inner Transition Examples . . . . . . . . . . . . . . . . . . . . . . . . . . .Processing Events with an Inner Transitionin an Exclusive (OR) State Example . . . . . . . . . . . . . . . . . . . . .Processing Events with an Inner Transitionto a Connective Junction Example . . . . . . . . . . . . . . . . . . . . . .Inner Transition to a History Junction Example . . . . . . . . . . .4-41Connective Junction Examples . . . . . . . . . . . . . . . . . . . . . . . .Label Format for Transition Segments Example . . . . . . . . . . .If-Then-Else Decision Construct Example . . . . . . . . . . . . . . . .Self-Loop Transition Example . . . . . . . . . . . . . . . . . . . . . . . . . .For Loop Construct Example . . . . . . . . . . . . . . . . . . . . . . . . . . .Flow Diagram Notation Example . . . . . . . . . . . . . . . . . . . . . . .4-504-504-514-534-544-554-274-324-334-414-454-48v

Transitions from a Common Sourceto Multiple Destinations Example . . . . . . . . . . . . . . . . . . . . . .Transitions from Multiple Sourcesto a Common Destination Example . . . . . . . . . . . . . . . . . . . . .Transitions from a Sourceto a Destination Based on a Common Event Example . . . . . .Backtracking Behavior in Flow Graphs Example . . . . . . . . . .4-574-594-604-61Event Actions in a Superstate Example . . . . . . . . . . . . . . . . 4-63Parallel (AND) State Examples . . . . . . . . . . . . . . . . . . . . . . . .Event Broadcast State Action Example . . . . . . . . . . . . . . . . . .Event Broadcast Transition Actionwith a Nested Event Broadcast Example . . . . . . . . . . . . . . . . .Event Broadcast Condition Action Example . . . . . . . . . . . . . .4-654-654-694-72Directed Event Broadcasting Examples . . . . . . . . . . . . . . . . 4-77Directed Event Broadcast Using send Example . . . . . . . . . . . 4-77Directed Event BroadcastingUsing Qualified Event Names Example . . . . . . . . . . . . . . . . . . 4-79Working with Charts5Creating a Stateflow Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3Using the Stateflow Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6Stateflow Diagram Editor Window . . . . . . . . . . . . . . . . . . . . . . . 5-7Drawing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8Displaying the Context Menu for Objects . . . . . . . . . . . . . . . . . 5-10Specifying Colors and Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10Selecting and Deselecting Objects . . . . . . . . . . . . . . . . . . . . . . . 5-13Cutting and Pasting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14Copying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14Editing Object Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15Viewing Data and Events from the Editor . . . . . . . . . . . . . . . . 5-15Zooming a Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15viContents

Undoing and Redoing Editor Operations . . . . . . . . . . . . . . . . . 5-17Keyboard Shortcuts for Stateflow Diagrams . . . . . . . . . . . . . . 5-18Using States in Stateflow Charts . . . . . . . . . . . . . . . . . . . . . .Creating a State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Moving and Resizing States . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Substates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Grouping States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Specifying Substate Decomposition . . . . . . . . . . . . . . . . . . . . .Specifying Activation Order for Parallel States . . . . . . . . . . . .Changing State Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . .Labeling States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Outputting State Activity to Simulink . . . . . . . . . . . . . . . . . . .5-215-215-225-235-235-245-245-255-265-29Using Transitions in Stateflow Charts . . . . . . . . . . . . . . . . .Creating a Transition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Straight Transitions . . . . . . . . . . . . . . . . . . . . . . . . . .Labeling Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Moving Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Changing Transition Arrowhead Size . . . . . . . . . . . . . . . . . . . .Creating Self-Loop Transitions . . . . . . . . . . . . . . . . . . . . . . . . .Creating Default Transitions . . . . . . . . . . . . . . . . . . . . . . . . . .Setting Smart Behavior in Transitions . . . . . . . . . . . . . . . . . .What Smart Transitions Do . . . . . . . . . . . . . . . . . . . . . . . . . . .What Nonsmart Transitions Do . . . . . . . . . . . . . . . . . . . . . . . .Changing Transition Properties . . . . . . . . . . . . . . . . . . . . . . . sing Boxes in Stateflow Charts . . . . . . . . . . . . . . . . . . . . . . . 5-50Using Graphical Functions in Stateflow Charts . . . . . . . . .Creating a Graphical Function . . . . . . . . . . . . . . . . . . . . . . . . .Calling Graphical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .Exporting Graphical Functions . . . . . . . . . . . . . . . . . . . . . . . . .Specifying Graphical Function Properties . . . . . . . . . . . . . . . .5-515-515-565-565-58Using Junctions in Stateflow Charts . . . . . . . . . . . . . . . . . . .Creating a Junction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Changing Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Moving a Junction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-605-605-615-61vii

Editing Junction Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-61Using Notes in Stateflow Charts . . . . . . . . . . . . . . . . . . . . . . .Creating Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Editing Existing Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Changing Note Font and Color . . . . . . . . . . . . . . . . . . . . . . . . .Moving Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Deleting Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-645-645-655-655-665-66Using Subcharts in Stateflow Charts . . . . . . . . . . . . . . . . . . .What Is a Subchart? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating a Subchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Manipulating Subcharts as Objects . . . . . . . . . . . . . . . . . . . . .Opening a Subchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Editing a Subchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Navigating Subcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-675-675-695-715-715-725-73Using Supertransitions in Stateflow Charts . . . . . . . . . . . .What Is a Supertransition? . . . . . . . . . . . . . . . . . . . . . . . . . . . .Drawing a Supertransition . . . . . . . . . . . . . . . . . . . . . . . . . . . .Labeling Supertransitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5-755-755-765-81Specifying Chart Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 5-82Checking the Chart for Errors . . . . . . . . . . . . . . . . . . . . . . . . 5-87Creating Chart Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-88Printing and Reporting on Charts . . . . . . . . . . . . . . . . . . . . .Printing and Reporting on Stateflow Charts . . . . . . . . . . . . . .Generating a Model Report in Stateflow . . . . . . . . . . . . . . . . .Printing the Current Stateflow Diagram . . . . . . . . . . . . . . . . .Printing a Stateflow Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . .viii Contents5-895-895-915-945-94

Defining Events and Data6Defining Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2Adding Events to the Data Dictionary . . . . . . . . . . . . . . . . . . . . 6-2Setting Event Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4Defining Local Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8Defining Input Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8Defining Output Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9Exporting Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9Importing Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10Specifying Trigger Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11Implicit Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-12Defining Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Adding Data to the Data Dictionary . . . . . . . . . . . . . . . . . . . . .Setting Data Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Defining Data Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Defining Input Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Defining Output Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Associating Ports with Data . . . . . . . . . . . . . . . . . . . . . . . . . . .Defining Temporary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Importing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ction Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .State Action Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Transition Action Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example of Action Type Execution . . . . . . . . . . . . . . . . . . . . . . .7-37-37-77-9Operations in Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Binary and Bitwise Operations . . . . . . . . . . . . . . . . . . . . . . . . .Unary Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Unary Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-127-127-157-16ix

Assignment Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16Pointer and Address Operations . . . . . . . . . . . . . . . . . . . . . . . . 7-17Type Cast Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-18Special Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-19Using Fixed-Point Data in Actions . . . . . . . . . . . . . . . . . . . . .Fixed-Point Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .How Stateflow Implements Fixed-Point Data . . . . . . . . . . . . .Specifying Fixed-Point Data in Stateflow . . . . . . . . . . . . . . . . .Tips and Tricks for Using Fixed-Point Data in Stateflow . . . .Offline and Online Conversions of Fixed-Point Data . . . . . . . .Fixed-Point Context-Sensitive Constants . . . . . . . . . . . . . . . . .Supported Operations with Fixed-Point Operands . . . . . . . . .Promotion Rules for Fixed-Point Operations . . . . . . . . . . . . . .Assignment ( , : ) Operations . . . . . . . . . . . . . . . . . . . . . . . . . .Overflow Detection for Fixed-Point Types . . . . . . . . . . . . . . . .Sharing Fixed-Point Data with Simulink . . . . . . . . . . . . . . . . .Fixed-Point “Bang-Bang Control” Example . . . . . . . . . . . . . . -45Calling C Functions in Actions . . . . . . . . . . . . . . . . . . . . . . . .Calling C Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .Calling the abs Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Calling min and max Functions . . . . . . . . . . . . . . . . . . . . . . . .Calling User-Written C Code Functions . . . . . . . . . . . . . . . . . .7-497-497-507-507-51Using MATLAB Functions and Data in Actions . . . . . . . . .ml Namespace Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ml Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ml Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Which ml Should I Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ml Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Inferring Return Size for ml Expressions . . . . . . . . . . . . . . . . .7-547-547-557-577-587-597-61Data and Event Arguments in Actions . . . . . . . . . . . . . . . . . 7-66Arrays in Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-68Array Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-68Arrays and Custom Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-69xContents

Broadcasting Events in Actions . . . . . . . . . . . . . . . . . . . . . . . 7-70Event Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-70Directed Event Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-72Condition Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-75Using Temporal Logic Operators in Actions . . . . . . . . . . . .Rules for Using Temporal Logic Operators . . . . . . . . . . . . . . .after Temporal Logic Operator . . . . . . . . . . . . . . . . . . . . . . . . .before Temporal Logic Operator . . . . . . . . . . . . . . . . . . . . . . . .at Temporal Logic Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .every Temporal Logic Operator . . . . . . . . . . . . . . . . . . . . . . . . .Conditional and Event Notation . . . . . . . . . . . . . . . . . . . . . . . .Temporal Logic Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-767-767-787-797-807-817-827-82Using Bind Actions to Control Function-Call SubsystemsBinding a Function-Call Subsystem Trigger Example . . . . . .Simulating a Bound Function Call Subsystem Event . . . . . . .Avoiding Muxed Trigger Events With Binding . . . . . . . . . . . .7-847-847-867-89Defining Stateflow Interfaces8Overview Stateflow Interfaces . . . . . . . . . . . . . . . . . . . . . . . . .Stateflow Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Typical Tasks to Define Stateflow Interfaces . . . . . . . . . . . . . . .Where to Find More Information on Events and Data . . . . . . .8-28-28-38-3Setting the Stateflow Block Update Method . . . . . . . . . . . . . 8-4Stateflow Block Update Methods . . . . . . . . . . . . . . . . . . . . . . . . 8-4Adding Input or Output Data and Events to Charts . . . . . .Adding Input Events from Simulink . . . . . . . . . . . . . . . . . . . . . .Adding Output Events to Simulink . . . . . . . . . . . . . . . . . . . . . .Adding Input Data from Simulink . . . . . . . . . . . . . . . . . . . . . . .Adding Output Data to Simulink . . . . . . . . . . . . . . . . . . . . . . . .8-68-68-78-88-9xi

Implementing Interfaces in Stateflow to Simulink . . . . . .Defining a Triggered Stateflow Block . . . . . . . . . . . . . . . . . . . .Defining a Sampled Stateflow Block . . . . . . . . . . . . . . . . . . . . .Defining an Inherited Stateflow Block . . . . . . . . . . . . . . . . . . .Defining a Continuous Stateflow Block . . . . . . . . . . . . . . . . . .Defining Function Call Output Events . . . . . . . . . . . . . . . . . . .Defining Edge-Triggered Output Events . . . . . . . . . . . . . . . . .8-118-118-128-138-148-168-19MATLAB Workspace Interfaces . . . . . . . . . . . . . . . . . . . . . . . 8-22Examining the MATLAB Workspace in MATLAB . . . . . . . . . 8-22Interfacing the MATLAB Workspace in Stateflow . . . . . . . . . 8-22Interface to External Sources . . . . . . . . . . . . . . . . . . . . . . . . .Exported Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Imported Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exported Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Imported Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-238-238-258-278-29Truth Tables9Introduction to Truth Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2What Are Truth Tables? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2Your First Truth Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4Create a Simulink Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-5Create a Stateflow Diagram . . . . . . . . . . . . . . . . .

Stateflow and Stateflow Coder User’s Guide COPYRIGHT 1997 - 2003 by The MathWorks, Inc. The software described