You are on page 1of 13

SYDDANSK UNIVERSITET

CAMPUS SØNDERBORG

EMBEDDED SOFTWARE ARCHITECTURE 3


SIS 3

COURSE ASSIGNMENT 2

REAL-TIME SYSTEM DESIGN USING THE


HARTEXμ KERNEL

Autumn 2007

Lectors: Christo Angelov


Nicolae Marian

Revised: Krzysztof Sierszecki

Marcelo Juan Peña Zúñiga


I NDEX

INTRODUCTION ................................................................................................................3
PROJECT REQUIREMENTS .................................................................................................3
PROJECT DESIGN AND DEVELOPMENT ..............................................................................4
CONCLUSIONS ................................................................................................................12
REFERENCES ..................................................................................................................13

L IST OF F IGURES
Figure 1. Basic washing machine structure. ............................................................................................................3
Figure 2. Washing machine control sequence. ........................................................................................................4
Figure 3. Washing machine handler flowchart. .......................................................................................................5
Figure 4. WMH Mealy machine top level representation........................................................................................6
Figure 5. Detail view of a composite state...............................................................................................................7
Figure 6. Tasks modeling layer................................................................................................................................8

C ODE S EGMENTS
Code Segment 1. Washing machine handler [1/3]...................................................................................................9
Code Segment 2. Wahsing machine handler [2/3].................................................................................................10
Code Segment 3. Washing machine handler [3/3].................................................................................................11
Code Segment 4. Cylinder motor handler..............................................................................................................12

2
INTRODUCTION

After becoming familiar with the HARTEXμ kernel and its primitives, it is time to move on. The goal
in this second project is to design and implement a multitasking system running under HARTEXμ.
From the document SIS3 Project Assignments[1] a project has to be chosen and execute the project
following the guidelines given in HARTEXu-project-2[2]. The project emphasizes model-based design
and builds on methodology and practical experience gained in the SIS1 and SIS2 courses, as well as
knowledge acquired during the SIS3 course.

PROJECT REQUIREMENTS
For this project the system selected is a
washing machine, for the sake of
continuity with SIS2 project, which was
the same system developed under
visulaSTATE. The washing machine
description is as follows: A program
selector controls the machine. Functiona-
lity is as follows, Water is taken in
through 1 of 3 valves. Water for pre-
washing is taken in through Valve1, water
for rinsing is taken in through Valve 2 and
water for main-wash is taken in through
Valve 3.

When starting the pump, used water is


pumped out. A sensor tells when the
machine is empty of water, or when the
Figure 1. Basic washing machine structure. machine is full of water.

An engine turns the washing cylinder in both right and left direction (Figure 1). The engine runs
slowly during washing and fast during spinning. When spinning the direction is not changed. During
washing the cylinder turns 6 times to the right and 6 times to the left. A pulse counter register each
time the cylinder passes the top point.

The control sequence will have only one washing program which is describe as follows: The pre-wash
runs for 7 minutes. Rinsing runs for 3 minutes and then the main wash runs for 9 minutes after this,
another rinsing cycle runs for 3 minutes. The pump runs until the machine is empty of water. There is
only one rinse after the main wash and one rinse after pre-wash. Spinning speed is twice the normal
wash speed and is running for 1 minute (Figure 2).

3
n
3mi
7min

9min
Rinsing
Pre-Wash
Cycle
Cycle
Main Wash
Cycle
3min

Rinsing
Cycle

1min
Washing Cylinder
Motor

Drying
Cycle

Figure 2. Washing machine control sequence.

PROJECT DESIGN AND DEVELOPMENT


A functional approach can be considered to design the system tasks, where each washing sequence can
be mapped into a task; however this will require a considerable number of tasks which is limited in
this version of the HARTEX kernel. Another approach will be to consider the different dynamics in
the system and mapping them into task. In this case the entire washing sequence is mapped into one
task or a handler, the washing machine cylinder motor can be handled by another task since it presents
different dynamics and execution periods than the ones exhibit in the washing sequence. A bottom up
methodology for the tasks can be applied, designing first the states machines in each task and then
design the tasks communication and interaction. It’s convenient to have a textual description of the
washing machine handler that can latter be easily transformed to a state transition diagram (Figure 3).

