• No results found

Embedded Real-Time Systems (TI-IRTS) State Machine Implementation

N/A
N/A
Protected

Academic year: 2021

Share "Embedded Real-Time Systems (TI-IRTS) State Machine Implementation"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)

State Machine

Implementation

Embedded Real-Time

Systems (TI-IRTS)

(2)

Agenda

STM notation and example

Five state machine implementation

techniques:

1. Switch based

2. Table driven implementation

3. GoF State Pattern (separate slides)

(3)

UML State Chart Notation

State_0 State_1 entry/ action_2 event2/ action_3 do/ activity_1 exit/ action_4 event1(parameter1)[guard1]/action_1

Class with State Machine +event1(parameter1) +event2() -entry() -exit() -action_1() -action_2() -action_3() -action_4() -activity() (task/thread)

(4)

IDLE

RUNNING

stop / stop motor

start / start motor

Mealy

Machine

Two State Machine Types

IDLE

RUNNING

do/

motor running

start stop

Moore

Machine

(5)

Example: Infusion Pump as a

Mealy Machine

Alarm Infusion Inactive Infusion Active clear / clearTotal alarmAcknowledged / stopAlarm start [door_closed ] / startInfusion /stopInfusion; startAlarm stop / stopInfusion /start_alarm alarmDetected doorOpened
(6)

Alarm do/ giveAlarm Infusion Inactive Infusion Active do/ infuseMedicine clear / clearTotal alarmAcknowledged start [doorClosed] stop

Example: Infusion Pump as a

Moore Machine

alarmDetected

(7)

main() // Mealy implementation { InfusionPump infPump; InfusionPumpControl iPumpControl(&infPump); EVENTS event; while(FOREVER) { event = readEvent(); iPumpControl.handleEvent(event); }

1. Switch Based Implementation (1)

-currentState: STATES +handleEvent(event: EVENTS)

InfusionPumpControl

Infusion

Pump

1

1

pIPump
(8)

Switch Based Implementation (2)

class InfusionPumpControl

{

public:

InfusionPumpControl(InfusionPump* pIP)

{ pIPump= pIP;

currentState= ALARM;

startAlarm();

}

void handleEvent(Events event);

private:

STATES currentState;

InfusionPump* pIPump;

startAlarm();

stopAlarm();

clearTotal();

}

(9)

void InfusionPumpControl::handleEvent(Events event)

