36
© 2015 The MathWorks, Inc. Introduction to Simulink and Stateflow Jonathan Agg

Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

© 2015 The MathWorks, Inc.

Introduction to Simulink

and Stateflow

Jonathan Agg

Page 2: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed
Page 3: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Topics we will address this session

▪ Why model a system?

▪ Why use Simulink?

▪ Getting to grips with the basics of Simulink and Stateflow through a worked

example

Page 4: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Why model a system?

Page 5: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Modelling & Simulation

gives you insight

Page 6: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Image credit: McLaren

Image credit: Peter Gronemann | Wikipedia

Page 7: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

MODEL

Integrate

Create

Import

SIMULATE

Test

Optimize

ConfigureAnalyze and

Document

DEPLOY

Integrate Into

Other Environments

Share Models

Test Controller

Page 8: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Why use

Simulink?

Page 9: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Model Based Design with Simulink

▪ Modelling and simulation

– Multidomain Dynamic Systems

– Nonlinear Systems

– Continuous-time, Discrete-time, Multi-Rate systems

▪ Plant and Controller Design

– Select/optimise control architecture and parameters

– Rapidly model “what-if” scenarios

– Communicate design ideas

– Embody performance specifications

▪ Implementation

– Automatic code generation

▪ Embedded systems, FPGAs, GPUs

– Rapid prototyping for HIL, SIL, PIL

– Verification and validation

Page 10: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Modelling Approaches

Data-Driven ModellingFirst Principles Modelling

Statistics and Machine

Learning Toolbox

Simscape SystemIdentification

ToolboxSimulink

Design Optimization

Simulink

MATLAB

Tools for Modelling Dynamic Systems

Page 11: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Optimise System-Level Performance

▪ Simulating plant and controller in one environment allows you to optimize

system-level performance.

– Automate tuning process using optimization algorithms

– Accelerate process using parallel computing

Plant

+u y

Controller

s1 s2

s3System

Ac

tua

tors

Se

ns

ors

Page 12: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Detect Integration Issues Earlier

Plant

+u y

Controller

s1 s2

s3

▪ Controls engineers and domain specialists can work together to detect integration issues in simulation– Convert plant models to C code for hardware-in-the-loop tests

– Share models with other internal users

– Share models with external users while protecting IP

System

Ac

tua

tors

Se

ns

ors

Page 13: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Using Simulink &

Stateflow

Page 14: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Model-Based Design Application

Closed-loop motor control

Page 15: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

What questions do we want to answer?

▪ Can I get the closed loop response I need?

▪ What current will my motor draw during operation?

▪ Does my system still work if component values change?

▪ What if…?

Page 16: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Steps in the process

1. Model the motor

2. Model the speed controller

3. Refine the motor model using measured data

4. Model the supervisory logic

5. Deploy the control model to hardware

At each stage: Simulate the model

Page 17: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

PID Control of a DC Motor

R

L

K

J,b

ω

i

V

dt

diLRiKV ++=

dt

dJbiKT

−−=−

dtRiKVL

