32
Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation demo: Jesung Kim Some slides are edited from previous slides made together with others in Hybrid Systems Group.

Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Modeling Hybrid Systems

Yerang Hur CIS 640, October 10, 2002Department of Computer and Information Science University of Pennsylvania

Code generation demo: Jesung KimSome slides are edited from previous slides made together with others in Hybrid Systems Group.

Page 2: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Outline

Hybrid Systems Simulink State-machine-based Representation CHARON Framework CHARON Language Examples Tool Demo

Page 3: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Hybrid Systems

Differential equations Algebraic equations Invariant constraints

CollisionsDecision logicDiscrete communication (send/receive)

Guarded transitions between modes

Represented with

Physical processes in control system e.g. robots, aircraft, ...

Continuous Behaviors Discrete Events

Page 4: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Hybrid Systems - two views

Control engineering-oriented view

discretecontrol command

generator

dynamicalsystem

reference

output

Page 5: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Hybrid Systems - two views (con’t)

Computer scientist-oriented view

- State machine embedding dynamics

dx/dt=f(x) dx/dt=g(x)guard

reset x

- Continuous state variables with discrete state variables

guard

reset x

cont xdisc y

dx/dt=f(x,y)

Y=1 update y

cont xdisc y

dx/dt=g(x,y)

Page 6: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Simulink

system blockdiagramsources sinks

Elements of Simulink model

Simulink is block diagram-based notations for modeling dynamical systems.

Page 7: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Simulink (cont’d)

Blocks for representing continuous components

State-Space Block

Gain Blockk yx : algebraic equation

y=kx

Sum Block

Derivative Block

Integrator Block

ab c: algebraic equation

c=a-b

+-

du/dtx y: differential equation y=dx/dt

x Y: Y =y0 + x(t)dt

X=Ax+BuY=Cx+Du

yu

.

Page 8: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Simulink (cont’d)

Hybrid system representation

-

+

Kp

Ki

Kd

zero orderholder

discrete timeintegrator

derivativeapproximation

+

++

physicalplant

reference

Page 9: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

State-machine-based Representation

x > 80.0

x < 70.0

x := 73.0

Thermostat

On

x>=82.0dx/dt=-x+100

Off

68.0<=xdx/dt=-x

Page 10: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Framework for Developing Hybrid System

1. High-level modeling

• platform independent

• hierarchical and modular– parallel composition of agents– specify modes and constraints – sequential composition of modes

2. Simulation, analysis, and automatic code generation

3. Deploy the code on actual target platform

Page 11: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

CHARON FrameworkCHARON Code(High level language)

Java Code

CHARON to Java TranslatorCHARON to Java Translator

Control Code GeneratorControl Code Generator

Java Libraries

Human InterfaceHuman InterfaceAnalysis

Simulator GeneratorSimulator Generator

Drivers

Page 12: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

CHARON Language

Individual components described as agents Composition, Instantiation, and Hiding

Individual behaviors described as modes Encapsulation, Instantiation, and Scoping of

variables

Shared variables as well as message passing Support for discrete and continuous behavior Well-defined formal semantics

Page 13: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Example: TwoAgent

mode0

mode1

Mode

choppyATop

a1

a2

Agent

TwoAgent A

top(10.0,0.0)

(9.0,-1.0)

Architectural Hierarchy

Behavioral Hierarchy

Page 14: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Example: TwoAgent

mode0

mode1

Mode

choppyATop

a1

a2

Agent

TwoAgent A

top(10.0,0.0)

(9.0,-1.0)

agent TwoAgent() {

private analog real v1,v2;agent a1 = A (10.0, 0.0) [vIn, vOut := v2, v1] ;agent a2 = A (9.0, -1.0)

[vIn, vOut := v1, v2] ; }

agent A(real initValue, real c) {read analog real vIn ;

write analog real vOut ; mode top=Atop(initValue,c) ; }

