To Record the time of the car

Dependencies:   mbed TextLCD

Revision:
2:c9889e740474
Parent:
1:db7799912cac
Child:
3:70ce5fd299bb
--- a/main.cpp	Fri Aug 07 17:10:27 2020 +0000
+++ b/main.cpp	Tue Aug 11 17:46:58 2020 +0000
@@ -2,13 +2,16 @@
     #include "mbed.h"
     #include "TextLCD.h"   
 //Define-USER SET
-    #define DEBUG                    1           //for debug , if formal just 
-    #define NO_DELAY                 1           //if define RESULT == 1, ignore delay time of RF
-    #define ACCEPT_LOCK              0           //if define ACCEPT LOCK == 1,need receive ACCEPT before next tx
+    #define DEBUG                    2           // 0: formal , 1: all information, 2: only delay time
+    #define NO_DELAY                 0           //if define RESULT == 1, ignore delay time of RF , if DEBUG == 2, this must be 0
     #define RX_MODE                  1           //1 for scanf ; 2 for getc 
     #define BUTTON_DEBOUNCE       0.3f
     #define LCD_SHOW_PERIOD       3.0f           //show last cycle for 2 sec
+    #define SENSOR_IN_DEBOUNCE    0.2f
 //Define-Don't Change
+    #if(DEBUG == 2 && NO_DELAY == 1)
+    #define NO_DELAY 0
+    #endif
     #define m_1             1000000.0f
     #define TX_SIZE                 16
     #define RX_SIZE                 16
@@ -47,13 +50,13 @@
     I2C i2c_lcd(PB_9,PB_8);                     // SDA,SCL 
     TextLCD_I2C lcd(&i2c_lcd, 0x40, TextLCD::LCD16x2);     //0x40 for all short(blue lcd) , 0x4E for A1 to A3 open(green lcd) - I2C Address
 //Sensor
-#if(DEBUG)
+//#if(DEBUG)
     DigitalOut  sensor_com(PA_8,1);             // D7
     InterruptIn SENSOR_IT(PB_4,PullDown);      //  D5
-#else
-    DigitalOut  sensor_com(PB_2,1);
-    InterruptIn SENSOR_IT(PB_1,PullDown);
-#endif
+//#else
+//    DigitalOut  sensor_com(PB_2,1);
+//    InterruptIn SENSOR_IT(PB_1,PullDown);
+//#endif
 //BT
     Serial bt (PA_0,PA_1,115200);              // Serial_4 tx,rx
     DigitalOut key(PC_1,0);                    // not AT Mode
@@ -76,6 +79,7 @@
 /**/ volatile int process = 0;                  //process 0:for ready ; 1:sensor in/confirm master or slave ; 2:calculation : 3:bt show 4 lcd show 
 //  time record
 /**/ Timer this_cycle_timer;                     //keep running timer
+     float sensor_in_debounce = 0.0;
 /**/ float total_time = 0.0;
 /**/ float last_cycle_time = 0.0;               //for LC print
 /**/ float best_cycle_time = 0.0;
@@ -91,9 +95,6 @@
      bool  rf_state = 0;                         //0 for disconnect
      bool  last_rf_state = 1;
      bool  Rx_flag = 0;                          //1 when receive a cmd over
-#if (ACCEPT_LOCK == 1)
-     bool  rf_accept_lock = 0;
-#endif
 /**/ int   role = 0;
 /**/ int   this_tcmd = 0;
 /**/ int   last_tcmd = 0;
