Top Banner
States and State Machines CSE 251 Dr. Charles B. Owen Programming in C 1
20

States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

Jun 07, 2018

Download

Documents

dothuy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

States and State Machines

CSE 251 Dr. Charles B. OwenProgramming in C1

Page 2: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

What is this for?

State machines are commonly used in…

Embedded Systems

Factory/Process Controls

CSE 251 Dr. Charles B. OwenProgramming in C2

Page 3: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

State

State – An abstraction of the current status of a system. States are assigned names.

Waiting for a KeypressWaiting for Elvis

Paper JammedBattery is Below LimitWaiting for Elvis

Raising Firearm to FireCellphone is DialingD O i

Battery is Below LimitPower is OnDoor Open

Door Opening Prius Accelerator Stuck

Verbs with “ing” Statement of condition

CSE 251 Dr. Charles B. OwenProgramming in C3

Page 4: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

States in a Garage Door Are there any more states?

DoorClosed

DoorOpen

CSE 251 Dr. Charles B. OwenProgramming in C4 A

Page 5: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

More States

DoorOpening

DoorClosing

CSE 251 Dr. Charles B. OwenProgramming in C5

Page 6: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

How we will express this in a program

/* Our possible garage door states */#define DoorClosed 1#define DoorOpening 2

int main(){i D Cl d#define DoorOpening 2

#define DoorOpen 3#define DoorClosing 4

int state = DoorClosed;…

In themain functionAbove main in our program

In the main function

Why do we care? We do different things y gdepending on the current state. What does the button do in each state?

CSE 251 Dr. Charles B. OwenProgramming in C6 B

Page 7: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

Naming Conventions ‐ States

We will usually name states with camel‐case and a capital first letter. We’ll use #defines in our programs for state names.

WaitingForKeypressWaitingForElvis

PaperJammedl i iWaitingForElvis

RaisingFirearmCellphoneDialing

BatteryBelowLimitPowerOnDoorOpen

DoorOpeningp

PriusAccelStuck

Verbs with “ing” Statement of conditionVerbs with  ing Statement of condition

CSE 251 Dr. Charles B. OwenProgramming in C7

Page 8: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

EventsA i i i I iAn event is an occurrence in time. It is considered atomic and instantaneous.  

Left mouse button pressedKey pressed

Paper is jammedMessage has timed outy p

Elvis has left the buildingFlight 529 has landedPower turned on

Message has timed out10 seconds has elapsedBattery is below limit

P j t 2 i dPower turned on Project 2 is due

Past tense verbs Onset of condition

CSE 251 Dr. Charles B. OwenProgramming in C8

Page 9: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

Events vs. State

clickedOnScreen screenSlideDone clickedOnScreen screenSlideDone

IdleClosed IdleOpenSlidingOpen SlidingClosed IdleClosed

An event is what causes us to change from state to state.

CSE 251 Dr. Charles B. OwenProgramming in C9

Page 10: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

State Diagrams State diagrams describe what we will implement in code as a state machine. 

CSE 251 Dr. Charles B. OwenProgramming in C10

Page 11: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

State Diagrams

Initial State

State

Transition

Event

Transition

CSE 251 Dr. Charles B. OwenProgramming in C11

Page 12: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

Starting out State Machine

int main(){i D Cl dint state = DoorClosed;…

CSE 251 Dr. Charles B. OwenProgramming in C12 C

Page 13: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

A Control Loopint main(){int state = DoorClosed;int state = DoorClosed;

printf("Garage Startup\n");GarageStartup();

A continuous loop in a controls application that controls the 

t Ri ht l iwhile(IsGarageRunning()){

system. Right now our loop is doing nothing. We’ll want to add code to make our garage door 

}

printf("Garage Shutdown\n");G Sh d ()

work.

GarageShutdown();return 0;

}

CSE 251 Dr. Charles B. OwenProgramming in C13

Page 14: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

Important Idea

We do something different depending on the state we are in. It makes sense to create a function for each state.

void StateDoorClosed(int *state){ Note the pointer. We 

th t t b} pass the state by reference. It is an in/out parameter

What should happen when we are in the DoorClosed state?

CSE 251 Dr. Charles B. OwenProgramming in C14

Page 15: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

DoorClosed state…

If the button is pressed:Start the motorGo to the DoorOpening statep g

otherwise:Do nothing…

CSE 251 Dr. Charles B. OwenProgramming in C15

Page 16: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

DoorClosed state…id St t D Cl d(i t * t t )

If the button is pressed:Start the motorGo to the DoorOpening state

void StateDoorClosed(int *state){if(WasButtonPressed()){p g

otherwise:Do nothing…

SetMotorPower(1);*state = DoorOpening;

}}}

CSE 251 Dr. Charles B. OwenProgramming in C16

Page 17: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

The Control Loop – Handling Stateswhile(IsGarageRunning()){switch(state){

We will put a switch statement in our control 

{case DoorClosed:StateDoorClosed(&state);break;

loop to handle the states.

}

}

CSE 251 Dr. Charles B. OwenProgramming in C17

Page 18: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

We now have this…

Trigger / Activity

Trigger is what causes the transition Activitythe transition. Activity is something that happens when the transition occurs.

This is a simple 2‐state statement.

CSE 251 Dr. Charles B. OwenProgramming in C18

Page 19: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

What happenswhile(IsGarageRunning()){switch(state){ void StateDoorClosed(int *state){case DoorClosed:StateDoorClosed(&state);break;

void StateDoorClosed(int state){if(WasButtonPressed()){

}

}

SetMotorPower(1);*state = DoorOpening;

}}

Control LoopState Function}

The control loop runs continuously (1000 times per second in this program). Each time it calls a function for the current state. That state function decides if we need to change the state and does anything else we need to do while in that state.

CSE 251 Dr. Charles B. OwenProgramming in C19

Page 20: States and State Machines - Michigan State Universitycse251/lecture10.pdf ·  · 2011-03-24State machines are commonly used in ... CSE 251 Dr. Charles B. Owen 4 A Programming in

A Garage Door Opener

CSE 251 Dr. Charles B. OwenProgramming in C20 1