i )(1 −−=

dtbiKTJ

)(1 −+=

Page 18: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Steps in the process

✓ Model the motor

✓ Model the speed controller

3. Refine the motor model using measured data

4. Model the supervisory logic

5. Deploy the control model to hardware

At each stage: Simulate the model

Page 19: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

From: Bill

To: Jonathan

Subject: Calibrating motor model

Hi Jonathan

I tried to run the motor model you sent, but the outputs didn’t match

up at all with the motor in our lab. Please can you send a model

that’s more useful!

Thanks

Bill

Page 20: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Estimating Parameters

Using Measured Data

Problem: Simulation results do not match

measured data

Solution: Use Simulink Design Optimization

to automatically tune model parameters

Model:

R = Resistance

L = Inductance

J = Inertia

B = Friction

K = Back EMF Constant

DC Motor

Motor

Servoamplifier

+-

Page 21: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Steps in the process

✓ Model the motor

✓ Model the speed controller

✓ Refine the motor model using measured data

4. Model the supervisory logic

5. Deploy the control model to hardware

At each stage: Simulate the model

Page 22: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

System Overview

Upstream

Processing

Supervisory

Logic

Motor

Speed

Controller

MotorSpeed

demandSpeed

command

Motor

commands

Speed

measurements

Page 23: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Stateflow Overview

▪ Extend Simulink with a design environment for developing state machines

and flow charts

▪ Design systems containing control, supervisory, and mode logic

▪ Describe logic in a natural and understandable form with deterministic

execution semantics

Page 24: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

What are State Machines?

▪ Represent reactive systems that have states or modes

▪ States change based on defined conditions and events

Page 25: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Supervisory Logic

▪ Receive input:

– velocityDemand (double): demanded motor speed

▪ Send three outputs:

– motorOn (boolean): whether motor should be enabled or not

– commandTypeEnum (enumeration): current operating mode

– velocityCommand (double): demanded motor speed

▪ When motor is enabled, run required motor calibration steps:

– Set mode to calibration

– Run at 25 rad/s for 1 second

– Stop motor for 1 seconds

– Set mode to velocity control How can we implement this?

Page 26: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

From: Bill

To: Jonathan

Subject: URGENT: Unexpected motor controller behaviour

Hi Jonathan

While testing out the motor control algorithms today, the motor

suddenly slowed down before spinning back to its demanded speed.

Please can you look into it asap and send us an updated algorithm.

Thanks

Bill

Page 27: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Supervisory Logic

▪ Receive input

▪ Send outputs

▪ Run required motor calibration steps

▪ Sanitise the input demand to ensure smooth operation of motor

Page 28: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Supervisory Logic

Speed

demand

changed

Speed

demand

constant

Start

Change in velocityDemand

New speed demand reached

velocityDemand quickly reverts

to previous value

How can we implement this?

Initialisation

Page 29: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

From: Bill

To: Jonathan

Subject: URGENT: New motor speed requirement

Hi Jonathan

We’ve just heard from the motor supplier that there’s a potential

design issue if the motor speed changes too quickly. Please can you

change the supervisory logic to avoid us running into this problem.

Thanks

Bill

Page 30: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Supervisory Logic

▪ Receive input

▪ Send outputs

▪ Run required motor calibration steps

▪ Sanitise the input demands to ensure smooth operation of motor

▪ Safely transition between large changes in input demands

Page 31: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Supervisory Logic

Speed

demand

changed:

Ramp to new

demanded

speed

Speed

demand

constant

Start

Change in velocityDemand

New speed demand reached

velocityDemand quickly reverts

to previous value

How can we implement this?

Initialisation

Speed

demand

changed

Page 32: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Next steps in the process

✓ Model the motor

✓ Model the speed controller

✓ Refine the motor model using measured data

✓ Model the supervisory logic

5. Deploy the control model to hardware

At each stage: Simulate the model

Page 33: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Next steps in the process

✓ Model the motor

✓ Model the speed controller

✓ Refine the motor model using measured data

✓ Model the supervisory logic

✓ Deploy the control model to hardware

✓ Simulate the model

Page 34: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Conclusions

▪ Modelling and simulation gives you insight to make smarter decisions,

earlier

▪ Simulink allows you to model the complete system in a single environment

Page 35: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

From: Bill

To: Jonathan

Subject: Project signed off

Hi Jonathan

Thanks for all your hard work on this project, the customer has just

signed it off! We’ve made great progress and I look forward to

working with you again.

Bill

Page 36: Introduction to Simulink and Stateflow · “Simulations with MATLAB and Simulink were essential to assessing feasibility and evaluating broad design tradeoffs as well as making detailed

Solar Impulse Develops Advanced

Solar-Powered Airplane

• Key design decisions

made early

• Vital pilot training

enabled

• Models reused and

shared throughout

development

“Simulations with MATLAB and Simulink were essential to assessing

feasibility and evaluating broad design tradeoffs as well as making

detailed design decisions—like the size of control surfaces and the

vertical tail—that directly affect aircraft dynamics and handling

qualities.”

–Ralph Paul, Solar Impulse