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. Outline Hybrid Systems Simulink State-machine-based Representation CHARON Framework CHARON Language Examples Tool Demo Hybrid Systems Continuous Behaviors Discrete Events Physical processes in control system e.g. robots, aircraft, ... Collisions Decision logic Discrete communication (send/receive) Represented with Differential equations Algebraic equations Invariant constraints Guarded transitions between modes Hybrid Systems - two views Control engineering-oriented view reference discrete control command generator dynamical system output Hybrid Systems - two views (con’t) Computer scientist-oriented view - State machine embedding dynamics dx/dt=f(x) guard reset x dx/dt=g(x) - Continuous state variables with discrete state variables cont x disc y dx/dt=f(x,y) Y=1 guard reset x update y cont x disc y dx/dt=g(x,y) Simulink Simulink is block diagram-based notations for modeling dynamical systems. sources system block diagram sinks Elements of Simulink model Simulink (cont’d) Blocks for representing continuous components k x y : algebraic equation Gain Block y=kx a + c : algebraic equation Sum Block b c=a-b Derivative Block x du/dt y : differential equation y=dx/dt Integrator Block x Y: Y = y0 + x(t)dt State-Space Block u . X=Ax+Bu Y=Cx+Du y Simulink (cont’d) Hybrid system representation reference + - zero order holder Kp discrete time integrator Ki derivative approximation Kd + + + physical plant State-machine-based Representation x := 73.0 On x>=82.0 dx/dt=-x+100 x > 80.0 x < 70.0 Thermostat Off 68.0<=x dx/dt=-x 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 CHARON Framework CHARON Code (High level language) CHARON to Java Translator Drivers Java Libraries Java Code Control Code Generator Simulator Generator Human Interface Analysis 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 Example: TwoAgent Behavioral Hierarchy Architectural Hierarchy Agent Mode ATop choppy a1 (10.0,0.0) top TwoAgent A a2 (9.0,-1.0) mode0 mode1 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} … Example: TwoAgent 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 Mode ATop choppy mode0 mode1 a1 (10.0,0.0) top TwoAgent A a2 (9.0,-1.0) agent A(real initValue, real c) { read analog real vIn ; write analog real vOut ; mode top=Atop(initValue,c) ; } Modes of Agent A1 variable declaration invariant: flow constraint transition guard: g1 v1<8.0 && |v1-v2|>1.1 write analog real v1 write discrete int p1 do {p1:=1} write analog real v1 write discrete int p1 inv invChoppy {v1>0.0} diff fv1 {d(v1)==2.0*v1-50.0+c} v1>12.0 && |v1-v2|>1.0 do {p1:=0} inv invChoppy {v1>0.0} diff fv1 {d(v1)==2.0*v1+1.0+c} dynamics mode0 mode name mode1 ATop Architectural Hierarchy write analog position pos1, pos2 class position {double x; double y;} Robots pos1 pos2 Monitor Variable Specifiers Update: discrete/analog I/O interface: read/write/private Architectural Hierarchy Robots r1Est1 Robots pos1 r1Est2 pos2 Robot1 Monitor pos1 r2Est1 r2Est2 Robot2 pos2 Behavioral Hierarchy pos == target private analog real timer awTarget arrive atTarget r2Est1 r2Est2 r1Est1 dPlan iAway Robot1 dTimer . pos.x == v * cos(phi) . pos.y == v * sin(phi) r1Est2 . dStop iAt timer/updateFreq == 0 pos timer == 1 moving dSteer aOmega iFreq omega == k * (theta – phi) sense move sensing dStop iConst arrive Example: the Simplex Architecturebased Inverted Pendulum us Safety Controller Baseline Controller Experimental Controller ub Decision Module Physical System x ue x0 Equilibrium state u Experimental Controller Safety Controller Baseline Controller The Inverted Pendulum g l q M uf x m 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 ] Controllers Physical System Decision Module Simulation Results 5 ctrlCmd 4 angle VaBC 3 VaSC 2 position 1 VaEC cVelocity 0 -2 -1 0 2 4 6 8 aVelocity time Initial Position : 0.0 Initial Cart Velocity : 0.0 m/s Initial Angle : 5.0 deg Initial Angular Velocity : 0.0 deg/s Sampling Frequency 50Hz Maximum Control Value : 3.78V < 4.96V Maximum Position Tracking Error : 0.17m < 0.2m Maximum Cart Velocity : 0.32m/s < 1.0m/s Maximum Angle : 5.0 deg< 10.0 deg CHARON Simulator Generation Differential Equations Process Assertions Algebraic Equations Agents Invariants CHARON Agent and Mode generator Guards Modes Simulator main generator Simulator main model Actions User-defined external classes Numerical integrator Trace generator Trace Plotter Wrap-up: Two Worlds Affect Each Other - Simulink is augemented with a hierarchical state machine. We call it Simulink/Stateflow. - Controllers/Decision Module/Physical system case study in CHARON: E.g. the Simplex-Architecture-based inverted pendulum controller[RTSSWIP 2000] and autonomous distributed multi-robots. - Among other design tools of interest are Ptolemy II with continuous time domain, SHIFT/Teja, Modelica, and RT-UML. 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 Wrap-up: Features of CHARON (cont’d) - 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 - Software engineering tool : reusable agent/mode definition, : simulator generator, verification tool box, code generator Wrap-up: Interface Qualifiers of Agent Variables Input from environment: Output to environment: input (v0.1) --> read (v0.37) 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) 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; Tool Demo For manuals and more examples, visit http://www.cis.upenn.edu/mobies/experiments.php3.