Controller Design Using Multiple Models of Computation Edward Lee Johan Eker with thanks to Paul Griffiths, Jie Liu, Xiaojun Liu, Steve Neuendorffer, and Yuhong Xiong MoBIES PI-Meeting, 16-18 July 2001, Jackson Hole Overview Mixed signal control systems The throttle control system by Paul Griffith Modal controllers Extended throttle controller by Johan Eker Design patterns The use of domains and hierarchy Mixed signal models Modal models Code generation co-compilation Eker & Lee, UC Berkeley 2 A Throttle Control System discrete-time controller throttle model Input: desired throttle angle (and derivatives) Throttle controller by Paul Griffiths (Mobies Phase II, UC Berkeley) Top-level model Continuous-time (CT) domain in Ptolemy II Eker & Lee, UC Berkeley 3 A Throttle Control System Result of executing the model shows intial convergence phase followed by tracking with a slight phase lag. The control signal chatters to overcome friction. Eker & Lee, UC Berkeley 4 Design Pattern: Discrete Time in Continuous Time Synchronous dataflow director indicates a new model of computation. No director indicates no new model of computation. Eker & Lee, UC Berkeley 5 Control Engineer View plant dynamics in continuous time controller in discrete time focus on stability, phase margins, rise time... assume ideal sampling with no or little latency Eker & Lee, UC Berkeley 6 Embedded System Engineer View dynamics modeled with RK-4, variable-step solver controller modeled in synthesizable SDF, FRP, … focus on scheduling, memory, communication... assume fixed controller design Eker & Lee, UC Berkeley 7 A More Integrated Approach controller design informed by software issues domain-oriented modeling language modeling = implementation latency and jitter are part of the model software design informed by controller issues expressing timing constraints correct-by-construction synthesis heterogeneous modeling Eker & Lee, UC Berkeley 8 Elaborated Throttle Control System Eker & Lee, UC Berkeley 9 Design Pattern: Modal Discrete-Time Controller in Continuous Time continuous time finite-state machine discrete time Hierarchical, heterogeneous model Eker & Lee, UC Berkeley 10 This is Still An Idealized Model No jitter, no delays Eker & Lee, UC Berkeley 11 Extend the ideal model Influence from implementation: Jitter Control delay Execution: Multitasking environment Incorporate the behavior of the RTOS Communication: Shared communication links Behavior of the network Eker & Lee, UC Berkeley 12 A more accurate model Eker & Lee, UC Berkeley 13 Software Realism Express timing constraints sample rates latency jitter tolerances Build models in appropriate abstractions Giotto: time-triggered HPM: hierarhical preemptive multitasking FRP: functional-reactive programming … These facilitate correct-by-construction implementation Eker & Lee, UC Berkeley 14 The Next Problem: Synthesizing an Implementation Eker & Lee, UC Berkeley 15 Outline of our Approach Jeff Tsay, Christopher Hylands, Steve Neuendorffer Model of Computation semantics defines communication, flow of control parser Ptolemy II model scheduler Schedule: - fire Gaussian0 - fire Ramp1 - fire Sine2 - fire AddSubtract5 - fire SequenceScope10 method call All actors are given in Java, then translated to embedded Java, C, VHDL, etc. if block method call block abstract syntax tree … for (int i = 0; i < plus.getWidth(); i++) { if (plus.hasToken(i)) { if (sum == null) { sum = plus.get(i); } else { sum = sum.add(plus.get(i)); } } } … target code Eker & Lee, UC Berkeley 16 Division of Responsibility MoC semantics defines flow of control across actors communication protocols between actors Actors define: functionality of components Actors are compiled by a MoC-aware compiler generate specialized code for actors in context Hierarchy: Code generation at a level of the hierarchy produces a new actor definition We call this co-compilation. Multiple domains may be used in the same model Eker & Lee, UC Berkeley 17 Software Progress Build on: First version on Titanium (UC Berkeley) Second version on Soot (McGill) Targeting: Simulation acceleration Embedded software synthesis Maryland subcontract Configurable hardware synthesis delegated to Brigham Young Eker & Lee, UC Berkeley 18 Our Generator Approach Actor libraries are built and maintained in Java more maintainable, easier to write polymorphic libraries are rich and small Java + MoC translates to target language concurrent and imperative semantics Efficiency gotten through code transformations specialization of polymorphic types code substitution using MoC semantics removal of unnecessary code Eker & Lee, UC Berkeley 19 Code transformations (data types) // Original actor source Token t1 = in.get(0); Token t2 = in.get(1); out.send(0, t1.multiply(t2)); specialization of Token declarations // With specialized types IntMatrixToken t1 = in.get(0); IntMatrixToken t2 = in.get(1); out.send(0, t1.multiply(t2)); The Ptolemy II type system supports polymorphic actors with propagating type constraints and static type resolution. The resolved types can be used in optimized generated code. See Jeff Tsay, A Code Generation Framework for Ptolemy II Eker & Lee, UC Berkeley 20 Type system by Yuhong Xiong Type System Input of general type - anything will do Output of type general - pure event with no value general double Polymorphic output - type depends on the parameters double general double int Opaque port - types propagated from inside Lossless runtime type conversion Polymorphic actor uses late binding in Java to determine implementation of addition. Eker & Lee, UC Berkeley 21 Type System Extensible type lattice Knowledgeable users can add full-featured types Unification infrastructure Finds a least fixed point Composite types records, arrays, matrices Higher-order types planned model = data Experiments with dependent types encoding MoC constraints Eker & Lee, UC Berkeley 22 Code transformations (MoC-informed) // With specialized types IntMatrixToken t1 = in.get(0); IntMatrixToken t2 = in.get(1); out.send(0, t1.multiply(t2)); MoC-polymorphic code is replaced with specialized code. transformation using MoC semantics // Extended Java with specialized communication int[][] t1 = _inbuf[0][_inOffset = (_inOffset+1)%5]; int[][] t2 = _inbuf[1][_inOffset = (_inOffset+1)%5]; _outbuf[_outOffset = (_outOffset+1)%8] = t1 * t2; See Jeff Tsay, A Code Generation Framework for Ptolemy II Eker & Lee, UC Berkeley 23 Synchronous Dataflow (SDF) Domain Balance equations (one for each channel): FAN = FBM Scheduled statically Decidable resource requirements get(0) send(0,t) B A N M token t Available optimizations: eliminate checks for input data statically allocate communication buffers statically sequence actor invocations (and inline?) Eker & Lee, UC Berkeley 24 Synchronous/Reactive Domain A discrete model of time progresses as a sequence of “ticks.” At a tick, the signals are defined by a fixed point equation: x OLf (1) O L M P M P y f ( z ) M P M P P M f ( x , y ) Nz P QM N Q A ,t B ,t C ,t A x C B Available optimizations: • Statically sequence fixed-point iteration • Communication via registers y z Eker & Lee, UC Berkeley 25 Other Domains with Useful Properties for Code Generation Strong static analyzability Giotto (time triggered) HPM (hierarchical preemptive multitasking) FRP (functional reactive programming – Yale) Finite state machines Discrete time Good for hardware descriptions Discrete events Process networks Continuous time (analog hardware) Eker & Lee, UC Berkeley 26 Hierarchical Heterogeneity Ptolemy II composes domains hierarchically, where components in a model can be refined into subcomponents where the component interactions follow distinct semantics. M: Manager E0 Transparent Composite Actor Opaque Composite Actor D1: local director E2 D2: local director E1 P1 P2 E4 P5 P6 E3 P3 P4 E5 P7 Eker & Lee, UC Berkeley 27 Conclusions Hierarchically heterogenous modeling matches the applications well… Hierarchically heterogenous modeling appears to be suited to high-quality sythesis Eker & Lee, UC Berkeley 28