Mastering Simulink - CERN Document Server

20 downloads 0 Views 511KB Size Report
15.4 xPC Target. 342. 15.4.1 Initialization. 342. 15.4.2 Building a Target. 343. 15.4.3 Starting Target. 345. 15.4.4 Controlling the Target. 345. 15.5 Summary. 347.
Mastering Simulink

James B. Dabney and Thomas L. Harman University of Houston—Clear Lake

PEARSON 1'1111We I Iall

Upper Saddle River, New Jersey 07458

Contents Preface

Using the Book New Users Experienced Users Advanced Users Acknowledgments

xvii

xvii xvü xviii xviii xix

1 Introduction

1.1 What Is Simulink? 1.2 Simulink Examples 1.2.1 Steel Rolling Plant Modeling 1.2.2 Automotive Engine Modeling 1.3 Simulink Employment Strategies 1.3.1 Rapid Prototyping 1.3.2 Rapid Application Development 1.4 Selection Considerations 1.5 Outline of Text References 2 Block Diagrams 2.1 Introduction 2.2 Continuous Systems 2.2.1 Gain Blocks 2.2.2 Sum Block 2.2.3 Derivative Block 2.2.4 Integrator 2.2.5 Simple Physical Models 2.2.6 Transfer Function Block 2.3 State-Space Block 2.3.1 The State-Space Concept ................. 2.3.2 Linear Single-Input, Single-Output Systems 2.3.3 Multiple Input, Multiple Output Systems 2.4 Discrete Systems 2.4.1 Discrete Gain Block 2.4.2 Discrete Sum Block

1













1 2 2 3 4 4 5 5 6 6 7 7 8 8 9 9 10 11 11 13 13 15 18 20 22 22

x Contents

2.5 2.6 2.7 2.8 3

22 23 24 25 26 28 28 30 30

Unit Delay 2.4.3 Discrete-Time Integrator Block 2.4.4 Simple Discrete System Models 2.4.5 Discrete Transfer Function Block 2.4.6 Discrete State-Space Block Nonlinear Blocks Hybrid Block Diagrams Summary References

32

Quick Start

3.1 3.2 3.3 3.4

3.5

Introduction Typographical Conventions 3.1.1 Building a Simple Model A More Complicated Model The Simulink Help System 3.4.1 Opening the User's Guide Browser User's Guide Browser Window . . . . 3.4.2 Summary References

4 Model Building

4.1 4.2 4.3

4.4

4.5

Introduction 4.1.1 Elements of a Model Opening a Model Model Window 4.3.1 Zooming 4.3.2 Block Data Tips 4.3.3 Simulink Block Library Manipulating Blocks 4.4.1 Resizing a Block 4.4.2 Rotating a Block 4.4.3 Copying a Block within a Model 4.4.4 Deleting Blocks 4.4.5 Selecting Multiple Blocks 4.4.6 Changing a Block Label 4.4.7 Changing Label Location 4.4.8 Hiding a Label .4.4.9 Adding a Drop Shadow 4.4.10 Using Color 4.4.11 Configuring Blocks Signal Lines 4.5.1 Moving a Segment 4.5.2 Moving a Vertex 4.5.3 Deleting a Signal Line 4.5.4 Splitting a Signal Line 4.5.5 Labeling a Signal Line

.

32 32 33 38 46 46 47 48 48 49

49 49 50 50 52 52 52 53 53 54 54 55 55 56 57 57 57 58 58 58 59 61 61 62 62

Contents

4.6

4.7

4.8

4.9 4.10

4.11 4.12

4.13 4.14

4.5.6 Moving or Copying a Signal Line Label 4.5.7 Editing a Signal Line Label 4.5.8 Signal Label Propagation Annotations ................. . Adding Annotations 4.6.1 Annotation Formatting 4.6.2 Adding Sources Common Sources 4.7.1 4.7.2 From Workspace Block 4.7.3 From File Input Block Adding Sinks Scope Block 4.8.1 4.8.2 XY Graph Data Types Configuring the Simulation 4.10.1 Solver Page 4.10.2 Workspace 1/0 Page 4.10.3 Diagnostics Page 4.10.4 Advanced Page Running a Simulation Printing a Model 4.12.1 Printing to the Printer Using Menus 4.12.2 Embedding the Model in a Document 4.12.3 Using the MATLAB Print Command Model Building Summary Summary References