− Wait till start key is pressed.


− If start has been pressed, activate pre-wash pump and wait till tank is full.
− If tank full, stop pre-wash pump. Activate pre-wash cycle for 7min.
− After 7min. activate drain pump and wait till the tank is empty.
− If tank empty, stop draining pump and activate rinsing pump and wait till tank is full.
− If tank is full, stop rinsing pump and activate rinsing cycle for 3min.
− After 3 min. activate drain pump and wait till the tank is empty.
− If tank empty, stop draining pump and activate main-wash pump and wait till tank is full.
− If tank full, stop main-wash pump and activate main-wash cycle for 9min.
− After 9min. activate drain pump and wait till tank is empty.
− If tank is empty, stop draining pump and activate rinsing pump and wait till tank is full.
− If tank is full, stop rinsing pump and activate rinsing cycle for 3min.
− After 3 min. activate drain pump and wait till the tank is empty.
− If tank is empty, stop draining pump and activate drying cycle for 1min.
− After 1 min. return to wait state.

4
Begin

0 1
Start Key Activate_Pump (PreWash_Pump)

Deactivate_Pump (PreWash_Pump)
0 1
Tank full Enable (Pre-wash_cycle (7min))
Enable (Motor(wash_speed))

Deactivate_Pump (Drain_Pump)
Activate_Pump (MainWash_Pump)

0 1 Deactivate_Pump (Drain_Pump)
Tank empty Activate_Pump (Rinsing_Pump)

0 1 Deactivate_Pump (Rinsing_Pump)
Tank full Enable (Rinsing_cycle (3min))
Enable (Motor(wash_speed))

Disable (Motor(spin_speed))
Activate_Pump (Drain_Pump)
Disable (Motor(wash_speed))

0 1 Deactivate_Pump (Drain_Pump)
Tank empty Activate_Pump (MainWash_Pump)

0 1 Deactivate_Pump (MainWash_Pump)
Tank full Enable (MainWash_cycle (9min))
Enable (Motor(wash_speed))

Activate_Pump (Drain_Pump)
Disable (Motor(wash_speed))

0 1 Deactivate_Pump (Drain_Pump)
Tank empty Activate_Pump (Rinsing_Pump)

0 Deactivate_Pump (Rinsing_Pump)
1
Tank full Enable (Rinsing_cycle (3min))
Enable (Motor(wash_speed))

Activate_Pump (Drain_Pump)
Disable (Motor(wash_speed))

Deactivate_Pump (Drain_Pump)
0 1
Tank empty Enable (Drying_cycle (1min))
Enable (Motor(spin_speed))

Figure 3. Washing machine handler flowchart.

5
Wait

Pre-Washing
Start_Key/ Cycle
Activate_Pump(PreWash_Pump)

Timer / -
Tank_Full
Filling Ending
Tank Cycle

Drying Timer
Cycle
Emptying Tank_Empty
Tank

Empty_Tank/ -

Rinsing Empty_Tank/
Cycle Activate_Pump(Rinsing_Pump) Rinsing
Cycle
Tank_Empty

Timer
Ending
Cycle Filling
Emptying Tank Tank_Full
Tank
Ending
Tank_Full Cycle

Timer
Filling
Tank Emptying
Tank Tank_Empty

Empty_Tank/
Activate_Pump(Rinsing_Pump)

Main-Washing
Cycle
Empty_Tank/
Tank_Empty Activate_Pump(MainWash_Pump)
Ending
Cycle
Timer

Emptying
Tank_Full
Tank

Filling
Tank

Figure 4. WMH Mealy machine top level representation.

6
Figure 3 shows the system flowchart, for each event there are several control actions to be taken,
notice the cylinder motor control task has been included in the diagram, the interaction between the
WMH and the CMH will be illustrated later on in higher layer task diagram. Figure 4 shows the state
transition chart for the system. Event triggered Mealy machines have been chosen to represent the
washing machine handler states, the washing cycles are composite states also using Mealy state
machines. As a detail example (Figure 5), the Pre-Washing cycle is shown with all the triggering
events and control steps.

