Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 INTRO TO STATE MACHINES CIS 4350 Rolf Lakaemper

2 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

3 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

4 We need to determine the States Inputs Transitions Modeling

5 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

6 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

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

8 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.

9 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!

10 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 + 4000 + (long) (Math.random() * 500); setVelocity(500, -500); state = SPINNING; } else { setVelocity(500, 500); state = FORWARD; } break; case FORWARD: if (detectWall()) { endSpinTime = startTime + 4000 + (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) { }


Download ppt "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,"

Similar presentations


Ads by Google