{

switch (currentState)

{ case ALARM: if (event == ALARM_ACKNOWLEDGED) { stopAlarm(); currentState = INFUSION_INACTIVE; } break; case INFUSION_INACTIVE: if (event == CLEAR) clearTotal();

else if ((event == START) && doorClosed())

{ // Guarded transition

pIPump->startInfusion();

currentState = INFUSION_ACTIVE;

}

break;

(10)

Discussion of Switch-based

Implementation

• Advantages:

– Very easy and straightforward to implement

– Easy to implement guarded transitions and entry/exit

actions

• Disadvantages:

– Problem with event parameters

– The logic and the behavior are tightly interconnected

– For larger state machines:

• the nested switch/case statements becomes very difficult to

read and modify

• Recommendation:

(11)

2. State-Event Table Implementation

Event1 Event2 Event3 …. State1 State2 State3 action1 State2 action2 State1 -action4 State2 action3 -action5 State3 action6 State1 New state

(12)

Infusion Pump State-Event Table

start AND stop alarm alarmDetected clear doorClosed Acknowledged OR doorOpened

stopAlarm

ALARM X X X X INFUSION

INACTIVE

start clear

INFUSION infusion Total

INACTIVE X X X

INFUSION X ACTIVE

stop startAlarm, INFUSION X Infusion stopInfusion

ACTIVE X X INFUSION ALARM

INACTIVE

X represents don't cares

(13)

State-Event Table Implementation (1)

StateTableMachine

-actualState

+handleEvent(event: int): int +getState(): int InfusionPumpControl

+handleEvent(event)

-preProcessEvent(event) -executeAction(actionNo) actionNo= handleEvent(event) class InfusionPumpControl {

static int[ ] stateTable = {

/* EVENTS E1 E2 E3 E4 E5 */ /* ALARM 0 */ NO,NO, NO,NO, A1,1, NO,NO, NO,NO,

/* INF_INA 1 */ A2, 2, NO,NO, NO,NO, NO,NO, A3,NO,

/* INF_ACT 2 */ NO,NO, A4, 1, NO,NO, A5, 0, NO,NO };

-stateTable

(14)

main()

{

int eventNo, actionNo;

InfusionPumpControl iPumpControl;

while ((eventNo=

getEvent())

!= END_PROGRAM)

iPumpControl.handleEvent(eventNo);

}

(15)

State-Event Table Implementation (3)

InfusionPumpControl::

InfusionPumpControl

()

{

pSTM = new

stateTableMachine

(

stateTable,NoOfEvents,

ALARM);

startAlarm(); // call initial action

}

InfusionPumpControl::

handleEvent

(int eventNo)

{

int pEvent=

preProcessEvent(eventNo);

int actionNo=

pSTM->handleEvent(pEvent);

executeAction(

actionNo

);

(16)

StateTableMachine::StateTableMachine(int[] _states, int maxEvent, int initState)

{

nofEvents = maxEvent; states = _states;

actualState = initState;

}

int StateTableMachine:: handleEvent(int eventNo)

{

int index = ((actualState * nofEvents) + eventNo) * 2;

// look for a state transition

if (states[index+1] != State.NO)

actualState = states[index+1];

// return action

return (states[index]);

}

int StateTableMachine:: getState() {return actualState;}

(17)

int InfusionPumpControl::preProcessEvent(int inEvent)

{

switch (inEvent) {

case START:

if (doorClosed()) // GUARDED TRANSITION return E1; break;

case STOP: return E2; break;

case ALARM_ACKNOWLEDGED: return E3; break; case ALARM_DETECTED:

case DOOR_OPENED: return E4; break; case CLEAR: return E5; break;

default: return NO_EVENT; }

return NO_EVENT; }

(18)

void InfusionPumpController::executeAction(int actionNo)

{

switch (actionNo) {

case NO: cout << "NO action defined in ”; break;

case A1: cout << "A1: stopAlarm; new "; break;

case A2: cout << "A2: startInfusion; new "; break;

case A3: cout << "A3: clearTotal; new "; break;

case A4: cout << ”A4: stopInfusion; new "; break;

case A5: cout << ”A5: startAlarm, stopInfusion; new "; break;

default: cout << "undefined action in ”; }

cout << "state = " << pSTM->getState());

}

(19)

start stop alarm alarm clear OK NOT_ Acknowled. Det.+xx OK stopAlarm ALARM X X X X X X INFUSION INACTIVE check_ clear INFUSION for_start Total INACTIVE X X X X X TRANSIT. X STATE start TRANSI- Infusion X TION- X X X X X

STATE INFUSION INFUSION

ACTIVE INACTIVE

stop stop INFUSION X Infusion Infusion

ACTIVE X startAl. X X X INFUSION

INACTIVE ALARM

State-Event Table Implementation (7)

(20)

State-Event Table Implementation (8)

Alarm Infusion Inactive Infusion Active clear / clearTotal alarmAcknowledged / stopAlarm start/ checkForStart stop / stopInfusion / start_alarm OK/ startInfusion NOT_OK Special: Transition state /stopInfusion; startAlarm alarmDetected doorOpened
(21)

Example with a Specialized State M. (1)

StateTableMachine

PreprocessingStateTableMachine

+handleEvent(int eventNo) +handleEvent(int eventNo) PreprocessingStateTableMachine(…) preprocessing eventTabel (cmdEventTable) as an extra constructor parameter

Infusion

Pump

Control

(22)

static int[] cmdEventTable = { START, E0, STOP, E1, ALARM_ACKNOWLEDGED, E2, ALARM_DETECTED, E3, DOOR_OPENED, E3, CLEAR, E4, OK, E5, NOT_OK, E6, END_OF_TABLE, NO_EVENT, }; InfusionPumpControl::InfusionPumpControl() {

// call of constructor with cmdEventTable

pSTM = new PreProcessingStateTableMachine(

stateTable,NoOfEvents,ALARM,cmdEventTable);

}

(23)

int PreProcessingStateTableMachine::

handleEvent

(int actualEventNo)

{

int i=0;

while ((cmdEvent[i] != END_OF_TABLE) &&

(cmdEvent[i] != actualEventNo))

i += 2;

if (cmdEvent[i] != END_OF_TABLE)

return

StateTableMachine::handleEvent(

cmdEvent[i+1]);

else

return NO; // NO= end of table

}

(24)

int InfusionPumpControl::executeAction(int actionNo) {

switch (actionNo)

{

...

case A6: // check_for_start

return (doorClosed() ? OK : NOT_OK); break; default:

}

return NO_EVENT; }

void InfusionPumpControl:: handleExternalEvent(int inEvent) {

while (inEvent != NO_EVENT)

inEvent = executeAction(pSTM->handleEvent(inEvent)); }

(25)

Discussion of State-Event Table

Implementation

• Disadvantages

– The events should be preprocessed, to consecutive

constants, before table lookup

– Guarded transitions requires extra transition state plus

internal events

– Difficult to implement hierarchic state machines

• Advantages

– if the state machine is specified as a state-event table:

• Easy to implement and verify

– The StateTableMachine can be a general utility class

• Recommendations:

– Use it for larger and flat state machines, with no or only few

guarded transition

(26)

3. State Pattern (GoF)

Implementation

See separate

(27)

Case tool STM Generations

• A few tools can automatically generate

code for state machines:

– Visual State (IAR)

– Rhapsody (IBM (Telelogic))

• Code generation for two different strategies

supported (switch based or state pattern

based)

References

Related documents

When in comparison to the complex fluorescence spectra, the DPPZ-COOH ligand weak emission was able to transfer minimal energy to the Eu( III) ion (Figure 23) which is indicative of

In this study, the methodology for define proposed IT Financial Management process shown in figure 1 as follows: Internal Factor Analysis External Factor Analysis

-8’ x 20’ Dye Sublimated Printed Carpet (optional) -Shipping Crate Included in Price and Weight -Booth is costed with single sided graphics, call for double sided

In addition, numerous five-star hotel and condominium mixed-use properties have opened in the past few years including the Residences at the Ritz-Carlton Toronto, Four Seasons

Currently little is known about men’s experiences leading them to desire high levels of muscle and engage in behaviours to increase their masculine capital.. Our purpose was to

• Mandated minimum benefit of 30 combined inpatient and outpatient days and 30 office visits per year for other mental health disorders. • Minimum benefits exclude

Among these are Sz ű cs’ (1998) analysis of Hungarian land prices and lease payments, and Csendes/Sz ű c’s (2002) research on the factors in fl uencing land market supply and

Our experience highlights that game-based interaction, competition, focused and goal-driven social interaction, “situated” virtual activities (i.e., activities that are anchored