Jasmine Karlsson / Mbed 2 deprecated train_rail

Dependencies:   mbed TextLCD

Revision:
31:9d973398554f
Parent:
30:63a8a5cefc6b
--- a/main.cpp	Mon Jun 25 14:57:17 2018 +0000
+++ b/main.cpp	Thu Jun 28 15:08:41 2018 +0000
@@ -57,7 +57,7 @@
 static const int cRight[] = {6,7,8};
 vector<int> C_right (cRight, cRight + sizeof(cRight) / sizeof(cRight[0]) );
 
-static const int cLeft[] = {0,1,2,12,13};
+static const int cLeft[] = {0,1,2,12,13,14,15};
 vector<int> C_left (cLeft, cLeft + sizeof(cLeft) / sizeof(cLeft[0]) );
 
 static const int junction39[] = {3,9};
@@ -85,6 +85,7 @@
 void init_mcp();
 void init_interrupt();
 void init_trains();
+void endFunction();
 void executeCase(int pos, Train* currTrain);
 Train& assignTrain(int pos);
 
@@ -142,21 +143,7 @@
         } 
     }
     
-    lcd.printf("Shutting down...");
-    bool in = true;
-    while(in){ 
-        redTrain.sendCommand();
-        silverTrain.sendCommand();
-        externalLed2 = 1;
-       // wait(0.2);
-        //externalLed2 = 0;
-        buzz = 1;
-        if(!readSwitch(sw3)){
-             in = false;   
-        }
-            
-    }
-    buzz = 0;
+    endFunction();
     return 0;
 }
 
@@ -199,6 +186,26 @@
     return newPos;
 }
 