5 Continuous Systems 5.1 Introduction 5.2 Scalar Linear Systems 5.2.1 Integrator Block 5.2.2 Transfer Function Blocks 5.3 Vector Linear Systems 5.3.1 Vector Signal Lines 5.3.2 State-Space Block 5.4 Modeling Nonlinear Systems 5.4.1 Function Blocks Summary 5.5 References 6 Discrete-Time Systems Introduction 6.1 Scalar Linear Discrete-Time Systems 6.1.1 Unit Delay 6.1.2 Discrete-Time Integrator 6.1.3 Discrete Transfer Function Blocks 6.1.4



xi

63 64 64 65 65 66 66 66 68 70 70 70 75 75 77 78 80 82 83 84 85 85 86 86 88 89 89 90 90 90 90 94 96 96 98 98 102 105 106 107 107 107 108 108 110

xii

Contents 6.2 6.3 6.4 6.5 6.6

Logical Blocks Vector Discrete-Time Systems Multirate Discrete-Time Systems Hybrid Systems Summary References

7 Subsystems and Masking Introduction 7.1 Simulink Subsystems 7.2 ..... • • . . Encapsulating a Subsystem 7.2.1 Subsystem Blocks 7.2.2 Masked Blocks 7.3 Converting a Subsystem into a Masked Subsystem 7.3.1 Mask Editor Documentation Page 7.3.2 7.3.3 Parameters Page 7.3.4 Initialization Page 7.3.5 Mask Editor Icon Page 7.3.6 Looking Under and Removing Masks 7.3.7 Using Masked Blocks 7.3.8 Creating a Block Library 7.3.9 Configurable Subsystems 7.4 Conditionally Executed Subsystems 7.4.1 Enabled Subsystems 7.4.2 Triggered Subsystems 7.4.3 Trigger-When-Enabled Subsystems 7.4.4 Discrete Conditionally Executed Subsystems . 7.5 Logical Subsystems 7.6 Iterative Subsystems 7.6.1 For Iterator Subsystem 7.6.2 While Iterator Subsystem Summary 7.7 References 8 Simulink Analysis Tools 8.1 Introduction 8.2 Determining Model Characteristics 8.2.1 Simulink State Vector Definition 8.2.2 Using the model Command 8.3 Executing Models from MATLAB . . . .. 8.3.1 Using sim to Run a Simulation 8.4 Setting Simulation Parameters with simset 8.4.1 Obtaining Simulation Parameters with simget 8.5 Linearization Tools 8.5.1 Linearization 8.5.2 Simulink Linearization Commands 8.6 Trim Tools

112 114 114 116 118 119 120 120 120 122 124 126 128 128 129 134 137 145 145 146 148 151 151 155 156 156 156 157 157 158 161 161 162 162 162 162 163 167 167 169 172 176 176 177 186

Contents xiii 8.7 Optimization Toolbox and Simulink 8.8 Other Toolboxes Useful with Simulink 8.9 Summary References

9 Callbacks 9.1 Introduction 9.2 Callback Function Overview 9.2.1 Callback Installation Dialog Boxes 9.2.2 Installing Callbacks Using set_param 9.3 Model Construction Commands 9.3.1 Finding the Name of the Current Block 9.3.2 Finding the Name of the Current Model 9.3.3 Finding the Name of the Current System 9.3.4 Setting Parameter Values 9.3.5 Reading Parameter Values 9.4 Graphical User Interfaces with Callbacks 9.4.1 Graphic User Interface Callback 9.4.2 Programming Issues 9.5 Callback-Based Animation 9.6 Summary References 10 S-Functions

10.1 Introduction 10.2 S-Function Block 10.3 S-Function Overview 10.4 M-file S-Functions 10.4.1 Initialization (f lag = 0) 10.4.2 Continuous State Derivatives (f lag = 1) 10.4.3 Discrete State Updates (f lag = 2) 10.4.4 Block Outputs (f lag = 3) 10.4.5 Next Sample Time (f lag = 4) . . .... 10.4.6 Terminate (f lag = 9) 10.4.7 Programming Considerations 10.4.8 M-file S-Function Examples 10.5 C Language S-Functions 10.5.1 C File Header 10.5.2 Initializing the Simulation Structure 10.5.3 Defining Sample Times and Offsets 10.5.4 Accessing Model Variables . . . . . . . ...... . . 10.5.5 Setting Initial Conditions 10.5.6 Setting the Output Vector 10.5.7 Updating the Discrete States 10.5.8 Computing the State Derivatives 10.5.9 End-of-Simulation Tasks 10.5.10 C S-Function File Trailer

192 195 195 195 197 197 197 197 198 201 201 202 202 203 203 204 204 207 215 218 218 219

219 219 220 221 222 224 224 225 226 226 226 227 235 239 240 242 243 244 244 245 245 246 246

