Blame |
Last modification |
View Log
| RSS feed
#include "train.h"
/******************************************************************/
void init_display
()
{
int i
,j
;
for (i
= 0; i
< MAX_CITY
; i
++)
grx_disc
(graph
[i
][0],graph
[i
][1],10,14);
for (i
= 0; i
< MAX_CITY
; i
++)
for (j
= 0; j
< MAX_CITY
; j
++)
{
if(i
!=j
)
grx_line
(source
[i
][0],source
[i
][1],destination
[j
][0],destination
[j
][1],5);
}
}// display ends here
void geninfo
(int identi
)
{
int src
,des
,track
;
int flag
;
track
=rand() % 12;
if(track
==0 || track
==6)
{
// flag= rand()%2;
if(track
==0){
src
= 0;
des
= 1;
}
else{
src
= 1;
des
= 0;
}
}
if(track
==1 || track
==7)
{
//flag= rand()%2;
if(track
==1){
src
= 1;
des
= 2;
}
else{
src
= 2;
des
= 1;
}
}
if(track
==2 || track
==8)
{
//flag= rand()%2;
if(track
==2){
src
= 2;
des
= 3;
}
else{
src
= 3;
des
= 2;
}
}
if(track
==3 || track
==9)
{
//flag= rand()%2;
if(track
==3){
src
= 3;
des
= 0;
}
else{
src
= 0;
des
= 3;
}
}
if(track
==4 || track
==10)
{
//flag= rand()%2;
if(track
==4){
src
= 0;
des
= 2;
}
else{
src
= 2;
des
= 0;
}
}
if(track
==5 || track
==11)
{
if(track
==5){
src
= 1;
des
= 3;
}
else{
src
= 3;
des
= 1;
}
}
train
[identi
].
tracknum = track
;
train
[identi
].
source= src
;
train
[identi
].
desti = des
;
if(track
==0 || track
==2 )
train
[identi
].
check = 0;
if(track
==6 ||track
==8 )
train
[identi
].
check = 1;
if(track
==1 || track
==4 ||track
==5 ||track
==9 )
train
[identi
].
check = 2;
if(track
==3 || track
==11 ||track
==10 ||track
==7 )
train
[identi
].
check = 3;
}//geninfo ends here
int check_reach
(int ox1
,int oy1
,int ox2
,int oy2
,int f
)
{
if(f
==0 )
{
if( ox1
>= ox2
)
return 0; // reached the destination
else return 1;
}
if(f
==1 )
{
if( ox1
<= ox2
)
return 0; // reached the destination
else return 1;
}
else if(f
==2 )
{
if( oy1
>= oy2
)
return 0; // reached the destination
else return 1;
}
else if(f
==3 )
{
if( oy1
<= oy2
)
return 0; // reached the destination
else return 1;
}
}// check if train reached destination
TASK display
(void *arg
){
char temp1
[200];
char temp2
[200];
char temp3
[200];
int i
,j
;
int pos
= 30;
int gposx
= 700;
int gposy
= 40;
int cab_val
;
char *c
;
char rbuf
[2];
while(1){
j
=1;
if(start
){
for(i
=0;i
<count
;i
++) {
c
= cab_getmes
(cid
[i
]);
memcpy(rbuf
,c
,2*sizeof(char));
sscanf(rbuf
,"%2d",&cab_val
);
cab_unget
(cid
[i
], c
);
pos
+=10;
sprintf(temp1
," ");
sprintf(temp1
,"TRAIN NO %d is At X %d Y %d GOING CITY %d TO CITY %d ON TRACK %d BLOCKTIME %f",i
,traininfo
[i
].
cx,traininfo
[i
].
cy,traininfo
[i
].
source,traininfo
[i
].
destination,cab_val
,_i_blocking
[i
]);
grx_text
(temp1
, 30,pos
,15,0);
}// for1
// display status here
for(i
=0;i
<count
;i
++) {
j
++;
if(status
[i
]==0){
// display red;
sprintf(temp2
,"NO %d ",i
);
grx_text
(temp2
, gposx
,gposy
,15,0);
gposx
+=60;
grx_disc
(gposx
,gposy
,10,12);
gposx
+= 35;
if((j
%3)==0){
gposy
+=40;
gposx
=700;
}//if
}// outer
else {
// display green;
sprintf(temp2
,"NO %d",i
);
grx_text
(temp2
, gposx
,gposy
,15,0);
gposx
+=60;
grx_disc
(gposx
,gposy
,10,10);
gposx
+= 35;
if((j
%3)==0){
gposy
+=40;
gposx
= 700;
}//if
}//else
}// for2
}// if
pos
= 30;
gposx
= 700;
gposy
= 40;
j
= 1;
task_endcycle
();
} //while
}// display
////////////////display task ends here
TASK controls
(void *arg
){
char temp1
[200];
char temp2
[200];
char temp3
[200];
int i
,j
;
int pos
= 30;
int gposx
= 700;
int gposy
= 40;
int cab_val
;
char *c
;
char rbuf
[2];
while(1){
j
=1;
if(start
){
for(i
=0;i
<count
;i
++) {
c
= cab_getmes
(cid
[i
]);
memcpy(rbuf
,c
,2*sizeof(char));
sscanf(rbuf
,"%2d",&cab_val
);
cab_unget
(cid
[i
], c
);
pos
+=10;
sprintf(temp1
," ");
sprintf(temp1
,"TRAIN NO %d is At X %d Y %d GOING FROM CITY %d TO CITY %d ON TRACK %d ",i
,traininfo
[i
].
cx,traininfo
[i
].
cy,traininfo
[i
].
source,traininfo
[i
].
destination,cab_val
);
grx_text
(temp1
, 30,pos
,15,0);
}// for1
// display status here
for(i
=0;i
<count
;i
++) {
j
++;
if(status
[i
]==0){
// display red;
sprintf(temp2
,"NO %d ",i
);
grx_text
(temp2
, gposx
,gposy
,15,0);
gposx
+=60;
grx_disc
(gposx
,gposy
,10,12);
gposx
+= 35;
if((j
%3)==0){
gposy
+=40;
gposx
=700;
}//if
}// outer
else {
// display green;
sprintf(temp2
,"NO %d",i
);
grx_text
(temp2
, gposx
,gposy
,15,0);
gposx
+=60;
grx_disc
(gposx
,gposy
,10,10);
gposx
+= 35;
if((j
%3)==0){
gposy
+=40;
gposx
= 700;
}//if
}//else
}// for2
}// if
pos
= 30;
gposx
= 700;
gposy
= 40;
j
= 1;
task_endcycle
();
} //while
}// contoller
TASK fly
(void *arg
)
{
//int x, y;
char *p
,*c
;
char wbuf
[2],rbuf
[2];
char temp1
[600];
char temp2
[650];
char temp3
[650];
char cab_str
[200];
int ox1
, ox2
,oy1
,oy2
,cab_val
;
int dx
, dy
, da
;
int teta
, col
,cur_col
,id
;
int outx
, outy
;
double r
,step_vel
;
int src
,des
;
int i
=0;
int no_of_times
,dt
;
int stepx
,stepy
,track
;
int semval
,wait
;
int go
= 1;
int _i_distance
,_i_speed
,_i_time_required
,cur_speed
;
//Time t;
TIME _time_this
,_time_unblock
;
float _i_waiting_time
;
id
= (int) arg
;
status
[id
] = 0;
_i_blocking
[id
]=0.0;
_i_speed
= rand() % 3+1;
geninfo
(id
);
cid
[id
] = cab_create
(cname
[id
], 2*sizeof(char), 2);
track
= train
[id
].
tracknum;
src
= train
[id
].
source;
des
= train
[id
].
desti;
ox1
=source
[src
][0];
ox2
=destination
[des
][0];
oy1
= source
[src
][1];
oy2
= destination
[des
][1];
//convert(&ox1,&ox2,&oy1,&oy2,src,des);
traininfo
[id
].
source=src
;
traininfo
[id
].
destination=des
;
traininfo
[id
].
track= track
;
traininfo
[id
].
cx= ox1
;
traininfo
[id
].
cy= oy1
;
/// getting the distance;
//sys_gettime(&_time_this);
_i_distance
= (int)sqrt((abs(ox1
- ox2
)*abs(ox1
- ox2
))+(abs(oy1
-oy2
)*abs(oy1
-oy2
)));
_i_time_required
= (int)( _i_distance
/ _i_speed
);
step_vel
= _i_speed
/ _i_time_required
;
///
grx_text
(" in the fly",15,20,10,14);
dy
= oy2
- oy1
;
dx
= ox2
- ox1
;
if (dy
< 0) { dy
= -dy
; stepy
= -1; } else { stepy
= 1; }
if (dx
< 0) { dx
= -dx
; stepx
= -1; } else { stepx
= 1; }
dy
<<= 1; // dy is now 2*dy
dx
<<= 1; // dx is now 2*dx
//grx_text(temp1,ox1,oy1+5,15,0);
if(id
==0) col
=15;
else col
=10;
cur_col
= col
; // if the train is delayed the coler of the train will be shown diffrent
while(1)
{
p
= cab_reserve
(cid
[id
]);
sprintf(wbuf
,"%2d",track
);
//grx_text(temp1,350,700+(id*8),15,0);
memcpy(p
,wbuf
,2*sizeof(char));
cab_putmes
(cid
[id
], p
);
sys_gettime
(&_time_this
);
sem_wait
(&mx_track
[track
]);
if(flag
[track
]==1)
{
sem_post
(&mx_track
[track
]);
go
= 0;
status
[id
]=0;
}
else
if(flag
[track
]==0)
{
flag
[track
]=1;
go
= 1;
sem_post
(&mx_track
[track
]);
}
if(go
){
sys_gettime
(&_time_unblock
);
_i_waiting_time
= (float) (_time_unblock
- _time_this
);
_i_blocking
[id
] = _i_waiting_time
;
cur_speed
= _i_speed
+ (step_vel
* _i_blocking
[id
]);
/****
if(_i_blocking[id] >= 0.010)
{
grx_text(" train is late",15,620,10,14);
cur_col = 4;
}
*/ status
[id
] = 1;
if (dx
> dy
)
{
int fraction
= dy
- (dx
>> 1);
while (src
!=des
) {
grx_disc
(ox1
,oy1
,2,0);
if(check_reach
(ox1
,oy1
,ox2
,oy2
,train
[id
].
check) == 0 )
{
flag
[track
]=0;
geninfo
(id
);
track
= train
[id
].
tracknum;
src
= train
[id
].
source;
des
= train
[id
].
desti;
/*ox1 =graph[src][0];
ox2 =graph[des][0];
oy1 = graph[src][1];
oy2 = graph[des][1];*/
ox1
=source
[src
][0];
ox2
=destination
[des
][0];
oy1
= source
[src
][1];
oy2
= destination
[des
][1];
traininfo
[id
].
source=src
;
traininfo
[id
].
destination=des
;
traininfo
[id
].
track= track
;
traininfo
[id
].
cx= ox1
;
traininfo
[id
].
cy= oy1
;
// grx_text(" in the fly",15,20,10,14);
dy
= oy2
- oy1
;
dx
= ox2
- ox1
;
if (dy
< 0) { dy
= -dy
; stepy
= -1; } else { stepy
= 1; }
if (dx
< 0) { dx
= -dx
; stepx
= -1; } else { stepx
= 1; }
dy
<<= 1; // dy is now 2*dy
dx
<<= 1; // dx is now 2*dx
break;
//src = des;
}
if (fraction
>= 0)
{
oy1
+= stepy
* cur_speed
;
fraction
-= dx
; // same as fraction -= 2*dx
}// if
ox1
+= stepx
* cur_speed
;
fraction
+= dy
; // same as fraction -= 2*dy
traininfo
[id
].
cx= ox1
;
traininfo
[id
].
cy= oy1
;
grx_line
(source
[src
][0],source
[src
][1],destination
[des
][0],destination
[des
][1],5);
grx_disc
(ox1
,oy1
,2,cur_col
);
task_endcycle
();
continue;
}//while
}// main if (dx > dy)
else
{
int fraction
= dx
- (dy
>> 1);
sem_wait
(&mx_track
[track
]);
if(flag
[track
]==1)
{
sem_post
(&mx_track
[track
]);
task_endcycle
();
//continue;
}
else if(flag
[track
]==0)
{
flag
[track
]=1;
sem_post
(&mx_track
[track
]);
}
while (src
!=des
)
{
grx_disc
(ox1
,oy1
,2,0);
if(check_reach
(ox1
,oy1
,ox2
,oy2
,train
[id
].
check) == 0)
{
flag
[track
]=0;
geninfo
(id
);
track
= train
[id
].
tracknum;
src
= train
[id
].
source;
des
= train
[id
].
desti;
ox1
=source
[src
][0];
ox2
=destination
[des
][0];
oy1
= source
[src
][1];
oy2
= destination
[des
][1];
traininfo
[id
].
source=src
;
traininfo
[id
].
destination=des
;
traininfo
[id
].
track= track
;
traininfo
[id
].
cx= ox1
;
traininfo
[id
].
cy= oy1
;
//grx_text(" in the fly",15,20,10,14);
dy
= oy2
- oy1
;
dx
= ox2
- ox1
;
if (dy
< 0) { dy
= -dy
; stepy
= -1; } else { stepy
= 1; }
if (dx
< 0) { dx
= -dx
; stepx
= -1; } else { stepx
= 1; }
dy
<<= 1; // dy is now 2*dy
dx
<<= 1; // dx is now 2*dx
break;
}
if (fraction
>= 0) {
ox1
+= stepx
* cur_speed
;
fraction
-= dy
;
}
oy1
+= stepy
* cur_speed
;
fraction
+= dx
;
traininfo
[id
].
cx= ox1
;
traininfo
[id
].
cy= oy1
;
grx_line
(source
[src
][0],source
[src
][1],destination
[des
][0],destination
[des
][1],5);
grx_disc
(ox1
,oy1
,2,cur_col
);
task_endcycle
();
continue;
}// while
}
}// else
task_endcycle
();
}// outer while
}// main
/****************************************************************/
/* This function is called when the system exits */
void byebye
(void *arg
)
{
grx_close
();
cprintf
("!!!!!!!!!!!!!!!!HAVE A NICE DAY !!!!!!!!!!!\n");
}
/****************************** MAIN ******************************/
int main
(int argc
, char **argv
)
{
HARD_TASK_MODEL m
;
SOFT_TASK_MODEL mp
;
int j
,sg
,val
;
char c
; /* character from keyboard */
int i
= 0; /* number of tasks created */
TIME seme
; /* used to init the random seed */
char temp1
[200];
/* Set the closing function */
sys_atrunlevel
(byebye
, NULL
, RUNLEVEL_BEFORE_EXIT
);
/* graphic card Initialization */
if (grx_init
() < 1) {
sys_abort
(1);
}
if (grx_open
(1024,768, 8) < 0) {
cprintf
("GRX Err\n");
sys_abort
(1);
}
for(i
=0;i
<MAX_TRACK
;i
++)
sem_init
(&mx_track
[i
],0,1);
for(i
=0;i
<MAX_TRACK
;i
++)
flag
[i
]=0;
/* The scenario */
grx_rect
(XMIN
-D
-1, YMIN
-D
-1, XMAX
+D
+1, YMAX
+D
+1, 14);
grx_text
("Simulation of Train Status", XMIN
, YMENU
+10, 13, 0);
grx_text
("SPACE create a Train" , XMIN
, YMENU
+20, 12, 0);
grx_text
("ESC exit to DOS" , XMIN
, YMENU
+30, 12, 0);
init_display
();
soft_task_default_model
(mp
);
soft_task_def_level
(mp
,1);
soft_task_def_ctrl_jet
(mp
);
soft_task_def_arg
(mp
, (void *)count
);
// soft_task_def_group(mp, FLYGROUP);
soft_task_def_met
(mp
, S_fly_wcet
);
soft_task_def_period
(mp
,S_fly_period
);
soft_task_def_usemath
(mp
);
controller
= task_create
("controller",controls
,&mp
,NULL
);
task_activate
(controller
);
if (controller
== NIL
) {
grx_close
();
perror("Could not create controller ");
sys_abort
(1);
}
//grx_text(" contoller here ",15,30,10,14);
/* The program waits a space to create a fly */
c
= keyb_getch
(BLOCK
);
/* randomize!!!! */
seme
= sys_gettime
(NULL
);
srand(seme
);
do {
if (c
== ' ') {
//itoa(count,train_str+5);
if(count
== 0){
param.
id = count
;
param.
col = RED
;
hard_task_default_model
(m
);
hard_task_def_ctrl_jet
(m
);
hard_task_def_arg
(m
, (void *)count
);
hard_task_def_wcet
(m
, H_fly_wcet
);
hard_task_def_mit
(m
, H_fly_period
);
hard_task_def_group
(m
, FLYGROUP
);
hard_task_def_usemath
(m
);
//pid = task_create("fly", fly, &m, NULL);
ptas_for_train
[count
] = task_create
("train",fly
,&m
,NULL
);
}
else{
param.
id = count
;
param.
col = 15;
soft_task_default_model
(mp
);
soft_task_def_level
(mp
,1);
soft_task_def_ctrl_jet
(mp
);
soft_task_def_arg
(mp
, (void *)count
);
soft_task_def_group
(mp
, FLYGROUP
);
soft_task_def_met
(mp
, S_fly_wcet
);
soft_task_def_period
(mp
,S_fly_period
);
soft_task_def_usemath
(mp
);
ptas_for_train
[count
] = task_create
("train",fly
,&mp
,NULL
);
// grx_text(" soft returned ",15,30,10,14);
}
i
++;
if (ptas_for_train
[count
] == NIL
) {
grx_close
();
perror("Could not create task <train>");
sys_abort
(1);
}
task_activate
(ptas_for_train
[count
]);
sprintf(temp1
," PID = %d ",ptas_for_train
[count
]);
grx_text
(temp1
,75,40,15,12);
count
++;
if (count
== 1) {
start
= 1;
}
}
c
= keyb_getch
(BLOCK
);
} while (c
!= ESC
);
sys_end
();
return 0;}