Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Factoring FSM States.

Slides:



Advertisements
Similar presentations
State Transition Diagrams Chapter 13
Advertisements

Chapter #10: Finite State Machine Implementation
MiniDraw Testing COMP 102 # T1
VEX and Robot C Chris Patterson Presented by Modified by J. Andazola.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
1 COMP541 State Machines Montek Singh Feb 8, 2012.
Advanced Digital Circuits ECET 146 Week 7 Professor Iskandar Hack ET 221B,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington “For each” & Patterns with.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
1 COMP541 Finite State Machines - 1 Montek Singh Sep 22, 2014.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
1 COMP541 Sequential Logic – 2: Finite State Machines Montek Singh Feb 29, 2016.
1 4-Integrating Peripherals in Embedded Systems. 2 Introduction Single-purpose processors  Performs specific computation task  Custom single-purpose.
Decision Making: while loops and Boolean Logic. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Finite State Machines ENGR 110 #7 2016
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Finite State Machines Mealy machine inputs Outputs next state function
UML for design: Class Diagrams ENGR 110 #
ROBOTC for VEX Online Professional Development
Chapter 3: Decisions and Loops
Welcome to Computer Science Jeopardy
ECS10 10/10
COMP541 Sequential Logic – 2: Finite State Machines
ECEN 248 Lab 9: Design of a Traffic Light Controller
FSM Controllers ENGR 110 #
Sequential logic examples
Digital Logic & Design Dr. Waseem Ikram Lecture 38.
Evaluation in Engineering Design Process Modeling and FSM
Introduction to the C Language
Java I.
While Loops and If-Else Structures
While Loops and If-Else Structures
While Loops and If-Else Structures
While Loops and If-Else Structures
ICT Programming Lesson 3:
Sequential logic implementation
if-else Structures Principles of Engineering
Topics discussed in this section:
Lecture 24 Logistics Last lecture Today HW7 back today
While Loops and If-Else Structures
While Loops and If-Else Structures
Loops CGS3416 Spring 2019 Lecture 7.
Introduction to Arduinos
CSCI1600: Embedded and Real Time Software
While Loops And If-Else Structures
LEGO Mindstorms Robot and Java
Control Structure.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Factoring FSM States ENGR 110 #

© Peter Andreae ENGR : 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

© Peter Andreae ENGR : 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”

© Peter Andreae ENGR : 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!

© Peter Andreae ENGR : 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!

© Peter Andreae ENGR : 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.

© Peter Andreae ENGR : 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; }

© Peter Andreae ENGR : 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

© Peter Andreae ENGR : 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

© Peter Andreae ENGR : 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

© Peter Andreae ENGR : 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

© Peter Andreae ENGR : 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

© Peter Andreae ENGR : 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){...

© Peter Andreae ENGR : 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 …

© Peter Andreae ENGR : 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