4 TC 00 Modelbased systems engineering 2 5
4 TC 00 Model-based systems engineering 2. 5 Time: hybrid automata part 1 Bert van Beek (dr. ir. D. A. van Beek) Gem-Z 0. 120 d. a. v. beek@tue. nl of 71
Group setup • Sunday 23: 00 all groups will be locked. • The 2 person group created last will be distributed over two other remaining 2 person groups to create two 3 person groups. • Questions? 2 of 71
Contents • Automata in hybrid systems • • Examples Clocks Self loops Behavior • Urgency • Discrete variables • Multiple events at the same time-step • SVG inputs • Uncontrollable events • Input events and deadlock • Monitors • Pusher manual mode and use case 3 of 71
Continuous-time systems and functions • Continuous-time systems: • Values of variables are functions of time: at each time point, variable has one value. • Value of continuous variables are continuous functions of time • Value of algebraic variable may be a discontinuous function of time • Examples: alg real a = if time < 1 : 0 else 2 end; cont x = 0. 5; equation x' = a; // derivative of x is discontinuous // value of x is continuous x' overlaps a in graph 4 of 71
Continuous-time and discrete-event systems Continuous-time system • Values of variables can change only due to passing of time. • Events do not exist. Discrete-event system • Values of variables can change only due to events. • Time does not exist. 5 of 71
Hybrid systems Hybrid system • Values of variables can change due to passing of time. • Values of variables can change due to events. Urgent hybrid system (very common) • hybrid system where events have priority over time passing, • time passing is allowed only when no events can be executed. 6 of 71
Example of automaton in hybrid system • Modeling discrete operating modes, such as on and off : tank controller 7 of 71
group tank: // sole purpose to group elements as one component cont V = 5. 0; alg real Qi = if control. Closed: 0 else 5. 0 end; alg real Qo = sqrt(V); equation V' = Qi - Qo; end automaton control: // controller for continuous system event open, close; location Closed: initial; edge open when tank. V <= 2 goto Opened; location Opened: edge close when tank. V >= 10 goto Closed; end 8 of 71
Recap automaton events for switching to other location automaton control: event open, close; location consisting of edges location Closed: guard is Boolean expression initial; edge open when tank. V <= 2 goto Opened; location Opened: edge close when tank. V >= 10 goto Closed; end edge consisting of event, guard, goto part 9 of 71
Adding SVG visualization • Add SVG statements in separate CIF file for optimal readability • One file for behavior • One file for visualization • CIF file with SVG statements needs access to state of the model for visualization • CIF model does not need access to SVG statements • Therefore, visualization file imports the CIF model: import "10_tank. cif" • Import statement is replaced by the contents of the CIF model 10 of 71
Model 10_tank_svg. cif Import CIF model for access to model state import "10_tank. cif"; svgfile "tank. svg"; // previously discussed model State of tank svgout id "V" text value fmt(" V = %. 1 f", tank. V); svgout id "Qi" text value fmt("Qi = %. 1 f", tank. Qi); svgout id "Qo" text value fmt("Qo = %. 1 f", tank. Qo); svgout id "water" attr "height" value 7. 5 * tank. V; State of controller svgout id "n" attr "fill" value if control. Closed: "red" else "lime" end; 11 of 71
Execute 11 tank_svg. toodef 2 for visualization from "lib: cif 3" import *; cif 3 sim( "11_tank_svg. cif", "-i auto", "--speed=1", ); 12 of 71
F 10: Execute tooldef script with specified set of simulation options 13 of 71
Same simulation with manual setting of parameters • See next slide 14 of 71
Right click CIF file to simulate CIF model 15 of 71
Automatic input mode 16 of 71
Enable Frame-rate for real-time visualization Setting speed factor also results in real-time simulation 17 of 71
automatic real-time simulation 18 of 71
Tank visualization demo 19 of 71
Automaton behavior • An automaton can switch from its active location to another location via an edge: • only if the guard of the edge evaluates to true • by executing the event on the edge, and the (optional) update on the edge • Switching from one location to another takes zero time 20 of 71
Model execution • A model is executed by a sequence of steps, also called transitions. • There are two kinds of steps: • action-step. • time-step. • Execution of an action-step is done by: • execution of an event, • while the model time remains unchanged. • Execution of a time-step is done by: • increasing the model time, (elaborated further on following slides) • while the discrete model state remains unchanged. 21 of 71
Action-step • Execution of an action is done by executing an enabled event • An event is enabled when it occurs on • an outgoing edge from the active location of an automaton • such that the guard of the edge evaluates to true 22 of 71
Time-step Execution of a time-step is done: • by letting time proceed, • while solving the differential equations, by calculating the values of all continuous and algebraic variables as functions of time, • while keeping the discrete model state unchanged: • active locations unchanged, • values of all discrete variables unchanged. 23 of 71
Urgency • Execution of urgent events has priority over taking a time-step. Therefore: time-step is possible only when all urgent events are disabled and time-step terminates directly when an urgent event becomes enabled • Events are urgent by default, and can only be defined as non-urgent in the simulation options. 24 of 71
Urgent system • Time steps and urgent events are mutually exclusive. • In an urgent system, all events are urgent. • Therefore, in an urgent system: either a time-step is possible or one or more urgent events are possible or neither is possible (deadlock) 25 of 71
Abbreviation for derivatives Derivatives can be defined at the declaration of continuous variables cont x = ini; equation x' = e; can be abbreviated as cont x = ini der e; 26 of 71
Clocks / timers cont x = 0; equation x' = 1; can be abbreviated as cont x der 1; which is often used for clocks / timers 27 of 71
Example of automaton in hybrid system • Sawtooth generator 28 of 71
automaton generator: // sawtooth generator cont x = -1 der 1; Assignment to continuous event reset; variable only allowed in do block on edge of automaton location L: initial; edge reset when 1 <= x do x : = -1 goto L; end 29 of 71
Behavior sawtooth generator Repetitive behavior: • time step, while time passes, and value of clock variable x is calculated as a function of time • generator. reset event, while time does not pass, and clock variable is reset to -1 30 of 71
file sawtooth. tooldef 2: time step and action step from "lib: cif 3" import *; cif 3 sim( "10_sawtooth. cif", // default interactive GUI input mode "--output-state-filter=generator. x", "--plotviz=1", "--plotviz-filter=generator. x", ); 31 of 71
Screenshot until first event at time point 2 No time passing for events 32 of 71
DEMO 12_sawtooth. debug. tooldef 2 • Demo in interactive input mode • Show sequence of delays and action steps • In action step, time does not pass, remains unchanged 33 of 71
Recap: simplification of self-loop location L: . . edge reset when 1 <= x do x : = -1 goto L; self loop with goto can optionally be simplified by leaving out the goto part: location L: . . edge reset when 1 <= x do x : = -1; 34 of 71
Recap: single location need not have a name automaton generator: // sawtooth generator cont x = -1 der 1; event reset; Unnamed single location: initial; edge reset when 1 <= x do x : = -1; end Self loop 35 of 71
Example of automaton in hybrid system • Modeling discontinuous behavior: conveyer use case 36 of 71
Model conveyer. cif: next make behavior repetitive const real V_BELT = 2; // belt velocity when moving alg bool actuator; // actuator is a boolean cont x = 0; // position of a product on the belt, // initially x = 0 // x' is derivative of x equation x' = V_BELT * if actuator: 1 else 0 end; equation actuator = if time < 1 : false // elif time < 3 : true // elif time < 4 : false // else true // end; 0 1 3 4 <= <= time < 1 time < 3 time < 4 time 37 of 71
group conveyer: alg bool actuator = usecase. a; cont x = 0; equation x' = if a_conveyer: V_BELT else 0 end; end automaton usecase: alg int[0. . 1] a; cont t der 1; // clock instead of time event reset; location: initial; equation a = if t < 1 : true elif t < 2 : false else true end; edge reset when 4 <= t do t : = 0; end Assignment to continuous variable only allowed in do block on edge of automaton 38 of 71
DEMO 1 conveyer use case from "lib: cif 3" import *; cif 3 sim( "10_conveyer_usecase. cif", "-t 8", "-i auto", "--plotviz=1", "--plotviz-mode=live", "--plotviz-filter=a_conveyer, conveyer. x, usecase. t", "--solver-outstep=0. 01", ); Demo 11_conveyer_usecase. tooldef 2 39 of 71
DEMO 2 conveyer use case cif 3 sim( "10_conveyer_usecase. cif", "-t 8", // "-i auto", "--stateviz=1", "--stateviz-filter=time, a_conveyer, conveyer. x, usecase. t", "--plotviz=1", "--plotviz-mode=live", "--plotviz-filter=a_conveyer, conveyer. x, usecase. t", "--solver-outstep=0. 1", ); • Demo 12_conveyer_usecase. debug. tooldef 2: interactive (default) gui mode • Add time to stateviz-filter to show that the repetitive reset event takes zero time to execute 40 of 71
Difference in use of locations and discrete variables • Automata are useful for, among others, modeling of operating modes, that require simple names. • Discrete variables are useful for, among others: • counting and other arithmetic operations, that require values. • storing and handling data, such as production recipes, that require structured data types. 41 of 71
Summary behavior of discrete variables in hybrid model Value of discrete variables remains unchanged in time-step Only way to change the value of discrete variables is by means of a (multi)-assignment Assignments only occur in do block of edge in automaton 42 of 71
Use of discrete variable for counting in hybrid model • Add automaton count and discrete variable n, to count the number of zero crossings of the previous generator. • File zerocrossing. cif on next slide. 43 of 71
import “ 10_sawtooth. cif"; // saw tooth generator alg real x = generator. x; // generator imported from 10_sawtooth. cif; automaton count: // count zero crossings event up, down; disc int n; // counter location Negative: initial x < 0; // initial loc depends on initial value x edge up when x > 0 do n : = n + 1 goto Positive; location Zero: initial x = 0; edge down when x < 0 goto Negative; edge up when x > 0 goto Positive; location Positive: initial x > 0; edge down when x < 0 do n : = n + 1 goto Negative; end 44 of 71
45 of 71
zerocrossing. tooldef 2 for interactive GUI mode from "lib: cif 3" import *; cif 3 sim( "zerocrossing. cif", "--output-state-filter=count*, x", "--plotviz=1", "--plotviz-filter=count. n, x", ); 46 of 71
DEMO zerocrossing. tooldef 2 • Use interactive GUI input mode to see counting of zero crossings: • time step where clock variable increases is interrupted by "up " event at zero crossing. multiple events at the same time-point • clock reset by event " generator. reset " is followed by " down " event for counting, both in zero time. 47 of 71
Towards SVG inputs: model button_lamp. cif automaton button: event toggle; location Off: initial; edge toggle goto On; location On: edge toggle goto Off; end alg bool lamp = button. On; 48 of 71
DEMO button_lamp. cif 49 of 71
Add SVG file button_lamp. svg for visualization 50 of 71
Model svg/button_lamp_svg. cif // Right button click > Simulate CIF 3 specification. . . import ". . /button_lamp. cif"; svgfile ". . /button_lamp. svg"; svgout id "button" attr "width" value if button. On: 12 else 36 end; svgout id "lamp" attr "fill" value if lamp: "yellow" else "silver" end; svgout id "time" text value fmt("Time: %. 1 f", time); 51 of 71
DEMO svg/button_lamp_svg. cif To execute default (highlighted) choice type/press Enter 52 of 71
How to achieve interactive visualization? • How can user provide input via the visualization window? • Solution: SVG inputs Click button to switch on/off 53 of 71
SVG inputs So far SVG output statements have been treated, e. g: • svgout id "water" attr "height" value 7. 5 * tank. V; There also SVG input statements, e. g: • svgin id "button" event toggle; An SVG input statement connects an SVG object to a CIF event: When the SVG object is clicked in the visualization window, the associated CIF event is executed. 54 of 71
Model svgin/button_lamp_svgin. cif import ". . /button_lamp. cif"; svgfile ". . /button_lamp. svg"; SVG input statement connects SVG object with id "button" to event button. toggle svgin id "button" event button. toggle; svgout id "button" attr "width" value if button. On: 12 else 36 end; svgout id "lamp" attr "fill" value if lamp: "yellow" else "silver" end; svgout id "time" text value fmt("Time: %. 1 f", time); 55 of 71
SVG input constraints Visualization using SVG inputs requires • SVG input mode • real-time simulation mode Simulation option "-i svg", "--speed=2", or "--frame-rate=40", Effect SVG input mode simulation speed or frame-rate implicitly selects real-time simulation mode 56 of 71
DEMO svgin/button_lamp_svgin. tooldef 2 Move cursor onto clickable SVG object: • cursor changes into hand • stroke of object turns red 57 of 71
Uncontrollable events • Introduce a new kind of event: an uncontrollable event. • Declaration in CIF: uncontrollable u_toggle; • Uncontrollable events indicate events that should never be blocked by a controller • Uncontrollable events should be used only to model external events: • events generated by a user or user interface, such as pressing of a button, which cannot be prevented/blocked by a controller • events generated by sensors • events that occur in an svgin statement 58 of 71
Meaning of uncontrollable events • In supervisory control synthesis, uncontrollable events affect the outcome of the synthesis algorithm (master course 4 CM 30). • In all other uses, such as simulation or real-time control, the use of uncontrollable events is a modeling convention for better readability: they do not change the behavior of the model. 59 of 71
Button model updated with uncontrollable events automaton button: event toggle; automaton button: uncontrollable u_toggle; location Off: initial; edge toggle goto On; location Off: initial; edge u_toggle goto On; location On: edge toggle goto Off; end location On: edge u_toggle goto Off; end 60 of 71
Pusher in manual operation • Start with pusher. cif model from introduction lecture • Add actuator button in svg file • Add automaton for user interaction via an event 61 of 71
const real v_set = 2; // file pusher_nested_if. cif const real x_max = 2; alg bool a_pusher = pusher_button. On; // actuator cont x = 0; equation x' = if x <= 0 : if a_pusher: v_set else 0 end elif x < x_max : if a_pusher: v_set else -v_set end // x > 0 else if a_pusher: 0 else -v_set end // x >= x_max end; automaton pusher_button: uncontrollable u_toggle; location On: edge u_toggle goto Off; location Off: initial; edge u_toggle goto On; end 62 of 71
File pusher. svg id "a_pusher_button" 63 of 71
File pusher_svg. cif import "pusher. cif"; svgfile "pusher. svg"; svgout id "shaft" attr "width" value 40 + 40 * x; svgout id "head" attr "x" value 200 + 40 * x; svg input svgin id "a_pusher_button" event a_pusher_state. u_toggle; svgout id "a_pusher_button" attr "fill" value if a_pusher: "green" else "lightgrey" end; 64 of 71
DEMO 1_pusher_svg. tooldef 2 from "lib: cif 3" import *; cif 3 sim( "pusher_svg. cif", "-i svg", "--stateviz=1", "--frame-rate=40", ); 65 of 71
DEMO 2_pusher_svg_gui. tooldef 2 from "lib: cif 3" import *; cif 3 sim( "pusher_svg. cif", "-i gui", "--stateviz=1", "--frame-rate=40", ); 66 of 71
Two equivalent equations for pusher equation x' = if x <= 0 and a_pusher: v_set elif x <= 0 and not a_pusher: 0 elif x < x_max and a_pusher: v_set // 0 < x_max elif x < x_max and not a_pusher: -v_set // 0 < x_max elif a_pusher: 0 // x_max <= x else -v_set // x_max <= x and not // a_pusher end; equation x' = if x <= 0 : if a_pusher: v_set else 0 end elif x < x_max : if a_pusher: v_set else -v_set end // x > 0 else if a_pusher: 0 else -v_set end // x >= x_max end; 67 of 71
Automaton model for pusher automaton pusher: event extend, at_max, at_min, retract; cont x = 0; location Retracted: initial; equation x' = 0; edge extend when a_pusher goto Moving; location Moving: equation x' = if a_pusher: v_set else -v_set end; edge at_max when a_pusher and x_max <= x goto Extended; edge at_min when not a_pusher and x <= 0 goto Retracted; location Extended: equation x' = 0; edge retract when not a_pusher goto Moving; end 68 of 71
SELF STUDY 2_pusher_automaton/1_pusher_svg. tooldef 2 from "lib: cif 3" import *; cif 3 sim( "pusher_svg. cif", "-i svg", "--stateviz=1", "--frame-rate=40", ); 69 of 71
SELF STUDY 2_pusher_automaton/1_pusher_svg_gui. tooldef 2 from "lib: cif 3" import *; cif 3 sim( "pusher_svg. cif", "-i gui", "--stateviz=1", "--frame-rate=40", ); 70 of 71
Homework for Tuesday See Course Guide Exercises: • 4. Scalable solutions 1 • 5. Time and SVG 5. 1 – 5. 8 Quizzes (ultimate closing deadline in one week): • Quiz: 4. Scalable solutions – reuse 1; 5. 0 Time (6 questions) You could also do the quiz for next week: • Quiz: 5. 2 SVG 71 of 71
- Slides: 71