You are on page 1of 31

REAL LIFE CASE STUDY FOR UNDERSTANDING THE USE OF C/OS-II FOR CONTROLLEING A CAR

I. Introduction
A number of embedded systems are used in automobiles. Section 12.2 lists these. Present case study describes a project done for understanding the use of COS-II (Section 9.2). It shows a control system for a car. It uses the computer keyboard, Pentium PC and interface circuit with a stepper motor in the car. An interfacing connects the motor and IBM PC parallel port. The project gave successfully results. It has been implemented by my students at School of Electronics Mr. Abhimanyu Singhal and Manish Sharma under the guidance of Ms. Sneha Ajmera during 2004-05.

II. Case Study Understanding the use of C/OS-II for controlling a CAR
Objective of the case study is to show the car control on a PC how a car is controlled using a real time kernel (Section 8.8). It uses COS-II (Section 9.2). It uses an interface between computer and stepper motor driven toy car. (Section VI below). Table 1 lists the differences between two systems one the present control and other actual control system in a car. The study uses, in place of the actual car, a car shaped stepper motor driven assembly to rotate the wheels. The assembly interfaces a circuit between the car and IBM PC parallel port. Keyboard and display at the IBM PC are used in the study. The actual car uses diesel or petrol engine for forward and reverse motion when driving in place of

Embedded Systems

stepper motor. The actual car uses a touch screen or voice activated commands in place of commands entered from keyboard.
Table 1 The differences between the present car control system and actual car control system Purpose Car Engine Gear up and down Horn Fuel Forward/Reverse Brake Collision control by Airbag Embedded Software-hardware platform Software Platform Present System Toy car Stepper Motor KP4M4-001 Actual Car Gas or Diesel or Petrol or Hybrid Engine Simulation by pressing up arrow and 0, 1, 2, 3, 4, 5 for pushing ranges of down arrow keys speed up and down Simulation by pressing H key Horn Simulation by pressing W and S Gas or Diesel or Petrol or battery keys for increasing or decreasing the power fuel Simulation by pressing F and R keys By five forward gears and a reverse gear Simulation by pressing SPACE key Brake Collision simulated by pressing O Airbag activates on collision key Pentium based IBM PC Microcontroller based system C (Section 5.2), GNU C compiler (Section 5.6.3) and learning version COS-II described in Jean J. Labrosse book (Appendix 2: Ref. 76) IBM PC keyboard VGA monitor IBM PC parallel port MISRA C (Section 12.3.6), IDE (Section 13.1.4) with system C compiler and real time operating system automotive version COS-II Touch screen (Section 3.3.5) display panel or voice activated commands Display panel in side the car in face of the driver CAN bus controller and CAN interface (Section 3.10.2)

Inputs Outputs Interface port

III. Functioning of the system Table 2 gives the actions of the keyboard keys, which are used in the car for driving as well as also simulate the control actions on display screen. The keyboard controls the

Real Life Case Studies

system as follows: The car stepper motor moves anticlockwise and clockwise, stops, slows and fastens as per the commands given by pressing the keys. The stepper motor currents are controlled by interfacing circuit, which gets inputs from computer parallel port. Four outputs from parallel port as per the command are given to the dc inputs to one of the coil at an instant in the appropriate sequences (Section VI).

Table 2
Actions of the keyboard keys, which are used for COS-II scheduling and control

Key Up arrow key Down arrow key O Space bar key W S H left arrow key right arrow key

Action Gear up (acceleration) Gear down (deceleration) Accident simulation to activate airbag Apply brake Fuel up (just for simulation) Fuel down (just for simulation) Horn Left indicator Right indicator

Table 3 gives the 15 tasks in the system, running and blocking of whom are controlled by the preemptive scheduler COS-II. Table second column gives the preset priorities of each task also, using which RTOS COS-II schedules and controls. Naturally the airbag and brake tasks have high priorities.
Table 3
The tasks in the system, running and blocking of whom are controlled by preemptive scheduler

COS-II S.No. 1 Task Start Priority 9 Action when the task runs Only at first time run (i) Create the following tasks: Manager, airbag, brake, GearUP, GearDn, FuelUP, FuelDN, (ii) Draw the car on the screen and in successive runs (iii) calculate CPU

Embedded Systems

performance and run task Stat every 1 s using