xiv Contents 246 250 250 258 258

10.5.11 Programming Considerations 10.5.12 Compiling a C S-Function 10.5.13 C S-Function Examples 10.6 Summary References

259 259 259 260 261 265 265 265 268 268 268 269 269 269 271 272 272

11 Graphical Animations 11.1 Introduction 11.2 S-Function Animations 11.2.1 Animation S-Function Initialization 11.2.2 Animation S-Function Updates 11.3 Animation Toolbox 11.3.1 Obtaining the Animation Toolbox 11.3.2 Using the Animation Toolbox 11.3.3 Animation Object Properties 11.3.4 Configuring an Animation 11.3.5 Modifying an Animation 11.3.6 Setting Initial Inputs 11.3.7 Saving and Loading Animation Files 11.4 Dials & Gauges Blockset 11.5 Virtual. Reality Toolbox 11.6 Summary References 12 Debugging 12.1 Introduction 12.2 Finding Model-Building Errors 12.3 Finding Implementation Problems 12.3.1 Simulink Execution Errors 12.3.2 Slow Execution 12.3.3 Incorrect Results 12.4 Simulink Debugger 12.4.1 Running the Debugger 12.4.2 Block Index 12.4.3 Single-Step Execution 12.4.4 Breakpoints 12.4.5 Inspecting Block Data 12.4.6 Algebraic Loop Commands 12.4.7 Debugger Status Commands 12.4.8 Ending Debugging 12.4.9 Debugger Command Summary 12.5 Summary 13 Numerical Issues 13.1 Introduction 13.2 Choosing a Solver







273 273 273 275 275 275 277 277 278 279 280 281 283 283 284 284 284 286 287 287 287

Contents xv 13.3 Algebraic Loops 13.3.1 Newton—Raphson Method 13.3.2 Eliminating Algebraic Loops 13.4 Multiple Sample Time Issues 13.5 Zero-Crossing Detection 13.6 Summary References 14 Introduction to Stateflow 14.1 Introduction 14.2 Finite State Machines 14.3 State Transition Diagrams 14.4 Stateflow Charts 14.4.1 Stateflow Chart Hierarchy 14.4.2 Compound Transitions 14.4.3 Inner Transitions 14.5 Stateflow Quick Start 14.6 Drawing Stateflow Charts 14.6.1 State Boxes 14.6.2 Transitions 14.6.3 Connective Junctions 14.6.4 Default Transitions 14.6.5 History Junctions 14.6.6 Specifying Parallel Decomposition 14.7 Labeling States and Transitions 14.7.1 State Labels 14.7.2 Transition Labels 14.8 Stateflow Explorer 14.9 Interfacing with Simulink 14.9.1 Inputs 14.9.2 Outputs 14.10 Chart Update Options 14.11 Flowcharts 14.12 Graphical Functions 14.13 Printing Stateflow Charts 14.14 Stateflow Debugger 14.15 Summary References 15 Introduction to Real-Time Workshop 15.1 Introduction 15.1.1 Development Scenarios 15.1.2 Build Process 15.2 Generic Real-Time Target 15.2.1 Stand-Alone GRT Target 15.2.2 Running the Stand-Alone GRT Model 15.2.3 Using External Mode

293 294 295 298 299 299 299





301 301 301 302 303 305 307 308 308 314 314 315 316 316 316 316 317 317 318 318 318 319 319 320 322 326 327 327 328 329 330 330 330 331 332 332 333 335

xvi

Contents

15.3

15.4

15.5

Rapid Simulation Target 15.3.1 Basketball Dynamical System 15.3.2 Basketball Simulink Model 15.3.3 Building the Rapid Simulation Target 15.3.4 Rapid Simulation Options 15.3.5 Minimizing Miss Distance xPC Target 15.4.1 Initialization 15.4.2 Building a Target 15.4.3 Starting Target 15.4.4 Controlling the Target Summary References

336 336 337 338 339 341 342 342 343 345 345 347 347

Appendix A Block Reference A.1 Continuous Block Library A.2 Discontinuities Block Library A.3 Discrete Block Library A.4 Lookup Tables Block Library A.5 Math Operations Block Library A.6 Ports & Subsystems Block Library A.7 Signal Attributes Block Library A.8 Signal Routing Block Library A.9 Sinks Block Library A.10 Sources Block Library A.11 User-Defined Functions Block Library

348 348 349 350 351 352 354 356 357 358 359 361

Appendix B Parameter Reference B.1 Model Parameters B.2 Common Block Parameters B.3 Mask Parameters B.4 Identifying Block Parameters

362 362 367 368 368

Index

371.