Start_Key/ Tank_Ful / Pre-Washing


Activate_Pump(PreWash_Pump) Deactivate_Pump (PreWash_Pump) Cycle
Enable (Pre-wash_cycle (7min))
Enable (Motor(wash_speed))

Filling Ending
Tank Cycle

Timer /
Activate_Pump (Drain_Pump)
Disable (Motor(wash_speed))

Emptying
Tank

Empty_Tank/
Deactivate_Pump (Drain_Pump)
Activate_Pump (Rinsing_Pump)

Figure 5. Detail view of a composite state.

Figures 3, 4 and 5 give a clear idea of what kind communication should exist between the washing
machine handler and the notification or signaling of the different events. As seen in the previous
assignment, semaphores are the way to communicate when event had occurred, so they will be use
here again. A semaphore can be use to test each of the events, so the filling and draining of the
washing machine cylinder will signal a semaphore, the same applies for the timers of the different
washing cycles and the drying cycle.

The next step is to model the following layer of abstraction in which the tasks are shown communica-
ting with each other including the possible triggering sources and their respective semaphores. This
case will include the cylinder motor handler (CMH) and the interaction with the washing machine
handler (WMH).

7
Start Key
START_KEY press ISR0

Tank Full
TANK_FULL ISR1
sensor
Tank Empty
TANK_EMPTY sensor ISR2

enable/disable (event)
Washing Machine
Handler

TIMER_7MIN Evt7MIN

TIMER_3MIN Evt3MIN

TIMER_9MIN Evt9MIN

TIMER_1MIN Evt1MIN
enable/disable (event)

s
00m
s||1
5 0m
Evt100ms WASH_SPEED
Cylinder Motor
Handler
Evt50ms SPIN_SPEED

DISPLAY PUMPS MOTOR


Figure 6. Tasks modeling layer.

There are two tasks running concurrently, the washing machine handler and the cylinder motor
handler. The WMH enables and disables the timer events according to the washing states, the same
goes for the motor timer which will reflect on the motor speed. The timers signal semaphores that will
release the WMH task. The system has 3 resources, the display and pumps are being accessed only by
the WMH task and the motor is being accessed by the CMH.

8
The subsequent code shows the implementation of the washing machine handler (Figure 4), following
the Mealy machine design pattern, is has been implemented by a conventional switch case.