OS_TaskStat ( ) is used to get the statistical data during the run for CPU usage and performance

2 3 4 5 6 7 8 9 10 11 12 13 14

Manager AirBag Brake GearUP GearDN FuelUP FuelDN WhlFrd WhlRBakWrd Horn LftInddctr RhtInddctr Speed

11 12 13 14 15 16 17 18 19 20 21 22 23

Manage the car functions using scan code function, which searches key pressed for car control Initiate Airbag Apply Brake Gear increase, Set the task running t higher speed when gear up pressed and move at speed as per new gear value Gear decrease, Set the task running at lower speed when gear down pressed and move at speed as per new gear value Fuel emptiness parameter decrease the corresponding parameter Fuel emptiness parameter increases till empty (=6). Rotate the stepper motor clockwise Rotate the stepper motor anticlockwise Horn for 500 ms Display left indicator Display right indicator Speed function display the car speed continuously on monitor. If fuel is empty, stop rotating. If fuel is low, display warning.

IV. Requirements, Design Metric and System Functioning


Assume that there is a stepper motor for use in electric toy car. Section VI will give the required outputs for the given inputs from keyboard for forward and reverse movements, respectively. Table 4 gives the requirements, design metrics and system functioning of the electronic controller for the car.
Table 4 Requirements, design metrics and system functioning of the electronic controller system for car
Requirement Description

Real Life Case Studies

Purpose Inputs Events System outputs Functions of the system Design metrics

Electronically control a four-phase stepper motor based toy car using four outputs at computer parallel port and speed control by pressing keys on computer keyboard A key O or space or , , , , F or RW or S or H (Table 2) Pressing a key (i) Parallel port four bits to interface cicuit, which connects the four coils of the stepper motor (ii) VGA monitor screen of computer. Given separately in Section III (i) Use IBM PC for embedded platform and Hardware (ii) 12 V Stepper motor in toy car assembly with 10 m cable from PC parallel port (iii) Continuous run at full speed within a confined area in a room (iv) An IC with seven Darlington transistors current drivers for maximum 500 mA per driver maximum 50 V, integrated reverse currents suppression diodes and TTL/CMOS compatible inputs Each program module tested individually as well as after system integration for different speeds and for forward and reverse motion for one hour continuous run without need of recharging.

Test and validation conditions

V. Hardware Architecture A PC is used as embedded applications system. Figure 1 shows the system architecture. The PC connects to the car using IBM PC parallel port. Section VI will give details of interfacing circuit. There are no feedback bits from the motor for the actual motor steps per second. This is because the motor interface is for the open loop control of motor.

Embedded Systems

Toy Car with stepper Motor KP4M4-001

Space, O, F, R, W. S. H. , , and
Interface circuit to Motor System Keypad System Display Parallel port address 0x378

Computer (IBM PC)

Figure 1 The system architecture

