Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/QNX4/Train/Railway.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__Train__Railway_CPP__
00002 #define __J2K__Train__Railway_CPP__
00003 
00004 void Railway::initTrains()
00005 {
00006   train[0].setTrainID(1);
00007   train[1].setTrainID(2);
00008   train[3].setTrainID(4);
00009 }
00010 
00011 void Railway::showCurrentGeometry()
00012 {
00013   track.showGeometry();
00014 }
00015 
00016 void Railway::setTrainVelocity( int ID, int V )
00017 {
00018   // Update the velocity
00019   train[ID-1].setVelocity(V);
00020 }
00021 
00022 //  This is the position tracker
00023 int Railway::getTrainPosition( int ID )
00024 {
00025   int trainPosition;
00026 
00027   printf("the position of train is %d\n", train[ID-1].getPosition() );
00028   trainPosition = train[ID-1].getPosition();
00029 
00030   if ( train[ID-1].getDirection() == CW ) {
00031     while(
00032       track.isSegmentOccupied( track.getNext(trainPosition) ) != TRUE
00033     ) {
00034      //printf("The position of train is %d\n", train[ID-1].getPosition());
00035     };
00036 
00037     train[ID-1].setPosition( track.getNext(trainPosition) );
00038 
00039   } else {
00040 
00041     while(
00042       track.isSegmentOccupied( track.getPrev(trainPosition) ) != TRUE
00043     ) {
00044       //printf("The position of train is %d\n", train[ID-1].getPosition());
00045     };
00046 
00047     train[ID-1].setPosition( track.getPrev(trainPosition) );
00048   }
00049 
00050   return train[ID-1].getPosition();
00051 }
00052 
00053 Railway::Railway()
00054 {
00055   initTrains();
00056 
00057   // For now comment the next line
00058   autoPositionTrains();
00059 
00060   // REMEMBER TO REMOVE THE FOLLOEING LINES
00061   //--------------------------
00062   // train[0].setPosition(1);
00063   // train[1].setPosition(8);
00064   // train[3].setPosition(9);
00065   //--------------------------
00066 }
00067 
00068 void Railway::displaySegmentsOccupied()
00069 {
00070   for( int i = 0; i < NSEG; i++ ) {
00071     if ( track.isSegmentOccupied(i+1) == TRUE )
00072       printf("%2d ",i+1);
00073     }
00074   }
00075 
00076   printf("\n");
00077 }
00078 
00079 void Railway::autoPositionTrains()
00080 {
00081   int SegBefore[ NSEG ];
00082   int SegAfter[  NSEG ];
00083   int i;
00084 
00085   displaySegmentsOccupied();
00086 
00087   // AutoPostion for Train 1
00088   // "Before" track segment state
00089   for( i = 0; i < NSEG; i++ ) {
00090     SegBefore[i] = track.isSegmentOccupied(i+1);
00091   }
00092 
00093   train[0].setVelocity( 12 );
00094   delay( 250 );
00095   train[0].setVelocity( 0 );
00096 
00097   // "After" track segment state
00098   for( i = 0; i < NSEG; i++ ) {
00099     SegAfter[i] = track.isSegmentOccupied(i+1);
00100   }
00101 
00102   // Compare the arrays
00103   for( i = 0; i < NSEG; i++ ) {
00104     if ( SegAfter[i]==1 && SegBefore[i]==0 ) {
00105       train[0].setPosition(i+1);
00106       break;
00107     }
00108   }
00109 
00110   displaySegmentsOccupied();
00111 
00112   // AutoPostion for Train 2
00113   // "Before" track segment state
00114   for( i = 0; i < NSEG; i++ ) {
00115     SegBefore[i] = track.isSegmentOccupied(i+1);
00116   }
00117 
00118   train[1].setVelocity(12);
00119 
00120   delay(1000);
00121   train[1].setVelocity(0);
00122 
00123   // "After" track segment state
00124   for( i = 0; i < NSEG; i++ ) {
00125     SegAfter[i] = track.isSegmentOccupied(i+1);
00126   }
00127 
00128   // Compare the arrays
00129   for( i = 0; i < NSEG; i++ ) {
00130     if ( SegAfter[i]==1 && SegBefore[i]==0 ) {
00131       train[1].setPosition(i+1);
00132       break;
00133     }
00134   }
00135 
00136   displaySegmentsOccupied();
00137 
00138   // AutoPostion for Train 4
00139   // "Before" track segment state
00140   for( i = 0; i < NSEG; i++ ) {
00141     SegBefore[i] = track.isSegmentOccupied(i+1);
00142   }
00143 
00144   train[3].setVelocity( 12 );
00145   delay( 250 );
00146   train[3].setVelocity( 0 );
00147 
00148   // "After" track segment state
00149   for( i = 0; i < NSEG; i++ ) {
00150     SegAfter[i] = track.isSegmentOccupied(i+1);
00151   }
00152 
00153   // Compare the arrays
00154   for( i = 0; i < NSEG; i++ ) {
00155     if (SegAfter[i]==1 && SegBefore[i]==0) {
00156       train[3].setPosition(i+1);
00157       break;
00158     }
00159   }
00160 
00161   displaySegmentsOccupied();
00162 
00163   printf("\n\n\n-------------------------------------------\n");
00164   printf("The System is Ready To Run..........\n");
00165   printf("Train 1 position:    %d\n", train[0].getPosition() );
00166   printf("Train 2 position:    %d\n", train[1].getPosition() );
00167   printf("Train 4 position:    %d\n", train[3].getPosition() );
00168 }
00169 
00170 void Railway::switchTrack( int ID, int P )
00171 {
00172   track.setSwitch( ID, P );
00173 }
00174 
00175 #endif

Generated on Sun Oct 14 18:46:42 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001