Embedded Control Systems Lecture 4 – Spring 2018

Knut Åkesson Modelling of Physcial Systems

Model knowledge is stored in books and human minds which computers cannot access

“The change of motion is proportional to the motive force impressed “ – Newton

Newtons second law of motion: F=m*a

Slide from: Open Source Consortium, Copyright © Equation Based Modelling

• Equations were used in the third millennium B.. • Equality sign was introduced by Robert Recorde in 1557

Newton still wrote text (Principia, vol. 1, 1686) “The change of motion is proportional to the motive force impressed ”

Programming languages usually do not allow equations!

Slide from: Open Source Modelica Consortium, Copyright © Languages for Equation-based Modelling of Physcial Systems

Two widely used tools/languages based on the same ideas

Modelica + Open standard + Supported by many different vendors, including open source implementations + Many existing libraries + A plant model in Modelica can be imported into - Matlab is often used for the control design History: The Modelica design effort was initiated in September 1996 by Hilding Elmqvist from Lund, Sweden.

Simscape + Easy integration in the Mathworks tool chain (Simulink//Simscape) - Closed implementation What is Modelica

A language for modeling of complex cyber-physical systems

• Robotics • Automotive • Aircrafts • Satellites • Power plants • Systems biology

Slide from: Open Source Modelica Consortium, Copyright © What is Modelica

A language for modeling of complex cyber-physical systems

Primary designed for simulation, but there are also other usages of models, e.g. optimization.

Slide from: Open Source Modelica Consortium, Copyright © What is Modelica

A language for modeling of complex cyber-physical systems i.e., Modelica is not a tool Free, open language There exist several free and commercial tools, specification: for example: • OpenModelica from OSMC • from Dassault systems • Wolfram System Modeler from Wolfram • SimulationX from ITI • MapleSim from MapleSoft • AMESIM from LMS • JModelica.org from Modelon Available at: www.modelica.org • MWORKS from Tongyang Sw & Control Developed and standardized • IDA Simulation Env, from Equa by Modelica Association • ESI Group Modeling tool, ESI Group

Slide from: Open Source Modelica Consortium, Copyright © Modelica

Declarative language Equations and mathematical functions allow acausal modeling, high level specification, increased correctness Multi-domain modeling Combine electrical, mechanical, thermodynamic, hydraulic, biological, control, event, real-time, etc... Everything is a class Strongly typed object-oriented language with a general class concept, Java & MATLAB-like syntax Visual component programming Hierarchical system architecture capabilities Efficient, non-proprietary Efficiency comparable to C; advanced equation compilation, e.g. 300 000 equations, ~150 000 lines on standard PC

Slide from: Open Source Modelica Consortium, Copyright © Acasual Modeling

What is acausal modeling/design? Why does it increase reuse? The acausality makes Modelica library classes more reusable than traditional classes containing assignment statements where the input-output causality is fixed. Example: a resistor equation: R*i = v; can be used in three ways: i := v/R; v := R*i; R := v/i;

Slide from: Open Source Modelica Consortium, Copyright © Modelica: Typed Declarative Equation-based Textual Langauge

A textual class-based language OO primary used for as a structuring concept

Behaviour described declaratively using • Differential algebraic equations (DAE) (continuous-time) • Event triggers (discrete-time)

Variable class VanDerPol "Van der Pol oscillator model" declarations Real x(start = 1) "Descriptive string for x”; Real y(start = 1) "y coordinate”; parameter Real lambda = 0.3; equation der(x) = y; der(y) = -x + lambda*(1 - x*x)*y; end VanDerPol;

Differential equations

Slide from: Open Source Modelica Consortium, Copyright © Simscape vs Simulink Van der Pol Equation in the Simscape Language Faster Development

Block Diagram (e.g. Simulink, ...) or Proprietary Code (e.g. Ada, Fortran, C,...) vs Modelica/Simscape

Causality Systems Derivation Definition Modeling of (manual derivation of System Subsystems Decomposition input/output relations) Implementation Simulation Proprietary Code

Block Diagram

Modelica/ Simscape

Slide from: Open Source Modelica Consortium, Copyright © Casual vs Acasual modelling

Modelica/Simscape (acasual): Keeps the Simulink (casual): Physical model – physical Signal-flow model – hard to structure easy to understand understand

Res2 sum3 Ind l2 p p -1 1 R2 1/L 1 s R1=10 R2=100 sum2 p n n +1 +1 AC=220

p p sinln sum1 Res1 Cap l1 n +1 1 1/R1 1/C -1 s C=0.01 L=0.1

n n

p G

Slide from: Open Source Modelica Consortium, Copyright © Multi-Domain Modeling in Modelica

• A DC motor can be thought of as an electrical circuit which also contains an electromechanical component model DCMotor Resistor R(R=100); Inductor L(L=100); VsourceDC DC(f=10); Ground G; ElectroMechanicalElement EM(k=10,J=10, b=2); Inertia load; equation R L connect(DC.p,R.n); EM connect(R.p,L.n); DC connect(L.p, EM.n); connect(EM.p, DC.n); load connect(DC.n,G.p); connect(EM.flange,load.flange); G end DCMotor

Slide from: Open Source Modelica Consortium, Copyright © Corresponding DC-Motor Model Equations

The following equations are automatically derived from the Modelica model:

(load component not included)

Automatic transformation to ODE or DAE for simulation:

Slide from: Open Source Modelica Consortium, Copyright © Model Translation Process to Hybrid DAE to Code

Modelica Graphical Editor Modelica Modelica Modelica Model Textual Editor Modelica Model Frontend Translator Modeling Flat model Hybrid DAE Environment Analyzer "Middle-end" Sorted equations Optimizer Optimized sorted equations Backend Code generator C Code C Compiler Executable Simulation

Slide from: Open Source Modelica Consortium, Copyright © Software Component Model

Interface Acausal coupling Connector

Component Connection Component Causal coupling

A component class should be defined independently of the environment, very essential for reusability A component may internally consist of other components, i.e. hierarchical modeling Complex systems usually consist of large numbers of connected components

Slide from: Open Source Modelica Consortium, Copyright © Connectors and Connector Classes in Modelica

Connectors are instances of connector classes

electrical connector

connector class connector Pin Voltage v; v + keyword flow flow Current i; pin indicates that currents end Pin; of connected pins i sum to zero. Pin pin; an instance pin of class Pin mechanical connector

connector class connector Flange Position s; flow Force f; s end Flange; flange f an instance flange Flange flange; of class Flange

Slide from: Open Source Modelica Consortium, Copyright © The flow prefix

Two kinds of variables in connectors: • Non-flow variables potential or energy level (Simscape: Across) • Flow variables represent some kind of flow (Simscape: Though) Coupling • Equality coupling, for non-flow variables • Sum-to-zero coupling, for flow variables

The value of a flow variable is positive when the current or the flow is into the component v pin positive flow direction: i +

Slide from: Open Source Modelica Consortium, Copyright © Energy flows

Across Through Nonflow Domain Potential Flow Carrier Type

Electrical Voltage Current Charge

Translational Position Force Linear momentum

Angular Rotational Angle Torque momentum Magnetic Magnetic Magnetic Magnetic flux potential flux rate Hydraulic Pressure Volume flow Volume Heat Temperature Heat flow Heat Chemical Chemical Particle flow Particles potential

Pneumatic Pressure Mass flow Air

Slide from: Open Source Modelica Consortium, Copyright © Connect equations in Modelica

Connections between connectors are realized as equations in Modelica

connect(connector1,connector2)

The two arguments of a connect-equation must be references to connectors, either to be declared directly within the same class or be members of one of the declared variables in that class

+ v v + pin1 pin2

i i

Pin pin1,pin2; //A connect equation //in Modelica: Corresponds to pin1.v = pin2.v; connect(pin1,pin2); pin1.i + pin2.i =0;

Slide from: Open Source Modelica Consortium, Copyright © Connect Equations in Modelica

Pin pin1,pin2; //A connect equation //in Modelica Corresponds to pin1.v = pin2.v; connect(pin1,pin2); pin1.i + pin2.i =0;

Multiple connections are possible: connect(pin1,pin2); connect(pin1,pin3); ... connect(pin1,pinN);

Each primitive connection set of nonflow variables is used to generate equations of the form:

v1 = v2 = v3 = ⋯ = vn

Each primitive connection set of flow variables is used to generate sum-to-zero equations of the form:

i1 + i2 + i3 + ⋯ + in = 0

Slide from: Open Source Modelica Consortium, Copyright © Conclusions

Equation-based modelling facilitate modelling of multi-domain systems.

More complex system benfit more from taking advantage of the better structuring and increased possiblity for reusability.

Industry is moving away from modelleing everything in Simulink to also used Modelica and/or Simscape.

Tradeoff between using an open standardized language and using an integrated tool-chain.

In the project you will use Simscape for modelling the physics and Simulink to implement the control algorithms.

Next: View Webinar about Physical Modelling with the Simscape Language. Webinar: Physcial Modeling with the Simscape Language

https://se.mathworks.com/videos/physical-modeling-with-the-simscape- language-81733.html Simscape Documentation

https://se.mathworks.com/help/physmod/simscape/