Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1664 pj 1
#ifndef _PLANE_HEADER_
2
#include "plane.h"
3
#endif
4
 
5
#include <modules/cabs.h>
6
 
7
#define PLANEINFODIST 200
8
#define MAXPLANEINFO 5
9
 
10
sem_t   mutex_i;        /* mutex to protect i */
11
int  i = 0;             /* number of tasks created      */
12
int reg_tasks[MAX_PLANES];
13
int num=0;
14
 
15
void  draw_plane(int x, int y, int c, int id, int p_size)
16
{
17
        char temp[3];
18
        sprintf(temp,"%d",id);
19
        sem_wait(&graphics_mutex);
20
        grx_circle(x, y, p_size,c);
21
        grx_text(temp,x-p_size/2, y-p_size/2,black,c);
22
        sem_post(&graphics_mutex);
23
}
24
 
25
void i_plus_1(void)
26
{
27
    sem_wait(&mutex_i);
28
    i++;
29
    sem_post(&mutex_i);
30
}
31
 
32
int get_i(void)
33
{
34
    int j;
35
 
36
    sem_wait(&mutex_i);
37
    j = i;
38
    sem_post(&mutex_i);
39
 
40
    return j;
41
}
42
 
43
TASK plane(void *arg)
44
{
45
        int ox, oy, fx,fy,dx,dy;
46
        int stepx, stepy;
47
        int planeid = (int)(arg);
48
        int plane_size;
49
        int SCALING_FACTOR;
50
//      char temp1[3];
51
//      char *tempcabptr;
52
 
53
        int initx,inity;
54
        int fdestx,fdesty;
55
        int reached=0;
56
 
57
        char *cab_ptr;
58
        //char *atc_cab_ptr;
59
 
60
        ox = airports[aeroplanes[planeid].src_airport_id].x;
61
        oy = airports[aeroplanes[planeid].src_airport_id].y;
62
        fx = airports[aeroplanes[planeid].dest_airport_id].x;
63
        fy = airports[aeroplanes[planeid].dest_airport_id].y;
64
 
65
        plane_size = planeinfo[aeroplanes[planeid].model_id].planesize;
66
 
67
        atcplanecabmsg[planeid].written='N';
68
        atcplanecabmsg[planeid].xtemp=fx;
69
        atcplanecabmsg[planeid].ytemp=fy;
70
 
71
        //tempcabptr=cab_reserve(atc_plane_cab_id[planeid]);
72
        //memcpy(tempcabptr,&atcplanecabmsg[planeid],sizeof(atcplanecab));
73
        //cab_putmes(atc_plane_cab_id[planeid],tempcabptr);
74
 
75
        initx=ox;
76
        inity=oy;
77
        fdestx=fx;
78
        fdesty=fy;
79
 
80
        draw_plane(ox, oy, red,planeid,plane_size);
81
        draw_plane(fx, fy, blue,planeid,plane_size);
82
        int fraction;
83
 
84
        //SCALING_FACTOR = (int)(aeroplanes[planeid].curr_speed/SCALE_DIST);
85
        SCALING_FACTOR=1;
86
 
87
        while(1)
88
        {
89
                dy = fy - oy;
90
                dx = fx - ox;
91
 
92
                if (dy < 0) { dy = -dy;  stepy = -1*SCALING_FACTOR;} else { stepy = 1*SCALING_FACTOR;}
93
                if (dx < 0) { dx = -dx;  stepx = -1*SCALING_FACTOR;} else { stepx = 1*SCALING_FACTOR;}
94
                dy <<= 1;                                                  // dy is now 2*dy
95
                dx <<= 1;                                                  // dx is now 2*dx
96
 
97
                if (dx > dy)
98
                {
99
                        fraction = dy - (dx >> 1);                   // same as 2*dy - dx
100
                        while (ox != fx)
101
                        {
102
                                if (fdestx-ox<PLANE_AIRPORT_DIST)
103
                                {
104
                                        aeroplanes[planeid].curr_speed=aeroplanes[planeid].curr_speed-aeroplanes[planeid].curr_speed/6;  //to reduce the speed of the aeroplane while landing
105
                                        if (abs(stepy)>1)
106
                                                stepy=stepy/SCALING_FACTOR;
107
                                        if (abs(stepx)>1)
108
                                                stepx=stepx/SCALING_FACTOR;
109
                                }
110
                                else if(abs(fx-ox)<10)
111
                                {
112
                                        if (abs(stepy)>1)
113
                                        stepy=stepy/SCALING_FACTOR;
114
                                        if (abs(stepx)>1)
115
                                        stepx=stepx/SCALING_FACTOR;
116
                                }
117
/*
118
                                atc_cab_ptr=cab_getmes(atc_plane_cab_id[planeid]);
119
                                memcpy(&atcplanecabmsg[planeid],atc_cab_ptr,sizeof(atcplanecab));
120
                                cab_unget(atc_plane_cab_id[planeid],atc_cab_ptr);
121
 
122
                                sprintf(temp1,"Written:%c Xtemp:%d YTemp:%d",atcplanecabmsg[planeid].written,atcplanecabmsg[planeid].xtemp,atcplanecabmsg[planeid].ytemp);
123
 
124
                                grx_text(temp1,100,120,white,black);
125
                                if (atcplanecabmsg[planeid].written == 'Y')
126
                                {
127
                                        aeroplanes[planeid].xtemp=atcplanecabmsg[planeid].xtemp;
128
                                        aeroplanes[planeid].ytemp=atcplanecabmsg[planeid].ytemp;
129
                                }
130
 
131
        */
132
 
133
                                if (aeroplanes[planeid].xtemp!=fdestx || aeroplanes[planeid].ytemp!=fdesty)  //read from CAB on which ATC writes
134
                                {
135
                                        fx = aeroplanes[planeid].xtemp;   //read from CAB
136
                                        fy = aeroplanes[planeid].ytemp;  //read from CAB on which ATC writes
137
                                        aeroplanes[planeid].xtemp=fdestx;
138
                                        aeroplanes[planeid].ytemp=fdesty;
139
                                        break;
140
                                }
141
 
142
                                draw_plane(ox, oy, black,planeid,plane_size);  //to redraw the area on which the plane flew
143
 
144
                                if (fraction >= 0)
145
                                {
146
                                        oy += stepy;
147
                                        fraction -= dx;                     // same as fraction -= 2*dx
148
                                }
149
                                ox += stepx;
150
                                fraction += dy;                       // same as fraction -= 2*dy
151
 
152
                                draw_plane(ox, oy, red,planeid,plane_size);  //to draw the new plane
153
 
154
                                aeroplanes[planeid].currx = ox;  //write this data to the CAB for ATC to read
155
                                aeroplanes[planeid].curry = oy;  //write this data to the CAB for ATC to read
156
 
157
                                cab_ptr=cab_reserve(plane_atc_cab_id[planeid]);
158
                                memcpy(cab_ptr,&aeroplanes[planeid],sizeof(aeroplane));
159
                                cab_putmes(plane_atc_cab_id[planeid],cab_ptr);
160
 
161
                                task_endcycle();
162
                        }
163
                        if (ox==fx)
164
                        {
165
                                if (fx==fdestx && fy==fdesty)
166
                                        reached=1;
167
                                else
168
                                {
169
                                        reached=0;
170
                                        fx=fdestx;
171
                                        fy=fdesty;
172
                                }
173
                        }
174
 
175
                }
176
                else
177
                {
178
                        int fraction = dx - (dy >> 1);
179
                        while (oy != fy)
180
                        {
181
                                if (fdestx-ox<PLANE_AIRPORT_DIST)
182
                                {
183
                                        aeroplanes[planeid].curr_speed=aeroplanes[planeid].curr_speed-aeroplanes[planeid].curr_speed/6;  //write on CAB for ATC to read
184
                                        if (abs(stepy)>1)
185
                                                stepy=stepy/SCALING_FACTOR;
186
                                        if (abs(stepx)>1)
187
                                                stepx=stepx/SCALING_FACTOR;
188
                                }
189
                                else if (abs(fx-ox)<10)
190
                                {
191
                                        if (abs(stepy)>1)
192
                                                stepy=stepy/SCALING_FACTOR;
193
                                        if (abs(stepx)>1)
194
                                                stepx=stepx/SCALING_FACTOR;
195
                                }
196
/*
197
                                atc_cab_ptr=cab_getmes(atc_plane_cab_id[planeid]);
198
                                memcpy(&atcplanecabmsg[planeid],atc_cab_ptr,sizeof(atcplanecab));
199
                                cab_unget(atc_plane_cab_id[planeid],atc_cab_ptr);
200
 
201
                                sprintf(temp1,"Written:%c Xtemp:%d YTemp:%d",atcplanecabmsg[planeid].written,atcplanecabmsg[planeid].xtemp,atcplanecabmsg[planeid].ytemp);
202
                                grx_text(temp1,100,130,white,black);
203
 
204
                                if (atcplanecabmsg[planeid].written == 'Y')
205
                                {
206
                                        aeroplanes[planeid].xtemp=atcplanecabmsg[planeid].xtemp;
207
                                        aeroplanes[planeid].ytemp=atcplanecabmsg[planeid].ytemp;
208
                                }
209
        */
210
                        //take mutex to access this structure
211
                                if (aeroplanes[planeid].xtemp!=fdestx || aeroplanes[planeid].ytemp!=fdesty) //read from CAB on which ATC writes
212
                                {
213
                                        fx = aeroplanes[planeid].xtemp; //read from CAB on which ATC writes
214
                                        fy = aeroplanes[planeid].ytemp; //read from CAB on which ATC writes
215
                                        aeroplanes[planeid].xtemp=fdestx;
216
                                        aeroplanes[planeid].ytemp=fdesty;
217
                                        break;
218
                                }
219
 
220
                                draw_plane(ox, oy, black,planeid,plane_size);
221
 
222
                                if (fraction >= 0)
223
                                {
224
                                ox += stepx;
225
                                fraction -= dy;
226
                                }
227
 
228
                                oy += stepy;
229
                                fraction += dx;
230
                                draw_plane(ox, oy, red,planeid,plane_size);
231
 
232
                                aeroplanes[planeid].currx = ox;   //write on CAB fo ATC to read
233
                                aeroplanes[planeid].curry = oy;   //write on CAB fo ATC to read
234
 
235
                                cab_ptr=cab_reserve(plane_atc_cab_id[planeid]);
236
                                memcpy(cab_ptr,&aeroplanes[planeid],sizeof(aeroplane));
237
                                cab_putmes(plane_atc_cab_id[planeid],cab_ptr);
238
 
239
                                task_endcycle();
240
                        }
241
                        if (oy==fy)
242
                        {
243
                                if (fx==fdestx && fy==fdesty)
244
                                        reached=1;
245
                                else
246
                                {
247
                                        reached=0;
248
                                        fx=fdestx;
249
                                        fy=fdesty;
250
                                }
251
                        }
252
                }
253
                if (reached==1)
254
                        break;
255
        }
256
 
257
        aeroplanes[planeid].is_flying=0;  //write this, currx and curry on CAb for ATC to read to
258
 
259
        cab_ptr=cab_reserve(plane_atc_cab_id[planeid]);
260
        memcpy(cab_ptr,&aeroplanes[planeid],sizeof(aeroplane));
261
        cab_putmes(plane_atc_cab_id[planeid],cab_ptr);
262
 
263
        mutex_lock(&mut_number_of_planes);
264
        number_of_planes--;
265
        if (number_of_planes==0)
266
        {
267
                mutex_unlock(&mut_number_of_planes);
268
                mutex_lock(&mut_STARTSIM);
269
                if (STARTSIM == 1)
270
                        STARTSIM = 0;
271
                mutex_unlock(&mut_STARTSIM);
272
        }
273
        else
274
        {
275
                mutex_unlock(&mut_number_of_planes);
276
        }
277
}
278
 
