INTRO TO STATE MACHINES CIS 4350 Rolf Lakaemper. State Machines A state machine represents a system as a set of states, the transitions between them,

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

What is a State Transition Diagram? Why are they used? How to draw them.
10/2/0810/2/2008ECE 561 -ECE Lecture 51 State Machine Implementation 10/2/20081ECE Lecture 5.
Written by: Dr. JJ Shepherd
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
14-Jun-15 State Machines. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine has some number.
CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
25-Jun-15 State Machines. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine has some number.
1 Intro to Finite Automata Chapter 2 introduces the concept of a Finite Automata (or FA). An FA has several properties: It is theoretical. It allows computer.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ for Engineers and Scientists Third Edition
Chapter 1 Program Design
14-Jul-15 State Machines Abbreviated lecture. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
More Multithreaded Programming in Java David Meredith Aalborg University.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Event Driven Programming
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
How to Create a Videogame By: Connor McCann. Java Java is one of many programming languages Java is used to run web browsers and most PC video games I.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Web Based Programming Section 8 James King 12 August 2003.
Lecture # 15 Theory Of Automata By Dr. MM Alam 1.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
Branches and Program Design
Wall Encounter By Made easy by Dwayne Abuel.
13-Nov-1513-Nov-1513-Nov-15 State Machines. What is a state machine? A state machine is a different way of thinking about computation A state machine.
EGR101-34R "lecture on hardware- software" FB 7/10/2004 Digital Electronics Logic Gates Logic gates work with the voltage level of the signals. They are.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Multithreading in JAVA
1 Software Construction and Evolution - CSSE 375 Exception Handling – Chaining & Threading Steve Chenoweth Office: Moench Room F220 Phone: (812)
Simulator Protocol. coordinator simulator Component tN tN. tL After each transition tN = t + ta(), tL = t simulator Component tN tN. tL simulator Component.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSCI1600: Embedded and Real Time Software Lecture 28: Verification I Steven Reiss, Fall 2015.
Dr Nick Mitchell (Room CM 224)
Digital System Design using VHDL
T HE G AME L OOP. A simple model How simply could we model a computer game? By separating the game in two parts: – the data inside the computer, and –
Computer Science 320 A First Program in Parallel Java.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
55:032 - Intro. to Digital DesignPage 1 VHDL and Processes Defining Sequential Circuit Behavior.
Automata & Formal Languages, Feodor F. Dragan, Kent State University 1 CHAPTER 3 The Church-Turing Thesis Contents Turing Machines definitions, examples,
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Collision Theory and Logic
CSCD 330 Network Programming
Chapter 13: Multithreading
Collision Theory and Logic
Lesson 9: "if-else-if" and Conditional Logic
Electronics II Physics 3620 / 6620
Event Driven Programming
Java Threads (Outline)
Java Threads (Outline)
Chapter 15 Multithreading
State Machines 6-Apr-196-Apr-19.
Threads.
State Machines 8-May-19.
State Machines 16-May-19.
STATE MACHINE AND CONCURRENT PROCESS MODEL
STATE MACHINE AND CONCURRENT
Lecture 19 A bigger FSM example: Hungry Robot Ant in Maze.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

INTRO TO STATE MACHINES CIS 4350 Rolf Lakaemper

State Machines A state machine represents a system as a set of states, the transitions between them, along with the associated inputs and outputs. A state machine is a particular conceptualization of a particular sequential process. State machines can be used for many things, they are used heavily in logic design, computer architecture, robotics and games

Let’s start with a simple example. We want to write control a robot, that moves forward. Whenever it detects a wall in close proximity in moving direction, it joyfully spins for a certain time. Then it checks if, in the resulting direction, there is still a wall present. It continues to move forward or to spin, depending. Example

We need to determine the States Inputs Transitions Modeling

The obvious states: Moving forward Spinning The obvious Inputs: Wall Non-wall Transitions: Let’s draw a diagram. (remark: this diagram is NOT the implementation of the problem! The robot does not spin “for a certain time”, but only until it does not see a wall!) Modeling Moving Forward Spinning Wall!No Wall Wall! No Wall

We could have multiple inputs Not all states must react to all inputs State transitions can lead to the same state Let’s model the real problem, i.e. incl. a timer Remarks

Implementation II Moving Forward Spinning Wall! No Wall Wait for End of Spinning Still Timer: OK Angular Velocity == 0 Wall! Init

Remarks  State machines can assume an underlying clock, which causes a state transition. Such state machines usually run in a separate Thread that handles a state and then sleeps for a while.  State machines can be event controlled, i.e. state transitions are caused by events of the inputs. In this case, the state machine must implement a “central input listener” that handles all possible input events.

Remarks  If you want to go deeper into the theory of state machines, Automata Theory is the word.  State machines are a convenient tool for many programming purposes. Typical examples are Video- Game AI control, Robotics and anything that otherwise would encounter horrible if-else constructs  State machines are typically implemented in a switch- case manner.  Very often they are helpful to solve real-world problems, like the wolf-man-goat-cabbage problem!

The Happy Wall Robot Thread, JAVA public void run() { long SLEEPYTIME = 100; int state = 0; // still while (true) { long startTime = System.currentTimeMillis(); switch (state) { case STILL: if (detectWall()) { endSpinTime = startTime (long) (Math.random() * 500); setVelocity(500, -500); state = SPINNING; } else { setVelocity(500, 500); state = FORWARD; } break; case FORWARD: if (detectWall()) { endSpinTime = startTime (long) (Math.random() * 500); setVelocity(800, -800); state = SPINNING; } break; case SPINNING: if (endSpinTime <= startTime) { setVelocity(0, 0); state = WAITFORSTILL; } break; case WAITFORSTILL: if (!detectSpinning()) { state = STILL; } break; } // timing long currentTime = System.currentTimeMillis(); long pauseTime = Math.max(1, SLEEPYTIME - (currentTime - startTime)); try { Thread.sleep(pauseTime); } catch (InterruptedException ex) { }