Jasmine Karlsson / Mbed 2 deprecated train_rail

Dependencies:   mbed TextLCD

Files at this revision

API Documentation at this revision

Comitter:
jasminealice
Date:
Thu Jun 21 13:42:31 2018 +0000
Parent:
27:9f9cd0df9a79
Child:
29:6031227dcac9
Commit message:
Stop working

Changed in this revision

Train.cpp Show annotated file Show diff for this revision Revisions of this file
Train.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Train.cpp	Wed Jun 20 14:38:20 2018 +0000
+++ b/Train.cpp	Thu Jun 21 13:42:31 2018 +0000
@@ -1,6 +1,6 @@
 #include "Train.h"
 
-Train::Train(const unsigned int newaddress, const unsigned int newinst, int pos, const unsigned int speed): normSpeed(speed)
+Train::Train(const unsigned int newaddress, const unsigned int newinst, int pos, const unsigned int speed, const unsigned int speed2): normSpeed(speed), slowSpeed(speed2)
 {
     //ctor
     address = newaddress;
@@ -9,7 +9,7 @@
     dirClockwise = false;
     nrPacket = 20;
     isStopped = false;
-    //normSpeed = speed;
+
 }
 
 Train::~Train()
@@ -17,10 +17,14 @@
     //dtor
 }
 