switch ( WMH_Main_State ){
case WMH_INITIAL_WAIT:
if ( testAndReset(START_KEY) ){
ACTIVATE_PUMP(PREWASH_PUMP);
LCDPrint(0,0,"PRE-WASH PUMP ACTIVE");
LCDPrint(1,0,"FILLING TANK...");
WMH_Main_State = WMH_PREWASH_CYCLE;
WMH_Sub_State = WMH_FILLING_TANK;
}
break;

case WMH_PREWASH_CYCLE:
switch ( WMH_Sub_State ){
case WMH_FILLING_TANK:
if ( testAndReset(TANK_FULL) ){
DEACTIVATE_PUMP(PREWASH_PUMP);
ClearDisplay();
LCDPrint(0,0,"PRE-WASH CYCLE");
enable(evt_7min);
enable(wash_speed);
WMH_Sub_State = WMH_ENDING_CYCLE;
}
break;

case WMH_ENDING_CYCLE:
if ( testAndReset(TIMER_7MIN) ){
ACTIVATE_PUMP(DRAIN_PUMP);
ClearDisplay();
LCDPrint(0,0,"CYCLE FINISHED");
LCDPrint(1,0,"DRAINING TANK...");
disable(wash_speed);
WMH_Sub_State = WMH_EMPTYING_TANK;
}
break;

case WMH_EMPTYING_TANK:
if ( testAndReset(TANK_EMPTY) ){
DEACTIVATE_PUMP(DRAIN_PUMP);
ACTIVATE_PUMP(RINSING_PUMP);
ClearDisplay();
LCDPrint(0,0,"RINSING PUMP ACTIVE");
LCDPrint(1,0,"FILLING TANK...");
WMH_Main_State = WMH_RINSING_CYCLE_I;
WMH_Sub_State = WMH_FILLING_TANK;
}
break;
}
break;

case WMH_RINSING_CYCLE_I:
switch ( WMH_Sub_State ){
case WMH_FILLING_TANK:
if ( testAndReset(TANK_FULL) ){
DEACTIVATE_PUMP(RINSING_PUMP);
ClearDisplay();
LCDPrint(0,0,"RINSING CYCLE");
enable(evt_3min);
enable(wash_speed);
WMH_Sub_State = WMH_ENDING_CYCLE;
}
break;

Code Segment 1. Washing machine handler [1/3].

9
case WMH_ENDING_CYCLE:
if ( testAndReset(TIMER_3MIN) ){
ACTIVATE_PUMP(DRAIN_PUMP);
ClearDisplay();
LCDPrint(0,0,"CYCLE FINISHED");
LCDPrint(1,0,"DRAINING TANK...");
disable(wash_speed);
WMH_Sub_State = WMH_EMPTYING_TANK;
}
break;

case WMH_EMPTYING_TANK:
if ( testAndReset(TANK_EMPTY) ){
DEACTIVATE_PUMP(DRAIN_PUMP);
ACTIVATE_PUMP(MAINWASH_PUMP);
ClearDisplay();
LCDPrint(0,0,"MAIN WASH PUMP ACTIVE");
LCDPrint(1,0,"FILLING TANK...");
WMH_Main_State = WMH_MAINWASH_CYCLE;
WMH_Sub_State = WMH_FILLING_TANK;
}
break;
}
break;

case WMH_MAINWASH_CYCLE:
switch ( WMH_Sub_State ){
case WMH_FILLING_TANK:
if ( testAndReset(TANK_FULL) ){
DEACTIVATE_PUMP(MAINWASH_PUMP);
ClearDisplay();
LCDPrint(0,0,"MAIN WASH CYCLE");
enable(evt_9min);
enable(wash_speed);
WMH_Sub_State = WMH_ENDING_CYCLE;
}
break;

case WMH_ENDING_CYCLE:
if ( testAndReset(TIMER_9MIN) ){
ACTIVATE_PUMP(DRAIN_PUMP);
ClearDisplay();
LCDPrint(0,0,"CYCLE FINISHED");
LCDPrint(1,0,"DRAINING TANK...");
disable(wash_speed);
WMH_Sub_State = WMH_EMPTYING_TANK;
}
break;

case WMH_EMPTYING_TANK:
if ( testAndReset(TANK_EMPTY) ){
DEACTIVATE_PUMP(DRAIN_PUMP);
ACTIVATE_PUMP(RINSING_PUMP);
ClearDisplay();
LCDPrint(0,0,"RINSING PUMP ACTIVE");
LCDPrint(1,0,"FILLING TANK...");
WMH_Main_State = WMH_RINSING_CYCLE_II;
WMH_Sub_State = WMH_FILLING_TANK;
}
break;
}
break;

Code Segment 2. Wahsing machine handler [2/3].

10
case WMH_RINSING_CYCLE_II:
switch ( WMH_Sub_State ){
case WMH_FILLING_TANK:
if ( testAndReset(TANK_FULL) ){
DEACTIVATE_PUMP(RINSING_PUMP);
ClearDisplay();
LCDPrint(0,0,"RINSING CYCLE");
enable(evt_3min);
enable(wash_speed);
WMH_Sub_State = WMH_ENDING_CYCLE;
}
break;

case WMH_ENDING_CYCLE:
if ( testAndReset(TIMER_3MIN) ){
ACTIVATE_PUMP(DRAIN_PUMP);
ClearDisplay();
LCDPrint(0,0,"CYCLE FINISHED");
LCDPrint(1,0,"DRAINING TANK...");
disable(wash_speed);
WMH_Sub_State = WMH_EMPTYING_TANK;
}
break;

case WMH_EMPTYING_TANK:
if ( testAndReset(TANK_EMPTY) ){
DEACTIVATE_PUMP(DRAIN_PUMP);
ClearDisplay();
LCDPrint(0,0,"DRYING CYCLE");
enable(evt_1min);
enable(spin_speed);
WMH_Main_State = WMH_DRYING_CYCLE;
}
break;
}
break;

case WMH_DRYING_CYCLE:
if ( testAndReset(TIMER_1MIN) ){
ClearDisplay();
LCDPrint(0,0,"PRESS START");
disable(spin_speed);
WMH_Main_State = WMH_INITIAL_WAIT;
}
break;

default:
break;
}

