Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Revision:
58:b60db1092088
Parent:
57:ee5da8a011e0
Child:
59:032005c5a495
--- a/main.cpp	Thu Jun 21 15:39:05 2018 +0000
+++ b/main.cpp	Thu Jun 21 16:55:20 2018 +0000
@@ -35,6 +35,7 @@
 DigitalIn station(p15); //Sensor in the middle of the station
 
 //p16
+
 //ENABLE - p17
 DigitalOut enable(p17);
 
@@ -51,7 +52,8 @@
 TextLCD lcd(p22,p21,p23,p24,p25,p26); // RS, E, A4, A5, A6, A7 // ldc.cls() to clear and printf(String up to 16char)
 
 ///p27
-///p28
+
+//I2C - p28
 I2C i2c(p28,p27);
 
 //LED1 - p29
@@ -90,7 +92,7 @@
 #define D21 14
 #define D22 15
 
-
+//Definition of the speeds trains can drive at. Will be interpreted as ints for the program's logic
 #define STOP 0
 #define SLOW 1
 #define MEDIUM 2
@@ -111,9 +113,11 @@
 *
 *get_pos() - 
 *get_prev_cw() - 
-*get_ccw() - 
+*get_next_cw() - 
+*add_prev_ccw() - 
+*add_cnext_ccw() - 
+*add_prev_ccw() -
 *add_prev_cw() - 
-*add_ccw() - 
 *
 **/
 class Position{
@@ -121,6 +125,7 @@
         int position; 
         vector <int> previous_cw;
         vector <int> previous_ccw;
+        
     public:
         Position(int p){
             position = p;
@@ -215,6 +220,7 @@
     }
 }
 