-const unsigned int Train::normalSpeed(){
+unsigned int Train::normalSpeed(){
     return normSpeed;    
 }
 
+unsigned int Train::slowlySpeed(){
+    return slowSpeed;    
+}
+
 int Train::getPosition(){
 
     return position;
@@ -35,6 +39,7 @@
 }
 
 void Train::changeSpeed(unsigned int speed){
+    isStopped = false;
     inst = speed;    
 }
 
@@ -91,7 +96,7 @@
             return true;
         break;
     case 6: 
-        if(position == 7 || position == 4 || position == 5 || position == 6)
+        if(position == 7 || position == 4 || position == 5 || position == 6 || position == 8)
             return true;
         break;
     case 7: 
--- a/Train.h	Wed Jun 20 14:38:20 2018 +0000
+++ b/Train.h	Thu Jun 21 13:42:31 2018 +0000
@@ -6,7 +6,7 @@
 class Train : public Track
 {
     public:
-        Train(const unsigned int newaddress, const unsigned int newinst, int pos,const unsigned int speed);
+        Train(const unsigned int newaddress, const unsigned int newinst, int pos,const unsigned int speed, const unsigned int speed2);
         virtual ~Train();
         int getPosition();
         void sendCommand();
@@ -18,7 +18,8 @@
         bool isClockwise();
         void changeDirection();
         bool checkStop();
-        const unsigned int normalSpeed();
+        unsigned int normalSpeed();
+        unsigned int Train::slowlySpeed();
         
 
     protected:
@@ -29,6 +30,7 @@
     bool dirClockwise;
     bool isStopped;
     const unsigned int normSpeed;
+    const unsigned int slowSpeed;
 };
 
 #endif // TRAIN_H
--- a/main.cpp	Wed Jun 20 14:38:20 2018 +0000
+++ b/main.cpp	Thu Jun 21 13:42:31 2018 +0000
@@ -38,7 +38,7 @@
 //move backwards/reverse
 
 //speed dial forward
-const unsigned int slow_speed = 0x73; //step 4
+const unsigned int slow_speed = 0x74; //step 4
 //const unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed
 const unsigned int normal_speed = 0x78; //step 13 1/2 speed
 
@@ -63,7 +63,7 @@
 static const int junction511[] = {5,11};
 vector<int> junction_511 (junction511, junction511 + sizeof(junction511) / sizeof(junction511[0]) );
 
-int redStartVal = 12;
+int redStartVal = 10;
 int silverStartVal = 2;
 
 /*----------------------------------------------------------------------------
@@ -79,8 +79,8 @@
 Train& assignTrain(int pos);
 
 //Trains
-Train redTrain(DCCaddress_red, normal_speed, redStartVal, high_speed);
-Train silverTrain(DCCaddress_silver,normal_speed, silverStartVal, normal_speed);
+Train redTrain(DCCaddress_red, normal_speed, redStartVal, high_speed, normal_speed);
+Train silverTrain(DCCaddress_silver,normal_speed, silverStartVal, normal_speed, slow_speed);
 //Switches
 Switch switch1(DCCaddress_switch,DCCinst_switch1);
 Switch switch2(DCCaddress_switch,DCCinst_switch2);
@@ -102,21 +102,24 @@
         silverTrain.sendCommand();
         
         if(!readSwitch(sw3)){ //Change speed
+            lcd.cls();
             lcd.printf("Go forward");
-            redTrain.changeSpeed(normal_speed);
+            redTrain.changeSpeed(high_speed);
             silverTrain.changeSpeed(normal_speed);
         }
         
         if(redTrain.checkStop()){                       //check if Train stopped
+            lcd.cls();
+            lcd.printf("%d", redTrain.getPosition());
             executeCase(redTrain.getPosition(), &redTrain);
-            wait(5);
         }
         else if(silverTrain.checkStop()){
             executeCase(silverTrain.getPosition(), &silverTrain);
-            wait(5);
         }
         
         if(redTrain.checkStop() && silverTrain.checkStop())  {
+            lcd.cls();
+            lcd.printf("Stop loop...");
             break;    
         }      
     }
@@ -176,9 +179,15 @@
     else if(silverTrain.checkInterupt(pos) && !silverTrain.checkStop()){
         silverTrain.setPosition(pos);
         return silverTrain;
-    }else{
+    }else if(redTrain.checkInterupt(pos) && redTrain.checkStop()){
+        return redTrain;    
+    }else if(silverTrain.checkInterupt(pos) && silverTrain.checkStop()){
+        return silverTrain;    
+    }
+    else{
     lcd.cls();
     lcd.printf("NO TRAIN ASSIGNED");
+    lcd.printf("%d", pos);
     silverTrain.Stop();
     redTrain.Stop();
     switch2.switchOff();
@@ -187,8 +196,7 @@
 }
 
 void executeCase(int pos, Train* currTrain){
-    
-    
+    lcd.cls();
     switch(pos){
     case 0:{
         lcd.printf("at 0");
@@ -210,48 +218,61 @@
        Stop everytime at 2     
        */
        lcd.printf("at 2"); 
-                             
-       if(!dect.checkReservation(4) && !dect.checkReservation(3)){     //Check if 4 or 3 is reserved
-            lcd.cls();
-            lcd.printf("none is reserved");
-           int v1 = rand() % 100;                                       //IF NOT randomize which way to go
-           if (v1 < 25){                                                //0.25 chance to enable the switch2,  reserve 3,9
-                switch2.switchOn();
+       /*if(!dect.checkReservation(10) && !dect.checkReservation(11)){
+            
+            currTrain->Stop();
+            lcd.printf("Stopping at 2");
+       }
+       else{ */
+                                
+           if(!dect.checkReservation(4) && !dect.checkReservation(3)){     //Check if 4 or 3 is reserved
+                lcd.cls();
+                lcd.printf("none is reserved");
+               int v1 = rand() % 100;                                       //IF NOT randomize which way to go
+               if (v1 < 50){                                                //0.25 chance to enable the switch2,  reserve 3,9
+                    switch2.switchOn();
+                    dect.makeReservation(junction_39);
+                } 
+                else {                                                      //0.75 chance to disable switch2, reserve 4
+                   switch2.switchOff(); 
+                   vector<int> detectors(4);
+                   dect.makeReservation(detectors);
+                }        
+            }else if(dect.checkReservation(4)){                             //IF 4 is reserved
+                switch2.switchOn();                                        //Enable switch2, reserve 3,9
+                lcd.cls();
+                lcd.printf("4 is reserved");
                 dect.makeReservation(junction_39);
-            } 
-            else {                                                      //0.75 chance to disable switch2, reserve 4
-               switch2.switchOff(); 
-               vector<int> detectors(4);
-               dect.makeReservation(detectors);
-            }        
-        }else if(dect.checkReservation(4)){                             //IF 4 is reserved
-            switch2.switchOn();                                        //Enable switch2, reserve 3,9
-            lcd.cls();
-            lcd.printf("4 is reserved");
-            dect.makeReservation(junction_39);
-                
-        }else{                                                          //IF 3 reserved 
-            lcd.cls();
-            lcd.printf("3 is reserved");
-            switch2.switchOff();                                         //Disable switch2, reserve 4
-            vector<int> detectors(4);
-            dect.makeReservation(detectors);
-        }
-        currTrain->changeSpeed(currTrain->normalSpeed());                          //Go forward
-             
+                    
+            }else{                                                          //IF 3 reserved 
+                lcd.cls();
+                lcd.printf("3 is reserved");
+                switch2.switchOff();                                         //Disable switch2, reserve 4
+                vector<int> detectors(4);
+                dect.makeReservation(detectors);
+            }
+            currTrain->changeSpeed(currTrain->normalSpeed());                          //Go forward
+        //}    
         break;}
     case 3:{
         lcd.printf("at 3");
        
         currTrain->changeDirection();               //change direction  
         switch2.switchOff();                        //Disable switch2
-        dect.clearReservation(C_left);              //free nr 0,1,2,12,13    
-        currTrain->changeSpeed(currTrain->normalSpeed());     //go forward                                                                 
+        dect.clearReservation(C_left);              //free nr 0,1,2,12,13 
+        if(!dect.checkReservation(5)){ 
+            currTrain->changeSpeed(currTrain->normalSpeed());     //go forward 
+        }   
+        else{
+            currTrain->Stop();
+            lcd.printf("5 is reserved");
+        }                                                           
         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
             lcd.printf("6 is reserved");
             currTrain->Stop();           //IF yes STOP
@@ -268,7 +289,14 @@
         currTrain->changeDirection();               //change direction  
         switch3.switchOn();                        //Enable switch3
         dect.clearReservation(C_right);              //free nr 6,7,8
-        currTrain->changeSpeed(currTrain->normalSpeed());          //go forward                                                               
+        
+        if(!dect.checkReservation(3)){
+            lcd.printf("3 is reserved");
+            currTrain->changeSpeed(currTrain->normalSpeed());     //go forward 
+        }   
+        else{
+            currTrain->Stop();
+        }                                                                
         
         break;}
     case 6:{
@@ -284,6 +312,7 @@
         else{                           
            if(dect.checkReservation(5)){                           //IF 5 is reserved
                 currTrain->Stop();
+                lcd.printf("5 is reserved");
                     
             }else{                                                          //else 5 reserved
                 switch3.switchOff();                                         //Enable switch3, reserve 4
@@ -305,11 +334,16 @@
             dect.clearReservation(junction_39);                                //Free 3,9,10
             currTrain->changeSpeed(currTrain->normalSpeed());                          //Move forward
         }
-       else{                                                               //IF CC                        
-            switch4.switchOff();                                         //Disable switch4, reserve 10
-            vector<int> detectors(10);
-            dect.makeReservation(detectors);
-            currTrain->changeSpeed(currTrain->normalSpeed());                          //Go forward
+       else{                                                               //IF CC        if 10 is reserved stop
+            if(dect.checkReservation(10)){
+                lcd.printf("10 is reserved");
+                currTrain->Stop();
+            }else{                
+                switch4.switchOff();                                         //Disable switch4, reserve 10
+                vector<int> detectors(10);
+                dect.makeReservation(detectors);
+                currTrain->changeSpeed(currTrain->slowlySpeed());                          //Go forward
+            }
         }         
         break;}
     case 9:{
@@ -321,7 +355,7 @@
 
         }
         else{
-            lcd.printf("from 9->8 : 8 reserved");         
+            lcd.printf("8 is reserved");         
             currTrain->Stop();       
         }
         break;}
@@ -329,13 +363,12 @@
         lcd.printf("At 10");        
 
         dect.clearReservation(C_right); //free nr 6, 7, 8, 9
-        switch1.switchOff();          //Turn off switch1
         if(dect.checkReservation(12)){   //Check if 12 is reserved
-            lcd.printf("12resv. STOP");
+            lcd.printf("12 is reserved");
             currTrain->Stop();           //IF yes STOP
         }
-        else{
-            lcd.printf("12 not reserved");                           
+        else{     
+            switch1.switchOff();          //Turn off switch1                     
             dect.makeReservation(C_left);
             currTrain->changeSpeed(currTrain->normalSpeed());      //go forward
         }     
@@ -351,7 +384,8 @@
             currTrain->changeSpeed(currTrain->normalSpeed());         //go forward   
         }
         else{
-            currTrain->Stop();       
+            currTrain->Stop();   
+            lcd.printf("12 is reserved");    
         }                                                             
         
         break;
@@ -370,7 +404,7 @@
         break;}
     case 14:{
         lcd.printf("at 21");            
-        currTrain->changeSpeed(slow_speed);
+        currTrain->changeSpeed(currTrain->slowlySpeed());
         break;}
     case 15:{
         lcd.printf("at 22");
@@ -403,8 +437,10 @@
     else 
         //PROBLEM
         lcd.printf("Fail to detect int");
-    Train* currTrain = &assignTrain(pos);
-    executeCase(pos, currTrain); 
+    if(pos!=7){
+        Train* currTrain = &assignTrain(pos);
+        executeCase(pos, currTrain); 
+    }
 }
 
 bool readSwitch(DigitalIn theSwitch){
@@ -460,9 +496,10 @@
     vector<int> res;
     res.push_back(2);
     //res.push_back(4);
-    dect.makeReservation(res); //Run trains counterclockwise and reserve 
+    dect.makeReservation(C_left); //Run trains counterclockwise and reserve 
     vector<int> res2;
-    res2.push_back(12);
+    res2.push_back(10);
     //res2.push_back(13);
     dect.makeReservation(res2);
+    dect.showReservation();
 }
\ No newline at end of file