1 / 10

Intro to State Machines

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.

elroy
Download Presentation

Intro to State Machines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  4. Modeling We need to determine the States Inputs Transitions

  5. 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!

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

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

  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 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;

More Related