States and State Machines 1 CSE 251 Dr

  • Slides: 20
Download presentation
States and State Machines 1 CSE 251 Dr. Charles B. Owen Programming in C

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

What is this for? State machines are commonly used in… Embedded Systems Factory/Process Controls

What is this for? State machines are commonly used in… Embedded Systems Factory/Process Controls 2 CSE 251 Dr. Charles B. Owen Programming in C

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

State – An abstraction of the current status of a system. States are assigned names. Waiting for a Keypress Waiting for Elvis Raising Firearm to Fire Cellphone is Dialing Door Opening Verbs with “ing” 3 Paper Jammed Battery is Below Limit Power is On Door Open Prius Accelerator Stuck Statement of condition CSE 251 Dr. Charles B. Owen Programming in C

Are there any more states? States in a Garage Door. Closed Door. Open 4

Are there any more states? States in a Garage Door. Closed Door. Open 4 TT A CSE 251 Dr. Charles B. Owen Programming in C

More States Door. Opening Door. Closing 5 CSE 251 Dr. Charles B. Owen Programming

More States Door. Opening Door. Closing 5 CSE 251 Dr. Charles B. Owen Programming in C

How we will express this in a program /* Our possible garage door states

How we will express this in a program /* Our possible garage door states */ #define Door. Closed 1 #define Door. Opening 2 #define Door. Open 3 #define Door. Closing 4 Above main in our program int main() { int state = Door. Closed; … In the main function Why do we care? We do different things depending on the current state. What does the button do in each state? 6 TT B CSE 251 Dr. Charles B. Owen Programming in C

Naming Conventions - States We will usually name states with camel-case and a capital

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. Waiting. For. Keypress Waiting. For. Elvis Raising. Firearm Cellphone. Dialing Door. Opening Verbs with “ing” 7 Paper. Jammed Battery. Below. Limit Power. On Door. Open Prius. Accel. Stuck Statement of condition CSE 251 Dr. Charles B. Owen Programming in C

Events An event is an occurrence in time. It is considered atomic and instantaneous.

Events An event is an occurrence in time. It is considered atomic and instantaneous. Left mouse button pressed Key pressed Elvis has left the building Flight 529 has landed Power turned on Paper is jammed Message has timed out 10 seconds has elapsed Battery is below limit Project 2 is due Past tense verbs Onset of condition 8 CSE 251 Dr. Charles B. Owen Programming in C

Events vs. State clicked. On. Screen Idle. Closed screen. Slide. Done Sliding. Open clicked.

Events vs. State clicked. On. Screen Idle. Closed screen. Slide. Done Sliding. Open clicked. On. Screen screen. Slide. Done Idle. Open Sliding. Closed An event is what causes us to change from state to state. 9 CSE 251 Dr. Charles B. Owen Programming in C Idle. Closed

State Diagrams 10 State diagrams describe what we will implement in code as a

State Diagrams 10 State diagrams describe what we will implement in code as a state machine. CSE 251 Dr. Charles B. Owen Programming in C

State Diagrams Initial State Transition Event 11 CSE 251 Dr. Charles B. Owen Programming

State Diagrams Initial State Transition Event 11 CSE 251 Dr. Charles B. Owen Programming in C

Starting out State Machine int main() { int state = Door. Closed; … 12

Starting out State Machine int main() { int state = Door. Closed; … 12 TT C CSE 251 Dr. Charles B. Owen Programming in C

int main() { int state = Door. Closed; printf("Garage Startupn"); Garage. Startup(); while(Is. Garage.

int main() { int state = Door. Closed; printf("Garage Startupn"); Garage. Startup(); while(Is. Garage. Running()) { } } 13 A Control Loop A continuous loop in a controls application that controls the system. Right now our loop is doing nothing. We’ll want to add code to make our garage door work. printf("Garage Shutdownn"); Garage. Shutdown(); return 0; CSE 251 Dr. Charles B. Owen Programming in C

Important Idea We do something different depending on the state we are in. It

Important Idea We do something different depending on the state we are in. It makes sense to create a function for each state. void State. Door. Closed(int *state) { } Note the pointer. We pass the state by reference. It is an in/out parameter What should happen when we are in the Door. Closed state? 14 CSE 251 Dr. Charles B. Owen Programming in C

Door. Closed state… If the button is pressed: Start the motor Go to the

Door. Closed state… If the button is pressed: Start the motor Go to the Door. Opening state otherwise: Do nothing… 15 CSE 251 Dr. Charles B. Owen Programming in C

Door. Closed state… void State. Door. Closed(int *state) { If the button is pressed:

Door. Closed state… void State. Door. Closed(int *state) { If the button is pressed: if(Was. Button. Pressed()) Start the motor Go to the Door. Opening state { Set. Motor. Power(1); otherwise: *state = Door. Opening; Do nothing… } } 16 CSE 251 Dr. Charles B. Owen Programming in C

The Control Loop – Handling States while(Is. Garage. Running()) { switch(state) { case Door.

The Control Loop – Handling States while(Is. Garage. Running()) { switch(state) { case Door. Closed: State. Door. Closed(&state); break; } We will put a switch statement in our control loop to handle the states. } 17 CSE 251 Dr. Charles B. Owen Programming in C

We now have this… Trigger / Activity This is a simple 2 -statement. 18

We now have this… Trigger / Activity This is a simple 2 -statement. 18 Trigger is what causes the transition. Activity is something that happens when the transition occurs. CSE 251 Dr. Charles B. Owen Programming in C

What happens while(Is. Garage. Running()) { switch(state) { case Door. Closed: State. Door. Closed(&state);

What happens while(Is. Garage. Running()) { switch(state) { case Door. Closed: State. Door. Closed(&state); break; } } Control Loop void State. Door. Closed(int *state) { if(Was. Button. Pressed()) { Set. Motor. Power(1); *state = Door. Opening; } State 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. 19 CSE 251 Dr. Charles B. Owen Programming in C

A Garage Door Opener 20 1 CSE 251 Dr. Charles B. Owen Programming in

A Garage Door Opener 20 1 CSE 251 Dr. Charles B. Owen Programming in C