Download presentation
Presentation is loading. Please wait.
Published byGervase Maxwell Modified over 8 years ago
1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Factoring FSM States ENGR 110 #10 2016
2
© Peter Andreae ENGR 110 20: 2 Menu Factoring states in a FSM Controller Admin: Assignment 3 (FSM) starting today. Test on Wed 10 Aug, 6-7pm, MC LT101, MC LT103
3
© Peter Andreae ENGR 110 20: 3 Understanding the Lights Controller EW Go EW Stopping timer → EW:red → NS:grn → setTimer(15) NS Go NS Stopping timer NS Go ready carEW NS Go must change timer → NS:amb → setTimer(3) EW Go ready EW Go carNS EW Go must change timer → EW:amb → setTimer(3) timer → EW:red → NS:grn → setTimer(15) EW Stopping carEW NS Stopping carNS timer → NS:red → EW:grn → setTimer(15) carNS carEW → NS:amb → setTimer(3) timer → NS:red → EW:grn → setTimer(15) carNS → EW:amb → setTimer(3) “NS Go, EW Stop, EW car present, waiting for long timer” “NS Go, EW Stop, waiting for EW car, waiting for long timer” “NS Go, EW Stop, long timer done, waiting for carEW” “EW Stopping, EW car present waiting for short timer”
4
© Peter Andreae ENGR 110 20: 4 Understanding the Lights Controller EW Go EW Stopping timer → EW:red → NS:grn → setTimer(15) NS Go timer NS Go ready carEW NS Go must change timer → NS:amb → setTimer(3) EW Go ready EW Go carNS EW Go must change timer → EW:amb → setTimer(3) timer → EW:red → NS:grn → setTimer(15) EW Stopping carEW NS Stopping carNS timer → NS:red → EW:grn → setTimer(15) carNS carEW → NS:amb → setTimer(3) timer → NS:red → EW:grn → setTimer(15) carNS → EW:amb → setTimer(3) NS Stopping NS Go carEW && timer → NS:amb → setTimer(3) One “base” state plus things to remember ONE SENSOR ONLY, EVER!
5
© Peter Andreae ENGR 110 20: 5 Design with Factored States timer → EW:red → NS:grn → setTimer(30) NS Go NS Stopping carEW & timerFlag → carEWflag=true timer && carEWflag OR carEW && timerFlag → carEWflag=false → timerFlag=false → NS:amb. → setTimer(5). state variables (things the FSM needs to remember) carEWflag carNSflag timerFlag timer → NS:red → EW:grn → setTimer(30) EW Stopping timer & carEWFlag → timerFlag=true carEW → carEWflag=true EW Go carNS & timerFlag → carNSflag=true timer & carNSFlag → timerFlag=true carEW → carEWflag=true timer && carNSflag OR carNS && timerFlag → carNSflag=false → timerFlag=false → NS:amb. → setTimer(5). Always need ONE sensor on each transition. May have any number of variable tests must reset flags once not needed!
6
© Peter Andreae ENGR 110 20: 6 Factoring State: Extra variables Identify groups of states which are only there to remember sensors not doing different actions Combine into single group Make Boolean variables for the sensors to be remembered Add conditions to the transitions based on the variables Turn the variables off once they have been “used” Add self-transitions to all states, for setting the variables, wherever they may need to be remembered.
7
© Peter Andreae ENGR 110 20: 7 Implementing with factored states private String state = "NSgo"; private boolean carEWflag; private boolean carNSflag; private boolean timerFlag; public void signal(String sensor){ if (state.equals("NSgo") { if (!timerFlag && sensor.equals("carEW") ) { carEWflag=true; } if (!carEWflag && sensor.equals(“timerExpired")) { timerFlag=true; } if ((carEWflag && sensor.equals(“timerExpired”) ) || (timerFlag && sensor.equals(“carEW”))) { intersection.NSamber();. resetTimer(5000); state = “NSStopping”; carEWflag=false; timerFlag=false; }
8
© Peter Andreae ENGR 110 20: 8 Adding Right Turns How do right turns work? timer → EW:red → NS:grn → setTimer(30) NS Go NS Stopping carEW & timerFlag → carEWflag=true timer && carEWflag OR carEW && timerFlag → carEWflag=false → timerFlag=false → NS:amb. → setTimer(5). timer → NS:red → EW:grn → setTimer(30) EW Stopping timer & carEWFlag → timerFlag=true carEW → carEWflag=true EW Go carNS & timerFlag → carNSflag=true timer & carNSFlag → timerFlag=true carEW → carEWflag=true timer && carNSflag OR carNS && timerFlag → carNSflag=false → timerFlag=false → NS:amb. → setTimer(5). Hide some details WE S N
9
© Peter Andreae ENGR 110 20: 9 Adding Right Turns timer → EW:red → NS:grn → setTimer(30) NS Go NS Stopping timer “and” carEW → NS:amb. → setTimer(5). timer → NS:red → EW:grn → setTimer(30) EW Stopping EW Go timer “and” carNS → NS:amb. → setTimer(5). timer carEW. timer carNS.carNS. carEW. WE S N
10
© Peter Andreae ENGR 110 20: 10 Adding Right Turns timer → EW:red → NS:grn → setTimer(30) NS Go NS Stopping timer “and” carEW → NS:amb. → setTimer(5). timer → NS:red → EW:grn → setTimer(30) EW Stopping EW Go timer “and” carNS → NS:amb. → setTimer(5). timer carEW. timer carNS. carEW. NS Right EW Right Stopping NS Right Stopping WE S N
11
© Peter Andreae ENGR 110 20: 11 Adding Right Turns timer && carNSrtFlag → EW:red → NS:grn → setTimer(30) NS Go NS Stopping timer “and” carEW → NS:amb. → setTimer(5). timer && carEWrtFlag → NS:red → EW:grn → setTimer(30) EW Stopping EW Go timer “and” carNS → NS:amb. → setTimer(5). timer carEW. timer carNS. carEW. NS Right EW Right Stopping NS Right Stopping timer → NSrt:red → NS:grn → setTimer(30) timer && carNSrtFlag → EW:red → NSrt:grn → setTimer(15) timer → NSrt:amb. → setTimer(5) → carNSrtflag=false. timer → EWrt:red → EW:grn → setTimer(30) timer → EWrt:amb. → setTimer(5) → carEWrtflag=false. timer && carEWrtFlag → NS:red → EWrt:grn → setTimer(15) WE S N
12
© Peter Andreae ENGR 110 20: 12 Alternative designs for signal(…) Simple design: Large nested if-else one case for each state one nested case for each transition from that state. –Can get very large and unwieldy Alternate: Large nested if-else one case for each sensor one nested case for each state that has a transition for that sensor One method per state: if-else on state each case calls a method for that state each method has if-else for transitions from that state Table driven: Have a big table of transitions. Could use switch statement
13
© Peter Andreae ENGR 110 20: 13 Decomposing signal(…) public void signal(String sensor){ if (state.equals("NSgo")) { signalNSgo(sensor); } else if (state.equals("EWgo")) { signalEWgo(sensor); } else if (state.equals("NSwait")){ signalNSwait(sensor); }... private void signalNSgo(String sensor){ if (sensor.equals("timer") && !carEWflg){ system.turnNSred(); state = "NSgoReady"; } else if (sensor.equals("carEW")){ careEWflg = true; } private void signalEWgo(String sensor){...
14
© Peter Andreae ENGR 110 20: 14 Look-up table of transitions Code Looks up current state and sensor in the table. Performs the actions Changes the state Much neater design: signal() method much smaller Easier to change A bit tricky to implement without more advanced Java features. Table Driven Design StateSensorActionNew State NSgotimerturnNSred turnEWgreen EWgo NSgocarEWcarEWflg=true EWgo && carNSflg timerturnEWred turnNSgreen NSgo …
15
© Peter Andreae ENGR 110 20: 15 Alternate Designs: Event Driven Controller Integrated Controller System : autonomous vehicle lift traffic lights etc. Controller Signals/sensors actions sensor action sensor System eg, Arduino processor controlling device sensors always reporting Controller infinite loop: access system state and internal variables action
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.