+
 /**
 *Defining areas for train detection and collision logic.
 *area_A_arr/area_B_arr - Arrays that hold the Dsensors for each area, used to initialize the vectors.
@@ -227,29 +233,35 @@
 const vector<int> area_B(area_B_arr,area_B_arr + sizeof(area_B_arr) / sizeof(int));
 
 
-
 /**
 *
 *Train class.
 *
+*@train_address - 
 *@position - 
 *@going_cw - 
+*@speed - 
 *
 *Train(int, bool) - 
-*Train(bool) - 
+*Train(address, s) - 
+*
+*set_speed(int) - 
+*set_position(int) - 
+*set_goes_cw(bool) - 
 *
 *Vector get_next_sensors() - 
-*set_position(int) - 
-*set_goes_cw(bool) - 
 *Position get_position() - 
 *Int get_position_number() - 
 *Bool goes_cw() -
+*Bool is_in_A() - 
+*Bool is_in_B() - 
+*
+*run(int) - 
 *
 **/
 class Train{
     
-    private:       
-        
+    private:               
         unsigned int train_address;    //stop the train        
         Position *position;
         bool going_cw;
@@ -263,25 +275,19 @@
         }
         
         /**
-        * Contructor that takes the address of the train and the speed with default value MEDIUM.
-        */
-        Train(unsigned int address, int s=MEDIUM){
+        *Constructor that takes the address of the train and the speed with default value MEDIUM.
+        **/
+        Train(unsigned int address, int s = MEDIUM){
+            
             train_address = address;
             speed = s;
         }
-        
-        Train(bool cw){ going_cw = cw; }
-                        
+                                
         vector<int> get_next_sensors(){
             
             //Checking direction
-            if(going_cw){
-                
-                return position->get_next_cw();
-            }else{
-                
-                return position->get_next_ccw();
-            }            
+            if(going_cw){return position->get_next_cw();}
+                else{return position->get_next_ccw();}            
         }
         
         void set_speed(int s){
@@ -292,7 +298,7 @@
         *   Sends a DCC command to the train with the speed indicaed by the attribute speed
         *   The number of times the command is sent can be indicated as an optional parameter. Default value is 1.
         */        
-        void run(int times=1){
+        void run(int times = 1){
             
             const unsigned int DCCinst_forward_slow = 0x66; //forward slow speed (step 9)
             const unsigned int DCCinst_forward_medium = 0x68; //forward half speed
@@ -302,21 +308,27 @@
             const unsigned int DCCinst_stop = 0x50;    //stop the train
             
             switch(speed){
-                case STOP:
+                
+                case STOP:                
                     DCC_send_command(train_address, DCCinst_stop,times);
                     break;
+                    
                 case SLOW:
                     DCC_send_command(train_address, DCCinst_forward_slow,times);
                     break;
+                    
                 case MEDIUM:
                     DCC_send_command(train_address, DCCinst_forward_medium,times);
                     break;
+                    
                 case FAST:
                     DCC_send_command(train_address, DCCinst_forward_fast,times);
                     break;
+                    
                 case FULL:
                     DCC_send_command(train_address, DCCinst_forward_full,times);
                     break;
+                    
                 case R_MEDIUM:
                     DCC_send_command(train_address, DCCinst_reverse_medium,times);
                     break;
@@ -348,7 +360,6 @@
             return going_cw;
         }
         
-        
         /**
         *
         *Checks if the element exists within the vector.
@@ -372,13 +383,13 @@
         }
         
         bool is_in_A(){
-            return in_vector(area_A,get_position_number());
             
+            return in_vector(area_A, get_position_number());
         }
         
         bool is_in_B(){
             
-            return in_vector(area_B,get_position_number());
+            return in_vector(area_B, get_position_number());
         }
 };
 
@@ -402,11 +413,6 @@
 Position d22(D22);
 
 
-
-
-
-
-
 //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
 const unsigned int DCCinst_forward = 0x68; //forward half speed
 const unsigned int DCCinst_forward_slow = 0x66; //forward slow speed (step 9)
@@ -613,9 +619,6 @@
 }
 
 
-
-
-
 /**
 *This method will check if there is a non-avoidable frontal collision(NAFC).
 *A NAFC will happen if:
@@ -650,6 +653,7 @@
     return NAC;     
 }
 
+
 /*
 void AFC_action(int switch_n, int sensor, Train *stop_train, Train * cont_train ){
     
@@ -665,6 +669,8 @@
 }
 */
 
+
+
 /**
 *
 *The function will check if there is an Avoidable Frontal Collision (AFC).
@@ -745,30 +751,30 @@
 *
 **/
 void update_train_pos(int sensor){
-    led2 = 1;
     
     bool found_DR = false;
     bool found_LR = false;
-    
-    string DR_dir,LR_dir;
+    string DR_dir, LR_dir;
     
     if(DR_train.goes_cw()){
+        
         DR_dir = "cw";
     }else{
+        
         DR_dir = "ccw";
     }
     
     if(LR_train.goes_cw()){
+        
         LR_dir = "cw";
     }else{
+        
         LR_dir = "ccw";
     }    
-    
-    
-    //wait(0.7);   
-    
+        
     if(sensor == DR_train.get_position_number() || sensor == LR_train.get_position_number()){
-        led2 = 0;        
+        //Ignore
+        
     }else{
         
         lcd.cls();
@@ -858,9 +864,7 @@
             lcd.printf("No train before :(");
         }
         */
-        
     }
-    
 }
 
 
@@ -897,12 +901,13 @@
      update_train_pos(sensor);
 }
 
+
 /**
 *
 *Clear current interrupts 
 *
 **/
-void init() { 
+void init(){ 
 
     mcp->_read(GPIOA); 
     mcp->_read(GPIOB); // Register callbacks 
@@ -910,10 +915,7 @@
     int1.fall(&on_int1_change); // Enable interrupts on MCP 
     mcp->_write(GPINTENA, (unsigned char )0xff); 
     mcp->_write(GPINTENB, (unsigned char )0xff); // Ready to go! 
-  }
-
-
-
+}
 
 
 /**
@@ -1013,29 +1015,33 @@
     bool exit = false;
     
     while(!exit){
+        
         if(switch3 == 0){
+            
             if(changed){
+               
                sensor++;
                sensor=sensor%15; //Only sensors from 0 to 15.
                changed=false; 
                lcd.cls();
                lcd.printf("%s: D%d",train,sensor);
             }
+        }else{
             
-        }else{
             changed = true;
             wait(0.2);
         }
         
         if(switch4 == 0){
+            
             exit = true;
             wait(0.2);
         }
     }
-    
     return sensor;
 }
 
+
 /**
 * Returns a boolean representing the direction. Everytimew switch3 is 0 it changes the direction.
 * When switch4 is 0 the selection is confirmed.
@@ -1047,8 +1053,10 @@
     string dir_string;
     
     if(init_going_cw){
+        
         dir_string = "cw";
     }else{
+        
         dir_string = "ccw";
     }
     
@@ -1060,33 +1068,76 @@
     bool changed = false;
     
     while(!exit){
+        
         if(switch3 == 0){
+            
             if(changed){
+                
                 going_cw = !going_cw;
                 changed = false;
                 string dir;
+                
                 if(going_cw){
+                    
                     dir = "cw";
                 }else{
+                    
                     dir = "ccw";
                 }
+        
                 lcd.cls();
                 lcd.printf("%s: %s",train,dir);
             }
         }else{
+            
             changed = true;
             wait(0.2);
         }
         
         if(switch4 == 0){
+            
             exit = true;
             wait(0.2);
         }
     }
-    
     return going_cw;
 }
 
+
+/**
+*
+*
+*
+**/
+void adjustSpeed(){
+    
+    float f = pot.read();
+    float vin = f * 3.3;
+    lcd.cls();
+   // lcd.printf("vin: %.4f",vin);
+    
+    if(0=< vin && vin< 0.60){
+        
+        //speed = slow
+        lcd.printf("SLOW AF");       
+        }else if(0.60 < vin && vin< 1.20){
+            
+            //speed medium
+            lcd.printf("MEDIUM");
+            }else if(1.20 < vin && vin< 2.20){
+                
+                //speed fast
+                lcd.printf("going fast boii");
+                }else if(2.20 < vin && vin<3.20){
+                    
+                    //speed full
+                    lcd.printf("full POWAH BABY");
+                    }
+}
+
+
+
+
 //**************** MAIN PROGRAM FOR DENVER TRAIN ****************//