+void endFunction(){
+    
+    lcd.printf("Shutting down...");
+    bool in = true;
+    while(in){ 
+        redTrain.sendCommand();
+        silverTrain.sendCommand();
+        externalLed2 = 1;
+       // wait(0.2);
+        //externalLed2 = 0;
+        buzz = 1;
+        if(!readSwitch(sw4)){
+             in = false;   
+        }
+            
+    }
+    externalLed2 = 0;    
+    buzz = 0;
+}
+
 Train& assignTrain(int pos){
     
     //Check which train got the interupt
@@ -218,35 +225,35 @@
         lcd.cls();
         lcd.printf("NO TRAIN ASSIGNED");
         lcd.printf("%d", pos);
+        
         silverTrain.Stop();
         redTrain.Stop();
-        switch2.switchOff();
+        endFunction();
     }
 }
 
 void executeCase(int pos, Train* currTrain){
     lcd.cls();
+    lcd.printf("At ");
+    lcd.printf("%d", pos);
     switch(pos){
     case 0:{
-        lcd.printf("at 0");
         currTrain->changeSpeed(currTrain->normalSpeed());
         break;}
     case 1:{
-        lcd.printf("at 1");
         //TO DO: Check which train is going and only slow down if it started from 2
         /*if(!currTrain->isClockwise()){
             currTrain->changeSpeed(slow_speed);//Slow down
             lcd.printf("slow down");
         }*/
         //else
-            currTrain->changeSpeed(currTrain->normalSpeed());
+        currTrain->changeSpeed(currTrain->slowlySpeed());
         
         break;}
     case 2:{
        /*
        Stop everytime at 2     
        */
-       lcd.printf("at 2"); 
        dect.showReservation();
        if(!dect.checkReservation(10) && !dect.checkReservation(11)){
             currTrain->Stop();
@@ -275,15 +282,12 @@
             currTrain->changeSpeed(currTrain->normalSpeed());                          //Go forward
         }    
         break;}
-    case 3:{
-        lcd.printf("at 3");
-       
-        
+    case 3:{   
         switch2.switchOff();                        //Disable switch2
         dect.clearReservation(C_left);              //free nr 0,1,2,12,13 
         if(!dect.checkReservation(5)){ 
             currTrain->changeDirection();               //change direction  
-            currTrain->changeSpeed(currTrain->normalSpeed());     //go forward 
+            currTrain->changeSpeed(currTrain->slowlySpeed());     //go forward 
         }   
         else{
             currTrain->Stop();
@@ -291,11 +295,15 @@
         break;
         }
     case 4:{
-        lcd.printf("At 4");
         dect.clearReservation(C_left);   //free nr 0,1,2,12,13
         switch3.switchOn();                 //Turn on switch3
         dect.showReservation();
-        if(dect.checkReservation(6)){   //Check if 6 is reserved
+        if(currTrain->isClockwise()){
+            lcd.printf("Switch 3 not activated");
+            redTrain.Stop();
+            silverTrain.Stop();    
+        }
+        else if(dect.checkReservation(6)){   //Check if 6 is reserved
             currTrain->Stop();           //IF yes STOP
         }
         else{
@@ -305,8 +313,7 @@
          break;  
         }    
     case 5:{
-        lcd.printf("at 5");
-        
+        dect.makeReservation(junction_511);
         switch3.switchOn();                        //Enable switch3
         dect.clearReservation(C_right);              //free nr 6,7,8
         
@@ -320,7 +327,6 @@
         break;
         }
     case 6:{
-        lcd.printf("At 6");
         if(!currTrain->isClockwise())        //IF CC goes towards 7
         {
             switch3.switchOn();                                             //Enable switch3
@@ -342,12 +348,10 @@
         }         
         break;
         }
-    case 7:{
-        lcd.printf("At 7");        
-        currTrain->changeSpeed(currTrain->normalSpeed());
+    case 7:{     
+        currTrain->changeSpeed(currTrain->slowlySpeed());
         break;}
-    case 8:{
-          lcd.printf("At 8");        
+    case 8:{    
         if(currTrain->isClockwise())        //IF C goes towards 7
         {
             switch4.switchOff();                                             //Disable switch4
@@ -365,9 +369,13 @@
         }         
         break;
         }
-    case 9:{
-        lcd.printf("at 9");         
-        if(!dect.checkReservation(8)){
+    case 9:{    
+        if(!currTrain->isClockwise()){
+            lcd.printf("Switch 4 is activated");
+            redTrain.Stop();
+            silverTrain.Stop();    
+        }
+        else if(!dect.checkReservation(8)){
             switch4.switchOn();         //Enable switch4
             dect.makeReservation(C_right);                                 //Reserve 6,7,8
             currTrain->changeSpeed(currTrain->normalSpeed());                    //Go forward
@@ -379,9 +387,8 @@
         }
         break;
         }
-    case 10:{
-        lcd.printf("At 10");        
-
+    case 10:{   
+        dect.makeReservation(lane_10);
         dect.clearReservation(C_right); //free nr 6, 7, 8, 9
         if(dect.checkReservation(12)){   //Check if 12 is reserved
             currTrain->Stop();           //IF yes STOP
@@ -394,9 +401,9 @@
         break;
         }
         
-    case 11:{
-        lcd.printf("at 11");        
-        
+    case 11:{      
+        dect.makeReservation(junction_511);
+        dect.showReservation();
         if(!dect.checkReservation(12)){
             
             switch1.switchOn();                        //Enable switch1
@@ -408,30 +415,27 @@
         }                                                             
         break;
         }
-    case 12: {
-        lcd.printf("at 12");        
+    case 12: {      
         switch1.switchOff();               //Turn of switch1
         //int arr2 [3] = {5,10,11};
         //vector<int> detectors2(arr2, arr2 + sizeof(arr2) / sizeof(int));
         dect.clearReservation(lane_10); //free nr 5, 10, 11
         dect.clearReservation(junction_511);
+        dect.makeReservation(C_left);
         currTrain->changeSpeed(currTrain->normalSpeed());                          //Move forward
         
         break;
         }
     case 13: {
-        lcd.printf("at 13");
         currTrain->changeSpeed(currTrain->normalSpeed());
         break;
         }
-    case 14:{
-        lcd.printf("at 21");            
+    case 14:{          
         currTrain->changeSpeed(currTrain->slowlySpeed());
         break;
         }
     case 15:{
-        lcd.printf("at 22");
-        currTrain->changeSpeed(currTrain->normalSpeed());
+        currTrain->changeSpeed(currTrain->slowlySpeed());
         break;
         }
     default:   {
@@ -459,13 +463,15 @@
         pos = mcp->_read(INTCAPB);
         pos = convertHextoDec(pos, 1);
     }
-    else 
+    else{ 
         //PROBLEM
         lcd.printf("Fail to detect int");
-    if(pos!=7){
-        Train* currTrain = &assignTrain(pos);
-        executeCase(pos, currTrain); 
+        return;
     }
+        
+    Train* currTrain = &assignTrain(pos);
+    executeCase(pos, currTrain); 
+    
 }
 
 bool readSwitch(DigitalIn theSwitch){