@@ -151,16 +152,17 @@
     {
         Initial_All();
         while(1) {
-            if(!role && mode == 1) {
+            if(role == 0 && mode == 1) {
                 rf_check_timer.start();
                 if(rf_check_timer.read() > 1){
                     rf_interval_cnt++;
                     rf_check_timer.reset();
                 }
-                if(rf_interval_cnt % 5 && rf_interval_cnt) {
+                if(rf_interval_cnt % 5 == 0 && rf_interval_cnt) {
                     RF_Send(CHECK);
                 }
                 if(rf_interval_cnt > 5) rf_state = 0;
+                if(rf_interval_cnt > 20000) rf_interval_cnt = 0;
             }
             else rf_check_timer.stop();
             time_cal();
@@ -227,14 +229,15 @@
         bt_print_flag = 0;
         this_cycle_timer.stop();
         this_cycle_timer.reset();
+        lcd_end_flag = 0;
+        bt_end_flag = 0;
         if( mode != 1 || (mode == 1 && this_rcmd == RESTART) || (mode == 1 && last_tcmd == RESTART) ){
             cycle = 1;
             best_cycle = 1;
             last_cycle_time = 0.0;
             best_cycle_time = 0.0;
             total_time = 0.0;
-            lcd_end_flag = 0;
-            bt_end_flag = 0;
+
         }
         if(mode == 1){
             memset(TX_buffer,0,TX_SIZE);
@@ -277,12 +280,18 @@
     void lcd_show(){
     //process 4 update 
         lcd.locate(0,0);
-        if( (!lcd_print_flag)&& last_cycle_time > 0.0f && (process == 4 && cycle >= 1 || mode == 1 && process == 4 && cycle >= 1)){
+        if( (!lcd_print_flag) && last_cycle_time > 0.0f && process == 4){
         //print out last cycle time flag
             lcd_print_flag = 1;
             lcd_show_result_time = us_ticker_read()/m_1;
         }
-        else if((!lcd_end_flag) && (cycle > SKIDPAD_CYCLE && mode == 2 || cycle > ENDURANCE_CYCLE && mode == 3)){
+        else if((!lcd_end_flag) && ( (cycle > SKIDPAD_CYCLE && mode == 2 || cycle > ENDURANCE_CYCLE && mode == 3) || 
+        #if(NO_DELAY == 1)
+                (last_tcmd == RESULT || this_rcmd == RESULT && mode == 1) )
+        #else    
+                (last_tcmd == DELAY || this_rcmd == DELAY && mode == 1) )
+        #endif
+        ){
         //mode end
             if(us_ticker_read()/m_1 - lcd_show_result_time > LCD_SHOW_PERIOD){
                 lcd_end_flag = 1;
@@ -291,9 +300,9 @@
         }
         
         //row 1 show
-        if(us_ticker_read()/m_1 - lcd_show_result_time < LCD_SHOW_PERIOD && last_cycle_time != 0)
+        if(us_ticker_read()/m_1 - lcd_show_result_time < LCD_SHOW_PERIOD && last_cycle_time != 0 && mode != 1)
             lcd.printf("LCT : %.2f sec  ",last_cycle_time);
-        else if(us_ticker_read()/m_1 - lcd_end_time < LCD_SHOW_PERIOD && last_cycle_time != 0)
+        else if(us_ticker_read()/m_1 - lcd_end_time < LCD_SHOW_PERIOD && last_cycle_time != 0 && mode != 1)
             lcd.printf("BC  :%d         ",best_cycle);
         else{
             switch(mode){
@@ -313,9 +322,10 @@
         }
         lcd.locate(0,1);
     //row 2 show
-        if(cycle == 1 && process == 0) lcd.printf("Ready to Go   !");                   //first in 
-        else if(lcd_end_flag) lcd.printf("BCT :%.2f sec",best_cycle_time);
+        if(cycle == 1 && this_cycle_timer.read() == 0) lcd.printf("Ready to Go   !");                   //first in 
+        else if(mode == 1 && lcd_print_flag)lcd.printf ("TC:%.2f sec  ",last_cycle_time);
         else if(mode == 1) lcd.printf("TC:%.2f sec  ",this_cycle_timer.read());
+        else if(lcd_end_flag && mode!=1) lcd.printf("BCT :%.2f sec",best_cycle_time);
         else if((cycle > SKIDPAD_CYCLE && mode == 2 || cycle > ENDURANCE_CYCLE && mode == 3)) lcd.printf("Mode %d End!   ",mode);
         else lcd.printf("%.2f,%.2f       ",this_cycle_timer.read(),this_cycle_timer.read()+total_time);
     //lcd life check   
@@ -333,12 +343,13 @@
             if (last_cycle_time == 0.0f) bt.printf("First in\r\n");
             else {
                 bt.printf("Mode:%d,cycle: %d,last cycle time:%.2f sec\r\n",mode,cycle,last_cycle_time);
-                if(cycle == SKIDPAD_CYCLE && mode == 2 ||  cycle == ENDURANCE_CYCLE && mode == 3){
+                if(cycle == SKIDPAD_CYCLE && mode == 2 ||  cycle == ENDURANCE_CYCLE && mode == 3 ){
                     bt.printf("Best Cycle:%d\r\n",best_cycle);
                     bt.printf("Best Cycle Time:%.2f sec\r\n",best_cycle_time);
                     bt.printf("Mode %d End & Reset\r\n",mode);
                     bt_end_flag = 1;
-                }   
+                } 
+                else if(mode == 1) bt_end_flag = 1;  
             }
         }
         else if(process == 3){
@@ -367,20 +378,20 @@
     }
     void show_flag_check(){
     //cycle update for mode != ACC
-        if(lcd_print_flag && bt_print_flag){
+        if(lcd_print_flag && bt_print_flag && mode != 1){
             //print ok
             process = 0;
             lcd_print_flag = 0;
             bt_print_flag = 0;
-            if(mode != 1) {
-                cycle++;
-            }
+            cycle++;
+            
         }
         if(lcd_end_flag && bt_end_flag){
         //notice reset
-            if(mode == 1 && us_ticker_read()/m_1 - lcd_end_time > LCD_SHOW_PERIOD) RF_Send(RESET);   //including local reset
+            if(mode == 1 && us_ticker_read()/m_1 - lcd_end_time > LCD_SHOW_PERIOD && lcd_end_flag )  RF_Send(RESET);   //including local reset
             else if(us_ticker_read()/m_1 - lcd_end_time > LCD_SHOW_PERIOD) reset_parameters();
         }
+        
     }
     void time_cal(){
         if(process == 1){
@@ -402,201 +413,193 @@
     }
 //RF communication function  , must end with \n ; //cycle update for mode ACC
     void RF_Send(int rf_tcmd){
-    #if (ACCEPT_LOCK == 1)
-        if(!rf_accept_lock || rf_tcmd == ACCEPT){
-    #endif
-            if(rf_tcmd != ACCEPT) this_tcmd = rf_tcmd;
-            switch(rf_tcmd){
+        if(rf_tcmd != ACCEPT) this_tcmd = rf_tcmd;
+        switch(rf_tcmd){
+           case ACCEPT:
+              RF.printf("%d\n",ACCEPT);
+              rf_state = 1;
+              rf_interval_cnt = -4;
+              break;
+           case DENY:
+              RF.printf("%d\n",DENY);
+              break;
+           case CHECK:
+           //以後加上cycle check
+              RF.printf("%d\n",CHECK);
+              break;
+           case MASTER:
+              role = 1;
+              RF.printf("%d\n",MASTER);
+              if(bt_state.read()) bt.printf("Car in \r\n");
+              else pc.printf("Car in \r\n");
+              break;
+           case SLAVE:
+              RF.printf("%d&%.2f\n",SLAVE,this_cycle_timer.read());
+              break;
+           case RESULT:
+              RF.printf("%d&%.2f\n",RESULT,this_cycle_timer.read());
+              break;
+           case DELAY:
+           //delay time abnormal detect
+              if(rf_delay_time < 1.0f) {
+                  RF.printf("%d&%.4f\n",DELAY,rf_delay_time);
+              #if (DEBUG == 1 || DEBUG == 2)
+                  if(bt_state.read()) bt.printf("Delay time:%.4f\r\n",rf_delay_time);
+                  else pc.printf("Delay time:%.4f\r\n",rf_delay_time);
+              #endif
+              }
+              else if(bt_state.read()) bt.printf("rf delay time > 1sec \r\n");
+              else pc.printf("rf delay time > 1 sec\r\n");
+              break;
+           case RESET:
+              cycle++;
+              reset_parameters();
+              if(bt_state.read()) bt.printf("ACC Set \r\n");
+              else pc.printf("ACC Set \r\n");
+              break;
+           case RESTART:
+              RF.printf("%d\n",RESTART);
+              //reset_parameters at button_fall_2
+              break;
+           default:
+              if(bt_state.read()) bt.printf("Unknown Tcmd\r\n");
+              else pc.printf("Unknown Tcmd\r\n");
+              break;
+        }
+        last_tcmd = this_tcmd;
+    }
+
+    void RF_Receive(int rf_rcmd){
+        if(mode == 1){
+            this_rcmd = rf_rcmd;
+        #if(DEBUG == 1) 
+            //if(bt_state.read()) bt.printf("this rcmd:%d\r\n",this_rcmd);
+            //else pc.printf("this rcmd:%d\r\n",this_rcmd);
+        #endif
+            switch(rf_rcmd){
                case ACCEPT:
-                  RF.printf("%d\n",ACCEPT);
                   rf_state = 1;
-                  rf_interval_cnt = -4;
+                  rf_interval_cnt = 0;
                   break;
                case DENY:
-                  RF.printf("%d\n",DENY);
+                  switch(last_tcmd){
+                  //master
+                    case MASTER:
+                      bt.printf("MASTER Request Denied!\r\n");
+                      break;
+                  //slave  //發送時間待補
+                    case SLAVE:
+                      bt.printf("SLAVE Request Denied!\r\n");
+                      break;
+                  //result
+                    case RESULT:
+                      bt.printf("RESULT Request Denied!\r\n");
+                      break;
+                  }
                   break;
                case CHECK:
-               //以後加上cycle check
-                  RF.printf("%d\n",CHECK);
+                  RF_Send(ACCEPT);
                   break;
                case MASTER:
-                  RF.printf("%d\n",MASTER);
-                  if(bt_state.read()) bt.printf("Car in \r\n");
-                  else pc.printf("Car in \r\n");
+               //will become Slave End
+                  if(role == 0){
+                      role = 2;
+                      if(bt_state.read()) bt.printf("Car in \r\n");
+                      else pc.printf("Car in \r\n");
+                  }
+                  else RF_Send(DENY);
                   break;
                case SLAVE:
-                  RF.printf("%d&%.2f\n",SLAVE,this_cycle_timer.read());
+               /*should be Master End*/
+               //calculate Delay time
+                  char slave_time[5];
+                  for(int i=0;i<5;i++) slave_time[i] = RX_buffer[i+2];
+                  rf_delay_time = (float)(( this_cycle_timer.read()-atof(slave_time) )/2);
+                  last_cycle_time = this_cycle_timer.read()-rf_delay_time;
+               //send back Delay time
+                  wait_ms(10);
+                  RF_Send(DELAY);
+                  wait_ms(10);
+               //Role off and interval = 0 ->maybe at reset?
+                  process = 3;
                   break;
                case RESULT:
-                  RF.printf("%d&%.2f\n",RESULT,this_cycle_timer.read());
+               //should be Master End
+                  char result_time[5];
+                  for(int i=0;i<5;i++) result_time[i] = RX_buffer[i+2];
+                  last_cycle_time = atof(result_time);
+                  process = 3;
                   break;
                case DELAY:
-               //delay time abnormal detect
-                  if(rf_delay_time < 1.0f) {
-                      RF.printf("%d&%.4f\n",DELAY,rf_delay_time);
-                  #if (DEBUG == 1)
-                      if(bt_state.read()) bt.printf("Delay time:%.4f\r\n",rf_delay_time);
-                      else pc.printf("Delay time:%.4f\r\n",rf_delay_time);
-                  #endif
-                  }
-                  else if(bt_state.read()) bt.printf("rf delay time > 1sec \r\n");
-                  else pc.printf("rf delay time > 1 sec");
+               // should be Slave End
+                  char delay_time [5];
+                  for(int i=0;i<5;i++) delay_time[i] = RX_buffer[i+2];
+                  rf_delay_time = atof(delay_time);
+                  last_cycle_time = this_cycle_timer.read()+ rf_delay_time;
+                  process = 3;
+                #if(DEBUG == 1 || DEBUG == 2)
+                  if(bt_state.read()) bt.printf("Delay time:%.4f \r\n",rf_delay_time);
+                  else pc.printf("Delay time:%.4f \r\n",rf_delay_time);
+                #endif
                   break;
                case RESET:
                   cycle++;
                   reset_parameters();
-                  if(bt_state.read()) bt.printf("ACC Set \r\n");
-                  else pc.printf("ACC Set \r\n");
+                  if(bt_state.read()) bt.printf("ACC Set\r\n");
+                  else pc.printf("ACC Set\r\n");
                   break;
                case RESTART:
-                  RF.printf("%d\n",RESTART);
-                  //reset_parameters at button_fall_2
+                  reset_parameters();
+                  if(bt_state.read()) bt.printf("ACC Restart\r\n");
+                  else pc.printf("ACC Restart\r\n");
                   break;
                default:
-                  if(bt_state.read()) bt.printf("Unknown Tcmd\r\n");
-                  else pc.printf("Unknown Tcmd\r\n");
+                  if(bt_state.read()) bt.printf("Unknown Rcmd\r\n");
+                  else pc.printf("Unknown Rcmd\r\n");
                   break;
             }
-    #if(ACCEPT_LOCK == 1)
-        }
-        if(rf_tcmd != ACCEPT){
-        //tx disable
-            rf_accept_lock = 1;
-        }
-    #else
-        last_tcmd = this_tcmd;
-    #endif
-    }
-
-    void RF_Receive(int rf_rcmd){
-        this_rcmd = rf_rcmd;
-    #if(DEBUG == 1) 
-        if(bt_state.read()) bt.printf("this rcmd:%d\r\n",this_rcmd);
-        else pc.printf("this rcmd:%d\r\n",this_rcmd);
-    #endif
-        switch(rf_rcmd){
-           case ACCEPT:
-        #if(ACCEPT_LOCK == 1)
-              if(rf_accept_lock){
-                  rf_accept_lock = 0;
-                  last_tcmd = this_tcmd;
-              }
-        #endif
-              rf_state = 1;
-              rf_interval_cnt = 0;
-              break;
-           case DENY:
-              switch(last_tcmd){
-              //master
-                case MASTER:
-                  bt.printf("MASTER Request Denied!\r\n");
-                  break;
-              //slave  //發送時間待補
-                case SLAVE:
-                  bt.printf("SLAVE Request Denied!\r\n");
-                  break;
-              //result
-                case RESULT:
-                  bt.printf("RESULT Request Denied!\r\n");
-                  break;
-              }
-              break;
-           case CHECK:
-              RF_Send(ACCEPT);
-              break;
-           case MASTER:
-           //will become Slave End
-              if(!role){
-                  role = 2;
-                  if(bt_state.read()) bt.printf("Car in \r\n");
-                  else pc.printf("Car in \r\n");
-              }
-              else RF_Send(DENY);
-              break;
-           case SLAVE:
-           /*should be Master End*/
-           //calculate Delay time
-              char slave_time[5];
-              for(int i=0;i<5;i++) slave_time[i] = RX_buffer[i+2];
-              rf_delay_time = (float)(( this_cycle_timer.read()-atof(slave_time) )/2);
-              last_cycle_time = this_cycle_timer.read()-rf_delay_time;
-           //send back Delay time
-              wait_ms(10);
-              RF_Send(DELAY);
-              wait_ms(10);
-           //Role off and interval = 0 ->maybe at reset?
-              process = 3;
-              break;
-           case RESULT:
-           //should be Master End
-              char result_time[5];
-              for(int i=0;i<5;i++) result_time[i] = RX_buffer[i+2];
-              last_cycle_time = atof(result_time);
-              process = 3;
-              break;
-           case DELAY:
-           // should be Slave End
-              char delay_time [5];
-              for(int i=0;i<5;i++) delay_time[i] = RX_buffer[i+2];
-              rf_delay_time = atof(delay_time);
-              last_cycle_time = this_cycle_timer.read()+ rf_delay_time;
-              process = 3;
-              if(bt_state.read()) bt.printf("Delay time:%.4f",rf_delay_time);
-              break;
-           case RESET:
-              cycle++;
-              reset_parameters();
-              if(bt_state.read()) bt.printf("ACC Set\r\n");
-              else pc.printf("ACC Set\r\n");
-              break;
-           case RESTART:
-              reset_parameters();
-              if(bt_state.read()) bt.printf("ACC Restart\r\n");
-              else pc.printf("ACC Restart\r\n");
-              break;
-           default:
-              if(bt_state.read()) bt.printf("Unknown Rcmd\r\n");
-              else pc.printf("Unknown Rcmd\r\n");
-              break;
         }
     }
     
 //IT
     void sensor_in(){
-        if(mode!=1){
-            if(cycle == 1){
-                this_cycle_timer.start();
-                process = 1;
-            }
-            if(this_cycle_timer.read() > CYCLE_TIME_PROTECT){
-                process = 1;
-                last_cycle_time = this_cycle_timer.read();
-                this_cycle_timer.reset();
-            }
-        //check mode2,3
-            if((cycle > SKIDPAD_CYCLE && mode == 2 || cycle > ENDURANCE_CYCLE && mode == 3)){
-                process = 0;
+        if(us_ticker_read()/m_1 - sensor_in_debounce > SENSOR_IN_DEBOUNCE){
+            sensor_in_debounce = us_ticker_read()/m_1;
+            if(mode!=1){
+                if(cycle == 1){
+                    this_cycle_timer.start();
+                    process = 1;
+                }
+                if(this_cycle_timer.read() > CYCLE_TIME_PROTECT){
+                    process = 1;
+                    last_cycle_time = this_cycle_timer.read();
+                    this_cycle_timer.reset();
+                }
+            //check mode2,3
+                if((cycle > SKIDPAD_CYCLE && mode == 2 || cycle > ENDURANCE_CYCLE && mode == 3)){
+                    process = 0;
+                }
             }
-        }
-        else if(mode == 1){
-        //mode acc
-            if(!role && rf_state){
-            //MASTER
-                this_cycle_timer.start();
-                RF_Send(MASTER);
-            }
-            if(role == 2 && this_cycle_timer.read() > CYCLE_TIME_PROTECT){
-                this_cycle_timer.stop();
-            //SLAVE
-            #if (NO_DELAY == 1)
-            //this_cycle_timer.start() at RxCallback
-                RF_Send(RESULT);
-                last_cycle_time = this_cycle_timer.read();
-                process = 3;
-            #else
-            //with delay calculation in Master End
-                RF_Send(SLAVE);
-            #endif
+            else if(mode == 1){
+            //mode acc
+                if(!role && rf_state){
+                //MASTER
+                    this_cycle_timer.start();
+                    RF_Send(MASTER);
+                }
+                if(role == 2 && this_cycle_timer.read() > CYCLE_TIME_PROTECT){
+                    this_cycle_timer.stop();
+                //SLAVE
+                #if (NO_DELAY == 1)
+                //this_cycle_timer.start() at RxCallback
+                    RF_Send(RESULT);
+                    last_cycle_time = this_cycle_timer.read();
+                    process = 3;
+                #else
+                //with delay calculation in Master End
+                    RF_Send(SLAVE);
+                #endif
+                }
             }
         }
     }
@@ -606,6 +609,8 @@
         if(button_RTC - button_LTC > BUTTON_DEBOUNCE){
             button_LTC = button_RTC;
             (mode >= 3 || mode < 0) ? mode = 0 : mode++ ;
+            (mode == 1)?RF.attach(&RxCallback,Serial::RxIrq):RF.attach(NULL,Serial::RxIrq);
+            if(mode != 1) rf_state = 0;
             last_tcmd = RESTART;
             reset_parameters();
             last_tcmd = ACCEPT;
@@ -621,8 +626,11 @@
             if(mode == 1) RF_Send(RESTART);
             reset_parameters();
             if(bt_state.read() ){
-                bt.printf("Restart!\r\n\r\n\r\n");
-                bt.printf("#####################\r\n");
+                if(mode == 1) bt.printf("ACC Restart\r\n");
+                else {
+                    bt.printf("Restart!\r\n\r\n\r\n");
+                    bt.printf("#####################\r\n");
+                }
             }
             else{
                 pc.printf("Restart!\r\n\r\n\r\n");
@@ -648,17 +656,18 @@
                     else if(RX_buffer[0]-'0' == SLAVE || RX_buffer[0]-'0' == RESULT) this_cycle_timer.stop();
                 }
             }
-            else {
+            else if(new_char == '\n') {
                 rx_cnt = 0;
                 Rx_flag = 1;
             }
         }
     #endif
     #if(DEBUG == 1)
-        if(bt_state.read()&& !rx_cnt) bt.printf("RX Callback,%s has been received\r\n",RX_buffer);
+        if(bt_state.read()&& !rx_cnt) bt.printf("RX Callback,'%s' has been received\r\n",RX_buffer);
     #endif
         if(Rx_flag == 1){
             RF_Receive(RX_buffer[0]-'0');
             Rx_flag = 0;
+            //清空rx??
         }
     }