279
TASK register_with_ATC(void *arg)
280
{
281
        int myid=(int)(arg);
282
 
283
        while(1)
284
        {
285
                if (aeroplanes[myid].is_flying==0)
286
                {
287
                        break;
288
                }
289
                aeroplanes[myid].zone_id=floor((aeroplanes[myid].currx-XMIN) / GRID_SIZE) + 4 * floor((aeroplanes[myid].curry-YMIN) / GRID_SIZE);
290
                task_endcycle();
291
        }
292
}
293
 
294
TASK display_plane_info(void *arg)
295
{
296
        int tempid;
297
        int tempi;
298
        char displayinf[25];
299
        char display[MAX_PLANES];
300
        grx_text("AEROPLANE INFORMATION",XMIN,YMAX+2, white, black);
301
        grx_line(XMIN, YMAX+12, XMAX, YMAX+12, blue);
302
 
303
        aeroplane temp_aeroplanes[MAX_PLANES];
304
        char *plane_cab_ptr[MAX_PLANES];
305
 
306
        for(tempid=0;tempid<MAX_PLANES;tempid++)
307
        {
308
                display[tempid]='T';
309
        }
310
 
311
        while(1)
312
        {
313
                tempi=get_i();
314
                sem_wait(&graphics_mutex);
315
                for(tempid=0;tempid<tempi;tempid++)
316
                {
317
                        plane_cab_ptr[tempid]=cab_getmes(plane_atc_cab_id[tempid]);
318
                        memcpy(&temp_aeroplanes[tempid],plane_cab_ptr[tempid],sizeof(aeroplane));
319
                        cab_unget(plane_atc_cab_id[tempid],plane_cab_ptr[tempid]);
320
 
321
                        if (temp_aeroplanes[tempid].is_flying==1 && display[tempid] =='T')
322
                        {
323
                                sprintf(displayinf,"Plane Id   : %d  ",temp_aeroplanes[tempid].id);
324
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+20, white, black);
325
 
326
                                sprintf(displayinf,"Model      : %s  ",planeinfo[temp_aeroplanes[tempid].model_id].model);
327
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+30, white, black);
328
 
329
                                sprintf(displayinf,"Src Airport: %s  ",airports[temp_aeroplanes[tempid].src_airport_id].airportname);
330
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+40, white, black);
331
 
332
                                sprintf(displayinf,"Dst Airport: %s  ",airports[temp_aeroplanes[tempid].dest_airport_id].airportname);
333
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+50, white, black);
334
 
