![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
To Record the time of the car
main.cpp@6:f5a4aa4a3fe4, 2020-08-12 (annotated)
- Committer:
- Dennis40816
- Date:
- Wed Aug 12 03:59:37 2020 +0000
- Revision:
- 6:f5a4aa4a3fe4
- Parent:
- 5:a897228b7306
- Child:
- 7:db04197d3c6a
1.1Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dennis40816 | 6:f5a4aa4a3fe4 | 1 | /* |
Dennis40816 | 6:f5a4aa4a3fe4 | 2 | Version 1.1 created at 2020/8/12 --- Dennis Liu |
Dennis40816 | 6:f5a4aa4a3fe4 | 3 | */ |
Dennis40816 | 6:f5a4aa4a3fe4 | 4 | |
Dennis40816 | 0:9c3829968b11 | 5 | //Include |
Dennis40816 | 0:9c3829968b11 | 6 | #include "mbed.h" |
Dennis40816 | 0:9c3829968b11 | 7 | #include "TextLCD.h" |
Dennis40816 | 0:9c3829968b11 | 8 | //Define-USER SET |
Dennis40816 | 2:c9889e740474 | 9 | #define DEBUG 2 // 0: formal , 1: all information, 2: only delay time |
Dennis40816 | 2:c9889e740474 | 10 | #define NO_DELAY 0 //if define RESULT == 1, ignore delay time of RF , if DEBUG == 2, this must be 0 |
Dennis40816 | 0:9c3829968b11 | 11 | #define RX_MODE 1 //1 for scanf ; 2 for getc |
Dennis40816 | 0:9c3829968b11 | 12 | #define BUTTON_DEBOUNCE 0.3f |
Dennis40816 | 1:db7799912cac | 13 | #define LCD_SHOW_PERIOD 3.0f //show last cycle for 2 sec |
Dennis40816 | 2:c9889e740474 | 14 | #define SENSOR_IN_DEBOUNCE 0.2f |
Dennis40816 | 4:1300ca51fc75 | 15 | #define NVIC_RESET_PROTECT 3.0f //all sys reset by pull blue button(button 2) for more than 4 seconds |
Dennis40816 | 0:9c3829968b11 | 16 | //Define-Don't Change |
Dennis40816 | 2:c9889e740474 | 17 | #if(DEBUG == 2 && NO_DELAY == 1) |
Dennis40816 | 2:c9889e740474 | 18 | #define NO_DELAY 0 |
Dennis40816 | 2:c9889e740474 | 19 | #endif |
Dennis40816 | 0:9c3829968b11 | 20 | #define m_1 1000000.0f |
Dennis40816 | 0:9c3829968b11 | 21 | #define TX_SIZE 16 |
Dennis40816 | 0:9c3829968b11 | 22 | #define RX_SIZE 16 |
Dennis40816 | 3:70ce5fd299bb | 23 | #if (DEBUG != 1) |
Dennis40816 | 0:9c3829968b11 | 24 | #define CYCLE_TIME_PROTECT 2.0f //used in formal , protect someone trigger the sensor |
Dennis40816 | 0:9c3829968b11 | 25 | #else |
Dennis40816 | 1:db7799912cac | 26 | #define CYCLE_TIME_PROTECT 1.0f |
Dennis40816 | 0:9c3829968b11 | 27 | #endif |
Dennis40816 | 4:1300ca51fc75 | 28 | #define ENDURANCE_CYCLE 11 |
Dennis40816 | 0:9c3829968b11 | 29 | #define SKIDPAD_CYCLE 4 |
Dennis40816 | 0:9c3829968b11 | 30 | |
Dennis40816 | 0:9c3829968b11 | 31 | //RF CMD |
Dennis40816 | 0:9c3829968b11 | 32 | #define ACCEPT 0 |
Dennis40816 | 0:9c3829968b11 | 33 | #define DENY 1 |
Dennis40816 | 0:9c3829968b11 | 34 | #define CHECK 2 |
Dennis40816 | 0:9c3829968b11 | 35 | #define MASTER 3 |
Dennis40816 | 0:9c3829968b11 | 36 | #define SLAVE 4 |
Dennis40816 | 0:9c3829968b11 | 37 | #define RESULT 5 |
Dennis40816 | 0:9c3829968b11 | 38 | #define DELAY 6 |
Dennis40816 | 0:9c3829968b11 | 39 | #define RESET 7 //cycle reset |
Dennis40816 | 0:9c3829968b11 | 40 | #define RESTART 8 //mode all reset |
Dennis40816 | 5:a897228b7306 | 41 | //mode |
Dennis40816 | 5:a897228b7306 | 42 | #define SING 0 |
Dennis40816 | 5:a897228b7306 | 43 | #define ACC 1 |
Dennis40816 | 5:a897228b7306 | 44 | #define SKID 2 |
Dennis40816 | 5:a897228b7306 | 45 | #define ENDU 3 |
Dennis40816 | 5:a897228b7306 | 46 | //role |
Dennis40816 | 5:a897228b7306 | 47 | #define NONE 0 |
Dennis40816 | 5:a897228b7306 | 48 | #define ROLE_MASTER 1 |
Dennis40816 | 5:a897228b7306 | 49 | #define ROLE_SLAVE 2 |
Dennis40816 | 0:9c3829968b11 | 50 | |
Dennis40816 | 0:9c3829968b11 | 51 | /* |
Dennis40816 | 0:9c3829968b11 | 52 | ############################### |
Dennis40816 | 0:9c3829968b11 | 53 | # Pin Init # |
Dennis40816 | 0:9c3829968b11 | 54 | # functions # |
Dennis40816 | 0:9c3829968b11 | 55 | ############################### |
Dennis40816 | 0:9c3829968b11 | 56 | */ |
Dennis40816 | 0:9c3829968b11 | 57 | //simulate the sensor |
Dennis40816 | 0:9c3829968b11 | 58 | // Serial Tutorial : http://www.stmcu.org.cn/module/forum/thread-608234-1-1.html |
Dennis40816 | 0:9c3829968b11 | 59 | //PC |
Dennis40816 | 0:9c3829968b11 | 60 | Serial pc(USBTX,USBRX,115200); |
Dennis40816 | 0:9c3829968b11 | 61 | //Wireless |
Dennis40816 | 0:9c3829968b11 | 62 | Serial RF(PC_10,PC_11,9600); //Default , product url: http://www.wenshing.com.tw/upload/file/1537953630.pdf |
Dennis40816 | 0:9c3829968b11 | 63 | //LCD |
Dennis40816 | 0:9c3829968b11 | 64 | I2C i2c_lcd(PB_9,PB_8); // SDA,SCL |
Dennis40816 | 0:9c3829968b11 | 65 | TextLCD_I2C lcd(&i2c_lcd, 0x40, TextLCD::LCD16x2); //0x40 for all short(blue lcd) , 0x4E for A1 to A3 open(green lcd) - I2C Address |
Dennis40816 | 0:9c3829968b11 | 66 | //Sensor |
Dennis40816 | 4:1300ca51fc75 | 67 | #if(DEBUG == 1) |
Dennis40816 | 0:9c3829968b11 | 68 | DigitalOut sensor_com(PA_8,1); // D7 |
Dennis40816 | 1:db7799912cac | 69 | InterruptIn SENSOR_IT(PB_4,PullDown); // D5 |
Dennis40816 | 4:1300ca51fc75 | 70 | #else |
Dennis40816 | 4:1300ca51fc75 | 71 | DigitalOut sensor_com(PB_2,1); |
Dennis40816 | 4:1300ca51fc75 | 72 | InterruptIn SENSOR_IT(PB_1,PullDown); |
Dennis40816 | 4:1300ca51fc75 | 73 | #endif |
Dennis40816 | 0:9c3829968b11 | 74 | //BT |
Dennis40816 | 0:9c3829968b11 | 75 | Serial bt (PA_0,PA_1,115200); // Serial_4 tx,rx |
Dennis40816 | 0:9c3829968b11 | 76 | DigitalOut key(PC_1,0); // not AT Mode |
Dennis40816 | 0:9c3829968b11 | 77 | DigitalIn bt_state(PA_4,PullDown); // high when connected with terminal |
Dennis40816 | 0:9c3829968b11 | 78 | //Mode Button |
Dennis40816 | 1:db7799912cac | 79 | DigitalOut button_high(PA_11,1); // 最右7 |
Dennis40816 | 1:db7799912cac | 80 | InterruptIn MY_BUTTON(PB_12,PullDown); // 最右8 |
Dennis40816 | 0:9c3829968b11 | 81 | //Reset Button |
Dennis40816 | 1:db7799912cac | 82 | DigitalOut button_high_2(PA_6,1); // Mode的左邊 |
Dennis40816 | 0:9c3829968b11 | 83 | InterruptIn MY_BUTTON_2(PA_7,PullDown); |
Dennis40816 | 0:9c3829968b11 | 84 | /* |
Dennis40816 | 0:9c3829968b11 | 85 | ############################### |
Dennis40816 | 0:9c3829968b11 | 86 | # Global # |
Dennis40816 | 0:9c3829968b11 | 87 | # Variable # |
Dennis40816 | 0:9c3829968b11 | 88 | ############################### |
Dennis40816 | 0:9c3829968b11 | 89 | */ |
Dennis40816 | 0:9c3829968b11 | 90 | // main process |
Dennis40816 | 0:9c3829968b11 | 91 | // /**/ -> need reset |
Dennis40816 | 5:a897228b7306 | 92 | volatile int mode = SING; //mode 0:Single 1:ACC 2:SkidPad 3:Endurance |
Dennis40816 | 0:9c3829968b11 | 93 | /**/ volatile int process = 0; //process 0:for ready ; 1:sensor in/confirm master or slave ; 2:calculation : 3:bt show 4 lcd show |
Dennis40816 | 0:9c3829968b11 | 94 | // time record |
Dennis40816 | 0:9c3829968b11 | 95 | /**/ Timer this_cycle_timer; //keep running timer |
Dennis40816 | 2:c9889e740474 | 96 | float sensor_in_debounce = 0.0; |
Dennis40816 | 0:9c3829968b11 | 97 | /**/ float total_time = 0.0; |
Dennis40816 | 0:9c3829968b11 | 98 | /**/ float last_cycle_time = 0.0; //for LC print |
Dennis40816 | 0:9c3829968b11 | 99 | /**/ float best_cycle_time = 0.0; |
Dennis40816 | 0:9c3829968b11 | 100 | // cycle |
Dennis40816 | 0:9c3829968b11 | 101 | /**/ int cycle = 1; |
Dennis40816 | 0:9c3829968b11 | 102 | /**/ int best_cycle = 1; |
Dennis40816 | 0:9c3829968b11 | 103 | // button debounce |
Dennis40816 | 0:9c3829968b11 | 104 | float button_RTC = 0.0; |
Dennis40816 | 0:9c3829968b11 | 105 | float button_LTC = 0.0; |
Dennis40816 | 0:9c3829968b11 | 106 | float button_RTC_2 = 0.0; |
Dennis40816 | 0:9c3829968b11 | 107 | float button_LTC_2 = 0.0; |
Dennis40816 | 0:9c3829968b11 | 108 | // RF related |
Dennis40816 | 0:9c3829968b11 | 109 | bool rf_state = 0; //0 for disconnect |
Dennis40816 | 0:9c3829968b11 | 110 | bool last_rf_state = 1; |
Dennis40816 | 0:9c3829968b11 | 111 | bool Rx_flag = 0; //1 when receive a cmd over |
Dennis40816 | 5:a897228b7306 | 112 | /**/ int role = NONE; |
Dennis40816 | 0:9c3829968b11 | 113 | /**/ int this_tcmd = 0; |
Dennis40816 | 0:9c3829968b11 | 114 | /**/ int last_tcmd = 0; |
Dennis40816 | 0:9c3829968b11 | 115 | /**/ int this_rcmd = 0; |
Dennis40816 | 0:9c3829968b11 | 116 | int rf_interval_cnt = 0; |
Dennis40816 | 0:9c3829968b11 | 117 | /**/ float rf_delay_time = 0.0; |
Dennis40816 | 0:9c3829968b11 | 118 | /**/ Timer rf_check_timer; |
Dennis40816 | 0:9c3829968b11 | 119 | // RF Data |
Dennis40816 | 0:9c3829968b11 | 120 | /**/ char TX_buffer[TX_SIZE]; |
Dennis40816 | 0:9c3829968b11 | 121 | /**/ char RX_buffer[RX_SIZE]; |
Dennis40816 | 0:9c3829968b11 | 122 | /**/ int tx_cnt = 0; |
Dennis40816 | 0:9c3829968b11 | 123 | /**/ int rx_cnt = 0; |
Dennis40816 | 0:9c3829968b11 | 124 | // LCD |
Dennis40816 | 0:9c3829968b11 | 125 | /**/ bool lcd_print_flag = 0; |
Dennis40816 | 0:9c3829968b11 | 126 | bool lcd_end_flag = 0; |
Dennis40816 | 0:9c3829968b11 | 127 | float lcd_life_check_time = 0.0; |
Dennis40816 | 0:9c3829968b11 | 128 | float lcd_show_result_time = 0.0; |
Dennis40816 | 0:9c3829968b11 | 129 | float lcd_end_time = 0.0; |
Dennis40816 | 0:9c3829968b11 | 130 | // BT |
Dennis40816 | 0:9c3829968b11 | 131 | /**/ bool bt_print_flag = 0; |
Dennis40816 | 0:9c3829968b11 | 132 | bool bt_end_flag = 0; |
Dennis40816 | 0:9c3829968b11 | 133 | /* |
Dennis40816 | 0:9c3829968b11 | 134 | ############################### |
Dennis40816 | 0:9c3829968b11 | 135 | # Declaration # |
Dennis40816 | 0:9c3829968b11 | 136 | ############################### |
Dennis40816 | 0:9c3829968b11 | 137 | */ |
Dennis40816 | 0:9c3829968b11 | 138 | // Initial |
Dennis40816 | 0:9c3829968b11 | 139 | void Initial_All(void); |
Dennis40816 | 0:9c3829968b11 | 140 | void Initial_Hardware(void); |
Dennis40816 | 0:9c3829968b11 | 141 | void Initial_IT(void); |
Dennis40816 | 0:9c3829968b11 | 142 | //main function |
Dennis40816 | 3:70ce5fd299bb | 143 | void ALL_SYS_RESET_CHECK(void); |
Dennis40816 | 0:9c3829968b11 | 144 | void reset_parameters(void); |
Dennis40816 | 0:9c3829968b11 | 145 | void show_all(void); |
Dennis40816 | 0:9c3829968b11 | 146 | void lcd_show(void); |
Dennis40816 | 0:9c3829968b11 | 147 | void bt_show(void); |
Dennis40816 | 0:9c3829968b11 | 148 | void show_flag_check(void); |
Dennis40816 | 0:9c3829968b11 | 149 | void time_cal(void); |
Dennis40816 | 0:9c3829968b11 | 150 | void RF_Send(int); |
Dennis40816 | 0:9c3829968b11 | 151 | void RF_Receive(int); |
Dennis40816 | 3:70ce5fd299bb | 152 | void RF_Check(void); |
Dennis40816 | 0:9c3829968b11 | 153 | // IT Functions |
Dennis40816 | 0:9c3829968b11 | 154 | void sensor_in(void); |
Dennis40816 | 0:9c3829968b11 | 155 | void button_fall(void); |
Dennis40816 | 0:9c3829968b11 | 156 | void button_fall_2(void); |
Dennis40816 | 1:db7799912cac | 157 | void test_button(void); |
Dennis40816 | 0:9c3829968b11 | 158 | void RxCallback(void); |
Dennis40816 | 0:9c3829968b11 | 159 | //protect parameters |
Dennis40816 | 0:9c3829968b11 | 160 | void parameters_protect(void); |
Dennis40816 | 0:9c3829968b11 | 161 | /* |
Dennis40816 | 0:9c3829968b11 | 162 | ############################### |
Dennis40816 | 0:9c3829968b11 | 163 | # Main # |
Dennis40816 | 0:9c3829968b11 | 164 | # function # |
Dennis40816 | 0:9c3829968b11 | 165 | ############################### |
Dennis40816 | 0:9c3829968b11 | 166 | */ |
Dennis40816 | 0:9c3829968b11 | 167 | int main() |
Dennis40816 | 0:9c3829968b11 | 168 | { |
Dennis40816 | 0:9c3829968b11 | 169 | Initial_All(); |
Dennis40816 | 0:9c3829968b11 | 170 | while(1) { |
Dennis40816 | 3:70ce5fd299bb | 171 | ALL_SYS_RESET_CHECK(); |
Dennis40816 | 3:70ce5fd299bb | 172 | RF_Check(); |
Dennis40816 | 0:9c3829968b11 | 173 | time_cal(); |
Dennis40816 | 0:9c3829968b11 | 174 | show_all(); |
Dennis40816 | 0:9c3829968b11 | 175 | } |
Dennis40816 | 0:9c3829968b11 | 176 | } |
Dennis40816 | 0:9c3829968b11 | 177 | /* |
Dennis40816 | 0:9c3829968b11 | 178 | ############################### |
Dennis40816 | 0:9c3829968b11 | 179 | # Functions # |
Dennis40816 | 0:9c3829968b11 | 180 | # Implementation # |
Dennis40816 | 0:9c3829968b11 | 181 | ############################### |
Dennis40816 | 0:9c3829968b11 | 182 | */ |
Dennis40816 | 0:9c3829968b11 | 183 | //Initial |
Dennis40816 | 0:9c3829968b11 | 184 | void Initial_All(){ |
Dennis40816 | 0:9c3829968b11 | 185 | Initial_Hardware(); |
Dennis40816 | 0:9c3829968b11 | 186 | Initial_IT(); |
Dennis40816 | 0:9c3829968b11 | 187 | } |
Dennis40816 | 0:9c3829968b11 | 188 | void Initial_Hardware(){ |
Dennis40816 | 0:9c3829968b11 | 189 | //lcd |
Dennis40816 | 0:9c3829968b11 | 190 | lcd.setCursor(TextLCD::CurOff_BlkOff); |
Dennis40816 | 0:9c3829968b11 | 191 | lcd.setBacklight(TextLCD::LightOn); |
Dennis40816 | 0:9c3829968b11 | 192 | lcd.locate(0,0); |
Dennis40816 | 0:9c3829968b11 | 193 | lcd.printf("NTHU RACING"); |
Dennis40816 | 0:9c3829968b11 | 194 | lcd.locate(0,1); |
Dennis40816 | 0:9c3829968b11 | 195 | lcd.printf("Initializing"); |
Dennis40816 | 0:9c3829968b11 | 196 | //bt |
Dennis40816 | 1:db7799912cac | 197 | if(bt_state.read()) { |
Dennis40816 | 1:db7799912cac | 198 | bt.printf("######################################\r\n"); |
Dennis40816 | 1:db7799912cac | 199 | bt.printf("NTHU RACING TIME RECORDER INITIALIZING\r\n\r\n"); |
Dennis40816 | 1:db7799912cac | 200 | } |
Dennis40816 | 0:9c3829968b11 | 201 | else{ |
Dennis40816 | 0:9c3829968b11 | 202 | lcd.locate(12,0); |
Dennis40816 | 6:f5a4aa4a3fe4 | 203 | lcd.printf("B0"); |
Dennis40816 | 1:db7799912cac | 204 | pc.printf("######################################\r\n"); |
Dennis40816 | 0:9c3829968b11 | 205 | pc.printf("NTHU RACING TIME RECORDER INITIALIZING\r\n\r\n"); |
Dennis40816 | 0:9c3829968b11 | 206 | } |
Dennis40816 | 0:9c3829968b11 | 207 | wait(1.5); |
Dennis40816 | 0:9c3829968b11 | 208 | } |
Dennis40816 | 0:9c3829968b11 | 209 | void Initial_IT(){ |
Dennis40816 | 0:9c3829968b11 | 210 | //BUTTON |
Dennis40816 | 0:9c3829968b11 | 211 | MY_BUTTON.rise(&button_fall); |
Dennis40816 | 0:9c3829968b11 | 212 | MY_BUTTON_2.rise(&button_fall_2); |
Dennis40816 | 0:9c3829968b11 | 213 | //SENSOR |
Dennis40816 | 0:9c3829968b11 | 214 | SENSOR_IT.rise(&sensor_in); |
Dennis40816 | 0:9c3829968b11 | 215 | //RF |
Dennis40816 | 0:9c3829968b11 | 216 | RF.attach(&RxCallback,Serial::RxIrq); |
Dennis40816 | 0:9c3829968b11 | 217 | lcd.cls(); |
Dennis40816 | 0:9c3829968b11 | 218 | lcd.locate(0,0); |
Dennis40816 | 0:9c3829968b11 | 219 | lcd.printf("Set up ok!"); |
Dennis40816 | 0:9c3829968b11 | 220 | if(bt_state.read()){ |
Dennis40816 | 4:1300ca51fc75 | 221 | bt.printf("Mode 0 -> Single Cycle\r\n"); |
Dennis40816 | 4:1300ca51fc75 | 222 | bt.printf("Mode 1 -> Accleration \r\n"); |
Dennis40816 | 4:1300ca51fc75 | 223 | bt.printf("Mode 2 -> Skidpad \r\n"); |
Dennis40816 | 4:1300ca51fc75 | 224 | bt.printf("Mode 3 -> Endurance:%d\r\n\r\n",ENDURANCE_CYCLE); |
Dennis40816 | 1:db7799912cac | 225 | bt.printf("NTHU RACING TIME RECORDER SET UP DONE\r\n"); |
Dennis40816 | 0:9c3829968b11 | 226 | bt.printf("#####################################\r\n"); |
Dennis40816 | 0:9c3829968b11 | 227 | } |
Dennis40816 | 0:9c3829968b11 | 228 | else{ |
Dennis40816 | 4:1300ca51fc75 | 229 | pc.printf("Mode 0 -> Single Cycle\r\n"); |
Dennis40816 | 4:1300ca51fc75 | 230 | pc.printf("Mode 1 -> Accleration \r\n"); |
Dennis40816 | 4:1300ca51fc75 | 231 | pc.printf("Mode 2 -> Skidpad \r\n"); |
Dennis40816 | 4:1300ca51fc75 | 232 | pc.printf("Mode 3 -> Endurance:%d\r\n",ENDURANCE_CYCLE); |
Dennis40816 | 1:db7799912cac | 233 | pc.printf("NTHU RACING TIME RECORDER SET UP DONE\r\n"); |
Dennis40816 | 0:9c3829968b11 | 234 | pc.printf("#####################################\r\n"); |
Dennis40816 | 0:9c3829968b11 | 235 | } |
Dennis40816 | 0:9c3829968b11 | 236 | wait(1); |
Dennis40816 | 0:9c3829968b11 | 237 | } |
Dennis40816 | 0:9c3829968b11 | 238 | //while loop |
Dennis40816 | 3:70ce5fd299bb | 239 | void ALL_SYS_RESET_CHECK(){ |
Dennis40816 | 3:70ce5fd299bb | 240 | if(MY_BUTTON_2.read()){ |
Dennis40816 | 3:70ce5fd299bb | 241 | if(us_ticker_read()/m_1 - button_RTC_2 > NVIC_RESET_PROTECT) NVIC_SystemReset(); |
Dennis40816 | 3:70ce5fd299bb | 242 | } |
Dennis40816 | 3:70ce5fd299bb | 243 | } |
Dennis40816 | 3:70ce5fd299bb | 244 | void RF_Check(){ |
Dennis40816 | 5:a897228b7306 | 245 | if(role == NONE && mode == ACC) { |
Dennis40816 | 3:70ce5fd299bb | 246 | rf_check_timer.start(); |
Dennis40816 | 3:70ce5fd299bb | 247 | if(rf_check_timer.read() > 1){ |
Dennis40816 | 3:70ce5fd299bb | 248 | rf_interval_cnt++; |
Dennis40816 | 3:70ce5fd299bb | 249 | rf_check_timer.reset(); |
Dennis40816 | 3:70ce5fd299bb | 250 | } |
Dennis40816 | 3:70ce5fd299bb | 251 | if(rf_interval_cnt % 5 == 0 && rf_interval_cnt) { |
Dennis40816 | 3:70ce5fd299bb | 252 | RF_Send(CHECK); |
Dennis40816 | 3:70ce5fd299bb | 253 | } |
Dennis40816 | 3:70ce5fd299bb | 254 | if(rf_interval_cnt > 5) rf_state = 0; |
Dennis40816 | 5:a897228b7306 | 255 | if(rf_interval_cnt >= INT_MAX) rf_interval_cnt = 0; |
Dennis40816 | 3:70ce5fd299bb | 256 | } |
Dennis40816 | 3:70ce5fd299bb | 257 | else rf_check_timer.stop(); |
Dennis40816 | 3:70ce5fd299bb | 258 | } |
Dennis40816 | 0:9c3829968b11 | 259 | void reset_parameters() { |
Dennis40816 | 0:9c3829968b11 | 260 | process = 0; |
Dennis40816 | 0:9c3829968b11 | 261 | lcd_print_flag = 0; |
Dennis40816 | 0:9c3829968b11 | 262 | bt_print_flag = 0; |
Dennis40816 | 0:9c3829968b11 | 263 | this_cycle_timer.stop(); |
Dennis40816 | 0:9c3829968b11 | 264 | this_cycle_timer.reset(); |
Dennis40816 | 2:c9889e740474 | 265 | lcd_end_flag = 0; |
Dennis40816 | 2:c9889e740474 | 266 | bt_end_flag = 0; |
Dennis40816 | 5:a897228b7306 | 267 | if( mode != ACC || (mode == ACC && this_rcmd == RESTART) || (mode == ACC && last_tcmd == RESTART) ){ |
Dennis40816 | 1:db7799912cac | 268 | cycle = 1; |
Dennis40816 | 1:db7799912cac | 269 | best_cycle = 1; |
Dennis40816 | 1:db7799912cac | 270 | last_cycle_time = 0.0; |
Dennis40816 | 1:db7799912cac | 271 | best_cycle_time = 0.0; |
Dennis40816 | 1:db7799912cac | 272 | total_time = 0.0; |
Dennis40816 | 2:c9889e740474 | 273 | |
Dennis40816 | 1:db7799912cac | 274 | } |
Dennis40816 | 5:a897228b7306 | 275 | if(mode == ACC){ |
Dennis40816 | 0:9c3829968b11 | 276 | memset(TX_buffer,0,TX_SIZE); |
Dennis40816 | 0:9c3829968b11 | 277 | memset(RX_buffer,0,RX_SIZE); |
Dennis40816 | 0:9c3829968b11 | 278 | |
Dennis40816 | 0:9c3829968b11 | 279 | tx_cnt = 0; |
Dennis40816 | 0:9c3829968b11 | 280 | rx_cnt = 0; |
Dennis40816 | 0:9c3829968b11 | 281 | rf_delay_time = 0.0; |
Dennis40816 | 5:a897228b7306 | 282 | role = NONE; |
Dennis40816 | 0:9c3829968b11 | 283 | this_tcmd = 0; |
Dennis40816 | 0:9c3829968b11 | 284 | last_tcmd = 0; |
Dennis40816 | 0:9c3829968b11 | 285 | this_rcmd = 0; |
Dennis40816 | 0:9c3829968b11 | 286 | } |
Dennis40816 | 1:db7799912cac | 287 | if(bt_state.read() ) bt.printf("#####################################\r\n"); |
Dennis40816 | 1:db7799912cac | 288 | else pc.printf("#####################################\r\n"); |
Dennis40816 | 0:9c3829968b11 | 289 | } |
Dennis40816 | 0:9c3829968b11 | 290 | void show_all(){ |
Dennis40816 | 0:9c3829968b11 | 291 | parameters_protect(); |
Dennis40816 | 0:9c3829968b11 | 292 | lcd_show(); |
Dennis40816 | 0:9c3829968b11 | 293 | bt_show(); |
Dennis40816 | 0:9c3829968b11 | 294 | show_flag_check(); |
Dennis40816 | 0:9c3829968b11 | 295 | } |
Dennis40816 | 0:9c3829968b11 | 296 | void parameters_protect(){ |
Dennis40816 | 0:9c3829968b11 | 297 | if(cycle >= 99){ |
Dennis40816 | 0:9c3829968b11 | 298 | cycle = 0; |
Dennis40816 | 0:9c3829968b11 | 299 | if(bt_state.read()) bt.printf("Cycle Protect \r\n"); |
Dennis40816 | 0:9c3829968b11 | 300 | else pc.printf("Cycle Protect \r\n"); |
Dennis40816 | 0:9c3829968b11 | 301 | } |
Dennis40816 | 0:9c3829968b11 | 302 | if(last_cycle_time >= 9999.99f ){ |
Dennis40816 | 0:9c3829968b11 | 303 | last_cycle_time = 0.0; |
Dennis40816 | 0:9c3829968b11 | 304 | if(bt_state.read()) bt.printf("Last Cycle Time Protect \r\n"); |
Dennis40816 | 0:9c3829968b11 | 305 | else pc.printf("Last Cycle Time Protect \r\n"); |
Dennis40816 | 0:9c3829968b11 | 306 | } |
Dennis40816 | 0:9c3829968b11 | 307 | if(total_time >= 9999.99f){ |
Dennis40816 | 0:9c3829968b11 | 308 | total_time = 0.0; |
Dennis40816 | 0:9c3829968b11 | 309 | if(bt_state.read()) bt.printf("Total Time Protect \r\n"); |
Dennis40816 | 0:9c3829968b11 | 310 | else pc.printf("Total Time Protect \r\n"); |
Dennis40816 | 0:9c3829968b11 | 311 | } |
Dennis40816 | 0:9c3829968b11 | 312 | } |
Dennis40816 | 0:9c3829968b11 | 313 | void lcd_show(){ |
Dennis40816 | 0:9c3829968b11 | 314 | //process 4 update |
Dennis40816 | 0:9c3829968b11 | 315 | lcd.locate(0,0); |
Dennis40816 | 2:c9889e740474 | 316 | if( (!lcd_print_flag) && last_cycle_time > 0.0f && process == 4){ |
Dennis40816 | 0:9c3829968b11 | 317 | //print out last cycle time flag |
Dennis40816 | 0:9c3829968b11 | 318 | lcd_print_flag = 1; |
Dennis40816 | 0:9c3829968b11 | 319 | lcd_show_result_time = us_ticker_read()/m_1; |
Dennis40816 | 0:9c3829968b11 | 320 | } |
Dennis40816 | 5:a897228b7306 | 321 | else if((!lcd_end_flag) && ( (cycle > SKIDPAD_CYCLE && mode == SKID || cycle > ENDURANCE_CYCLE && mode == ENDU) || |
Dennis40816 | 2:c9889e740474 | 322 | #if(NO_DELAY == 1) |
Dennis40816 | 5:a897228b7306 | 323 | (last_tcmd == RESULT || this_rcmd == RESULT && mode == ACC) ) |
Dennis40816 | 2:c9889e740474 | 324 | #else |
Dennis40816 | 5:a897228b7306 | 325 | (last_tcmd == DELAY || this_rcmd == DELAY && mode == ACC) ) |
Dennis40816 | 2:c9889e740474 | 326 | #endif |
Dennis40816 | 2:c9889e740474 | 327 | ){ |
Dennis40816 | 0:9c3829968b11 | 328 | //mode end |
Dennis40816 | 1:db7799912cac | 329 | if(us_ticker_read()/m_1 - lcd_show_result_time > LCD_SHOW_PERIOD){ |
Dennis40816 | 1:db7799912cac | 330 | lcd_end_flag = 1; |
Dennis40816 | 1:db7799912cac | 331 | lcd_end_time = us_ticker_read()/m_1; |
Dennis40816 | 1:db7799912cac | 332 | } |
Dennis40816 | 0:9c3829968b11 | 333 | } |
Dennis40816 | 0:9c3829968b11 | 334 | |
Dennis40816 | 0:9c3829968b11 | 335 | //row 1 show |
Dennis40816 | 5:a897228b7306 | 336 | if(us_ticker_read()/m_1 - lcd_show_result_time < LCD_SHOW_PERIOD && last_cycle_time != 0 && mode != ACC) |
Dennis40816 | 0:9c3829968b11 | 337 | lcd.printf("LCT : %.2f sec ",last_cycle_time); |
Dennis40816 | 5:a897228b7306 | 338 | else if(us_ticker_read()/m_1 - lcd_end_time < LCD_SHOW_PERIOD && last_cycle_time != 0 && mode != ACC) |
Dennis40816 | 1:db7799912cac | 339 | lcd.printf("BC :%d ",best_cycle); |
Dennis40816 | 0:9c3829968b11 | 340 | else{ |
Dennis40816 | 0:9c3829968b11 | 341 | switch(mode){ |
Dennis40816 | 5:a897228b7306 | 342 | case SING : |
Dennis40816 | 6:f5a4aa4a3fe4 | 343 | lcd.printf("SING B%dR%d C%d ",bt_state.read(),rf_state,cycle); |
Dennis40816 | 0:9c3829968b11 | 344 | break; |
Dennis40816 | 5:a897228b7306 | 345 | case ACC: |
Dennis40816 | 6:f5a4aa4a3fe4 | 346 | lcd.printf("ACC B%dR%d C%d ",bt_state.read(),rf_state,cycle); |
Dennis40816 | 0:9c3829968b11 | 347 | break; |
Dennis40816 | 5:a897228b7306 | 348 | case SKID: |
Dennis40816 | 6:f5a4aa4a3fe4 | 349 | lcd.printf("SKID B%dR%d C%d ",bt_state.read(),rf_state,cycle); |
Dennis40816 | 0:9c3829968b11 | 350 | break; |
Dennis40816 | 5:a897228b7306 | 351 | case ENDU: |
Dennis40816 | 6:f5a4aa4a3fe4 | 352 | lcd.printf("ENDU B%dR%d C%d ",bt_state.read(),rf_state,cycle); |
Dennis40816 | 0:9c3829968b11 | 353 | break; |
Dennis40816 | 0:9c3829968b11 | 354 | } |
Dennis40816 | 0:9c3829968b11 | 355 | } |
Dennis40816 | 0:9c3829968b11 | 356 | lcd.locate(0,1); |
Dennis40816 | 0:9c3829968b11 | 357 | //row 2 show |
Dennis40816 | 2:c9889e740474 | 358 | if(cycle == 1 && this_cycle_timer.read() == 0) lcd.printf("Ready to Go !"); //first in |
Dennis40816 | 5:a897228b7306 | 359 | else if(mode == ACC && lcd_print_flag)lcd.printf ("TC:%.2f sec ",last_cycle_time); |
Dennis40816 | 5:a897228b7306 | 360 | else if(mode == ACC) lcd.printf("TC:%.2f sec ",this_cycle_timer.read()); |
Dennis40816 | 2:c9889e740474 | 361 | else if(lcd_end_flag && mode!=1) lcd.printf("BCT :%.2f sec",best_cycle_time); |
Dennis40816 | 5:a897228b7306 | 362 | else if((cycle > SKIDPAD_CYCLE && mode == SKID || cycle > ENDURANCE_CYCLE && mode == ENDU)) lcd.printf("Mode %d End! ",mode); |
Dennis40816 | 0:9c3829968b11 | 363 | else lcd.printf("%.2f,%.2f ",this_cycle_timer.read(),this_cycle_timer.read()+total_time); |
Dennis40816 | 0:9c3829968b11 | 364 | //lcd life check |
Dennis40816 | 0:9c3829968b11 | 365 | lcd.locate(15,1); |
Dennis40816 | 0:9c3829968b11 | 366 | if(us_ticker_read()/m_1 - lcd_life_check_time < 1.0f) lcd.printf("."); |
Dennis40816 | 0:9c3829968b11 | 367 | else if(us_ticker_read()/m_1 - lcd_life_check_time < 2.0f) lcd.printf(" "); |
Dennis40816 | 0:9c3829968b11 | 368 | else lcd_life_check_time = us_ticker_read()/m_1; |
Dennis40816 | 0:9c3829968b11 | 369 | } |
Dennis40816 | 0:9c3829968b11 | 370 | void bt_show(){ |
Dennis40816 | 0:9c3829968b11 | 371 | //process 3 update and print |
Dennis40816 | 0:9c3829968b11 | 372 | if(bt_state.read() && process == 3){ |
Dennis40816 | 0:9c3829968b11 | 373 | //process change |
Dennis40816 | 0:9c3829968b11 | 374 | process = 4; |
Dennis40816 | 1:db7799912cac | 375 | bt_print_flag = 1; |
Dennis40816 | 0:9c3829968b11 | 376 | if (last_cycle_time == 0.0f) bt.printf("First in\r\n"); |
Dennis40816 | 0:9c3829968b11 | 377 | else { |
Dennis40816 | 1:db7799912cac | 378 | bt.printf("Mode:%d,cycle: %d,last cycle time:%.2f sec\r\n",mode,cycle,last_cycle_time); |
Dennis40816 | 5:a897228b7306 | 379 | if(cycle == SKIDPAD_CYCLE && mode == SKID || cycle == ENDURANCE_CYCLE && mode == ENDU ){ |
Dennis40816 | 1:db7799912cac | 380 | bt.printf("Best Cycle:%d\r\n",best_cycle); |
Dennis40816 | 1:db7799912cac | 381 | bt.printf("Best Cycle Time:%.2f sec\r\n",best_cycle_time); |
Dennis40816 | 1:db7799912cac | 382 | bt.printf("Mode %d End & Reset\r\n",mode); |
Dennis40816 | 0:9c3829968b11 | 383 | bt_end_flag = 1; |
Dennis40816 | 2:c9889e740474 | 384 | } |
Dennis40816 | 5:a897228b7306 | 385 | else if(mode == ACC) bt_end_flag = 1; |
Dennis40816 | 0:9c3829968b11 | 386 | } |
Dennis40816 | 0:9c3829968b11 | 387 | } |
Dennis40816 | 0:9c3829968b11 | 388 | else if(process == 3){ |
Dennis40816 | 1:db7799912cac | 389 | process = 4; |
Dennis40816 | 1:db7799912cac | 390 | bt_print_flag = 1; |
Dennis40816 | 0:9c3829968b11 | 391 | if (last_cycle_time == 0.0f) pc.printf("First in\r\n"); |
Dennis40816 | 0:9c3829968b11 | 392 | else { |
Dennis40816 | 1:db7799912cac | 393 | pc.printf("Mode:%d,cycle: %d,last cycle time:%.2f sec\r\n",mode,cycle,last_cycle_time); |
Dennis40816 | 5:a897228b7306 | 394 | if(cycle == SKIDPAD_CYCLE && mode == SKID || cycle == ENDURANCE_CYCLE && mode == ENDU){ |
Dennis40816 | 1:db7799912cac | 395 | pc.printf("Mode %d End & Reset\r\n",mode); |
Dennis40816 | 0:9c3829968b11 | 396 | bt_end_flag = 1; |
Dennis40816 | 4:1300ca51fc75 | 397 | } |
Dennis40816 | 5:a897228b7306 | 398 | else if(mode == ACC) bt_end_flag = 1; |
Dennis40816 | 0:9c3829968b11 | 399 | } |
Dennis40816 | 0:9c3829968b11 | 400 | } |
Dennis40816 | 0:9c3829968b11 | 401 | if(last_rf_state != rf_state){ |
Dennis40816 | 0:9c3829968b11 | 402 | last_rf_state = rf_state; |
Dennis40816 | 0:9c3829968b11 | 403 | if(rf_state){ |
Dennis40816 | 0:9c3829968b11 | 404 | if(bt_state.read()) bt.printf("RF Connected!\r\n"); |
Dennis40816 | 0:9c3829968b11 | 405 | else pc.printf("RF Connected!\r\n"); |
Dennis40816 | 0:9c3829968b11 | 406 | } |
Dennis40816 | 0:9c3829968b11 | 407 | else{ |
Dennis40816 | 0:9c3829968b11 | 408 | if(bt_state.read()) bt.printf("RF Disconnected!\r\n"); |
Dennis40816 | 0:9c3829968b11 | 409 | else pc.printf("RF Disonnected!\r\n"); |
Dennis40816 | 0:9c3829968b11 | 410 | } |
Dennis40816 | 0:9c3829968b11 | 411 | } |
Dennis40816 | 0:9c3829968b11 | 412 | } |
Dennis40816 | 0:9c3829968b11 | 413 | void show_flag_check(){ |
Dennis40816 | 0:9c3829968b11 | 414 | //cycle update for mode != ACC |
Dennis40816 | 5:a897228b7306 | 415 | if(lcd_print_flag && bt_print_flag && mode != ACC){ |
Dennis40816 | 0:9c3829968b11 | 416 | //print ok |
Dennis40816 | 0:9c3829968b11 | 417 | process = 0; |
Dennis40816 | 0:9c3829968b11 | 418 | lcd_print_flag = 0; |
Dennis40816 | 0:9c3829968b11 | 419 | bt_print_flag = 0; |
Dennis40816 | 2:c9889e740474 | 420 | cycle++; |
Dennis40816 | 2:c9889e740474 | 421 | |
Dennis40816 | 0:9c3829968b11 | 422 | } |
Dennis40816 | 0:9c3829968b11 | 423 | if(lcd_end_flag && bt_end_flag){ |
Dennis40816 | 0:9c3829968b11 | 424 | //notice reset |
Dennis40816 | 5:a897228b7306 | 425 | if(mode == ACC && us_ticker_read()/m_1 - lcd_end_time > LCD_SHOW_PERIOD && lcd_end_flag ) RF_Send(RESET); //including local reset |
Dennis40816 | 0:9c3829968b11 | 426 | else if(us_ticker_read()/m_1 - lcd_end_time > LCD_SHOW_PERIOD) reset_parameters(); |
Dennis40816 | 0:9c3829968b11 | 427 | } |
Dennis40816 | 2:c9889e740474 | 428 | |
Dennis40816 | 0:9c3829968b11 | 429 | } |
Dennis40816 | 0:9c3829968b11 | 430 | void time_cal(){ |
Dennis40816 | 0:9c3829968b11 | 431 | if(process == 1){ |
Dennis40816 | 0:9c3829968b11 | 432 | process = 2; |
Dennis40816 | 0:9c3829968b11 | 433 | //calculation time variable |
Dennis40816 | 0:9c3829968b11 | 434 | //last_cycle_time update in sensor in |
Dennis40816 | 5:a897228b7306 | 435 | if(mode != ACC) total_time += last_cycle_time; |
Dennis40816 | 0:9c3829968b11 | 436 | if(cycle == 1){ |
Dennis40816 | 0:9c3829968b11 | 437 | best_cycle = cycle; |
Dennis40816 | 0:9c3829968b11 | 438 | best_cycle_time = last_cycle_time; |
Dennis40816 | 0:9c3829968b11 | 439 | } |
Dennis40816 | 0:9c3829968b11 | 440 | else if(last_cycle_time < best_cycle_time){ |
Dennis40816 | 0:9c3829968b11 | 441 | best_cycle = cycle; |
Dennis40816 | 0:9c3829968b11 | 442 | best_cycle_time = last_cycle_time; |
Dennis40816 | 0:9c3829968b11 | 443 | } |
Dennis40816 | 0:9c3829968b11 | 444 | //get in update show period |
Dennis40816 | 0:9c3829968b11 | 445 | process = 3; |
Dennis40816 | 0:9c3829968b11 | 446 | } |
Dennis40816 | 0:9c3829968b11 | 447 | } |
Dennis40816 | 0:9c3829968b11 | 448 | //RF communication function , must end with \n ; //cycle update for mode ACC |
Dennis40816 | 0:9c3829968b11 | 449 | void RF_Send(int rf_tcmd){ |
Dennis40816 | 2:c9889e740474 | 450 | if(rf_tcmd != ACCEPT) this_tcmd = rf_tcmd; |
Dennis40816 | 2:c9889e740474 | 451 | switch(rf_tcmd){ |
Dennis40816 | 2:c9889e740474 | 452 | case ACCEPT: |
Dennis40816 | 2:c9889e740474 | 453 | RF.printf("%d\n",ACCEPT); |
Dennis40816 | 2:c9889e740474 | 454 | rf_state = 1; |
Dennis40816 | 2:c9889e740474 | 455 | rf_interval_cnt = -4; |
Dennis40816 | 2:c9889e740474 | 456 | break; |
Dennis40816 | 2:c9889e740474 | 457 | case DENY: |
Dennis40816 | 2:c9889e740474 | 458 | RF.printf("%d\n",DENY); |
Dennis40816 | 2:c9889e740474 | 459 | break; |
Dennis40816 | 2:c9889e740474 | 460 | case CHECK: |
Dennis40816 | 2:c9889e740474 | 461 | //以後加上cycle check |
Dennis40816 | 2:c9889e740474 | 462 | RF.printf("%d\n",CHECK); |
Dennis40816 | 2:c9889e740474 | 463 | break; |
Dennis40816 | 2:c9889e740474 | 464 | case MASTER: |
Dennis40816 | 5:a897228b7306 | 465 | role = ROLE_MASTER; |
Dennis40816 | 2:c9889e740474 | 466 | RF.printf("%d\n",MASTER); |
Dennis40816 | 2:c9889e740474 | 467 | if(bt_state.read()) bt.printf("Car in \r\n"); |
Dennis40816 | 2:c9889e740474 | 468 | else pc.printf("Car in \r\n"); |
Dennis40816 | 2:c9889e740474 | 469 | break; |
Dennis40816 | 2:c9889e740474 | 470 | case SLAVE: |
Dennis40816 | 2:c9889e740474 | 471 | RF.printf("%d&%.2f\n",SLAVE,this_cycle_timer.read()); |
Dennis40816 | 2:c9889e740474 | 472 | break; |
Dennis40816 | 2:c9889e740474 | 473 | case RESULT: |
Dennis40816 | 2:c9889e740474 | 474 | RF.printf("%d&%.2f\n",RESULT,this_cycle_timer.read()); |
Dennis40816 | 2:c9889e740474 | 475 | break; |
Dennis40816 | 2:c9889e740474 | 476 | case DELAY: |
Dennis40816 | 2:c9889e740474 | 477 | //delay time abnormal detect |
Dennis40816 | 2:c9889e740474 | 478 | if(rf_delay_time < 1.0f) { |
Dennis40816 | 2:c9889e740474 | 479 | RF.printf("%d&%.4f\n",DELAY,rf_delay_time); |
Dennis40816 | 2:c9889e740474 | 480 | #if (DEBUG == 1 || DEBUG == 2) |
Dennis40816 | 2:c9889e740474 | 481 | if(bt_state.read()) bt.printf("Delay time:%.4f\r\n",rf_delay_time); |
Dennis40816 | 2:c9889e740474 | 482 | else pc.printf("Delay time:%.4f\r\n",rf_delay_time); |
Dennis40816 | 2:c9889e740474 | 483 | #endif |
Dennis40816 | 2:c9889e740474 | 484 | } |
Dennis40816 | 2:c9889e740474 | 485 | else if(bt_state.read()) bt.printf("rf delay time > 1sec \r\n"); |
Dennis40816 | 2:c9889e740474 | 486 | else pc.printf("rf delay time > 1 sec\r\n"); |
Dennis40816 | 2:c9889e740474 | 487 | break; |
Dennis40816 | 2:c9889e740474 | 488 | case RESET: |
Dennis40816 | 2:c9889e740474 | 489 | cycle++; |
Dennis40816 | 2:c9889e740474 | 490 | reset_parameters(); |
Dennis40816 | 2:c9889e740474 | 491 | if(bt_state.read()) bt.printf("ACC Set \r\n"); |
Dennis40816 | 2:c9889e740474 | 492 | else pc.printf("ACC Set \r\n"); |
Dennis40816 | 2:c9889e740474 | 493 | break; |
Dennis40816 | 2:c9889e740474 | 494 | case RESTART: |
Dennis40816 | 2:c9889e740474 | 495 | RF.printf("%d\n",RESTART); |
Dennis40816 | 2:c9889e740474 | 496 | //reset_parameters at button_fall_2 |
Dennis40816 | 2:c9889e740474 | 497 | break; |
Dennis40816 | 2:c9889e740474 | 498 | default: |
Dennis40816 | 2:c9889e740474 | 499 | if(bt_state.read()) bt.printf("Unknown Tcmd\r\n"); |
Dennis40816 | 2:c9889e740474 | 500 | else pc.printf("Unknown Tcmd\r\n"); |
Dennis40816 | 2:c9889e740474 | 501 | break; |
Dennis40816 | 2:c9889e740474 | 502 | } |
Dennis40816 | 2:c9889e740474 | 503 | last_tcmd = this_tcmd; |
Dennis40816 | 2:c9889e740474 | 504 | } |
Dennis40816 | 2:c9889e740474 | 505 | |
Dennis40816 | 2:c9889e740474 | 506 | void RF_Receive(int rf_rcmd){ |
Dennis40816 | 5:a897228b7306 | 507 | if(mode == ACC){ |
Dennis40816 | 2:c9889e740474 | 508 | this_rcmd = rf_rcmd; |
Dennis40816 | 2:c9889e740474 | 509 | #if(DEBUG == 1) |
Dennis40816 | 2:c9889e740474 | 510 | //if(bt_state.read()) bt.printf("this rcmd:%d\r\n",this_rcmd); |
Dennis40816 | 2:c9889e740474 | 511 | //else pc.printf("this rcmd:%d\r\n",this_rcmd); |
Dennis40816 | 2:c9889e740474 | 512 | #endif |
Dennis40816 | 2:c9889e740474 | 513 | switch(rf_rcmd){ |
Dennis40816 | 0:9c3829968b11 | 514 | case ACCEPT: |
Dennis40816 | 0:9c3829968b11 | 515 | rf_state = 1; |
Dennis40816 | 2:c9889e740474 | 516 | rf_interval_cnt = 0; |
Dennis40816 | 0:9c3829968b11 | 517 | break; |
Dennis40816 | 0:9c3829968b11 | 518 | case DENY: |
Dennis40816 | 2:c9889e740474 | 519 | switch(last_tcmd){ |
Dennis40816 | 2:c9889e740474 | 520 | //master |
Dennis40816 | 2:c9889e740474 | 521 | case MASTER: |
Dennis40816 | 2:c9889e740474 | 522 | bt.printf("MASTER Request Denied!\r\n"); |
Dennis40816 | 2:c9889e740474 | 523 | break; |
Dennis40816 | 2:c9889e740474 | 524 | //slave //發送時間待補 |
Dennis40816 | 2:c9889e740474 | 525 | case SLAVE: |
Dennis40816 | 2:c9889e740474 | 526 | bt.printf("SLAVE Request Denied!\r\n"); |
Dennis40816 | 2:c9889e740474 | 527 | break; |
Dennis40816 | 2:c9889e740474 | 528 | //result |
Dennis40816 | 2:c9889e740474 | 529 | case RESULT: |
Dennis40816 | 2:c9889e740474 | 530 | bt.printf("RESULT Request Denied!\r\n"); |
Dennis40816 | 2:c9889e740474 | 531 | break; |
Dennis40816 | 2:c9889e740474 | 532 | } |
Dennis40816 | 0:9c3829968b11 | 533 | break; |
Dennis40816 | 0:9c3829968b11 | 534 | case CHECK: |
Dennis40816 | 2:c9889e740474 | 535 | RF_Send(ACCEPT); |
Dennis40816 | 0:9c3829968b11 | 536 | break; |
Dennis40816 | 0:9c3829968b11 | 537 | case MASTER: |
Dennis40816 | 2:c9889e740474 | 538 | //will become Slave End |
Dennis40816 | 5:a897228b7306 | 539 | if(role == NONE){ |
Dennis40816 | 5:a897228b7306 | 540 | role = ROLE_SLAVE; |
Dennis40816 | 2:c9889e740474 | 541 | if(bt_state.read()) bt.printf("Car in \r\n"); |
Dennis40816 | 2:c9889e740474 | 542 | else pc.printf("Car in \r\n"); |
Dennis40816 | 2:c9889e740474 | 543 | } |
Dennis40816 | 2:c9889e740474 | 544 | else RF_Send(DENY); |
Dennis40816 | 0:9c3829968b11 | 545 | break; |
Dennis40816 | 0:9c3829968b11 | 546 | case SLAVE: |
Dennis40816 | 2:c9889e740474 | 547 | /*should be Master End*/ |
Dennis40816 | 2:c9889e740474 | 548 | //calculate Delay time |
Dennis40816 | 2:c9889e740474 | 549 | char slave_time[5]; |
Dennis40816 | 2:c9889e740474 | 550 | for(int i=0;i<5;i++) slave_time[i] = RX_buffer[i+2]; |
Dennis40816 | 2:c9889e740474 | 551 | rf_delay_time = (float)(( this_cycle_timer.read()-atof(slave_time) )/2); |
Dennis40816 | 2:c9889e740474 | 552 | last_cycle_time = this_cycle_timer.read()-rf_delay_time; |
Dennis40816 | 2:c9889e740474 | 553 | //send back Delay time |
Dennis40816 | 2:c9889e740474 | 554 | wait_ms(10); |
Dennis40816 | 2:c9889e740474 | 555 | RF_Send(DELAY); |
Dennis40816 | 2:c9889e740474 | 556 | wait_ms(10); |
Dennis40816 | 2:c9889e740474 | 557 | //Role off and interval = 0 ->maybe at reset? |
Dennis40816 | 2:c9889e740474 | 558 | process = 3; |
Dennis40816 | 0:9c3829968b11 | 559 | break; |
Dennis40816 | 0:9c3829968b11 | 560 | case RESULT: |
Dennis40816 | 2:c9889e740474 | 561 | //should be Master End |
Dennis40816 | 2:c9889e740474 | 562 | char result_time[5]; |
Dennis40816 | 2:c9889e740474 | 563 | for(int i=0;i<5;i++) result_time[i] = RX_buffer[i+2]; |
Dennis40816 | 2:c9889e740474 | 564 | last_cycle_time = atof(result_time); |
Dennis40816 | 2:c9889e740474 | 565 | process = 3; |
Dennis40816 | 0:9c3829968b11 | 566 | break; |
Dennis40816 | 0:9c3829968b11 | 567 | case DELAY: |
Dennis40816 | 2:c9889e740474 | 568 | // should be Slave End |
Dennis40816 | 2:c9889e740474 | 569 | char delay_time [5]; |
Dennis40816 | 2:c9889e740474 | 570 | for(int i=0;i<5;i++) delay_time[i] = RX_buffer[i+2]; |
Dennis40816 | 2:c9889e740474 | 571 | rf_delay_time = atof(delay_time); |
Dennis40816 | 2:c9889e740474 | 572 | last_cycle_time = this_cycle_timer.read()+ rf_delay_time; |
Dennis40816 | 2:c9889e740474 | 573 | process = 3; |
Dennis40816 | 2:c9889e740474 | 574 | #if(DEBUG == 1 || DEBUG == 2) |
Dennis40816 | 2:c9889e740474 | 575 | if(bt_state.read()) bt.printf("Delay time:%.4f \r\n",rf_delay_time); |
Dennis40816 | 2:c9889e740474 | 576 | else pc.printf("Delay time:%.4f \r\n",rf_delay_time); |
Dennis40816 | 2:c9889e740474 | 577 | #endif |
Dennis40816 | 0:9c3829968b11 | 578 | break; |
Dennis40816 | 0:9c3829968b11 | 579 | case RESET: |
Dennis40816 | 0:9c3829968b11 | 580 | cycle++; |
Dennis40816 | 0:9c3829968b11 | 581 | reset_parameters(); |
Dennis40816 | 2:c9889e740474 | 582 | if(bt_state.read()) bt.printf("ACC Set\r\n"); |
Dennis40816 | 2:c9889e740474 | 583 | else pc.printf("ACC Set\r\n"); |
Dennis40816 | 0:9c3829968b11 | 584 | break; |
Dennis40816 | 0:9c3829968b11 | 585 | case RESTART: |
Dennis40816 | 2:c9889e740474 | 586 | reset_parameters(); |
Dennis40816 | 2:c9889e740474 | 587 | if(bt_state.read()) bt.printf("ACC Restart\r\n"); |
Dennis40816 | 2:c9889e740474 | 588 | else pc.printf("ACC Restart\r\n"); |
Dennis40816 | 0:9c3829968b11 | 589 | break; |
Dennis40816 | 0:9c3829968b11 | 590 | default: |
Dennis40816 | 2:c9889e740474 | 591 | if(bt_state.read()) bt.printf("Unknown Rcmd\r\n"); |
Dennis40816 | 2:c9889e740474 | 592 | else pc.printf("Unknown Rcmd\r\n"); |
Dennis40816 | 0:9c3829968b11 | 593 | break; |
Dennis40816 | 0:9c3829968b11 | 594 | } |
Dennis40816 | 0:9c3829968b11 | 595 | } |
Dennis40816 | 0:9c3829968b11 | 596 | } |
Dennis40816 | 0:9c3829968b11 | 597 | |
Dennis40816 | 0:9c3829968b11 | 598 | //IT |
Dennis40816 | 0:9c3829968b11 | 599 | void sensor_in(){ |
Dennis40816 | 2:c9889e740474 | 600 | if(us_ticker_read()/m_1 - sensor_in_debounce > SENSOR_IN_DEBOUNCE){ |
Dennis40816 | 2:c9889e740474 | 601 | sensor_in_debounce = us_ticker_read()/m_1; |
Dennis40816 | 5:a897228b7306 | 602 | if(mode!=ACC){ |
Dennis40816 | 2:c9889e740474 | 603 | if(cycle == 1){ |
Dennis40816 | 2:c9889e740474 | 604 | this_cycle_timer.start(); |
Dennis40816 | 2:c9889e740474 | 605 | process = 1; |
Dennis40816 | 2:c9889e740474 | 606 | } |
Dennis40816 | 2:c9889e740474 | 607 | if(this_cycle_timer.read() > CYCLE_TIME_PROTECT){ |
Dennis40816 | 2:c9889e740474 | 608 | process = 1; |
Dennis40816 | 2:c9889e740474 | 609 | last_cycle_time = this_cycle_timer.read(); |
Dennis40816 | 2:c9889e740474 | 610 | this_cycle_timer.reset(); |
Dennis40816 | 2:c9889e740474 | 611 | } |
Dennis40816 | 2:c9889e740474 | 612 | //check mode2,3 |
Dennis40816 | 5:a897228b7306 | 613 | if((cycle > SKIDPAD_CYCLE && mode == SKID || cycle > ENDURANCE_CYCLE && mode == ENDU)){ |
Dennis40816 | 2:c9889e740474 | 614 | process = 0; |
Dennis40816 | 2:c9889e740474 | 615 | } |
Dennis40816 | 1:db7799912cac | 616 | } |
Dennis40816 | 5:a897228b7306 | 617 | else if(mode == ACC){ |
Dennis40816 | 2:c9889e740474 | 618 | //mode acc |
Dennis40816 | 2:c9889e740474 | 619 | if(!role && rf_state){ |
Dennis40816 | 2:c9889e740474 | 620 | //MASTER |
Dennis40816 | 2:c9889e740474 | 621 | this_cycle_timer.start(); |
Dennis40816 | 2:c9889e740474 | 622 | RF_Send(MASTER); |
Dennis40816 | 2:c9889e740474 | 623 | } |
Dennis40816 | 5:a897228b7306 | 624 | if(role == ROLE_SLAVE && this_cycle_timer.read() > CYCLE_TIME_PROTECT){ |
Dennis40816 | 2:c9889e740474 | 625 | this_cycle_timer.stop(); |
Dennis40816 | 2:c9889e740474 | 626 | //SLAVE |
Dennis40816 | 2:c9889e740474 | 627 | #if (NO_DELAY == 1) |
Dennis40816 | 2:c9889e740474 | 628 | //this_cycle_timer.start() at RxCallback |
Dennis40816 | 2:c9889e740474 | 629 | RF_Send(RESULT); |
Dennis40816 | 2:c9889e740474 | 630 | last_cycle_time = this_cycle_timer.read(); |
Dennis40816 | 2:c9889e740474 | 631 | process = 3; |
Dennis40816 | 2:c9889e740474 | 632 | #else |
Dennis40816 | 2:c9889e740474 | 633 | //with delay calculation in Master End |
Dennis40816 | 2:c9889e740474 | 634 | RF_Send(SLAVE); |
Dennis40816 | 2:c9889e740474 | 635 | #endif |
Dennis40816 | 2:c9889e740474 | 636 | } |
Dennis40816 | 0:9c3829968b11 | 637 | } |
Dennis40816 | 0:9c3829968b11 | 638 | } |
Dennis40816 | 0:9c3829968b11 | 639 | } |
Dennis40816 | 0:9c3829968b11 | 640 | void button_fall(void){ |
Dennis40816 | 0:9c3829968b11 | 641 | //debounce |
Dennis40816 | 0:9c3829968b11 | 642 | button_RTC = us_ticker_read()/m_1; |
Dennis40816 | 0:9c3829968b11 | 643 | if(button_RTC - button_LTC > BUTTON_DEBOUNCE){ |
Dennis40816 | 0:9c3829968b11 | 644 | button_LTC = button_RTC; |
Dennis40816 | 5:a897228b7306 | 645 | (mode >= ENDU || mode < SING) ? mode = SING : mode++ ; |
Dennis40816 | 5:a897228b7306 | 646 | (mode == ACC)?RF.attach(&RxCallback,Serial::RxIrq):RF.attach(NULL,Serial::RxIrq); |
Dennis40816 | 5:a897228b7306 | 647 | if(mode != ACC) rf_state = 0; |
Dennis40816 | 6:f5a4aa4a3fe4 | 648 | //make sure reset_parameters success when mode 1-> 2 |
Dennis40816 | 1:db7799912cac | 649 | last_tcmd = RESTART; |
Dennis40816 | 0:9c3829968b11 | 650 | reset_parameters(); |
Dennis40816 | 1:db7799912cac | 651 | last_tcmd = ACCEPT; |
Dennis40816 | 0:9c3829968b11 | 652 | if(bt_state.read()) bt.printf("Mode change to :%d \r\n",mode); |
Dennis40816 | 0:9c3829968b11 | 653 | else pc.printf("Mode change to :%d \r\n",mode); |
Dennis40816 | 0:9c3829968b11 | 654 | } |
Dennis40816 | 0:9c3829968b11 | 655 | } |
Dennis40816 | 0:9c3829968b11 | 656 | void button_fall_2(void){ |
Dennis40816 | 0:9c3829968b11 | 657 | //send reset cmd |
Dennis40816 | 0:9c3829968b11 | 658 | button_RTC_2 = us_ticker_read()/m_1; |
Dennis40816 | 0:9c3829968b11 | 659 | if(button_RTC_2 - button_LTC_2 > BUTTON_DEBOUNCE){ |
Dennis40816 | 0:9c3829968b11 | 660 | button_LTC_2 = button_RTC_2; |
Dennis40816 | 5:a897228b7306 | 661 | if(mode == ACC) RF_Send(RESTART); |
Dennis40816 | 0:9c3829968b11 | 662 | reset_parameters(); |
Dennis40816 | 0:9c3829968b11 | 663 | if(bt_state.read() ){ |
Dennis40816 | 5:a897228b7306 | 664 | if(mode == ACC) bt.printf("ACC Restart\r\n"); |
Dennis40816 | 2:c9889e740474 | 665 | else { |
Dennis40816 | 2:c9889e740474 | 666 | bt.printf("Restart!\r\n\r\n\r\n"); |
Dennis40816 | 2:c9889e740474 | 667 | bt.printf("#####################\r\n"); |
Dennis40816 | 2:c9889e740474 | 668 | } |
Dennis40816 | 0:9c3829968b11 | 669 | } |
Dennis40816 | 1:db7799912cac | 670 | else{ |
Dennis40816 | 1:db7799912cac | 671 | pc.printf("Restart!\r\n\r\n\r\n"); |
Dennis40816 | 1:db7799912cac | 672 | pc.printf("#####################\r\n"); |
Dennis40816 | 1:db7799912cac | 673 | } |
Dennis40816 | 0:9c3829968b11 | 674 | } |
Dennis40816 | 0:9c3829968b11 | 675 | } |
Dennis40816 | 0:9c3829968b11 | 676 | void RxCallback(){ |
Dennis40816 | 0:9c3829968b11 | 677 | //read from buffer |
Dennis40816 | 3:70ce5fd299bb | 678 | #if(RX_MODE == 1) // by scanf ,printf |
Dennis40816 | 0:9c3829968b11 | 679 | RF.scanf("%s",RX_buffer); |
Dennis40816 | 0:9c3829968b11 | 680 | if(RX_buffer[0]-'0' == MASTER) this_cycle_timer.start(); |
Dennis40816 | 0:9c3829968b11 | 681 | else if(RX_buffer[0]-'0' == SLAVE || RX_buffer[0]-'0' == RESULT) this_cycle_timer.stop(); |
Dennis40816 | 0:9c3829968b11 | 682 | Rx_flag = 1; |
Dennis40816 | 3:70ce5fd299bb | 683 | #elif(RX_MODE == 2) //by getc , printf |
Dennis40816 | 0:9c3829968b11 | 684 | while(RF.readable()){ |
Dennis40816 | 0:9c3829968b11 | 685 | char new_char = RF.getc(); |
Dennis40816 | 0:9c3829968b11 | 686 | if(new_char != '\n') { |
Dennis40816 | 0:9c3829968b11 | 687 | RX_buffer[rx_cnt++] = new_char; |
Dennis40816 | 0:9c3829968b11 | 688 | if(rx_cnt == 1){ |
Dennis40816 | 0:9c3829968b11 | 689 | //timer management |
Dennis40816 | 0:9c3829968b11 | 690 | if(RX_buffer[0]-'0' == MASTER) this_cycle_timer.start(); |
Dennis40816 | 0:9c3829968b11 | 691 | else if(RX_buffer[0]-'0' == SLAVE || RX_buffer[0]-'0' == RESULT) this_cycle_timer.stop(); |
Dennis40816 | 0:9c3829968b11 | 692 | } |
Dennis40816 | 0:9c3829968b11 | 693 | } |
Dennis40816 | 2:c9889e740474 | 694 | else if(new_char == '\n') { |
Dennis40816 | 0:9c3829968b11 | 695 | rx_cnt = 0; |
Dennis40816 | 0:9c3829968b11 | 696 | Rx_flag = 1; |
Dennis40816 | 0:9c3829968b11 | 697 | } |
Dennis40816 | 0:9c3829968b11 | 698 | } |
Dennis40816 | 0:9c3829968b11 | 699 | #endif |
Dennis40816 | 0:9c3829968b11 | 700 | #if(DEBUG == 1) |
Dennis40816 | 2:c9889e740474 | 701 | if(bt_state.read()&& !rx_cnt) bt.printf("RX Callback,'%s' has been received\r\n",RX_buffer); |
Dennis40816 | 0:9c3829968b11 | 702 | #endif |
Dennis40816 | 0:9c3829968b11 | 703 | if(Rx_flag == 1){ |
Dennis40816 | 0:9c3829968b11 | 704 | RF_Receive(RX_buffer[0]-'0'); |
Dennis40816 | 0:9c3829968b11 | 705 | Rx_flag = 0; |
Dennis40816 | 0:9c3829968b11 | 706 | } |
Dennis40816 | 0:9c3829968b11 | 707 | } |