vervolg c hogeschool van utrecht / institute for computer, communication and media technology 1...

Post on 08-Jun-2015

219 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

1vervolg C

Onderwerpen voor vandaag

• Gelinkte lijsten• Finite State Machine (Eindige Toestands Machine)

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

2vervolg C

Lijst operaties

1. Voeg toe achteraan (enqueue)

2. Haal weg vooraan (dequeue, pop)

3. Voeg toe vooraan (push)

4. Haal weg achteraan (?)

5. Voeg toe in ‘t midden

6. Haal weg in ‘t midden

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

3vervolg C

Singly linked, double linked

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

4vervolg C

Ending, circular

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

5vervolg C

Insert, delete (singly linked)

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

6vervolg C

Insert, delete (double linked)

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

7vervolg C

Sentinels

Sentinel ( ~ wachter ): dummy queue element dat er altijd is. 1 bij circulaire lijsten, 2 bij ‘eindigende’ lijsten.

Voordeel: geen apart geval meer in de code voor begin of einde van de lijst.

Nadeel: extra geheugen nodig voor de sentinel. Sentinel == zelfde type als lijst elementen, dus als een lijstelement ‘groot’ is kan dit kostbaar zijn.

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

8vervolg C

lijstenLink Vorm Sentinels

Enkel Eindig Nee Eenvoudig concept

Enkel Eindig Ja Code voor lege lijst en laatste element eenvoudiger

Enkel Circulair Nee Geen aparte ‘first’ en ‘last’ pointers nodig

Enkel Circulair ja

Dubbel Eindig Nee Delete kan met alleen pointer naar het element

Dubbel Eindig Ja

Dubbel Circulair Nee

Dubbel Circulair ja Ingewikkeld concept, maar de eenvoudigste code, zonder if’s

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

9vervolg C

Finite State Machine (= Eindige Toestands Machine)

• states (toestanden)

• events (gebeurtenissen, boodschappen, triggers)

• actions (wat je programma doet)

Diverse notaties in gebruik, schrik niet als je een iets andere notatie tegenkomt.

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

10vervolg C

FSM voorbeeld : knipper LED op 1/2 Hz

States : – LED is aan– LED is uit

Events : – 1 seconde timer tick

Actions :– LED aanzetten– LED uitzetten

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

11vervolg C

knipper LED op 1/2 Hz : FSM diagram

LED is uit LED is aan

initLED uitzetten 1s timer tick

LED aanzetten

1s timer tickLED uitzetten

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

12vervolg C

knipper LED op 1/2 Hz : State-Transistion diagram

Event

State

1s timer tick

LED is uit LED aanzetten

LED is aan

LED is aan LED uitzetten

LED is uit

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

13vervolg C

FSM voorbeeld : knipper LED op 1/2 Hz

// states

#define knipper_state_init 0

#define knipper_state_uit 1

#define knipper_state_aan 2

// events

#define knipper_event_tick 100

// current state

int knipper_state = knipper_state_init;

// acties

void Zet_LED( int led, int x ){

...

}

void fsm_knipper( int & state, int led, int event ){ if( *state == knipper_state_init ){ Zet_LED( led, 0 ); *state = knipper_state_uit; }

if( *state == knipper_state_uit ){ if( event == knipper_event_tick ){ Zet_LED( led, 1 ); *state = knipper_state_aan; return; } }

if( *state == knipper_state_aan ){ if( event == knipper_event_tick ){ Zet_LED( led, 0 ); *state = knipper_state_uit; return; } }}

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

14vervolg C

FSM voorbeeld : Toegangspoort 1

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

15vervolg C

FSM voorbeeld : Toegangspoort 2

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

16vervolg C

FSM voorbeeld : Toegangspoort 3

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

17vervolg C

FSM voorbeeld : Toegangspoort 3

Event

State

Coin Pass Reset Ready

Locked Unlock()

Unlocked

Alarm()

Violation Locked Locked

Unlocked Thankyou()

Unlocked

Lock()

Locked Unlocked Unlocked

Violation

Violation Violation

ResetAlarm()

Violation

Lock()

ResetAlarm()

Locked

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

18vervolg C

// states#define poort_state_init 0#define poort_state_locked 1#define poort_state_unlocked 2#define poort_state_violation 3

// events#define poort_event_coin 100#define poort_event_pass 100#define poort_event_reset 100

// current stateint poort_state = poort_state_init;

// actionsvoid poort_thankyou( void );void poort_lock( void );void poort_unlock( void ); void poort_start_alarm( void );void poort_stop_alarm( void );

void fms_poort( int event ){ if( state == poort_state_init ){ poort_lock(); state = poort_state_locked;}

if( state == poort_state_locked ){ if( event == poort_event_coin ){ poort_unlock(); state = poort_state_unlocked(); } if( event == poort_event_pass ){ poort_alarm(); state = poort_state_violation(); } return; }

...}

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

19vervolg C

knipper LED op X Hz : State-Transistion diagram

Event

State1ms timer tick

LED is uit n++;

if( n == 500/x ){

LED aanzetten

n = 0;

state = LED is aan

}

LED is aan n++;

if( n == 500/x ){

LED uitzetten

n = 0;

state = LED is uit

}

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

20vervolg C

FSM voorbeeld : knipper LED op X Hzvoid fsm_knipper( int & state, int led, int X, int *n, int event ){ if( *state == knipper_state_init ){ Zet_LED( led, 0 ); *state = knipper_state_uit; *n == 0; return; }

if( *state == knipper_state_uit ){ if( event == knipper_event_tick ){ *n++; if( *n == 500/x ){ Zet_LED( led, 1 ); *n = 0; *state = knipper_state_aan; } return; } }

if( *state == knipper_state_aan ){ if( event == knipper_event_tick ){ *n++; if( *n == 500/x ){ Zet_LED( led, 0 ); *n = 0; *state = knipper_state_uit; } return; } }}

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

21vervolg C

FSM voorbeeld : knipper diverse LEDs

while( 1 ){ wait_1ms(); fsm_knipper( &state1, l, 10, &n1, knipper_event_tick ); fsm_knipper( &state2, 2, 12, &n2, knipper_event_tick ); fsm_knipper( &state3, 3, 300, &n3, knipper_event_tick ); fsm_knipper( &state4, 4, 5, &n4, knipper_event_tick ); ...

}

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

22vervolg C

Rotary encoder (quadrature encoder)

Sensor voor het bijhouden van de draaiing van een as.

Toegepassingen:

– user interface (afstemknop)

– computermuis

– motorsturing

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

23vervolg C

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

24vervolg C

Opdracht ”rotary encoder”

Een rotary encoder FSM moet in de stand van een rotary encoder bijhouden. Bij een click naar rechts +1, bij een klick naar links -1. Er moet rekening gehouden worden met denderen en ‘halve bewegingen’ (= effect is hetzelfde als denderen).

Hogeschool van Utrecht / Institute for Computer, Communication and Media Technology

25vervolg C

Opdracht ”rotary encoder”

Stel de lijst van states (4), events (4) en acties (2), op voor een rotary encoder FSM

- Maak het State Transition Diagram (er zijn ‘fysiek-logisch onmogelijke’ events, die hoef je niet te laten zien)

- Vertaal je STD naar C code

top related