5.1 5.2 What is state? • You see a DPS officer approaching you. Are you happy? – It's late at night and ______________________. – It's late at night and you've been ___________________________. Unit 5 • Your interpretation is based on more than just what your senses are telling you RIGHT NOW, but by what ___________ _____________________ State Machines – The sum of all your previous experiences is what is known as _______ – Your ________ determines your ______________ of your senses and thoughts • In a circuit, 'state' refers to all the _______ being remembered (___________ or memory) • In software, 'state' refers to all the _____________ values that are being used 5.3 5.4 State Machine Block Diagram State Diagrams • A system that utilizes state is often referred to as a state machine • Abstractly a state machine can be visualized and represented – A.k.a. ____________________________________________ as a flow chart (or state diagram) • Most state machines can be embodied in the following form – Circles or boxes represent _________ – Logic examines what's happening NOW (inputs) & in the PAST (state) to… – Arrows show what input causes a _______________ • Produce outputs (actions you do now) – Outputs can be generated whenever you reach a particular state or • Update the state (which will be used in the future to change the decision) based on the combination of state + input • Inputs will go away or change, so state needs to summarize/capture State Machine to check for two consecutive 1's on a digital input anything that might need to be ___________ and used in the future Input=__ Input=__ Input=__ Input=__ Inputs Logic Outputs (A-to-D, Timer, S0 S1 S2 Buttons) Out=False out=True Out=False Input=__ State On startup Input=__ (memory)
5.5 5.6 Washing Machine State Diagram Washing Machine State Diagram /RESET /RESET Stay in the initial state Move to the Fill state until there is enough when there is enough COINS + DOOR COINS + DOOR Idle Idle money (coins) and money (coins) and N=2 N=2 the door is closed the door is closed COINS • DOOR COINS • DOOR Fill Fill FULL FULL WV = 1 WV = 1 FULL FULL Agitate Agitate We move through the 5 MIN 5 MIN Motor = 1 Motor = 1 states based on the conditions. Outputs 5 MIN / Reset_Timer=1 5 MIN / Reset_Timer=1 get asserted when the Drain Drain machine is in that EMPTY EMPTY DV = 1 DV = 1 state and the transition is true. EMPTY EMPTY N > 0 N > 0 N = N - 1 N = N - 1 N = 0 N = 0 5.7 5.8 Washing Machine State Diagram Washing Machine State Diagram /RESET /RESET COINS + DOOR COINS + DOOR Idle Idle Stay in the Fill state N=2 N=2 until it is full…also COINS • DOOR COINS • DOOR Move to the Agitate set the Water Valve state after it is full Open output to be Fill Fill FULL FULL true WV = 1 WV = 1 FULL FULL Agitate Agitate 5 MIN 5 MIN Motor = 1 Motor = 1 5 MIN / Reset_Timer=1 5 MIN / Reset_Timer=1 Drain Drain EMPTY EMPTY DV = 1 DV = 1 EMPTY EMPTY N > 0 N > 0 N = N - 1 N = N - 1 N = 0 N = 0
5.9 5.10 Software vs. Hardware Software Implementation • Store 'state' in some variable and assign ________ to • Software • Hardware represent state (0=Idle, 1=Fill, etc.) – State = just a variable(s) – State = Register int main() • Use a timer or just ______ (D-Flip-Flops) – Logic = if statements to { certain inputs and then make bool coins, door; update the next state – Logic = AND/OR Gates to produce int currst = 0, nextst = 0, n = 2; appropriate transitions the next state & outputs • if(state == 0 && input == 1) while(1) { { state = 1; } /RESET – Transitions triggered by clock _delay_ms(10); – Transitions triggered by input signal coins = PIND & (1 << PD0); Idle COINS + DOOR door = PIND & (1 << PD1); N=2 or timers – More on this later in the if(currst == 0){ COINS • DOOR if( coins && door ){ – We'll start by implementing semester Fill FULL WV = 1 nextst = 1; state machines in SW FULL } Agitate 5 MIN Use nested 'if' } Motor = 1 statements: Inputs Outputs 5 MIN / Reset _Timer=1 else if(currst == 1){ Logic outer 'if' selects Drain ... EMPTY (ADC, Timer, DV = 1 state then inner } EMPTY 'if' statements Buttons) ... N > 0 N = N - 1 examine inputs State currst = nextst; // update state N = 0 } (memory) State Diagram for a return 0; Washing Machine } 5.11 5.12 More Implementation Tips State Machines as a Problem Solving Technique // input = PD0, output = PD7 int main() • Continuously loop { // be sure to init. state • Modeling a problem as a state machine is a powerful unsigned char state=0, nstate=0; • Each iteration: unsigned char input, output; problem-solving tool while(1) – Poll inputs { _delay_ms(10); – Use _________________to decide nstate = state; // stay by default • When you need to write a program, design HW, or input = PIND & (1 << PD0); current state Select current state solve a more abstract problem at least consider if it if(state == 0){ – In each state, update state PORTD &= ~(1 << PD7); can be modeled with a state machine if( input ){ nstate = 1; } appropriately based on desired } transitions from that state Select input val. else if(state == 1){ – Ask questions like: PORTD &= ~(1 << PD7); – Produce appropriate output from if( input ){ nstate = 2; } • What do I need _______________ to interpret my inputs or that state else { nstate = 0; } produce my outputs? [e.g. Checking for two consecutive 1's] } else { // state == 2 • Is there a distinct sequence of __________________ that are used PORTD |= (1 << PD7); (each step/mode is a ________) [e.g. Thermostat, washing if( !input ) { nstate = 0; } } machine, etc.] state = nstate; // update state } return 0; }
5.13 5.14 Example: Ad-hoc Implementation Example: FSM implementation int main() { // Ad-hoc implementation • Formulated as a state machine: int state = 0, cnt = 0; int main() • Consider a program that checks while(1) { { while(1) – Separate code to update state and two buttons { // Update the state int i; then perform actions based on state if (checkInput(1) == 0) { if(checkInput(1) == 0){ state = 1; cnt = 0; – When button 1 is pressed, blink an for(i=0; i < 10; i++) } • Tip: Avoid loops other than the { else if (checkInput(2) == 0) LED 10 times at 2 HZ blink(250); // on for 250, off for 250 state = 2; cnt = 0; if(checkInput(2) == 0) primary while and use state and } – When button 2 is pressed, blink an { else if( (state == 1 && cnt == 10) || break; if statements, instead (state == 2 && cnt == 15 ) ) { LED 15 times at 5 HZ } state = 0; cnt = 0; } } } • If during the blinking of one LED if(checkInput(2) == 0){ State1 BTN1 for(i=0; i < 15; i++) // Use state to determine actions the other button is pressed, you { (LED1 @ if(state == 1) { blink(100); // on for 100, off for 100 2Hz) blink(250); // on for 250, off for 250 CNT==10 && if(checkInput(1) == 0) should immediately stop the cnt++; State0 BTN1 { ! BTN1 && ! BTN2 } break; current blink cycle and start the else if(state == 2) { } No Blinking CNT==15 && blink(100); // on for 100, off for 100 } ! BTN1 && ! BTN2 } cnt++; blink cycle of the other button. State2 // delays are in the blink() functions } // so no delay needed here } ! BTN1 && (LED2 @ BTN2 • After a blink cycle is complete and return 0; BTN2 5Hz) // Problem: what if button that caused } // break from for loop is released right now no button is pressed, simply wait } return 0; and do nothing. } Assume if no transition it true, we intend to stay in the same state. 5.15 5.16 Operations at Different Rates (1) Operations at Different Rates (2) int main() { int main() • Use separate state machines int cnt1 = 0, cnt2 = 0; • Consider a program to blink one { while(1) running in parallel // set initial state of LEDs as "on" { LED at a rate of 2 Hz and another int s1 = 1, s2 = 1; LED1_ON(); LED1_ON(); _delay_ms(250); • Given various rates of operations, at 5 Hz at the same time LED2_ON(); LED1_OFF(); _delay_ms(250); find the GCD of the various rates while(1) • Problem : Does the code to the { LED2_ON(); and loop with that delay, using cnt1++; _delay_ms(100); right work correctly? if(cnt1 == 5) LED2_OFF(); { counts to track time _delay_ms(100); if(s1) { LED1_OFF(); s1 = 0; } – No! When one LED ____________ else { LED1_ON(); s1 = 1; } } _______________ cnt1 = 0; } cnt1 == 5 / return 0; Reset cnt1 } LED1 ON LED1 OFF cnt2++; if(cnt2 == 2) cnt1++ cnt1++ { cnt1 == 5 / if(s2) { LED2_OFF(); s2 = 0; } Reset cnt1 else { LED2_ON(); s2 = 1; } cnt2 = 0; } cnt2 == 2 / Reset cnt2 LED2 ON LED2 OFF // Delay the minimum granularity _delay_ms(50); cnt2++ cnt2++ } cnt2 == 2 / return 0; Reset cnt2 }
Recommend
More recommend