53
Integrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department of Automatic Control, Lund University Hilding Elmqvist Ulf Nordström Dynasim, Lund

Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Embed Size (px)

Citation preview

Page 1: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Integrated Modeling, ControlSynthesis and Code Generation

with Modelica and Dymola

Johan ÅkessonDepartment of Automatic Control, Lund University

Hilding Elmqvist

Ulf NordströmDynasim, Lund

Page 2: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Project in Automatic ControlFRT090

Johan Åkesson

Department of Automatic Control

Faculty of Engineering

Lund University

Page 3: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Project in Automatic Control

• Advanced level course 7.5 ECTS units

• Course runs for seven weeks

• Team effort • Collaborative problem solving• Collaborative problem solving

• Get practical experience• Work in the lab

• Apply course knowledge• Modeling and identification

• Control design and implementation

Page 4: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Project in Automatic Control2009

• 25 students• Mostly from Lund but some exchange

students

• Several disciplines• Several disciplines• Engineering physics

• Applied mathematics

• Computer sciences

• Chemical engineering

• Two groups working with Lego/Dymola• Five students in each group

Page 5: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego with Modelica/Dymola

• Build the NXTway two-wheel robot

• Physical modeling with Modelica– Multi-body dynamics

• Model calibration• Model calibration– Experiments

– Dymola Calibration module

• Control design– Derive simple model

– Develop control scheme

Page 6: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego with Modelica/Dymola

• Automatic generation of fixed point controller code– No C programming

– Software in the loop evaluation

• User interaction• User interaction

• Deployment on NXTway

• Animation in Dymola• Real-time animation

• Get in touch with industry– Tutorials held by Dynasim personnel

Page 7: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego Dymola Groups

Page 8: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Course plan

• w1. Form groups and planning• Tuesday March 17th – group announcement

• Friday March 20th – project plan dead line

• w2.-w3.• Tutorial

• Weekly meetings with project supervisors

• w4.-w7• Weekly meetings with project supervisors

• Presentation and demo in w7.

Page 9: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego Dymola Tutorials

1. Introduction to Modelica (AC)

2. Multi-body modeling (Dynasim)• Wheel models (by Martin Otter)

3. Code generation with Dymola (Dynasim)• Modelica_Embedded• Modelica_Embedded

Page 10: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica/Dymola Modeling

Hilding Elmqvist

Page 11: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

MultiBody modeling

Modelica.Mechanics.MultiBody

Bodies and Joints

Automatic 3D animation

Page 12: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Example – Double pendulum

1D rotational damper

World system(= Inertial system)

Rotational joint

Bodies

1D rotational flangefor drive

Page 13: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Wheel Model

The MultiBody library does not contain any wheel models.

A separate library IdealWheels contains a wheel set where each wheel can be driven separately.

This is thus suitable for modeling of LEGO Mindstorms robotsMindstorms robots

Page 14: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Body with two wheels

The separate model WheelSetAndBody contains a body with two wheels.

The wheels are driven by two sinusoidal torques with different frequencies.

worldz

z

ground

x

w heelSet

torque1

tau

torque2

tau

sine

freqHz=0.1

sine1

freqHz=0.15

a b

n={0,1,0}

revolute bodyBox

r={0,0,0.2}

ba

Page 15: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Resulting animations

Page 16: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Linearization

x

world

x

z

w heelSet

torque1

tau

torque2

tau

a b

n={0,1,0}

revolute bodyBox

r={0,0,0.2}

ba

tau1

tau2

x0

z

ground

Page 17: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Linear model

Inputs

Outputs

States

Page 18: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Animation with LEGO shapes

Dymola supports vizualization of shapes described by DXF, STL and VRML (subset) files.

LEGO building blocks are described in .DAT format:http://www.ldraw.org

DAT2DXF converter available:http://www.ldraw.org/Downloads-req-viewdownload-cid-6.htmlhttp://www.ldraw.org/Downloads-req-viewdownload-cid-6.html

LEGO parts are available at:http://www.ldraw.org/Downloads-req-viewdownload-cid-1.html

Electric Mindstorms NXT shape:http://www.ldraw.org/cgi-bin/ptdetail.cgi?f=parts/53788.dat

Page 19: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Ldraw to DXF Converter

Download parts directories from:http://www.ldraw.org/Downloads-req-viewdownload-cid-1.html

Mindstorms parts are among the unofficial parts

See dat2dxf\readme.txt for instructions on how to organize files