335
                                sprintf(displayinf,"Dept Time  : %d  ",temp_aeroplanes[tempid].dept_time);
336
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+60, white, black);
337
 
338
                                sprintf(displayinf,"Arr Time   : %d  ",temp_aeroplanes[tempid].arr_time);
339
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+70, white, black);
340
 
341
                                sprintf(displayinf,"Curr Speed : %d  ",temp_aeroplanes[tempid].curr_speed);
342
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+80, white, black);
343
 
344
                                sprintf(displayinf,"Curr X     : %d  ",temp_aeroplanes[tempid].currx);
345
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+90, white, black);
346
 
347
                                sprintf(displayinf,"Curr Y     : %d  ",temp_aeroplanes[tempid].curry);
348
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+100, white, black);
349
 
350
                                sprintf(displayinf,"Is Flying  : %d  ",temp_aeroplanes[tempid].is_flying);
351
                                grx_text(displayinf,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+110, white, black);
352
                        }
353
                        else if (temp_aeroplanes[tempid].is_flying==0 && display[tempid] =='T' )
354
                        {
355
                                grx_box(((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN,YMAX+20,((tempid%MAXPLANEINFO)*PLANEINFODIST)+XMIN+PLANEINFODIST,YMAX+120,black);
356
                                display[tempid]='F';
357
                        }
358
                }
359
                sem_post(&graphics_mutex);
360
                task_endcycle();
361
        }
