100 likes | 284 Views
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.
E N D
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
Example 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.
Modeling We need to determine the States Inputs Transitions
Modeling No Wall 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!) Moving Forward Wall! No Wall Spinning Wall!
Remarks 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
Implementation II Init Moving Forward Still No Wall Wall! Wall! Angular Velocity == 0 Spinning Wait for End of Spinning Timer: OK
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 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) { } } } public void run() { long SLEEPYTIME = 100; intstate = 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;