/* * Sheila McKain * Dave Hess * Assignment #2 * Realtime Systems * * 2 -red * 1 -white * 3 -green * 5 -yellow * 6 -blue * */ #include #include #include #include #include #include "sem.c" #define LEFT #define WS_ID 1 /* PHIGS Workstation ID Number */ #define STRUCT_ID 129 /* PHIGS Structure ID Number */ #define SIGN1 130 #define SIGN2 131 #define TEXT1 132 #define TEXT2 133 #define TEXT3 134 #define TEXT4 135 #define TEXT5 136 #define TEXT6 137 #define GREEN1 138 #define GREEN2 139 #define RED1 140 #define RED2 141 #define GO1 142 #define GO2 143 #define WAIT1 144 #define WAIT2 145 #define GO3 146 #define GO4 147 #define RTTALLY 148 #define LFTALLY 149 #define HEADER 150 Pmatrix3 rt_matrix,lf_matrix; Pvector3 rt_vector,lf_vector; Pint error_ind; Ppoint text_pts20; Ppoint text_pts21; Ppoint text_pts30; Ppoint text_pts31; Ppoint text_pts32; Ppoint text_pts40; Ppoint text_pts41; Ppoint text_pts42; Ptxalign text_align; static int priority = 0.0; int loop; int PHIGwait[2]; struct stat x; action(); main() { key_t semkey = 0x200; int arvrt[2],arvlf[2],deprt[2],deplf[2],randrt[2],randlf[2]; int PHIGarvlf[2], PHIGarvrt[2],PHIGdeplf[2],PHIGdeprt[2]; int PHIGscan[2],PHIGrtn[2]; int pidRBC,pidLBC,pidSCAN,pidrt,pidlf,pidPHIG,semid; int n,car_num; int num_car_rt = 0; int num_car_lf = 0; char rtbuff[3],lfbuff[3],carbuff[3]; char *s = "0"; char strbuff[5]; char rttally[3][21],lftally[3][21]; int z,irtfront,irtrear,ilffront,ilfrear,jrtfront,jrtrear,jlffront,jlfrear; Ppoint text_pts; Ppoint text_pts2; Ppoint text_pts3; Ppoint text_pts4; Ppoint text_pts5; Ppoint text_pts6; Ppoint text_pts11; text_pts.x = 0.17; text_pts.y = 0.765; text_pts2.x = 0.63; text_pts2.y = 0.765; text_pts3.x = 0.20; text_pts3.y = 0.52; text_pts4.x = 0.73; text_pts4.y = 0.52; text_pts5.x = 0.20; text_pts5.y = 0.52; text_pts6.x = 0.73; text_pts6.y = 0.52; text_pts11.x = 0.73; text_pts11.y = 0.81; text_pts20.x = 0.24; text_pts20.y = 0.81; text_pts21.x = 0.72; text_pts21.y = 0.81; text_align.hor = PAH_LEFT; text_align.ver = PAV_BOTTOM; text_pts30.x = 0.06; text_pts30.y = 0.94; text_pts31.x = 0.06; text_pts31.y = 0.92; text_pts32.x = 0.06; text_pts32.y = 0.90; text_pts40.x = 0.64; text_pts40.y = 0.94; text_pts41.x = 0.64; text_pts41.y = 0.92; text_pts42.x = 0.64; text_pts42.y = 0.90; /* Open PHIGS ---- DIFFERENT FROM BOOK - RCC */ popenphigs((Pchar*)NULL, PDEFAULT_MEM_SIZE); popenws(WS_ID, (Pconnid)NULL, phigs_ws_type_sun_tool); psetdisplayupdatest(WS_ID,PWAIT,PUQUM); if(pipe(arvrt) < 0) { perror("pipe arvrt fail"); } if(pipe(arvlf) < 0) { perror("pipe arvlf fail"); } if(pipe(deprt) < 0) { perror("pipe deprt fail"); } if(pipe(deplf) < 0) { perror("pipe deplf fail"); } if(pipe(randrt) < 0) { perror("pipe randrt fail"); } if(pipe(randlf) < 0) { perror("pipe randlf fail"); } if(pipe(PHIGarvlf) < 0) { perror("pipe PHIGarvlf fail"); } if(pipe(PHIGarvrt) < 0) { perror("pipe PHIGarvrt fail"); } if(pipe(PHIGdeplf) < 0) { perror("pipe PHIGdeplf fail"); } if(pipe(PHIGdeprt) < 0) { perror("pipe PHIGdeprt fail"); } if(pipe(PHIGscan) < 0) { perror("pipe PHIGscan fail"); } if(pipe(PHIGrtn) < 0) { perror("pipe PHIGrtn fail"); } if(pipe(PHIGwait) < 0) { perror("pipe PHIGwait fail"); } /* Draw the bridge */ popenstruct(STRUCT_ID); create_polyline_attributes(); create_bridge(); create_yellow_lines(); pclosestruct(); ppoststruct(WS_ID,STRUCT_ID,priority); /* Write words */ popenstruct(TEXT1); psettextfont(PFONT_DUPLEX); psetcharheight(0.015); ptext(&text_pts, "Controls Left Traffic"); pclosestruct(); ppoststruct(WS_ID, TEXT1, 0.0); popenstruct(TEXT2); psettextfont(PFONT_DUPLEX); psetcharheight(0.015); ptext(&text_pts2, "Controls Right Traffic"); pclosestruct(); ppoststruct(WS_ID, TEXT2, 0.0); /* Set up the Right and Left Waiting Signs */ popenstruct(WAIT1); Lft_Cars_Wait(); plabel(1); psetcharheight(0.03); ptext(&text_pts20,s); pclosestruct(); ppoststruct(WS_ID,WAIT1,priority); popenstruct(WAIT2); Rt_Cars_Wait(); plabel(1); psetcharheight(0.03); ptext(&text_pts21,s); pclosestruct(); ppoststruct(WS_ID,WAIT2,priority); /* Left redlight */ popenstruct(SIGN1); create_sign1(); pclosestruct(); ppoststruct(WS_ID,SIGN1,priority); /* green light of left sign */ popenstruct(GREEN1); create_green_sign1(); pclosestruct(); ppoststruct(WS_ID,GREEN1,priority); /* red light of left sign */ popenstruct(RED1); create_red_sign1(); pclosestruct(); ppoststruct(WS_ID,RED1,priority); /* green light of RIGHT sign */ popenstruct(GREEN2); create_green_sign2(); pclosestruct(); ppoststruct(WS_ID,GREEN2,priority); /* redlight of right sign */ popenstruct(RED2); create_red_sign2(); pclosestruct(); ppoststruct(WS_ID,RED2,priority); /* Right redlight */ popenstruct(SIGN2); create_sign2(); pclosestruct(); ppoststruct(WS_ID,SIGN2,priority); /* Car Waiting Queues */ init_lf_tally(); init_rt_tally(); /* Title */ header(); /* Init the red lights */ Init_lt_light(); Init_rt_light(); if((semid = initsem(semkey))==-1) perror("semaphore exists"); /********************** Beginning Of Forked Tasks *************************/ /* Update PHIGS Task -- collect all PHIG update requests from other tasks */ if((pidPHIG = fork()) == -1) { perror("fork RBC fail"); exit(1); } if(pidPHIG == 0) { printf("I am upPHIG child\n"); close(PHIGarvlf[1]); close(PHIGdeplf[0]); close(PHIGarvrt[1]); close(PHIGdeprt[0]); close(PHIGrtn[0]); close(PHIGscan[1]); close(PHIGwait[1]); /* initialize car waiting queues */ for(irtfront = 0;irtfront < 3;irtfront++) for(irtrear = 0;irtrear < 20;irtrear++) { rttally[irtfront][irtrear] = ' '; lftally[irtfront][irtrear] = ' '; } rttally[0][20] = rttally[1][20] = rttally[2][20] = '\0'; lftally[0][20] = lftally[1][20] = lftally[2][20] = '\0'; irtfront=irtrear=ilffront=ilfrear=jrtfront=jrtrear=jlffront=jlfrear=0; while(1) { /* poll PHIG update pipes from RBC,LBC & SCAN tasks */ if(fstat(PHIGarvrt[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { read(PHIGarvrt[0],carbuff,3); /* communications with RBC task */ switch(carbuff[0]) { case '1' : /* turn rt grn light on */ Chng_rl_to_green(); break; case '2' : /* move a car across bridge rt side */ car_num = carbuff[1]; move_car_rt(car_num); /* car departing bridge trip sensor */ write(PHIGdeprt[1],carbuff,3); punpoststruct(WS_ID,car_num); pdelstruct(car_num); /* delete car from front of rt car waiting queue */ z = irtfront; subtally(rttally,&irtfront,&jrtfront); update_rt_tally(rttally,z); break; case '3' : /* turn rt red light on */ Chng_rl_to_red(); break; } } if(fstat(PHIGarvlf[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { read(PHIGarvlf[0],carbuff,3); /* communications with LBC task */ switch(carbuff[0]) { case '1' : /* turn left grn light on */ Chng_ll_to_green(); break; case '2' : /* move a car across bridge lf side */ car_num = carbuff[1]; move_car_lf(car_num); /* car departing bridge trip sensor */ write(PHIGdeplf[1],carbuff,3); punpoststruct(WS_ID,car_num); pdelstruct(car_num); /* delete car from front of lf car waiting queue */ z = ilffront; subtally(lftally,&ilffront,&jlffront); update_lf_tally(lftally,z); break; case '3' : /* turn left red light on */ Chng_ll_to_red(); break; } } if(fstat(PHIGscan[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { read(PHIGscan[0],carbuff,3); /* communications with SCAN task */ switch(carbuff[0]) { case '1' : /* create a car at rt side */ itoa(carbuff[1],strbuff); rt_vector.x = 0.0; rt_vector.y = 0.0; rt_vector.z = 0.0; ptranslate3(&rt_vector,&error_ind,rt_matrix); popenstruct(carbuff[1]); plabel(1); psetlocaltran3(rt_matrix,PREPLACE); create_car_rt(strbuff); pclosestruct(); ppoststruct(WS_ID,carbuff[1],priority); /* add car to rear of rt car waiting queue */ z = irtrear; addtally(rttally,strbuff,&irtrear,&jrtrear); update_rt_tally(rttally,z); /* create car complete, request update of car waiting */ write(PHIGrtn[1],carbuff,3); break; case '2' : /* create a car at lf side */ itoa(carbuff[1],strbuff); lf_vector.x = 0.0; lf_vector.y = 0.0; lf_vector.z = 0.0; ptranslate3(&lf_vector,&error_ind,lf_matrix); popenstruct(carbuff[1]); plabel(1); psetlocaltran3(lf_matrix,PREPLACE); create_car_lf(strbuff); pclosestruct(); ppoststruct(WS_ID,carbuff[1],priority); /* add car to rear of lf car waiting queue */ z = ilfrear; addtally(lftally,strbuff,&ilfrear,&jlfrear); update_lf_tally(lftally,z); /* create car complete, request update of car waiting */ write(PHIGrtn[1],carbuff,3); break; } } if(fstat(PHIGwait[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* update car waiting from SCAN task */ read(PHIGwait[0],carbuff,3); switch(carbuff[0]) { case '3' : /* update rt car waiting */ itoa(carbuff[1],strbuff); pseteditmode(PEDIT_REPLACE); update_rt(strbuff); pseteditmode(PEDIT_INSERT); break; case '4' : /* update lf car waiting */ itoa(carbuff[1],strbuff); pseteditmode(PEDIT_REPLACE); update_lf(strbuff); pseteditmode(PEDIT_INSERT); break; } } } } /* RBC Task -- */ if((pidRBC = fork()) == -1) { perror("fork RBC fail"); exit(1); } if(pidRBC == 0) { /* RBC Task */ printf("I am RBC child\n"); signal(SIGALRM,action); close(arvrt[1]); close(deprt[0]); close(PHIGarvrt[0]); close(PHIGdeprt[1]); while(1) { /* wait for right car arrival from SCAN */ read(arvrt[0],carbuff,3); p(semid); carbuff[0] = '1'; /* command to turn rt green light on (if not blocked by semaphore) */ write(PHIGarvrt[1],carbuff,3); carbuff[0] = '2'; sleep(1); /* allow cars to cross uninterrupted for 120 seconds if too many cars */ /* alarm(120); */ loop = 1; while(loop) { /* cmd move a car across bridge from rt side */ write(PHIGarvrt[1],carbuff,3); /* wait for car to depart from bridge */ read(PHIGdeprt[0],carbuff,3); /* relay car departure to SCAN to update car waiting */ write(deprt[1],carbuff,3); if(fstat(arvrt[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* if another car waiting, move it across */ read(arvrt[0],carbuff,3); carbuff[0] = '2'; } else /* drop out of loop */ loop = 0; } carbuff[0] = '3'; /* turn rt red light on */ write(PHIGarvrt[1],carbuff,3); v(semid); } } /* LBC Task */ #ifdef LEFT if((pidLBC = fork()) == -1) { perror("fork LBC fail"); exit(1); } if(pidLBC == 0) { /* LBC Task */ signal(SIGALRM,action); close(arvlf[1]); close(deplf[0]); close(PHIGarvlf[0]); close(PHIGdeplf[1]); while(1) { /* wait for left car arrival from SCAN */ read(arvlf[0],carbuff,3); p(semid); carbuff[0] = '1'; /* command to turn lf green light on (if not blocked by semaphore) */ write(PHIGarvlf[1],carbuff,3); carbuff[0] = '2'; sleep(1); /* allow cars to cross uninterrupted for 120 seconds if too many cars */ /* alarm(120); */ loop = 1; while(loop) { /* cmd move a car across bridge from lf side */ write(PHIGarvlf[1],carbuff,3); /* wait for car to depart from bridge */ read(PHIGdeplf[0],carbuff,3); /* relay car departure to SCAN to update car waiting */ write(deplf[1],carbuff,3); if(fstat(arvlf[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* if another car waiting, move it across */ read(arvlf[0],carbuff,3); carbuff[0] = '2'; } else /* drop out of loop */ loop = 0; } carbuff[0] = '3'; /* turn left red light on */ write(PHIGarvlf[1],carbuff,3); v(semid); } } #endif /* SCAN Task */ if((pidSCAN = fork()) == -1) { perror("fork SCAN fail"); exit(1); } if(pidSCAN == 0) { printf("I am SCAN child\n"); close(arvrt[0]); close(deprt[1]); close(arvlf[0]); close(deplf[1]); close(randrt[1]); close(randlf[1]); close(PHIGrtn[1]); close(PHIGscan[0]); close(PHIGwait[0]); carbuff[2] = '\0'; car_num = 0; while(1) { /* scan "sensors" and relay info */ if(fstat(randrt[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* rt car arrival from random gen */ read(randrt[0],rtbuff,2); num_car_rt = num_car_rt % 127 + 1; car_num = car_num % 127 + 1; carbuff[0] = '1'; carbuff[1] = (char)car_num; /* command create car rt side */ write(PHIGscan[1],carbuff,3); /* wait for create car complete */ read(PHIGrtn[0],carbuff,3); /* signal car arrival to RBC */ write(arvrt[1],carbuff,3); carbuff[0] = '3'; carbuff[1] = (char)num_car_rt; /* command inc rt car waiting */ write(PHIGwait[1],carbuff,3); } if(fstat(randlf[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* left car arrival from random gen */ read(randlf[0],lfbuff,2); num_car_lf = num_car_lf % 127 + 1; car_num = car_num % 127 + 1; carbuff[0] = '2'; carbuff[1] = (char)car_num; /* command create car lf side */ write(PHIGscan[1],carbuff,3); /* wait for create car complete */ read(PHIGrtn[0],carbuff,3); /* signal car arrival to LBC */ write(arvlf[1],carbuff,3); carbuff[0] = '4'; carbuff[1] = (char)num_car_lf; /* command inc lf car waiting */ write(PHIGwait[1],carbuff,3); } if(fstat(deprt[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* if car from rt side departs bridge */ read(deprt[0],rtbuff,3); num_car_rt--; carbuff[0] = '3'; carbuff[1] = (char)num_car_rt; /* command dec rt car waiting */ write(PHIGwait[1],carbuff,3); } if(fstat(deplf[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* if car from lf side departs bridge */ read(deplf[0],lfbuff,3); num_car_lf--; carbuff[0] = '4'; carbuff[1] = (char)num_car_lf; /* command dec lf car waiting */ write(PHIGwait[1],carbuff,3); } } } /* Task to generate right car random arrivals */ if((pidrt = fork()) == -1) { perror("fork randrt fail"); exit(1); } if(pidrt == 0) { printf("I am randrt child\n"); close(randrt[0]); while(1) { n = (rand()/512) % 20 + 2; printf("right delay = %d\n",n); sleep(n); /* trip rt car arrival "sensor" */ write(randrt[1],"1",2); } } /* Task to generate left car random arrivals */ #ifdef LEFT if((pidlf = fork()) == -1) { perror("fork randlf fail"); exit(1); } if(pidlf == 0) { printf("I am randlf child\n"); close(randlf[0]); while(1) { n = (rand()/1024) % 30 + 2; printf("left delay = %d\n",n); sleep(n); /* trip rt car arrival "sensor" */ write(randlf[1],"1",2); } } #endif /*********************** End Of Forked Tasks ******************************/ wait(0); /* Close the workstation */ pclosews(WS_ID); /* Close PHIGS */ pclosephigs(); } /* End of main program */ /* alarm interrupt handler */ action() { loop = 0; alarm(0); printf("alarm\n"); } /* Procedure to move a car from right side across bridge */ move_car_rt(car_num) int car_num; { int i,j; char strbuff[5],carbuff[3]; rt_vector.x = 0.0; rt_vector.y = 0.0; rt_vector.z = 0.0; pseteditmode(PEDIT_REPLACE); for(i = 100;i > 0;i--) { rt_vector.x -= 0.01; if(i < 90 && i > 75) rt_vector.y -= 0.0067; if(i < 45 && i > 30) rt_vector.y += 0.0067; ptranslate3(&rt_vector,&error_ind,rt_matrix); punpoststruct(WS_ID,car_num); popenstruct(car_num); psetelemptr(0); psetelemptrlabel(1); poffsetelemptr(1); psetlocaltran3(rt_matrix,PREPLACE); pclosestruct(); ppoststruct(WS_ID,car_num,0.0); if(fstat(PHIGwait[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* update car waiting from SCAN task */ read(PHIGwait[0],carbuff,3); switch(carbuff[0]) { case '3' : /* update rt car waiting */ itoa(carbuff[1],strbuff); update_rt(strbuff); break; case '4' : /* update lf car waiting */ itoa(carbuff[1],strbuff); update_lf(strbuff); break; } } for(j = 200000;j > 0;j--); } pseteditmode(PEDIT_INSERT); } /* Procedure to move a car from left side across bridge */ move_car_lf(car_num) int car_num; { int i,j; char strbuff[5],carbuff[3]; lf_vector.x = 0.0; lf_vector.y = 0.0; lf_vector.z = 0.0; pseteditmode(PEDIT_REPLACE); for(i = 100;i > 0;i--) { lf_vector.x += 0.01; if(i < 90 && i > 75) lf_vector.y += 0.0067; if(i < 45 && i > 30) lf_vector.y -= 0.0067; ptranslate3(&lf_vector,&error_ind,lf_matrix); punpoststruct(WS_ID,car_num); popenstruct(car_num); psetelemptr(0); psetelemptrlabel(1); poffsetelemptr(1); psetlocaltran3(lf_matrix,PREPLACE); pclosestruct(); ppoststruct(WS_ID,car_num,0.0); if(fstat(PHIGwait[0],&x) == -1)perror("fstat error"); if(x.st_size > 0) { /* update car waiting from SCAN task */ read(PHIGwait[0],carbuff,3); switch(carbuff[0]) { case '3' : /* update rt car waiting */ itoa(carbuff[1],strbuff); update_rt(strbuff); break; case '4' : /* update lf car waiting */ itoa(carbuff[1],strbuff); update_lf(strbuff); break; } } for(j = 200000;j > 0;j--); } pseteditmode(PEDIT_INSERT); } /* Procedure to add a car to car waiting queue */ addtally(tally,strbuff,i,j) char tally[3][21],strbuff[]; int *i,*j; { int k; for(k = 0;strbuff[k] != '\0';k++) tally[*i][*j+k] = strbuff[k]; *j += 4; if(*j > 18) { *j = 0; (*i)++; } if(*i > 2) *i = 0; } /* Procedure to delete a car from car waiting queue */ subtally(tally,i,j) char tally[3][21]; int *i,*j; { int k; for(k = 0;k < 4;k++,(*j)++) tally[*i][*j] = ' '; if(*j > 18) { *j = 0; (*i)++; } if(*i > 2) *i = 0; } /* Procedure to update lf car waiting queue */ update_lf_tally(tally,n) char tally[3][21]; int n; { Ppoint *x[3]; x[0] = &text_pts30; x[1] = &text_pts31; x[2] = &text_pts32; pseteditmode(PEDIT_REPLACE); popenstruct(LFTALLY); psetelemptr(0); psetelemptrlabel(1); poffsetelemptr(1+n); ptext(x[n],tally[n]); pclosestruct(); ppoststruct(WS_ID,LFTALLY,0.0); pseteditmode(PEDIT_INSERT); } /* Procedure to update rt car waiting queue */ update_rt_tally(tally,n) char tally[3][21]; int n; { Ppoint *x[3]; x[0] = &text_pts40; x[1] = &text_pts41; x[2] = &text_pts42; pseteditmode(PEDIT_REPLACE); popenstruct(RTTALLY); psetelemptr(0); psetelemptrlabel(1); poffsetelemptr(1+n); ptext(x[n],tally[n]); pclosestruct(); ppoststruct(WS_ID,RTTALLY,0.0); pseteditmode(PEDIT_INSERT); } /* Procedure to create lf car waiting queue */ init_lf_tally() { popenstruct(LFTALLY); psettextalign(&text_align); lf_tally(); psetcharheight(0.015); plabel(1); ptext(&text_pts30," "); ptext(&text_pts31," "); ptext(&text_pts32," "); pclosestruct(); ppoststruct(WS_ID,LFTALLY,0.0); } /* Procedure to create rt car waiting queue */ init_rt_tally() { popenstruct(RTTALLY); psettextalign(&text_align); rt_tally(); psetcharheight(0.015); plabel(1); ptext(&text_pts40," "); ptext(&text_pts41," "); ptext(&text_pts42," "); pclosestruct(); ppoststruct(WS_ID,RTTALLY,0.0); } /* procedure to update rt car waiting count */ update_lf(t) char *t; { popenstruct(WAIT1); psetelemptr(0); psetelemptrlabel(1); poffsetelemptr(1); psetcharheight(0.03); poffsetelemptr(1); ptext(&text_pts20,t); pclosestruct(); ppoststruct(WS_ID,WAIT1,0.0); } /* procedure to update lf car waiting count */ update_rt(t) char *t; { popenstruct(WAIT2); psetelemptr(0); psetelemptrlabel(1); poffsetelemptr(1); psetcharheight(0.03); poffsetelemptr(1); ptext(&text_pts21,t); pclosestruct(); ppoststruct(WS_ID,WAIT2,0.0); } /* Procedure to convert from integer to ASCII */ itoa( n, s) int n; char s[]; { int c,i,j,sign; if((sign = n) < 0) n = -n; i = 0; do { s[i++] = n % 10 + '0'; } while((n /= 10) > 0); if(sign < 0) s[i++] = '-'; s[i] = '\0'; for(i = 0, j = strlen(s) - 1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } } /*Procedure to create the yellow lines in the road */ create_yellow_lines() { Ppoint line1[2]; Ppoint line2[2]; Ppoint line3[2]; Ppoint line4[2]; psetlinecolourind(5); line1[0].x = 0.0; line1[0].y = 0.275; line1[1].x = 0.20; line1[1].y = 0.275; line2[0].x = 0.0; line2[0].y = 0.325; line2[1].x = 0.20; line2[1].y = 0.325; line3[0].x = 0.80; line3[0].y = 0.275; line3[1].x = 1.00; line3[1].y = 0.275; line4[0].x = 0.80; line4[0].y = 0.325; line4[1].x = 1.00; line4[1].y = 0.325; ppolyline (2,line1); ppolyline (2,line2); ppolyline (2,line3); ppolyline (2,line4); } /*Procedure to create the left redlight */ create_sign1() { Ppoint sign1[12]; sign1[0].x = 0.25; sign1[0].y = 0.60; sign1[1].x = 0.20; sign1[1].y = 0.625; sign1[2].x = 0.20; sign1[2].y = 0.625; sign1[3].x = 0.20; sign1[3].y = 0.725; sign1[4].x = 0.20; sign1[4].y = 0.725; sign1[5].x = 0.25; sign1[5].y = 0.750; sign1[6].x = 0.25; sign1[6].y = 0.750; sign1[7].x = 0.30; sign1[7].y = 0.725; sign1[8].x = 0.30; sign1[8].y = 0.725; sign1[9].x = 0.30; sign1[9].y = 0.625; sign1[10].x = 0.30; sign1[10].y = 0.625; sign1[11].x = 0.25; sign1[11].y = 0.60; psetlinecolourind(6); ppolyline (12,sign1); } /* Procedure to create greenlight of sign1 */ create_green_sign1() { Ppoint green1[12]; green1[0].x = 0.25; green1[0].y = 0.625; green1[1].x = 0.230; green1[1].y = 0.640; green1[2].x = 0.230; green1[2].y = 0.640; green1[3].x = 0.230; green1[3].y = 0.655; green1[4].x = 0.230; green1[4].y = 0.655; green1[5].x = 0.25; green1[5].y = 0.670; green1[6].x = 0.25; green1[6].y = 0.670; green1[7].x = 0.27; green1[7].y = 0.655; green1[8].x = 0.27; green1[8].y = 0.655; green1[9].x = 0.27; green1[9].y = 0.640; green1[10].x = 0.27; green1[10].y = 0.640; green1[11].x = 0.25; green1[11].y = 0.625; psetlinecolourind(3); ppolyline (12,green1); } /* Procedure to create redlight of sign1 */ create_red_sign1() { Ppoint red1[12]; red1[0].x = 0.25; red1[0].y = 0.685; red1[1].x = 0.230; red1[1].y = 0.700; red1[2].x = 0.230; red1[2].y = 0.700; red1[3].x = 0.230; red1[3].y = 0.715; red1[4].x = 0.230; red1[4].y = 0.715; red1[5].x = 0.25; red1[5].y = 0.730; red1[6].x = 0.25; red1[6].y = 0.730; red1[7].x = 0.27; red1[7].y = 0.715; red1[8].x = 0.27; red1[8].y = 0.715; red1[9].x = 0.27; red1[9].y = 0.700; red1[10].x = 0.27; red1[10].y = 0.700; red1[11].x = 0.25; red1[11].y = 0.685; psetlinecolourind(2); ppolyline (12,red1); } /* Procedure to create greenlight of sign2 */ create_green_sign2() { Ppoint green2[12]; green2[0].x = 0.75; green2[0].y = 0.625; green2[1].x = 0.73; green2[1].y = 0.640; green2[2].x = 0.73; green2[2].y = 0.640; green2[3].x = 0.73; green2[3].y = 0.655; green2[4].x = 0.73; green2[4].y = 0.655; green2[5].x = 0.75; green2[5].y = 0.670; green2[6].x = 0.75; green2[6].y = 0.670; green2[7].x = 0.77; green2[7].y = 0.655; green2[8].x = 0.77; green2[8].y = 0.655; green2[9].x = 0.77; green2[9].y = 0.640; green2[10].x = 0.77; green2[10].y = 0.640; green2[11].x = 0.75; green2[11].y = 0.625; psetlinecolourind(3); ppolyline (12,green2); } /* Procedure to create redlight of sign2 */ create_red_sign2() { Ppoint red2[12]; red2[0].x = 0.75; red2[0].y = 0.685; red2[1].x = 0.73; red2[1].y = 0.700; red2[2].x = 0.730; red2[2].y = 0.700; red2[3].x = 0.730; red2[3].y = 0.715; red2[4].x = 0.730; red2[4].y = 0.715; red2[5].x = 0.75; red2[5].y = 0.730; red2[6].x = 0.75; red2[6].y = 0.730; red2[7].x = 0.77; red2[7].y = 0.715; red2[8].x = 0.77; red2[8].y = 0.715; red2[9].x = 0.77; red2[9].y = 0.700; red2[10].x = 0.77; red2[10].y = 0.700; red2[11].x = 0.75; red2[11].y = 0.685; psetlinecolourind(2); ppolyline (12,red2); } /* Procedure to create the left redlight */ create_sign2() { Ppoint sign2[12]; sign2[0].x = 0.75; sign2[0].y = 0.60; sign2[1].x = 0.70; sign2[1].y = 0.625; sign2[2].x = 0.70; sign2[2].y = 0.625; sign2[3].x = 0.70; sign2[3].y = 0.725; sign2[4].x = 0.70; sign2[4].y = 0.725; sign2[5].x = 0.75; sign2[5].y = 0.750; sign2[6].x = 0.75; sign2[6].y = 0.750; sign2[7].x = 0.80; sign2[7].y = 0.725; sign2[8].x = 0.80; sign2[8].y = 0.725; sign2[9].x = 0.80; sign2[9].y = 0.625; sign2[10].x = 0.80; sign2[10].y = 0.625; sign2[11].x = 0.75; sign2[11].y = 0.60; psetlinecolourind(6); ppolyline (12,sign2); } /* Procedure to create the bridge */ create_bridge() { Ppoint vertex[10]; Ppoint u2vertex[10]; vertex[0].x = 0.0; vertex[0].y = 0.50; vertex[1].x = 0.25; vertex[1].y = 0.50; vertex[2].x = 0.25; vertex[2].y = 0.5; vertex[3].x = 0.40; vertex[3].y = 0.40; vertex[4].x = 0.40; vertex[4].y = 0.40; vertex[5].x = 0.6; vertex[5].y = 0.40; vertex[6].x = 0.6; vertex[6].y = 0.40; vertex[7].x = 0.75; vertex[7].y = 0.50; vertex[8].x = 0.75; vertex[8].y = 0.50; vertex[9].x = 1.00; vertex[9].y = 0.50; u2vertex[0].x = 0.0; u2vertex[0].y = 0.10; u2vertex[1].x = 0.25; u2vertex[1].y = 0.10; u2vertex[2].x = 0.25; u2vertex[2].y = 0.10; u2vertex[3].x = 0.40; u2vertex[3].y = 0.20; u2vertex[4].x = 0.40; u2vertex[4].y = 0.20; u2vertex[5].x = 0.60; u2vertex[5].y = 0.20; u2vertex[6].x = 0.60; u2vertex[6].y = 0.20; u2vertex[7].x = 0.75; u2vertex[7].y = 0.10; u2vertex[8].x = 0.75; u2vertex[8].y = 0.10; u2vertex[9].x = 1.00; u2vertex[9].y = 0.10; ppolyline (10,vertex); ppolyline (10,u2vertex); } /* Procedure to create a car on the right side */ create_car_rt(car) char car[]; { Ppoint dcar[27]; Ppoint text_pts7; text_pts7.x = 0.89; text_pts7.y = 0.43; dcar[0].x = 1.00; dcar[0].y = 0.42; dcar[1].x = 1.00; dcar[1].y = 0.44; dcar[2].x = 1.00; dcar[2].y = 0.44; dcar[3].x = 0.96; dcar[3].y = 0.44; dcar[4].x = 0.96; dcar[4].y = 0.44; dcar[5].x = 0.92; dcar[5].y = 0.46; dcar[6].x = 0.92; dcar[6].y = 0.46; dcar[7].x = 0.88; dcar[7].y = 0.46; dcar[8].x = 0.88; dcar[8].y = 0.46; dcar[9].x = 0.84; dcar[9].y = 0.44; dcar[10].x = 0.84; dcar[10].y = 0.44; dcar[11].x = 0.80; dcar[11].y = 0.44; dcar[12].x = 0.80; dcar[12].y = 0.44; dcar[13].x = 0.80; dcar[13].y = 0.42; dcar[14].x = 0.80; dcar[14].y = 0.42; dcar[15].x = 1.00; dcar[15].y = 0.42; dcar[16].x = 0.83; dcar[16].y = 0.42; dcar[17].x = 0.83; dcar[17].y = 0.40; dcar[18].x = 0.83; dcar[18].y = 0.40; dcar[19].x = 0.85; dcar[19].y = 0.40; dcar[20].x = 0.85; dcar[20].y = 0.42; dcar[21].x = 0.96; dcar[21].y = 0.42; dcar[22].x = 0.96; dcar[22].y = 0.40; dcar[23].x = 0.96; dcar[23].y = 0.40; dcar[24].x = 0.94; dcar[24].y = 0.40; dcar[25].x = 0.94; dcar[25].y = 0.40; dcar[26].x = 0.94; dcar[26].y = 0.42; psetintstyle (PSOLID); psetintcolourind (4); pfillarea (27,dcar); psettextfont(PFONT_DUPLEX); psetcharheight(0.015); ptext(&text_pts7, car); } /* Procedure to create a car on the left side */ create_car_lf(car) char car[]; { Ppoint lcar[27]; Ppoint text_pts8; text_pts8.x = 0.08; text_pts8.y = 0.18; lcar[0].x = 0.00; lcar[0].y = 0.17; lcar[1].x = 0.00; lcar[1].y = 0.19; lcar[2].x = 0.00; lcar[2].y = 0.19; lcar[3].x = 0.04; lcar[3].y = 0.19; lcar[4].x = 0.04; lcar[4].y = 0.19; lcar[5].x = 0.08; lcar[5].y = 0.21; lcar[6].x = 0.08; lcar[6].y = 0.21; lcar[7].x = 0.12; lcar[7].y = 0.21; lcar[8].x = 0.12; lcar[8].y = 0.21; lcar[9].x = 0.16; lcar[9].y = 0.19; lcar[10].x = 0.16; lcar[10].y = 0.19; lcar[11].x = 0.20; lcar[11].y = 0.19; lcar[12].x = 0.20; lcar[12].y = 0.19; lcar[13].x = 0.20; lcar[13].y = 0.17; lcar[14].x = 0.20; lcar[14].y = 0.17; lcar[15].x = 0.00; lcar[15].y = 0.17; lcar[16].x = 0.17; lcar[16].y = 0.17; lcar[17].x = 0.17; lcar[17].y = 0.15; lcar[18].x = 0.17; lcar[18].y = 0.15; lcar[19].x = 0.15; lcar[19].y = 0.15; lcar[20].x = 0.15; lcar[20].y = 0.17; lcar[21].x = 0.04; lcar[21].y = 0.17; lcar[22].x = 0.04; lcar[22].y = 0.15; lcar[23].x = 0.04; lcar[23].y = 0.15; lcar[24].x = 0.06; lcar[24].y = 0.15; lcar[25].x = 0.06; lcar[25].y = 0.15; lcar[26].x = 0.06; lcar[26].y = 0.17; psetintstyle (PSOLID); psetintcolourind (4); pfillarea (27,lcar); psettextfont(PFONT_DUPLEX); psetcharheight(0.015); ptext(&text_pts8, car); } /* Procedure to create lf car waiting count */ Lft_Cars_Wait() { Ppoint box[8]; Ppoint text_pts9; text_pts9.x = 0.175; text_pts9.y = 0.86; box[0].x = 0.17; box[0].y = 0.80; box[1].x = 0.17; box[1].y = 0.88; box[2].x = 0.17; box[2].y = 0.88; box[3].x = 0.37; box[3].y = 0.88; box[4].x = 0.37; box[4].y = 0.88; box[5].x = 0.37; box[5].y = 0.80; box[6].x = 0.37; box[6].y = 0.80; box[7].x = 0.17; box[7].y = 0.80; psetintstyle (PSOLID); psetintcolourind (2); pfillarea (8,box); psetlinetype(PLN_SOLID); psetlinecolourind(5); psetcharheight(0.015); ptext(&text_pts9, "CARS WAITING"); } /* Procedure to create rt car waiting count */ Rt_Cars_Wait() { Ppoint box[8]; Ppoint text_pts10; text_pts10.x = 0.635; text_pts10.y = 0.86; box[0].x = 0.63; box[0].y = 0.80; box[1].x = 0.63; box[1].y = 0.88; box[2].x = 0.63; box[2].y = 0.88; box[3].x = 0.83; box[3].y = 0.88; box[4].x = 0.83; box[4].y = 0.88; box[5].x = 0.83; box[5].y = 0.80; box[6].x = 0.83; box[6].y = 0.80; box[7].x = 0.63; box[7].y = 0.80; psetintstyle (PSOLID); psetintcolourind (2); pfillarea (8,box); psetlinetype(PLN_SOLID); psetlinecolourind(4); psetcharheight(0.015); ptext(&text_pts10, "CARS WAITING"); } create_polyline_attributes() { static Pint line_colour = 1; static Pfloat line_width = 2.0; /* Set polyline color */ psetlinecolourind(line_colour); /* Set the polyline type */ psetlinetype(PLN_SOLID); /* Set the polyline width */ psetlinewidth(line_width); } /* delete the left red light */ delete_lt_red() { punpoststruct(WS_ID, GO1); pdelstruct(GO1); } /* delete the right red light */ delete_rt_red() { punpoststruct(WS_ID, GO4); pdelstruct(GO4); } /* delete the left green light */ delete_lt_green() { punpoststruct(WS_ID, GO2); pdelstruct(GO2); } /* delete the right green light */ delete_rt_green() { punpoststruct(WS_ID, GO3); pdelstruct(GO3); } /* Change the right light from red to green */ Chng_rl_to_green() { delete_rt_red(); popenstruct(GO3); create_rt_car_turn_green(); pclosestruct(); ppoststruct(WS_ID,GO3,priority); ppoststruct(WS_ID,RED2,priority); } /* Change the left light from red to green */ Chng_ll_to_green() { delete_lt_red(); popenstruct(GO2); create_lt_car_turn_green(); pclosestruct(); ppoststruct(WS_ID,GO2,priority); ppoststruct(WS_ID,RED1,priority); } /* Change the right light from green to red */ Chng_rl_to_red() { delete_rt_green(); popenstruct(GO4); create_rt_car_turn_red(); pclosestruct(); ppoststruct(WS_ID,GO4,priority); ppoststruct(WS_ID,GREEN2,priority); } /* Change the left light from green to red */ Chng_ll_to_red() { delete_lt_green(); popenstruct(GO1); create_lt_car_turn_red(); pclosestruct(); ppoststruct(WS_ID,GO1,priority); ppoststruct(WS_ID,GREEN1,priority); } /* set the right light to red - to start */ Init_rt_light() { popenstruct(GO4); create_rt_car_turn_red(); pclosestruct(); ppoststruct(WS_ID,GO4,priority); } /* set the left light to red - to start */ Init_lt_light() { popenstruct(GO1); create_lt_car_turn_red(); pclosestruct(); ppoststruct(WS_ID,GO1,priority); } /* create red light of sign2 */ create_rt_car_turn_red() { Ppoint red2b[12]; red2b[0].x = 0.75; red2b[0].y = 0.685; red2b[1].x = 0.73; red2b[1].y = 0.700; red2b[2].x = 0.730; red2b[2].y = 0.700; red2b[3].x = 0.730; red2b[3].y = 0.715; red2b[4].x = 0.730; red2b[4].y = 0.715; red2b[5].x = 0.75; red2b[5].y = 0.730; red2b[6].x = 0.75; red2b[6].y = 0.730; red2b[7].x = 0.77; red2b[7].y = 0.715; red2b[8].x = 0.77; red2b[8].y = 0.715; red2b[9].x = 0.77; red2b[9].y = 0.700; red2b[10].x = 0.77; red2b[10].y = 0.700; red2b[11].x = 0.75; red2b[11].y = 0.685; psetintstyle (PSOLID); psetintcolourind(2); pfillarea(12,red2b); } /* create green light of sign1 */ create_lt_car_turn_green() { Ppoint green1b[12]; green1b[0].x = 0.25; green1b[0].y = 0.625; green1b[1].x = 0.230; green1b[1].y = 0.640; green1b[2].x = 0.230; green1b[2].y = 0.640; green1b[3].x = 0.230; green1b[3].y = 0.655; green1b[4].x = 0.230; green1b[4].y = 0.655; green1b[5].x = 0.25; green1b[5].y = 0.670; green1b[6].x = 0.25; green1b[6].y = 0.670; green1b[7].x = 0.27; green1b[7].y = 0.655; green1b[8].x = 0.27; green1b[8].y = 0.655; green1b[9].x = 0.27; green1b[9].y = 0.640; green1b[10].x = 0.27; green1b[10].y = 0.640; green1b[11].x = 0.25; green1b[11].y = 0.625; psetintstyle (PSOLID); psetintcolourind(3); pfillarea(12,green1b); } /* create green light of sign2 */ create_rt_car_turn_green() { Ppoint green2b[12]; green2b[0].x = 0.75; green2b[0].y = 0.625; green2b[1].x = 0.73; green2b[1].y = 0.640; green2b[2].x = 0.73; green2b[2].y = 0.640; green2b[3].x = 0.73; green2b[3].y = 0.655; green2b[4].x = 0.73; green2b[4].y = 0.655; green2b[5].x = 0.75; green2b[5].y = 0.670; green2b[6].x = 0.75; green2b[6].y = 0.670; green2b[7].x = 0.77; green2b[7].y = 0.655; green2b[8].x = 0.77; green2b[8].y = 0.655; green2b[9].x = 0.77; green2b[9].y = 0.640; green2b[10].x = 0.77; green2b[10].y = 0.640; green2b[11].x = 0.75; green2b[11].y = 0.625; psetintstyle (PSOLID); psetintcolourind(3); pfillarea(12,green2b); } /* create red light of sign1 */ create_lt_car_turn_red() { Ppoint red1b[12]; red1b[0].x = 0.25; red1b[0].y = 0.685; red1b[1].x = 0.230; red1b[1].y = 0.700; red1b[2].x = 0.230; red1b[2].y = 0.700; red1b[3].x = 0.230; red1b[3].y = 0.715; red1b[4].x = 0.230; red1b[4].y = 0.715; red1b[5].x = 0.25; red1b[5].y = 0.730; red1b[6].x = 0.25; red1b[6].y = 0.730; red1b[7].x = 0.27; red1b[7].y = 0.715; red1b[8].x = 0.27; red1b[8].y = 0.715; red1b[9].x = 0.27; red1b[9].y = 0.700; red1b[10].x = 0.27; red1b[10].y = 0.700; red1b[11].x = 0.25; red1b[11].y = 0.685; psetintstyle (PSOLID); psetintcolourind (2); pfillarea(12,red1b); } /* Procedure to create lf cars waiting queue */ lf_tally() { Ppoint box[8]; Ppoint text_pts33; text_pts33.x = 0.07; text_pts33.y = 0.96; box[0].x = 0.05; box[0].y = 0.99; box[1].x = 0.37; box[1].y = 0.99; box[2].x = 0.37; box[2].y = 0.99; box[3].x = 0.37; box[3].y = 0.89; box[4].x = 0.37; box[4].y = 0.89; box[5].x = 0.05; box[5].y = 0.89; box[6].x = 0.05; box[6].y = 0.89; box[7].x = 0.05; box[7].y = 0.99; psetintstyle (PSOLID); psetintcolourind (1); pfillarea (8,box); psetlinetype(PLN_SOLID); psettextcolourind(0); psetcharheight(0.015); ptext(&text_pts33, "CARS WAITING QUEUE"); } /* Procedure to create rt cars waiting queue */ rt_tally() { Ppoint box[8]; Ppoint text_pts43; text_pts43.x = 0.65; text_pts43.y = 0.96; box[0].x = 0.63; box[0].y = 0.99; box[1].x = 0.95; box[1].y = 0.99; box[2].x = 0.95; box[2].y = 0.99; box[3].x = 0.95; box[3].y = 0.89; box[4].x = 0.95; box[4].y = 0.89; box[5].x = 0.63; box[5].y = 0.89; box[6].x = 0.63; box[6].y = 0.89; box[7].x = 0.63; box[7].y = 0.99; psetintstyle (PSOLID); psetintcolourind (1); pfillarea (8,box); psetlinetype(PLN_SOLID); psettextcolourind(0); psetcharheight(0.015); ptext(&text_pts43, "CARS WAITING QUEUE"); } /* Procedure to create title */ header() { Ppoint text_pts1; Ppoint text_pts2; Ppoint text_pts3; Ppoint text_pts4; Ppoint text_pts5; Ppoint text_pts6; text_pts1.x = 0.34; text_pts1.y = 0.58; text_pts2.x = 0.34; text_pts2.y = 0.55; text_pts3.x = 0.34; text_pts3.y = 0.52; text_pts4.x = 0.34; text_pts4.y = 0.49; text_pts5.x = 0.34; text_pts5.y = 0.46; text_pts6.x = 0.34; text_pts6.y = 0.43; popenstruct(HEADER); psettextalign(&text_align); psetcharheight(0.015); psetlinecolourind(4); ptext(&text_pts1,"SHARED BRIDGE CONTROL"); ptext(&text_pts2," Realtime System"); ptext(&text_pts3," by"); ptext(&text_pts4," Sheila McKain"); ptext(&text_pts5," &"); ptext(&text_pts6," Dave Hess"); pclosestruct(); ppoststruct(WS_ID,HEADER,0.0); }