mode Atop(real iVal, real c) {read analog real vIn ;write analog real vOut ; mode mode0=choppy(2.0,-50.0,c);mode mode1=choppy(2.0,1.0,c) ;

trans initTrans from default to mode0 when true do {vOut=iVal}

Page 15: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Modes of Agent A1

ATop

write analog real v1 write discrete int p1

inv invChoppy {v1>0.0}

diff fv1 {d(v1)==2.0*v1-50.0+c}

mode0 mode1

write analog real v1 write discrete int p1

inv invChoppy {v1>0.0}

diff fv1 {d(v1)==2.0*v1+1.0+c}

v1<8.0 &&|v1-v2|>1.1

v1>12.0 &&|v1-v2|>1.0

transition guard: g1

mode name

variabledeclaration

dynamics

invariant:flow constraint

do {p1:=1}

do {p1:=0}

Page 16: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Architectural Hierarchy

Robots

Monitor

pos1 pos2

write analog position pos1, pos2

class position {double x; double y;}

Variable Specifiers

Update: discrete/analog

I/O interface: read/write/private

Page 17: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Architectural Hierarchy

Robot1

Robots

Robot2

pos1 pos2

r1Est1

r1Est2

r2Est1

r2Est2

Robots

Monitor

pos1 pos2

Page 18: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Behavioral Hierarchy

pos

r2Est1

r2Est2

r1Est1

r1Est2

Robot1

dTimer

timer == 1.

private analog real timer

awTargetdPlaniAway

atTargetdStopiAt

arrive

pos == target

movingdSteeraOmegaiFreq

sensingdStopiConst

sense

move

arrive

timer/updateFreq == 0

omega == k * (theta – phi)

pos.x == v * cos(phi)

pos.y == v * sin(phi)

.

.

Page 19: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Example: the Simplex Architecture-based Inverted Pendulum

Safety Controller

Baseline Controller

Experimental Controller

DecisionModule

PhysicalSystem

us

ub

ue

xu

Safety ControllerBaseline Controller

Experimental Controllerx0

Equilibrium state

Page 20: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

The Inverted Pendulum

m

l

x

g

Muf

Page 21: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Control Task and CHARON Description Control objective: to move the cart from one position to a

desired target position xt maintaining the pendulum at the upright position.

Planner: generates a set point xs every T seconds with rate c until the generated set point reaches the target position.

Lower level: stabilizes the system about [xs, 0, 0, 0 ]

Page 22: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Controllers

Page 23: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Physical System

Page 24: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Decision Module

Page 25: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Simulation Results

-1

0

1

2

3

4

5

-2 0 2 4 6 8

time

ctrlCmd

angle

VaBC

VaSC

position

VaEC

cVelocity

aVelocity

Sampling Frequency 50Hz

Maximum Position Tracking Error : 0.17m < 0.2m Initial Position : 0.0Initial Cart Velocity : 0.0 m/sInitial Angle : 5.0 degInitial Angular Velocity : 0.0 deg/s

Maximum Control Value : 3.78V < 4.96V

Maximum Cart Velocity : 0.32m/s < 1.0m/s

Maximum Angle : 5.0 deg< 10.0 deg

Page 26: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

CHARON Simulator Generation Process

CHARON

model

Agent and Mode generator

Simulator maingenerator

Agents

Modes

Simulatormain

TracePlotter

Tracegenerator

User-definedexternal classes

Numerical integrator

Differential Equations

Invariants

Algebraic Equations

Guards

Actions

Assertions

Page 27: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Wrap-up:Two Worlds Affect Each Other

- Simulink is augemented with a hierarchical state machine. We call it Simulink/Stateflow.

- Among other design tools of interest are Ptolemy II with continuous time domain, SHIFT/Teja, Modelica, and RT-UML.

- Controllers/Decision Module/Physical system case studyin CHARON: E.g. the Simplex-Architecture-based inverted pendulum controller[RTSSWIP 2000] andautonomous distributed multi-robots.

Page 28: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Wrap-up: Features of CHARON

- Discrete event/discrete state representation: discrete int/bool

- Discrete event/continuous state representation: discrete real

- Continuous time/continuous state representation: analog real

- Behavior description: mode with hierarchy

- Architecture description: agent with hierarchy

-Structured modern programming language : variable scoping, type-checking rules, formal semantics

Page 29: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Wrap-up: Features of CHARON (cont’d)

- Software engineering tool : reusable agent/mode definition, : simulator generator, verification tool box, code generator

- Non-determinism : non-deterministic execution of agents when more than one agent observe enabled transitions at once, currently interleaving semantics : non-deterministic choice of transitions when more than one transitions are enabled at once, currently randomly selected or user-guided : non-deterministic non-urgent jump when a guard is enabled while the invariant is not violated, currently probabilistically taken and 80/20 rule is embedded : non-deterministic differential and algebraic constraints with inequalities, currently only front-end supports this

Page 30: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Wrap-up: Interface Qualifiers of Agent Variables Input from environment:

input (v0.1) --> read (v0.37) Output to environment:

output (v0.1) --> write (v0.37) --> write/readwrite (v0.4) --> write exclusive/write shared (v0.7)

Local private (v0.1) --> hiding operation (v0.12)

--> private (v0.6)

Page 31: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Wrap-up: Other Type Qualifiers Update Qualifiers:

analog for continuous update or reset discrete for discrete update

Channel Qualifiers: buffered non-blocking communication

channel combined with buffer size, message type,

buffer management policy Example: write channel [2] of real cold chan1;

Page 32: Modeling Hybrid Systems Yerang Hur CIS 640, October 10, 2002 Department of Computer and Information Science University of Pennsylvania Code generation

Tool Demo

For manuals and more examples, visit http://www.cis.upenn.edu/mobies/experiments.php3.