Page 20: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

LEGO blocks resource

Page 21: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Dymola animation of NXT (shape 53788)

bodyShape

r

1 ba

worldy

a b

n={0,0,1}

revolute fixedTransl...

r={0,100,0}

a b

rx

n={0,0,1}

Page 22: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

TutorialModelica and Dymola for System Design

Model Building and Calibration

H. ElmqvistDynasim AB, Lund

Page 23: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Calibrate engine map parameter and friction

23

Page 24: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystems and Code generation for Lego Mindstorms NXT

Ulf Nordström

Page 25: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Contents

Overview

Modelica_EmbeddedSystems

Fixed point Code Generation

Lego Mindstorms

Page 26: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystemsKey Components

Communication

Configuration

Target

Task

SubtaskSubtask

Page 27: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystemsKey Components

Communication

Page 28: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystemsKey Components

Configuration

Target

TaskTask

Subtask

Page 29: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystemsKey Components

Configuration

Target

TaskTask

Subtask

Page 30: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystemsKey Components

Configuration

Target

TaskTask

Subtask

Page 31: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Use pull-down menue to select target/task/subtask”

Modelica_EmbeddedSystemsKey Components

Page 32: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystems

Model In the LoopQuantization

Communication delay

Software In the LoopTask decomposition

Scenarios

Task decomposition

Effects of fixed point arithmetics

Embedded

Page 33: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystems

Model In the LoopQuantization

Communication delay

Scenarios

Page 34: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystems

Software In the LoopEffects of fixed point arithmetics

Scenarios

Fixed point signal

Real signal

reference

Page 35: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystems

Embedded

Change target identifier to ”lego”

Will be discussed in the Lego Mindstorms section

Scenarios

Page 36: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystems

Use ”extend from” to create new configurations/scenarios from the same base model

Use modifiers to change attributes

Scenarios

Right click

Page 37: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystemsExample – Software In the Loop

Set Hidden.DecomposeInTasks = true

Task decompositionReference subtask

Feedback subtask

Plant subtask

Page 38: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystemsExample – Software In the Loop

Task decompositionReference subtask

Feedback subtask

Plant subtask

Page 39: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Modelica_EmbeddedSystemsExample – Software In the Loop

Page 40: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

FixedPoint Code Generation

Setup/ConfigurationMapping annotation

Restrictions

Page 41: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

FixedPoint Code GenerationSetup/Configuration – mapping annotation

FixedPoint variables must be annotatedmin

max

resolution

Page 42: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

FixedPoint Code GenerationSetup/Configuration – mapping annotation

Declaration

Q[nQ, nQ] = [integer bits, fractional bits]

Generated code

Page 43: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

FixedPoint Code GenerationSetup/Configuration – mapping annotation

Use parameter dialog to input annotations as modifiers

Page 44: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

FixedPoint Code GenerationSetup/Configuration – enable FixedPoint

FixedPoint will be enabled when the subtask belongsto a target with ”floatingpointUnit = false”

Page 45: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego Mindstorms

Frameworkdsmodel and dymosim

dymola_wrapper in nxtOSEK

Setup/Configuration

Limitations

ExampleExample

Page 46: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego MindstormsFramework

declarations.c

equations.c

+dymola_wrapper.c

Codegeneration

Codegeneration dsmodel.c

build

Send internal variables over bluetooth

d

Page 47: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego MindstormsFramework – dymola_wrapper.c

Generated by Dymola

Page 48: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego MindstormsSetup/Configuration

Configuration for Lego target and bluetoothcommunication

Page 49: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego MindstormsSetup/Configuration

Translate and compile Dymola modelCD to ”..\nxtOSEK\samples\dymola”Press translate button in Dymola

Compile dymola_wrapper using CygwinCompile dymola_wrapper using CygwinCD to ”..\nxtOSEK\samples\dymola”Compile with ”make all”Start Lego and download executable with ”./ramboot.sh”

Page 50: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego MindstormsSetup/Configuration

Starting Lego and dymosim for Bluetoothcommunication and program execution

Page 51: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Lego MindstormsSetup/Configuration

Results are automatically stored in Dymola

Page 52: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department
Page 53: Integrated Modeling, Control Synthesis and Code Generation ... · PDF fileIntegrated Modeling, Control Synthesis and Code Generation with Modelica and Dymola Johan Åkesson Department

Thank you for you attentionand

Good Luck with your projects