362
}
363
 
364
void init_planes()
365
{
366
        char c;             /* character from keyboard      */
367
        number_of_planes= 0;     /* number of tasks created      */
368
 
369
        TIME temptime,temptime2;
370
        struct timespec planetime;
371
        int src,dest;
372
        int ox,oy,fx,fy;
373
        int planeid;
374
 
375
        SOFT_TASK_MODEL m;  //model for the TASK to plane to move
376
        SOFT_TASK_MODEL reg_msg;   //model for the TASK to register with ATC
377
        SOFT_TASK_MODEL disp_info;  //model for the TASK to display the information of the planes
378
 
379
        int local_i=0;        /* local copy of i */
380
        sem_init(&mutex_i,0,1);
381
        int local_no_of_planes=0;
382
 
383
 
384
        /* The program waits a space to create a plane */
385
        c = keyb_getch(BLOCK);
386
 
387
        //activate the display plane info TASK
388
        soft_task_default_model(disp_info);
389
        soft_task_def_level(disp_info,1);
390
        soft_task_def_ctrl_jet (disp_info);
391
        //soft_task_def_arg (disp_info, (void *)local_i);
392
        soft_task_def_met (disp_info, DISPPLANEINFOWCET);
393
        soft_task_def_group (disp_info, PLANEGROUP);  //modify this later
394
        soft_task_def_period (disp_info, DISPPLANEINFOPERIOD);
395
        soft_task_def_usemath (disp_info);
396
        disp = task_create("DspPlane", display_plane_info, &disp_info, NULL);
397
        if (disp == NIL)
398
        {
399
                grx_close();
400
                perror("Could not create task <RegWithATC>");
401
                sys_abort(1);
402
        }
403
        task_activate(disp);
404
 
405
        do {
406
                planeid = local_i = get_i();
407
 
408
                mutex_lock(&mut_number_of_planes);
409
                local_no_of_planes=number_of_planes;
410
                mutex_unlock(&mut_number_of_planes);
411
 
412
                if ((c == ' ') && (local_i < MAX_PLANES) && (local_no_of_planes<MAX_SIMUL_PLANES))
413
                {
414
 
415
                        srand(sys_gettime(NULL));
416
                        src = (int)rand()%(NUM_OF_AIRPORTS);      // generate random source and destination airports
417
                                //check that the previous 2 aeroplanes have not started from the same airport
418
                        dest = (int)rand()%(NUM_OF_AIRPORTS);
419
                        if (dest==src)
420
                        {
421
                                if (dest==0)
422
                                        dest=NUM_OF_AIRPORTS-1;
423
                                else
424
                                        dest=(dest+src)%NUM_OF_AIRPORTS;
425
                        }
426
 
427
                        ox = airports[src].x;
428
                        oy = airports[src].y;
429
                        fx = airports[dest].x;
430
                        fy = airports[dest].y;
431
 
432
                        temptime = sys_gettime(&planetime);
433
                        temptime = ((planetime.tv_sec / TIME_SPEED) % 24) * 100 ;
434
 
435
                        //populate the aeroplane structure with data
436
                        aeroplanes[planeid].id = planeid;
437
                        aeroplanes[planeid].model_id = planeid%NUM_OF_AEROPLANE_MODELS;
438
                        aeroplanes[planeid].src_airport_id = airports[src].id;
439
                        aeroplanes[planeid].dest_airport_id  = airports[dest].id;
440
                        aeroplanes[planeid].xtemp=fx;
441
                        aeroplanes[planeid].ytemp=fy;
442
                        aeroplanes[planeid].curr_speed=planeinfo[aeroplanes[planeid].model_id].max_speed;
443
                        temptime2 = temptime + ((sqrt((fy-oy)*(fy-oy)+(fx-ox)*(fx-ox))*SCALE_DIST) / aeroplanes[planeid].curr_speed);
444
                        aeroplanes[planeid].curr_altitude=(int)((rand()%1)*planeinfo[aeroplanes[planeid].model_id].max_altitude);
445
                        aeroplanes[planeid].dept_time = temptime;
446
                        aeroplanes[planeid].arr_time = temptime2;
447
                        aeroplanes[planeid].is_flying=1;
448
 
449
                        //create the CABS
450
                        plane_atc_cab_id[planeid]= cab_create(plane_atc[planeid],sizeof(aeroplane),2);  //this cab will be used by the plane task to write in. The ATC will read from this task
451
        //              atc_plane_cab_id[planeid]= cab_create(atc_plane[planeid],sizeof(atcplanecab),2);
452
//this cab will be used by the ATC to write new coordinates to the plane
453
                        soft_task_default_model(m);
454
                        soft_task_def_level(m,1);
455
                        soft_task_def_ctrl_jet (m);
456
                        soft_task_def_arg (m, (void *)local_i);
457
                        soft_task_def_met (m, PLANEWCET);
458
                        soft_task_def_period (m, PLANEPERIOD);
459
                        soft_task_def_group (m, PLANEGROUP);
460
                        soft_task_def_usemath (m);
461
                        pid[local_i] = task_create("Plane", plane, &m, NULL);
462
                        if (pid[local_i] == NIL)
463
                        {
464
                                grx_close();
465
                                perror("Could not create task <plane>");
466
                                sys_abort(1);
467
                        }
468
 
469
                        task_activate(pid[local_i]);
470
 
471
                        //activate the register message TASK
472
                        soft_task_default_model(reg_msg);
473
                        soft_task_def_level(reg_msg,1);
474
                        soft_task_def_ctrl_jet (reg_msg);
475
                        soft_task_def_arg (reg_msg, (void *)local_i);
476
                        soft_task_def_met (reg_msg, REGWITHATCWCET);
477
                        soft_task_def_group (reg_msg, PLANEGROUP);  //modify this later
478
                        soft_task_def_period (reg_msg, REGWITHATCPERIOD);
479
                        soft_task_def_usemath (reg_msg);
480
                        pid2 = task_create("RegToATC", register_with_ATC, &reg_msg, NULL);
481
                        if (pid2 == NIL)
482
                        {
483
                                grx_close();
484
                                perror("Could not create task <RegToATC>");
485
                                sys_abort(1);
486
                        }
487
                        task_activate(pid2);
488
                        i_plus_1();
489
 
490
                        mutex_lock(&mut_number_of_planes);
491
                        ++number_of_planes;
492
                        mutex_unlock(&mut_number_of_planes);
493
 
494
                        if (local_i+1>0)
495
                        {
496
                                mutex_lock(&mut_STARTSIM);
497
                                STARTSIM = 1;
498
                                mutex_unlock(&mut_STARTSIM);
499
                        }
500
 
501
                }
502
                c = keyb_getch(BLOCK);
503
        } while (c != ESC);
504
        sys_end();
505
}