3813474 Matlab Simulink Using Simulink and Stateflow in Automotive Applications
Design and Verify Control Systems using Simulink … ® Modeling: understanding your design problem...
Transcript of Design and Verify Control Systems using Simulink … ® Modeling: understanding your design problem...
© 2
009 T
he M
ath
Work
s, In
c.
® ®
Design and Verify
Control Systems using
Simulink
Coorous Mohtadi
2
® ®
What are we doing today?
3
® ®
Problem Formulation
Higher Performance:
1. Faster trajectory
Vmax: 150 → 250 rad/s
Amax: 2000 → 5000 rad/s2
2. Decreased error at standstill
error < 1 rad
4
® ®
Flexible Drive
Higher Performance:
1. Faster trajectory
Vmax: 150 → 250 rad/s
Amax: 2000 → 5000 rad/s2
2. Decreased error at standstill
error < 1 rad
5
® ®
Executable
Specifications
from Models
Continuous
Test and
Verification
Implementation
with Automatic
Code Generation
Design
with
Simulation
Models
Workflow
Modelling:
understanding your design problem
Control Design:
solving your design problem
Real-time Testing:
verifying the design
6
® ®
Agenda
What is Simscape?
Real-Time Simulation and Control
7
® ®
Workflow
Tunable
Controller
Tff
Ref
Pos
Vact
Reference
and FeedforwardRigid Body P 2P GUI
Tff
Ref
PlantModel
Vact pos_L
Measured
Position
Tff
Ref
8
® ®
Challenges in System Design
Early
testing
Multi-
domain
Data source: System Design: New Product Development for Mechatronics, January 2008 -
Aberdeen Group Survey of 160 enterprises
9
® ®
Challenges in System Design
Tools & processes that support only bottom-up design Design at a low-level
Provide a specification to the higher level
Examples: CAD
Lack of top-down (system) design tools No rigour – when is a design optimal for given requirements?
Some methods e.g. function-means, weighted objectives
Bond graphs – some use, but often too abstract
Simulink® - usefulness depends on system
Disruptive technologies Potentially a subsystem change forces a complete redesign for
the whole system
CAD better suited to incremental design changes
Communication between functional groups
10
® ®
Using Simulink® for System Modelling
Fits some systems well
Airframes
Some aspects of vehicle components
Systems defined by ODEs
Does not fit others so well
Mechanical
Hydraulic and flow-based systems
Electrical/electromechanical
Systems defined by DAEs
Simscape™ Language and Simscape blocksets
Aimed at systems described by DAEs
Thermal, electrical, mechanical, hydraulic, pneumatic, custom…
11
® ®
DC Motor
Electrical Systems in Simulink
R
L
Kt,Ke
J,b
θ
i
dt
diLRiKv m
windwindme
dt
dJDiKT mt
12
® ®
Electrical Systems in Simscape
DC Motor
Simscape model advantages
Easier to read than equations
Quicker to create
More intuitive – easier to explain to other engineers
R
L
Kt,Ke
J,b
i
R
13
® ®
Electrical Systems in Simscape Language
DC Motor
Simscape language advantages
For custom component, often
easier to express equations
Flexibility & extensibility
R
L
Kt,Ke
J,b
dt
diLRiKv m
windwindme
dt
dJDiKT mt
14
® ®
Modeling Dynamic Systems: two approaches
First-Principles Modeling
Use an understanding of the
system’s physics to derive a
mathematical representation
)(sin1
)cos()cos())sin(()sin())sin(()sin(2
22
22
ne
nnenwL &&& d
15
® ®
First-Principles Modeling Data-Driven Modeling
Use system test data to derive a
mathematical representation
)(sin1
)cos()cos())sin(()sin())sin(()sin(2
22
22
ne
nnenwL &&& d
1
3
23
1
)(
2
2
23
ss
s
ss
s
sH
Use an understanding of the
system’s physics to derive a
mathematical representation
Modeling Dynamic Systems: two approaches
16
® ®
Both have advantages & disadvantages
Advantages: Provides insight into the system’s
underlying behavior
Enables broad parameter
sweeps to optimize design
Disadvantages: Effects like friction and turbulent
flow are difficult to model
Time consuming to develop
Advantages: Fast to develop a model
Highly accurate
Disadvantages: Requires plant hardware to
acquire test data
Not a design tool for plant
First-Principles Modeling Data-Driven ModelingComplete Modelling Environment
17
® ®
Tools that span both modeling approaches
SimulinkDesign
Optimization
SimulinkStateflow
First-Principles
SystemIdentification
Toolbox
Data-Driven
Complete Modelling Environment
SimMechanics
SimDriveline
SimHydraulics
SimElectronics
SimPowersystems
Simscape
18
® ®
Modeling: understanding your design problem
Data Driven methods: the existing system
Transfer function measurement using live data
System Identification for obtaining a model
‘First Principles’ modelling: the new design
Setting up a differential equation description
Using Physical Modeling tools
Parameter Estimation: the first prototype
Physical parameters from measurements
Data DrivenFirst Principles
Parameter Estimation
19
® ®
Data Driven Methods: the existing system
Transfer Function
Measurement using
live data
20
® ®
System Identification
Data Driven Methods: the existing system
21
® ®
Step response of
identified system
Data Driven Methods: the existing system
22
® ®
Modeling: understanding your design problem
Data Driven methods: the existing system
Transfer function measurement using live data
System Identification for obtaining a model
‘First Principles’ modeling: the new design
Setting up a differential equation description
Using Physical Modeling tools
Parameter Estimation: the first prototype
Physical parameters from measurements
First Principles
Parameter Estimation
Data Driven
23
® ®
Digital Motion Control Setup
Load Encoder
Motor Encoder
Motor
Power
Amplifier
Flexible Shaft
I/O connectors
24
® ®
Digital Motion Control Setup
Load EncoderActuator Inertia
Flexible
Shaft
Load Inertia
Demo
25
® ®
‘First Principles’ Modeling: the new design
Implementing with
State-Space model
26
® ®
‘First Principles’ Modeling: the new design
Implementing in block diagram form
27
® ®
‘First Principles’ Modeling: the new design
Now, is there another way?.....
Suppose you designed your system in CAD!
28
® ®
29
® ®
30
® ®
31
® ®
Demo
32
® ®
CAD Translator for SimMechanics
33
® ®
SimMechanics:
modeling of 3D mechanical systems
Plant
Actu
ato
rs
Mecha-
nical
Device Sensors
3D Multi-Body Dynamics
Bodies and Joints
CAD Translation
34
® ®
Traditional process
Using physical modeling tools
First-principles Modeling in Simulink
Set up differential
and algebraic equationsPlant
Actu
ato
rs
Physical
System
Sensors
Plant
Actu
ato
rs
Physical
System
Sensors
Build model by
combining components
35
® ®
Simscape
‘First Principles’ Modeling: the new design
36
® ®
Simscape:
modeling in multiple domains
Use Fundamental
Analogy between
Physical Domains
SimscapeMechanical Hydraulic Electrical Ph. Signals
f(x,y)
Domain Across Variable Through Variable
Mechanical
(translational/rotational)
Position Angle
Velocity Angular velocity
Acceleration Angular acceleration
Force Torque
Hydraulic Pressure Volumetric flow rate
Mass flow rate, Volume
Electrical Voltage
Flux
Current
Charge
Thermal Temperature Heat flow
Enthalpy, Entropy
Thermal
A B
37
® ®
Simscape Language
MATLAB-based
Create new domains
Custom components
User defined libraries
Object oriented
38
® ®
Physical Systems in Simulink
Sim
Mec
han
ics
Mechanical dynamics (3-D)
Sim
Dri
vel
ine
Drivetrain systems (1-D)
Sim
Hy
dra
uli
cs
Fluid power and control
Multidomain physical systems
Electrical power systems
Sim
Po
wer
Syst
em
s
MATLAB, Simulink
Sim
Po
wer
Sy
stem
s
Simscape
Sim
Mec
ha
nic
s
Sim
Driv
eli
ne
Sim
Hy
dra
uli
cs
Sim
Ele
ctro
nic
s
Sim
elec
tro
nic
s
Electromechanical and
electronic systems
Simscape
39
® ®
Modeling: understanding your design problem
Data Driven methods: the existing system
Transfer function measurement using live data
System Identification for obtaining a model
‘First Principles’ modeling: the new design
Setting up a differential equation description
Using Physical Modeling tools
Parameter Estimation: the first prototype
Physical parameters from measurements
First Principles
Parameter Estimation
Data Driven
40
® ®
Parameter Estimation: the first prototype
41
® ®
Modeling Tools
MATLAB
Simulink
Simscape
Sim
Mech
an
ics
Sim
Dri
velin
e
Sim
Hyd
rau
lics
Sim
Po
wers
yste
ms
Aero
sp
ac
e B
lockset
RF
Blo
ckset
Op
tim
izati
on
To
olb
ox
Sys
tem
Id
en
tifi
cati
on
To
olb
ox
Sim
uli
nk D
esig
n
Op
tim
izati
on
First-Principles Modeling Data-Driven ModelingComplete Modeling Environment
Sim
Ele
ctr
on
ics
42
® ®
Agenda
What is Simscape Language?
Real-Time Simulation and Control
43
® ®
Real-time Testing: verifying the design
Prepare Simulink model for Rapid Prototyping
Automatic code generation with Real-Time Workshop
Interfacing with your implemented design
44
® ®
Real-time Testing: verifying the design
Target PC implements controller
Setup replaces plant model
Demo
45
® ®
Preparation for Real-time Testing
Specify I/O
Signal Conditioning
Data Monitoring
46
® ®
Automatic Code Generation
47
® ®
Interfacing with Your Implemented Design
48
® ®
1. Extending an Existing Simscape Library
Nonlinear Rotational Spring
Torque varies as square of
deflection (Torque = -k*θ2)
All required elements are defined
in a single file (parameters,
equations, description, etc.)
The model is compatible with
Simscape foundation library+Mechanical
NonlinRotSpring.ssc
NonlinRotSpring.jpg
49
® ®
Defining Components: Declaration
Declaration section
Declare component members
(node, input, variables, etc.)
Component Model
Declaration
Nodes
Inputs, Outputs
Variables
(through, across, and internal)
Parameters
Setup
Parameter checking
Define relationship between
component variables and nodes
Initial conditions
Derived parameters
Equations
Algebraic, discontinuous,
differential
50
® ®
Defining Components: Declaration
Component Model
Block name and block description
come directly from the Simscape file
component NonlinRotSpring
% Nonlinear Rotational Spring
% This block implements a nonlinear rotational
% spring. t=-k*theta^2, where k is the spring rate.
51
® ®
Defining Components: Declaration
Component Model component NonlinRotSpring
% Nonlinear Rotational Spring
% This block implements a nonlinear rotational
% spring. t=-k*theta^2, where k is the spring rate.
Declaration
Nodes
nodes
r = foundation.mechanical.rotational.rotational;
% r:left
c = foundation.mechanical.rotational.rotational;
% c:right
end
Reuse Simscape physical
domains to create
compatible components
Node declaration creates
domain specific physical ports
Define the port label and the side where the port appears
r c
52
® ®
Defining Components: Declaration
Component Model
Declaration
Nodes
Variables
component NonlinRotSpring
% Nonlinear Rotational Spring
% This block implements a nonlinear rotational
% spring. t=-k*theta^2, where k is the spring rate.
nodes
r = foundation.mechanical.rotational.rotational;
% r:left
c = foundation.mechanical.rotational.rotational;
% c:right
end
variables
t = { 0, 'N*m' }; % torque through
w = { 0, 'rad/s' }; % velocity across
theta = { 0, 'rad' };
end
Through and across
variables from
domain definition
Internal variable, used in equations
Variable section is used to define across and through variables
as well as other internal variables
53
® ®
Defining Components: Declaration
Component Model component NonlinRotSpring
% Nonlinear Rotational Spring
% This block implements a nonlinear rotational
% spring. t=-k*theta^2, where k is the spring rate.
Declaration
Nodes
Variables
Parameters
nodes
r = foundation.mechanical.rotational.rotational;
% r:left
c = foundation.mechanical.rotational.rotational;
% c:right
end
variables
t = { 0, 'N*m' }; % torque through
w = { 0, 'rad/s' }; % velocity across
theta = { 0, 'rad' };
end
parameters
k = { 10, 'N*m/rad^2' }; % Spring rate
theta0 = { 0, 'rad' }; % Initial Deformation
end
Parameter label,
default value, and units
defined in the file
54
® ®
Defining Components: Setup
Setup section used to:
Validate parameters
Compute derived parameters
Set initial conditions
Define relationship between
nodes and variables
Executed once per component
instance during model
compilation as regular
MATLAB code
Use function syntax,
not command syntax
Component Model
Declaration
Nodes
Inputs, Outputs
Variables
(through, across, and internal)
Parameters
Setup
Parameter checking
Define relationship between
component variables and nodes
Initial conditions
Derived parameters
Equations
Algebraic, discontinuous,
differential
55
® ®
Defining Components: Setup
Setup
Parameter checking
Define relationship
between component
variables and nodes
Initial conditions
function setup
if k < 0
error('Spring rate must be greater than 0');
end
across( w, r.w, c.w );
% velocity variable w from node r to node c
through( t, r.t, c.t );
% torque variable t from node r to node c
theta = theta0;
end
Leverage MATLAB for tasks
such as analyzing parameters,
performing preliminary computations,
and initializing system variables
Use functions across and through
to define relationship between
component variables and nodes
Check lower limit on parameter, using MATLAB
56
® ®
Defining Components: Equation
Establishes mathematical
relationships among
component variables, inputs,
outputs, time, and time
derivatives
Can be DAEs, ODEs, etc.
Specifying inputs and outputs
not required
Executed throughout simulation
Component Model
Declaration
Nodes
Inputs, Outputs
Variables
(through, across, and internal)
Parameters
Setup
Parameter checking
Define relationship between
component variables and nodes
Initial conditions
Derived parameters
Equations
Algebraic, discontinuous,
differential
57
® ®
Defining Components: Equation
Equation equations
t == k * theta * theta * sign(theta);
w == theta.der;
end
end
Torque = -
Angular Velocity =
2k
dt
d
equations section
establishes relationships among
component variables, inputs,
outputs, time, and time derivatives
58
® ®
Equations in the Simscape Language
== is used to represent a symmetrical mathematical
relationship, NOT assignment and NOT a boolean operation
Conditional equations can be defined using IF statements
equations
a + b == c;
end
equations
0 == c - a - b;
endEquivalent
equations
if abs(x) < xc
f == k*x; % Linear region
else
f == k*xc*sign(x) + k1*sign(x)*(abs(x)-xc)^3;
end
end
59
® ®
Equations in the Simscape Language
Equations expressions can be vectors or matrices
Van der Pol‘s equation: x′′ - μ(1-x2)x′ + x = 0
equations
y.der == [y(2);
mu * (1 - y(1)^2) * y(2) - y(1)] * {1,'1/s'};
out1 == y(1);
out2 == y(2);
end
y =x
x′ , y′ =x′
x′′
y2
μ(1-y12) y2+ y1
=
60
® ®
Building Simscape Libraries
Use ssc_build to build
Simscape libraries
Generates <package>_lib.mdl
in parent of top level directory
Package directory
must begin with a ‘+‘
Parent of top level directory
must be on MATLAB path
Executed in .\Example_1\+Mechanical
or any subdirectory of +Mechanical
creates .\Example_1\Mechanical_lib.mdl
+Mechanical
Example_1
Executed in .\Example_1
creates .\Example_1\Mechanical_lib.mdl
>> ssc_build
>> ssc_build Mechanical
61
® ®
2. Defining Physical Domains
Define pneumatic domain
Across variable is pressure
Through variable is mass flow rate
Domain-wide parameter
specific heat capacity (Cp)
Domain Model
Declaration
Across Variables
Through Variables
Parameters
PhysicalDomain
AcrossVariable
ThroughVariable
Pneumatic Pressure Mass flow rate
Pneumatic.ssc
+Pneumatic
+MySimscape
62
® ®
Defining Domains (Pneumatic.ssc)
Domain Model
Declaration
Across Variables
Through Variables
Parameters
domain Pneumatic
variables
p = { 1 , 'Pa' }; % pressure
end
variables(Balancing = true)
mdot = { 1 , 'kg/s' }; % mass flow
end
parameters
Cp = { 1005 , 'J/(kg*K)' }; % specific heat
end
end
Use keywords variables to define
the across and through variables
for the new domain
Use keyword parameters to
define network-wide parameters
specific to this domain
63
® ®
Innovation Explore unique features through rapid
design iterations
Conduct cost-effective design trade-off studies
Quality Prevent errors from reaching the
hardware stage of the design process
Reduce rework
Cost Reduce expensive physical prototypes
Reduce testing costs
Time-to-market Get it right the first time
Accelerate the development process
The Value of Model-Based Design
64
® ®
Executable
Specifications
from Models
Continuous
Test and
Verification
Implementation
with Automatic
Code Generation
Design
with
Simulation
Models
Summary
Modelling:
understanding your design problem
Control Design:
solving your design problem
Real-time Testing:
verifying the design