Code Segment 3. Washing machine handler [3/3].

In the case of the motor, task number 2 is in charge of handling it. For practical convenience a stepper
motor has been used, it has a for 4 bit encoder, giving a 18o increment per step, however it’s not
relevant for this application, the goal is to show the 6 turnings per side and the spinning speed. The
WMH can enable two events which trigger the CMH ultimately resulting in 2 speeds. Each of the
events will signal a semaphore use to determine if the motor should turn both sides at the “normal”
speed or if the motor should rotate only one way at twice the “normal” speed. The handler is shown in
the following code segment.

11
PORTB = motor_step;

if ( testAndReset(WASH_SPEED) ){
if ( motor_rotation_drctn == RIGHT){
motor_step <<= MOVE_ONE_STEP;

if ( motor_step == LAST_STEP_RIGHT ){
motor_step = FIRST_STEP_RIGHT;
motor_rotation_cntr += 1;
}

if ( motor_rotation_cntr == SIX_TURNS_COUNTER){
RESET(motor_rotation_cntr);
motor_rotation_drctn = LEFT;
motor_step = INITIAL_STEP_LEFT;
}
}
else
{
motor_step >>= MOVE_ONE_STEP;

if ( motor_step == LAST_STEP_LEFT ){
motor_step = FIRST_STEP_LEFT;
motor_rotation_cntr += 1;
}

if ( motor_rotation_cntr == SIX_TURNS_COUNTER){
RESET(motor_rotation_cntr);
motor_rotation_drctn = RIGHT;
motor_step = INITIAL_STEP_RIGHT;
}
}
}
else
{
if ( testAndReset(SPIN_SPEED) ){
motor_step <<= MOVE_ONE_STEP;

if ( motor_step == LAST_STEP_RIGHT ){
motor_step = FIRST_STEP_RIGHT;
}
}
}

Code Segment 4. Cylinder motor handler.

CONCLUSIONS
The implementation of a real time system has been done sucssesfully, with some minor drawbacks.
There is a small issue in the kernel configurator, the desing presented in this document is tries to use
events to signal semaphores which will release a task, inside this task the signaled semaphore is use to
determine the actions to be taken and the next state, this is kind of “tricky” on the tool, the semaphore
section is quite obvious, the semaphore releases the tasks that are checkbox, however in the event
section the corresponding semaphore is checkbox, but to indicate which task to signal the tool uses the
same row as for releasing! If a task is going to be release by a semaphore and the semaphore will be
use inside the task, in the event section the semaphores has to be indicated, also the which task is
going to be signal, but the release box has to remain unchecked. Regarding the implementation,
Figure 4 shows a hierarchical state machine and as explained in the design section, the modeling was
done based on the system’s dynamic behavior, which seem more appropriated since the number of
tasks is limited, however for this small system a functional approach could also have worked.

12
REFERENCES

[1] N. Marian: SIS3 Project Assignments. Mads Clausen Institute for Product Innovation, 2005.

[2] C.K. Angelov, N. Marian: HARTEXu-project-2. Mads Clausen Institute for Product Innovation,
2005.

[3] C.K. Angelov: Lecture Notes in Embedded Software Engineering – part 1: Model-Based Design of
Embedded Software. Mads Clausen Institute for Product Innovation, 2005 (lectures on formal
specification and design of software for sequential control systems)

[4] C.K. Angelov: Lecture Notes in Embedded Software Engineering – part 2: Real-Time Systems.
Mads Clausen Institute for Product Innovation, 2005

[5] Q. Li, C. Yao: Real-Time Concepts for Embedded Systems. CMPBooks. 2003

13