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

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
Driving Test 1 Marking Scheme Focus on five areas to pass driving test 1.
PIIT Computer Science Summer Camp - Alice July 11, 2012 Brenda Parker Computer Science Department MTSU.
Events Chapter 7. Interactivity The real world is interactive User determines order of actions instead of programmer.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Scratch Programming Session 9 of 10 Review elements to use in stories, games, etc.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Analysis Modeling Dynamic Modeling. Requirements analysis Results in static and dynamic models – Scenario models: use cases (static), swimlane diagrams.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Arduino Part 2 Topics: Serial Communication Programming Constructs: functions, loops and conditionals Digital Input.
Chapter 5 – System Modeling
chipKit Sense Switch & Control LED
Calculator Challenge By: Chris Brown Under the direction of Professor Susan Rodger Duke University, January 2013.
Fall 2010 CS4310 Requirements Engineering UML: Dynamic Modeling Dr. Guoqiang Hu Department of Computer Science UTEP 1.
1 Software Engineering Dr. K. T. Tsang Lecture 8 State modeling
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Object-Oriented Modeling Using UML CS 3331 Section 2.3 of Jia 2003.
FIRST GADGETEER PROJECT. Where are you? Making a VS project Parts of a C# program Basics of C# syntax Debugging in VS Questions? 2.
EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่ เกิดขึ้น ตัวอย่างของเหตุการณ์ อาทิ การเคลื่อน หรือ คลิกเมาส์
Overview of Verb Tenses UUEG, Chapter 1. The Simple Tenses Simple Present Simple Past Simple Future These tenses make up 90% of the verb tenses we use!
1 Modeling interactions and behavior Lecturer Dr. Mai Fadel.
Visual Basic.NET BASICS Lesson 3 Events and Code.
Digital Logic Design Lecture # 21 University of Tehran.
1 State Modeling  Events  States  Transitions and Conditions  State Diagrams  State Diagram Behavior  Practical Tips.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
3-1 State Design Pattern C Sc 335 Rick Mercer. State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object.
Applications Development
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
UML Discussion on State Machines Perfectly static system is intensely uninteresting Because nothing ever happens.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
State Modeling. Introduction A state model describes the sequences of operations that occur in response to external stimuli. As opposed to what the operations.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Forms and Controls Part 3 dbg --- Naming conventions, Button, Event handlers, Label.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Event Handling Tonga Institute of Higher Education.
Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code.
Elevator Example.
State Machines State diagrams SE-2030 Dr. Mark L. Hornick 1.
Forms and Controls Part 3 dbg --- Naming conventions, Button, Event handlers, Label.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures.
State Modeling. Introduction A state model describes the sequences of operations that occur in response to external stimuli. As opposed to what the operations.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
03 October, 2007Information System Design IT60105, Autumn 2007 Information System Design IT60105 Lecture 14 Statechart Diagrams.
Scratch Programming Cards
Event Loops and GUI Intro2CS – weeks
More Events.
Finite State Machines introduction to hunter-prey 10/8/10
Analysis Modeling Dynamic Modeling.
Introduction to Events
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Programming – Touch Sensors
C# Event Processing Model
Flooding © 2018.
HAPPY NEW YEAR! Lesson 7: If-statements unplugged
Chapter 5 state Modeling
CSE 451 Autumn 2003 Section 3 October 16.
By.Emilia.
Tonga Institute of Higher Education
A simple function.
State-Transition Diagrams
Presentation transcript:

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

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

CSE 251 Dr. Charles B. Owen Programming in C3 State 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 Paper Jammed Battery is Below Limit Power is On Door Open Prius Accelerator Stuck Verbs with “ing”Statement of condition

CSE 251 Dr. Charles B. Owen Programming in C4 States in a Garage Door DoorClosed DoorOpen Are there any more states? A

CSE 251 Dr. Charles B. Owen Programming in C5 More States DoorOpening DoorClosing

CSE 251 Dr. Charles B. Owen Programming in C6 How we will express this in a program /* Our possible garage door states */ #define DoorClosed 1 #define DoorOpening 2 #define DoorOpen 3 #define DoorClosing 4 int main() { int state = DoorClosed; … Above main in our program 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? B

CSE 251 Dr. Charles B. Owen Programming in C7 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. WaitingForKeypress WaitingForElvis RaisingFirearm CellphoneDialing DoorOpening PaperJammed BatteryBelowLimit PowerOn DoorOpen PriusAccelStuck Verbs with “ing”Statement of condition

CSE 251 Dr. Charles B. Owen Programming in C8 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 verbsOnset of condition

CSE 251 Dr. Charles B. Owen Programming in C9 Events vs. State IdleClosed clickedOnScreenscreenSlideDoneclickedOnScreenscreenSlideDone IdleOpenSlidingOpenSlidingClosedIdleClosed An event is what causes us to change from state to state.

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

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

CSE 251 Dr. Charles B. Owen Programming in C12 Starting out State Machine int main() { int state = DoorClosed; … C

CSE 251 Dr. Charles B. Owen Programming in C13 A Control Loop int main() { int state = DoorClosed; printf("Garage Startup\n"); GarageStartup(); while(IsGarageRunning()) { } printf("Garage Shutdown\n"); GarageShutdown(); return 0; } 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.

CSE 251 Dr. Charles B. Owen Programming in C14 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) { } What should happen when we are in the DoorClosed state? Note the pointer. We pass the state by reference. It is an in/out parameter

CSE 251 Dr. Charles B. Owen Programming in C15 DoorClosed state… If the button is pressed: Start the motor Go to the DoorOpening state otherwise: Do nothing…

CSE 251 Dr. Charles B. Owen Programming in C16 DoorClosed state… If the button is pressed: Start the motor Go to the DoorOpening state otherwise: Do nothing… void StateDoorClosed(int *state) { if(WasButtonPressed()) { SetMotorPower(1); *state = DoorOpening; }

CSE 251 Dr. Charles B. Owen Programming in C17 The Control Loop – Handling States while(IsGarageRunning()) { switch(state) { case DoorClosed: StateDoorClosed(&state); break; } We will put a switch statement in our control loop to handle the states.

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

CSE 251 Dr. Charles B. Owen Programming in C19 What happens while(IsGarageRunning()) { switch(state) { case DoorClosed: StateDoorClosed(&state); break; } void StateDoorClosed(int *state) { if(WasButtonPressed()) { SetMotorPower(1); *state = DoorOpening; } 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. Control Loop State Function

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