[TUT] What is a finite state machine?

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

[TUT] What is a finite state machine?

Post by KawaGeo »

You'll find full explanation regarding finite state machines [FSM] at: http://en.wikipedia.org/wiki/Finite_state_machine

We could use an event driven FSM for our game development. An example below best illustrates how it is implemented. The example is a resumable LED Timer, slightly modded one from the standard distribution (ver 0.11) created by me and changed for new Timer class by Shine. It would be handy to show the "mickey mouse" diagram on blackboard but impossible here. Instead, I'll try to explain in written form.

I am using 6 states for the example. The description for each is given below.

Code: Select all

	State 0:
	    reset Timer
	    display Dial

	State 1:
	    start Timer
	    change State to 2

	State 2:
	    display Dial
	    if Timer exceeds Dial's range,
	        change State to 0

	State 3
	    stop Timer
	    change State to 4

	State 4:
	    display Dial

	State 5
	    start Timer

	    change State to 2
If you like, draw 6 big circles in a row and mark them with numbers 0,1,2,3,4, and 5, one inside each circle.

Event driven occurance is done by pressing X button. It changes the state 0 to state 1 to start, state 2 to state 3 to stop and state 4 to state 5 to resume.

Draw curved arrows marked with "X" between them as explained here.

All states 1, 3, 5 are changed immediately to the next state.

Draw curved dashed arrows between them as described showing the state changing automatically.

The states 0, 2, and 4 employ the displaying the dial indefinitely until the button X is pushed.

A special case when the dial is about out of range: change state 2 to state 0.

Draw the dashed arrow from state 2 to state 0 showing the special case.

Another short dashed arrow to state 0 from outside showing the initialization.

Pressing button O can occur anytime from any state to state 0 to reset the timer. No need to draw any further unless you wish to do so.

You might ask me why not changing state 5 to state 1 without needing to restart the timer in the state 5. It does the same thing, isn't it? Yes, it does BUT Shine explained that Controls.read() delays somewhat in the PSP hardware and the pad is read twice in the getState() function before resuming the timer. So it is better to restart immediately in the state 5.

The state machine can be modded with only 4 states. I'll leave that for your homework.

Here is the modded code for the "game loop" to show the complete state machine...

Code: Select all

local timer = Timer.new()
local state = 0

repeat
  state = getState(state)
  
  if state == 0 then       -- reset timer
    timer:reset()
    displayDial(timer)
    
  elseif state == 1 then   -- start timer
    timer:start()
    state = 2
    
  elseif state == 2 then
    if displayDial(timer) then state = 0 end
    
  elseif state == 3 then   -- stop timer
    timer:stop()
    state = 4
    
  elseif state == 4 then
    displayDial(timer)

  elseif state == 5 then   -- resume timer
    timer:start()
    state = 2
  end
  
until state == -1
Here is the screenshot:
Image

And you can download the fully packed louser compatible zipfile at my webpage.

Hope you enjoy the tutorial.
Geo Massar
Retired Engineer
arrggg
Posts: 11
Joined: Thu Sep 29, 2005 10:07 am

Post by arrggg »

Thanks for the great tutorial! I got a couple ideas for this now.
Post Reply