VI. Interfacing Circuit and Stepper Motor in the Car Figure 2 shows the interfacing of stepper motor KP4M4-001 set of coils between the parallel port output bits at data 0, data 1, data 2 and data 3 pins through the coils driver IC ULN 2003. Figure also gives the IC Pin connections. The stepper motor has five connections, common set of four coils (black), individual coil-pair 1 (red), coil-pair 2 (green), coil-pair 3 (brown) and coil-pair 4 (white). An individual coil-pair is a set of two parallel coils, which are placed nearby to the common coil linked to the common. It enables to make each set of three coils A, B, C and D independent. The current is always switched on in set A, or B or C or D at an instance. The IC ULN2003/MC 1413 is a 7-bit 50 V 500 mA TTL input NPN Darlington driver. Only 4bit input at pins 3, 5, 4 and 6 and 4-bit output at pins 14, 12, 13 and 11 are used. 3-pins for input (1, 2 and 7) and 3 (16, 155 and 10 for output are not used. It is used to control a common set of coils and four coil pairs in stepper motor.

Real Life Case Studies

12V Zener Data 0 Data 1 Data 2 Data 3 2 3 4 5 2 3 4 5 9 IC ULN 2003 8

0V 14 13 12 11

Common Black A C B D Red Brown Green white

IBM PC Parallel 25 Pins Port

Figure 2 The interfacing of stepper motor sets of coils A, B, C and D to the parallel port data 0, data 1, data 2 and data 3 pins through the coils driver IC ULN 2003 interface circuit between IBM PC parallel port and coils

The current is switched from one to next neighboring set of coils and cyclically in the four sets in a four phase stepper motor. Rotor consists of permanent magnets. Stator consists of four electromagnetic coil sets. It can move in 180 steps or 360 half steps or large number of micro-steps in one full 360 o rotation. A stepper motor program switches and controls the coil currents in stator (through transistors in the IC ULN 2003A). Table 4 shows current changes in the coil-sets A, B, C and D for forward rotation and Table 5 accordingly for backward rotation. Table 4 gives how to control steps using ULN 2003A IC four outputs switched in emitters of four internal transistors in the IC. The interface circuit interfaces the ULN 2003A IC to parallel port of IBM PC for forward rotation. Figure 3 gives the currents in winding A, B, C and D during forward movement at different steps given in the Table 4.

Embedded Systems

Table 4 Control of ULN 2003A outputs switched to emitters of four internal transistors using parallel port of IBM PC for forward rotation
Phase Sequence

ULN 2003A output switched in emitters of four internal transistors


Q1 Q2 0 1 0 0 0 1 Q3 0 0 1 0 0 0 Q4 0 0 0 1 0 0 1 0 0 0 1 0

Phase Current in A

Phase Current in B

Phase Current in C

Phase Current in D

1F 2F 3F 4F 1F 2F

1 (o) 2 ( + 2o) 3 ( + 4o) 4 ( + 6o) 1 (o + 8o) 2 ( + 10o)

on off off off on off

off on off off off on

off off on off off off

off off off on off off

Table 5 gives control using the ULN 2003A IC. Figure 4 shows the currents in winding A, B, C and D in the reverse rotation at different steps.

Common A

PA

1 PB B

5 D PD

2 6 3 C PC

Figure 3 Currents in four set of windings A, B, C and D of Stepper motor at six steps, 1, 2, 3, 4, 5 and 6 in forward direction

Real Life Case Studies

Common 1

PA A 5 D 2 PD

PB

4 C 3 PC 6

Figure 4 Current directions in four set of windings A, B, C and D of Stepper motor at six instances, 1, 2, 3, 4, 5 and 6 in reverse direction

Table 5 Control of ULN 2003A outputs switched to emitters of four internal transistors using parallel port of IBM PC for reverse rotation
Phase Sequence

ULN 2003A output switched in emitters of four internal transistors


Q1 Q2 0 0 0 1 0 0 Q3 0 0 1 0 0 0 Q4 0 1 0 0 0 1 1 0 0 0 1 0

Phase Current in A

Phase Current in B

Phase Current in C

Phase Current in D

1R 2R 3R 4R 1R 2R

1 (o) 2 ( 2o) 3 ( 4o) 4 ( 6o) 1 ( 8o ) 2 ( 10o)

on off off off on off

off off off on off off

off off on off off off

off on off off off on

VII. State Diagrams The state diagram of the task scheduling and running RTOS running is shown in Figure 5.

10

Embedded Systems

Start RTOSCOS-II Run Pressed O Pressed SP False True task AIRBAG Pressed W False True task BRAKE Pressed S False False True task FuelUP True task FuelDN True task GearUP Pressed H False True task Horn Pressed False False True task GearDN Pressed False True task LftIndctr True task WhlFrd Pressed False True task RhtIndctr False False True task WhlBakWrd Delay 10 ticks False True ScnCodeRdy Pressed Pressed F Pressed R

task Start

stack size of 14 tasks

OSStatInit ( )

create 14 tasks

Draw Car and simulation screen

OS_TaskStat ( ) True 1 s over False

Delay 1 s task Manager

True Delay Over False RTOS Run Stop End

task Speed

Figure 5 State diagram for Tasks in the system and their scheduling and running using RTOS

Real Life Case Studies

11

VIII. Software Tools The tools required for the present system are as follows: BOOTLOAD.BIN: The IBM PC runs this program after power on self test to disk bootstrap loader BOOTLOAD.BIN from boot sector of the disk. DJGPP: DJGPP is a port of GNU C compiler and linker, a compatible assembler and run time library. GCC is a command line compiler that can be invoked from the DOS command line. It compiles and link in a single file C program. LDScript: It creates linker script file for LD. It uses LDSCRIPT.exe. It redirects the output into a file LINK.CMD. This file is a linker script. It tells the loader how to build your application. . Libepc.a: An embedded runtime library in the IBM PC. This library is linked to the application program. The library code initializes the 8239, 8253 and run time environment. It also provides some low level routines from keyboard input and display output COS-II.a: The kernel library file builds preemptive multithreaded application COS-II codes.

IX. Program Consider the program in Example. Figure 6 shows the required preprocessor commands, main function, tasks and OS functions for CPU usage. .
Example

12

Embedded Systems

Include three header files: dos.h, libepc.h, os_cpu.h and uscos_ii.h Define Task_STK_SIZE 1024, AIRBAG 0x100, BRAKE 0x3900, UP 0x4800, DOWN 0x5000, FORWARD 0x2100, REVERSE 0x300, FUELUP 0x1100, FUELDN 0x1F00, HORN 0x2300, LEFT 0x4B00, RIGHT 0x4D00 and address 0x378 (for parallel port) Undef OS_TASK_STA_EN, and define OS_TAK_STAT_EN 1 (i) 8 bit unsigned and signed integers: OSTaskCtr (for number of tasks) and OSCPUUsage in %, (ii) unsigned 32-bit integers OSIdlCtr, OSIdleCtrMax and OSIdleCtrRun, (iii) Boolean OSStatRdy (iv) static initial values 16-bit words dly 1000, gear 0, c , fuel 500, count 0 Main Function specify TaskStart ( ) task stack size, clear scree, initiate OS by OSInit ( ), Create TaskStart of priority 9, start OS by OSStart ( ) Task Functions TaskStart ( ), Manager ( ), AirBag ( ),, Brake ( ), WhelFrd ( ), WhlBakWrd ( ) GearUP ( ), GearDN ( ), Speed ( ), FuelUP, FuelDN, Horn ( ) LftIndctr ( ) and RhtIndCtr ( ) OS Functions for CPU usage

OS_TaskStat ( ) OSStatInit ( )

Figure 6 Preprocessor commands, main function, tasks and OS functions for CPU usage in Example /************************************************************************ File: RealTimeCar.c Purpose: To control a car using real time kernel UCOS-II on x86 system using a Keyboard for controlling and display is done through VGA monitor. Embedded.bin is our final binary, which is loaded on floppy diskette with bootstrap loader .This way application program runs as a bootable image on any system having 25 pin parallel port. Date : 6th June 2005 Created By: Abhimanyu Singhal and Manish Sharma

Real Life Case Studies

13

For any queries contact us at : abhimanyu.singhal@gmail.com or manishrma@gmail.com **************************************************************************** */ #include <dos.h> #include "libepc.h" #include "os_cpu.h" #include "os_cfg.h" #include "ucos_ii.h" #define TASK_STK_SIZE #define AIRBAG #define BRAKE 0x1800 0x3900 1024 /* press O for accident simulation*/ /* press SPACE for Brake */

#define UP 0x4800 /* press UP arrow for gear up */ #define DOWN 0x5000 /* press DOWN arrow for gear down */ #define FORWARD 0x2100 #define REVERSE 0x1300 #define FUELUP #define FUELDN #define HORN 0x1100 0x1F00 0x2300 /* press F for moving forward */ /* press R for car reversing */ /* press W for car fueling */ /* press S for decreasing fuel,just for simulation */ /* press H for horn */

#define LEFT 0x4B00 #define RIGHT 0x4D00 #define adress 0x378

/* press left arrow for left indicator */ /* press right arrow for right indicator */ /* parallel port address of LPT1 */

#undef OS_TASK_STAT_EN #define OS_TASK_STAT_EN 1 INT8S INT32U INT32U BOOLEAN INT8U INT32U OSCPUUsage; OSIdleCtrMax; OSIdleCtrRun; OSStatRdy; OSTaskCtr; OSIdleCtr; /* Percentage of CPU used */ /* Max. value that idle ctr can take in 1 sec. */ /* Val. reached by idle ctr at run time in 1 sec. */ /* Flag indicating that the statistic task is rdy */ /* Number of tasks created */ /* Idle counter */

14

Embedded Systems

static WORD16 dly=1000, gear=0,c=5,fuel=500,count=0; void Manager() { while (1) { if(ScanCodeRdy()) { WORD16 CT =(ScanCode2Ascii(GetScanCode()) & 0xff00 ); switch(CT) { case AIRBAG : OSTaskResume(12); break; case BRAKE : OSTaskResume(13); break;

case UP

: OSTaskResume(14); break; case DOWN : OSTaskResume(15); break; case FORWARD : SetCursorPosition(19,2); PutString("Moving Forwards "); OSTaskSuspend(19); OSTaskResume(18); break; case REVERSE : SetCursorPosition(19,2); PutString("Moving backwards "); OSTaskSuspend(18); OSTaskResume(19); break; case FUELUP : c=c++; OSTaskResume(16); break; case FUELDN : c=c--; OSTaskResume(16); break;

Real Life Case Studies

15

case HORN case LEFT

: OSTaskResume(20); break;

: OSTaskResume(21); break; case RIGHT : OSTaskResume(22); break; } } OSTimeDly(10); } }

/* Highest priority task resume on accident as Air bag This task enables when any accident occurs (here by pressing 'O' ) and disables motion of car */ void AirBag() { while(1) { SetCursorPosition(22,0); PutAttb(192,80); SetCursorPosition(23,31); PutString(" A I R B A G "); Sound(1000); SetCursorPosition(9,32); PutAttb(64,11); SetCursorPosition(24,0); PutAttb(64,80);

16

Embedded Systems

OSTaskSuspend(13) ; OSTaskSuspend(11) ; OSTaskSuspend(14) ; OSTaskSuspend(15) ; OSTaskSuspend(16) ; OSTaskSuspend(17) ; OSTaskSuspend(18) ; OSTaskSuspend(19) ; OSTaskSuspend(20) ; OSTaskSuspend(21) ; OSTaskSuspend(22) ; OSTaskSuspend(OS_PRIO_SELF); } } /************************************ BRAKE ****************************************** */ void Brake() { while (1) { SetCursorPosition(10,11) ; PutString("ON "); OSTaskResume(15); /* Resumes Gear down task */ if(gear==0) { OSTaskSuspend(18); OSTaskSuspend(19); OSTimeDly(500); OSTaskResume(18); } OSTimeDly(500); SetCursorPosition(10,11) ; PutString("OFF "); OSTaskSuspend(OS_PRIO_SELF); execution */ } /* if gear becomes is zero then suspend motor rotation for half second then starts again */

/* task suspends itself after single

Real Life Case Studies

17

/**************** Moving Stepper motor clockwise *************************/ /* This task rotates the wheel in forward direction with a delay specified with the gear */ void WhlFrd() { outportb(adress,0x00); /* sending zeros at begining */ SetCursorPosition(19,2); PutString("Moving Forwards "); while(1) { outportb(adress,0x01); OSTimeDly(dly) ; outportb(adress,0x02); OSTimeDly(dly) ; outportb(adress,0x04); OSTimeDly(dly) ; outportb(adress,0x08); OSTimeDly(dly) ; } }

/************** Moving Stepper motor Anti clockwise **********************/ /* This task rotates the wheel in reverse direction with a delay specified with the gear */ void WhlBakWrd() {

18

Embedded Systems

OSTaskSuspend(OS_PRIO_SELF); while(1) { outportb(adress,0x08); OSTimeDly(dly) ; outportb(adress,0x04); OSTimeDly(dly) ; outportb(adress,0x02); OSTimeDly(dly) ; outportb(adress,0x01); OSTimeDly(dly) ; } }

/* task suspends itself at the begining*/

/* This task is set to running state when gear up is pressed */ void GearUP() { OSTaskSuspend(OS_PRIO_SELF); while (1) { (gear>=4)?((dly=5) && (gear=4)):(gear++); if(gear==1) dly=300; if(gear==2) dly=50;

Real Life Case Studies

19

if(gear==3) dly=10; if(gear==4) dly=5; SetCursorPosition(8,11); PutUnsigned(gear,10,1); OSTaskSuspend(OS_PRIO_SELF); } }

/* This task is set in running state when gear down is pressed */ void GearDN() { OSTaskSuspend(OS_PRIO_SELF); while (1) { (gear<1)?((dly=1000)&&(gear=0)):(gear--); if(gear==1) dly=300; if(gear==2) dly=50; if(gear==3) dly=10; if(gear==4) dly=5; SetCursorPosition(8,11); PutUnsigned(gear,10,1); OSTaskSuspend(OS_PRIO_SELF); } } /* This task displays the speed of the car continuously on the monitor */

20

Embedded Systems

void Speed() { int spd=0; while (1) { if(gear==4) spd=80; else if(gear==3) spd=60; else if(gear==2) spd=30; else if(gear==1) spd=10; else if (gear==0) spd=00; SetCursorPosition(6,11) ; PutAttb(11,10); PutUnsigned(spd,10,3); fuel-= gear; SetCursorPosition(6,65) ; PutAttb(11,4); PutUnsigned(fuel,10,4); OSTimeDlyHMSM(0,0,1,0) ; count=count + gear; if(count>=100) { c--; OSTaskResume(16); } if(fuel<=6) */ { fuel = 0; SetCursorPosition(6,27) ; PutAttb(192,20); PutString(" FUEL IS EMPTY "); /* condition when fuel is empty, motor stops rotating

Real Life Case Studies

21

OSTaskSuspend(18) ; OSTaskSuspend(19) ; OSTaskSuspend(OS_PRIO_SELF); SetCursorPosition(6,27) ; PutAttb(4,20); PutString(" "); } if(fuel<=100 && fuel>0) /* warning condition when fuel is low */ { SetCursorPosition(5,27) ; PutAttb(192,20); PutString("WARNING : LOW FUEL"); } else { SetCursorPosition(5,27) ; PutAttb(0,20); PutString(" "); } } }

/* This task is used for fueling pressing 'W' key*/ void FuelUP() { c=5; while (1) { if(c>=10) c=10; if(c<=0)

22

Embedded Systems

c=0; SetCursorPosition(5,(65 + c)) ; PutAttb(16,10-c); SetCursorPosition(5, 65); PutAttb(32,c); if(fuel<=6) OSTaskResume(17); fuel=c*100; count=0; OSTaskSuspend(OS_PRIO_SELF) ; } }

/*This task is used for decreasing fuel tank by pressing 'S' key, although this is just for simulation purpose void FuelDN() { c=5; while (1) { c=c--; SetCursorPosition(7,(65 + c)) ; PutAttb(16,10-c); SetCursorPosition(7, 65); PutAttb(32,c); fuel=c*100; count=0; OSTaskSuspend(OS_PRIO_SELF) ; } } */

Real Life Case Studies

23

/* This task is used for Horn by pressing 'H' */ void Horn() { OSTaskSuspend(OS_PRIO_SELF); while (1) { SetCursorPosition(12,11) ; Sound(2000); PutAttb(9,4); PutString("ON") ; OSTimeDlyHMSM(0,0,0,500); Sound(0); SetCursorPosition(12, 9) ; PutString(" ") ; OSTaskSuspend(OS_PRIO_SELF); } }

/* This task blinks for Left indicator by pressing left key */ void LftIndctr() { OSTaskSuspend(OS_PRIO_SELF); while (1) { SetCursorPosition(14,2) ; PutString("LEFT") ; OSTimeDlyHMSM(0,0,1,0); SetCursorPosition(14,2) ; PutString(" "); OSTaskSuspend(OS_PRIO_SELF); } }

24

Embedded Systems

/* This task blinks for Right indicator by pressing left key */ void RhtIndctr() { OSTaskSuspend(OS_PRIO_SELF); while (1) { SetCursorPosition(14,10) ; PutString("RIGHT") ; OSTimeDly(1000); SetCursorPosition(14,10) ; PutString(" ") ; OSTaskSuspend(OS_PRIO_SELF); } }

/*This is a function used to gather statistical data of CPU */ void OS_TaskStat() { DWORD32 run; DWORD32 usage; static INT8U foo=0; OS_ENTER_CRITICAL(); OSIdleCtrRun = OSIdleCtr; run = OSIdleCtr; OSIdleCtr = 0L; OS_EXIT_CRITICAL(); if(OSIdleCtrMax >0) { usage = (100L-(100L *run)/OSIdleCtrMax); if(usage >100) OSCPUUsage =100; else if(usage<0)

Real Life Case Studies

25

OSCPUUsage = 0; else OSCPUUsage = usage ; SetCursorPosition(13,60) ; PutUnsigned(run,10,10); } else OSCPUUsage =0; }

/* This task is used to create all other task and drawing graphical car on screen and then calculates the performance of CPU and running task statistics using OS_TaskStat() function after every 1 second */ void TaskStart() {

WINDOW *Window1,*Window2 ,*win; static OS_STK Task0Stk[TASK_STK_SIZE] ; static OS_STK Task1Stk[TASK_STK_SIZE] ; static OS_STK Task2Stk[TASK_STK_SIZE] ; static OS_STK Task3Stk[TASK_STK_SIZE] ; static OS_STK Task4Stk[TASK_STK_SIZE] ; static OS_STK Task5Stk[TASK_STK_SIZE] ; static OS_STK Task6Stk[TASK_STK_SIZE] ; static OS_STK Task7Stk[TASK_STK_SIZE] ; static OS_STK Task8Stk[TASK_STK_SIZE] ; static OS_STK Task9Stk[TASK_STK_SIZE] ; static OS_STK Task11Stk[TASK_STK_SIZE] ; static OS_STK Task12Stk[TASK_STK_SIZE] ; static OS_STK Task13Stk[TASK_STK_SIZE]; OSStatInit(); /* To calculate Maximum value of Idle counter*/

26

Embedded Systems

OSTaskCreate(AirBag, NULL, &Task0Stk[TASK_STK_SIZE], 12) ; OSTaskCreate(Brake , NULL, &Task1Stk[TASK_STK_SIZE], 13) ; OSTaskCreate(Manager, NULL, &Task4Stk[TASK_STK_SIZE], 11) ; OSTaskCreate(GearUP, NULL, &Task5Stk[TASK_STK_SIZE], 14) ; OSTaskCreate(GearDN, NULL, &Task6Stk[TASK_STK_SIZE], 15) ; OSTaskCreate(FuelUP, NULL, &Task9Stk[TASK_STK_SIZE], 16) ; OSTaskCreate(Speed, NULL, &Task7Stk[TASK_STK_SIZE], 23) ; OSTaskCreate(WhlFrd , NULL, &Task2Stk[TASK_STK_SIZE], 18) ; OSTaskCreate(WhlBakWrd,NULL, &Task3Stk[TASK_STK_SIZE], 19) ; OSTaskCreate(Horn, NULL, &Task11Stk[TASK_STK_SIZE],20) ;

OSTaskCreate(LftIndctr, NULL, &Task12Stk[TASK_STK_SIZE],21) ; OSTaskCreate(RhtIndctr, NULL, &Task13Stk[TASK_STK_SIZE],22) ;

//OSTaskCreate(FuelDN, NULL, &Task10Stk[TASK_STK_SIZE],17) ;

SetCursorPosition(6, 4) ; PutAttb(9,5); PutString("SPEED: "); SetCursorPosition(8,4) ; PutAttb(9,5); PutString("GEAR:") ; SetCursorPosition(10, 4) ; PutAttb(9,7); PutString("BRAKE: ") ; SetCursorPosition(14,1) ; PutAttb(96,5); SetCursorPosition(14,10) ; PutAttb(96,5); SetCursorPosition(5,60) ;

Real Life Case Studies

27

PutAttb(9,6); PutString("FUEL: ") ; SetCursorPosition(12, 4) ; PutAttb(9,4); PutString("HORN") ; SetCursorPosition(6,71) ; PutAttb(1,2); PutString("ml");

/* title

*/

SetCursorPosition(0,0) ; PutAttb(7,80); PutString("********************************************************************* ********") ; SetCursorPosition(1,20) ; PutAttb(12,80); PutString("CAR CONTROL SYSTEM USING UCOS RTOS"); SetCursorPosition(2,0) ; PutAttb(7,80); PutString("********************************************************************* ********") ;

/* for graphical car on VGA monitor */ Window1 = WindowCreate(" ",8,18,30,45); Window2 = WindowCreate("",10,14,35,40); SetCursorPosition(8,27) ; PutAttb(112,3); SetCursorPosition(9,27) ; PutAttb(112,3); SetCursorPosition(16,26) ;

28

Embedded Systems

PutAttb(112,4); SetCursorPosition(17,26) ; PutAttb(112,4); SetCursorPosition(18,26) ; PutAttb(112,4); SetCursorPosition(8,46) ; PutAttb(112,3); SetCursorPosition(9,46) ; PutAttb(112,3); SetCursorPosition(16,46) ; PutAttb(112,4); SetCursorPosition(17,46) ; PutAttb(112,4); SetCursorPosition(18,46) ; PutAttb(112,4); /* For CPU statistics on the screen */ win = WindowCreate(" CPU STATS SetCursorPosition(10,55) ; PutAttb(9,22); PutString("Idle Task Maxcounter: ") ; SetCursorPosition(12,55) ; PutAttb(9,18); PutString("Idle Task counter: ") ; SetCursorPosition(14,55) ; PutAttb(9,18); PutString("Context Switching: ") ; SetCursorPosition(16,55) ; PutAttb(9,10); PutString("CPU usage: ") ; SetCursorPosition(18,55) ; PutAttb(9,13); PutString("Task Running: ") ; ",8,20,53,78);

Real Life Case Studies

29

/* task calculating stats after every second */ while (1) { OS_TaskStat(); SetCursorPosition(11,60) ; PutUnsigned(OSIdleCtrMax,10,10); SetCursorPosition(15,60) ; PutUnsigned(OSCtxSwCtr,10,5); SetCursorPosition(17,60) ; PutUnsigned(OSCPUUsage,10,4); SetCursorPosition(19,60) ; PutUnsigned(OSTaskCtr,10,3); OSCtxSwCtr = 0; OSTimeDlyHMSM(0, 0, 1, 0); } } /* This function is used to calculate the maximum calue of idle counter that can be achieved */ void OSStatInit() { OSTimeDly(2); OS_ENTER_CRITICAL(); OSIdleCtr =0L; OS_EXIT_CRITICAL(); /* disable interrupts */ /* zero value of idle counter */ /*enable interrupts */ /* delay for idle counter to increment */ /* Clear context switch counter /* Wait one second */ */

OSTimeDly(OS_TICKS_PER_SEC);

30

Embedded Systems

OS_ENTER_CRITICAL(); OSIdleCtrMax = OSIdleCtr; OS_EXIT_CRITICAL(); }

/* disable interrupts */ /* enable interrupts */

/* This is the main function of the code where only one task is created which creates all other task afterwards.UCOS is initialized and shedular starts from here. */ int main(void) { static OS_STK TaskStartStk[TASK_STK_SIZE] ; ClearScreen(0x07) ; OSInit() ; OSTaskCreate(TaskStart, NULL, &TaskStartStk[TASK_STK_SIZE], 9) ; OSStart() ; return 0 ; }

X. Method (i) Install COS-II on x86 computer and tools listed in Section VII, (ii) connect parallel port to interface circuit for the toy car with 10 m cable. The toy car is confined to area of 8 m. Connect power supply. First create and edit a new project file RealTimeCar.C with C program of Example. Now compile the program and create final binary file Embedded.bin. If no errors then final binary file will be created. Alternatively contact the email IDs of code authors for the ready .bin file Now, start the executable file and study the results.

Real Life Case Studies

31

XI. Results Car is run as per the actions mentioned in Table 2. Keep watching VGA monitor for results and car for the movements. 1. 2. 3. 4. 5. 6. Firstly put the fuel by W key. Press key for gear to 1. Press F to move forward. The car starts moving. Press key for gear to 2. When the car reaches dead end in the given area, press space key for brake then gear down task resumes, if gear = 0, then verify that display on screen shows OFF and whlFrd and WhlBakWrd tasks suspends 7. Press R for reversing the car. 8. Start again from step 2. 9. Press S key to reduce the fuel successively, when fuel is low, the VGA display gives the warning message. On warning repeat from step 2. 10. Repeat again by changing the gears and using Study by changing the gears REPEAT AGAIN keys. 11. Press key when there is diversion to left during the run and verify that display on screen shows the left indicator ON 12. Press key when there is diversion to left during the run and verify that display on screen shows the right indicator ON 13. Press O to verify the action on accident and verify that display on screen shows A I R B A G and all the all tasks suspend. The control of the car using keyboard keys and simulation on screen gets verified by the continuous runs and correct simulations on the VGA monitor for 60 minutes.

You might also like