test
Dependencies: mbed-STM32F103C8T6 mbed-rtos mbed-dev
Diff: main.cpp
- Revision:
- 9:e859598fa5d8
- Parent:
- 8:a32b83084287
--- a/main.cpp Wed Apr 29 03:14:26 2020 +0000 +++ b/main.cpp Wed Apr 29 12:14:31 2020 +0000 @@ -19,11 +19,9 @@ void system_init(); uint8_t sensor_cnt,cal_cnt, cal_cnt2, cur_cnt, tar_cnt, pre_sensor_cnt; -//uint8_t dir; float sense_value; uint8_t ov_flag, init_flag, motor1_ready_flag, motor2_ready_flag, sensor_flag, ready_flag; uint8_t open_flag,close_flag; -float m_val = 0; #define MAX_LENGTH_STEPS 55 #define MIN_LENGTH_STEPS 10 @@ -33,10 +31,10 @@ #define MOVING_BACKWARD 2 #define STOP 0 +#define DEBOUNCE 20 + //--------------------------------------------------- -DigitalOut led1(PC_13); - -//Serial pc(PB_10,PB_11); +DigitalOut WIFI_PWREN(PC_13); UARTSerial *_serial; @@ -49,19 +47,17 @@ Timeout button1_timeout; // Used for debouncing // Enables button when bouncing is over -void button1_enabled_cb(void) -{ - button1_enabled = true; +void button1_enabled_cb(void){ + button1_enabled = true; } // ISR handling button pressed event -void button1_onpressed_cb(void) -{ - if (button1_enabled) { // Disabled while the button is bouncing - button1_enabled = false; - button1_pressed = true; // To be read by the main loop - button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms - } +void button1_onpressed_cb(void){ + if (button1_enabled){ // Disabled while the button is bouncing + button1_enabled = false; + button1_pressed = true; // To be read by the main loop + button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms + } } @@ -78,14 +74,17 @@ //--------------------------------------------------- ATCmdParser *_parser; + void sensor_capture_cb(void){ sensor_cnt++; if(open_flag){ - cur_cnt++; + if(cur_cnt<tar_cnt){ + cur_cnt++; + } } if(close_flag){ if(cur_cnt>0){ - cur_cnt--; + cur_cnt--; } } } @@ -96,7 +95,8 @@ while(true){ Thread::wait(200); /*unit millisec*/ sense_value = SensorCurrent.read(); - if((sense_value>0.5)&&sensor_flag){ + //if((sense_value>0.5)&&sensor_flag){ + if(sense_value>0.5){ sprintf(len, "Power_thread: sense_value = %0.4f > 0.5 \r\n", sense_value); debug_uart.write(len, sizeof(len)); i++; @@ -104,9 +104,8 @@ ov_flag = 1; } }else{ - i = 0; - } - + i = 0; + } } } @@ -125,7 +124,7 @@ sensor_cnt = 0; motor1_move(MOVING_FORWARD); wait(1); - sensor_flag = 1; /*enable current monitoring*/ + //sensor_flag = 1; /*enable current monitoring*/ while(!ov_flag){ debug_uart.write("Motor1_thread: waiting for ov_flag = 1 \r\n",41); wait(1); @@ -161,56 +160,65 @@ motor1_ready_flag = 1; while(!init_flag){debug_uart.write("Motor1_thread: -------------------------------\r\n",48);wait(1);} }else{ - debug_uart.write("Motor1_thread: calibration done, wait for motor2 action.\r\n", 58); + debug_uart.write("Motor1_thread: -----check motor2_ready_flag-----\r\n",50); while(1){ - if(motor2_ready_flag){break;}else{ - wait(1); - debug_uart.write("Motor1_thread: -------------------------------\r\n",48); - } + if(motor2_ready_flag){break;} + debug_uart.write("Motor1_thread: -----check motor2_ready_flag-----\r\n",50); + wait(1); } + debug_uart.write("Motor1_thread: ------motor2_ready_flag = 1------\r\n",50); motor2_ready_flag = 0; + sprintf(len, "Motor1_thread: cur_cnt = %d. \r\n", cur_cnt); + debug_uart.write(len, sizeof(len)); if(open_flag){ - motor1_move(MOVING_FORWARD); - while(1){ - sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); - debug_uart.write(len, sizeof(len)); - sprintf(len, "Motor1_thread: tar_cnt is %d \r\n", tar_cnt); - debug_uart.write(len, sizeof(len)); - debug_uart.write("Motor1_thread: **************************** \r\n", 46); - wait(1); - if(cur_cnt>=tar_cnt){ - break; - } - if(ov_flag){ - break; - } + motor1_move(MOVING_FORWARD); + while(1){ + sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); + debug_uart.write(len, sizeof(len)); + sprintf(len, "Motor1_thread: tar_cnt is %d \r\n", tar_cnt); + debug_uart.write(len, sizeof(len)); + wait(1); + if(cur_cnt>=tar_cnt){ + debug_uart.write("Motor1_thread: *********************************\r\n", 50); + sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); + debug_uart.write(len, sizeof(len)); + break; } + if(ov_flag){ + debug_uart.write("Motor1_thread: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", 50); + break; + } + } } if(close_flag){ - motor1_move(MOVING_BACKWARD); - while(1){ - sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); - debug_uart.write(len, sizeof(len)); - sprintf(len, "Motor1_thread: tar_cnt is %d \r\n", tar_cnt); - debug_uart.write(len, sizeof(len)); - debug_uart.write("Motor1_thread: **************************** \r\n", 46); - wait(1); - if(cur_cnt<=tar_cnt){ - break; - } - if(ov_flag){ - break; - } + motor1_move(MOVING_BACKWARD); + while(1){ + sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); + debug_uart.write(len, sizeof(len)); + sprintf(len, "Motor1_thread: tar_cnt is %d \r\n", tar_cnt); + debug_uart.write(len, sizeof(len)); + wait(1); + if(cur_cnt<=tar_cnt){ + debug_uart.write("Motor1_thread: *********************************\r\n", 50); + sprintf(len, "Motor1_thread: cur_cnt is %d \r\n", cur_cnt); + debug_uart.write(len, sizeof(len)); + break; } + if(ov_flag){ + debug_uart.write("Motor1_thread: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", 50); + break; + } + } } motor1_move(STOP); if(!ov_flag){ - cur_cnt = tar_cnt; + cur_cnt = tar_cnt; + debug_uart.write("Motor1_thread: xxxxxxcur_cnt = tar_cntxxxxxxxxxx\r\n", 50); }else{ - if(open_flag){cur_cnt = 90;} - if(close_flag){cur_cnt = 0;} + if(open_flag){cur_cnt = cal_cnt;} + if(close_flag){cur_cnt = 0;} + ov_flag = 0; } - ov_flag = 0; motor1_ready_flag = 1; } } @@ -258,46 +266,46 @@ motor2_move(STOP); ready_flag = 0; debug_uart.write("Motor2_thread: motor2 thread done\r\n",35); - sprintf(len, "Main_thread: mean current is =%2.4f \r\n", m_val); - debug_uart.write(len, sizeof(len)); cur_cnt = 0; init_flag = 1; }else{ - debug_uart.write("Motor2_thread: calibration done.wait for open/close flag\r\n", 58); + debug_uart.write("Motor2_thread: ------------command?-------------\r\n", 50); + sprintf(len, "Motor2_thread: cur_cnt = %d. \r\n", cur_cnt); + debug_uart.write(len, sizeof(len)); while(1){ - if(open_flag){break;} - if(close_flag){break;} - wait(1); - debug_uart.write("Motor2_thread: ------------command?------------- \r\n", 51); + if(open_flag){break;} + if(close_flag){break;} + debug_uart.write("Motor2_thread: ------------command?-------------\r\n", 50); + wait(1); } if(open_flag){ - debug_uart.write("Motor2_thread: --------------open--------------- \r\n", 51); + debug_uart.write("Motor2_thread: --------------open---------------\r\n", 50); motor2_move(MOVING_UP); - while(Stopper1){;} - motor2_move(STOP); - motor2_ready_flag = 1; + while(1){ + if(!Stopper1){i++;}; + if(i>DEBOUNCE){break;} + } } if(close_flag){ - debug_uart.write("Motor2_thread: -------------close-------------- \r\n", 50); + debug_uart.write("Motor2_thread: -------------close---------------\r\n", 50); motor2_move(MOVING_DOWN); while(1){ - if(!Stopper2){i++;}; - if(i>20){break;} + if(!Stopper2){i++;}; + if(i>DEBOUNCE){break;} } - i = 0; - motor2_move(STOP); - motor2_ready_flag = 1; } + i = 0; + motor2_move(STOP); + motor2_ready_flag = 1; while(1){ - if(motor1_ready_flag){break;}else{ - wait(1); - debug_uart.write("Motor2_thread: --------------------------------- \r\n", 51); - } + if(motor1_ready_flag){break;} + debug_uart.write("Motor2_thread: ---check motor1_ready_flag-------\r\n",50); + wait(1); } + debug_uart.write("Motor2_thread: -------motor1_ready_flag = 1-------\r\n",50); motor1_ready_flag = 0; - debug_uart.write("Motor2_thread: motor2 move to center\r\n", 38); wait(1); - ready_flag = 1; + debug_uart.write("Motor2_thread: -------motor2 move to center-------\r\n",50); if(open_flag){ motor2_move(MOVING_DOWN); } @@ -306,9 +314,9 @@ } wait(1.6); motor2_move(STOP); - ready_flag = 0; - debug_uart.write("Motor2_thread: motor2 thread done\r\n", 35); - pre_sensor_cnt = sensor_cnt; + debug_uart.write("Motor2_thread: --------motor2 thread done-------\r\n",50); + sensor_cnt = 0; + pre_sensor_cnt = 0; open_flag = 0; close_flag = 0; } @@ -466,9 +474,7 @@ } } -void system_init() -{ - +void system_init(){ MOTOA1 = 0; MOTOB1 = 0; MOTOA2 = 0; @@ -479,86 +485,76 @@ motor1_ready_flag = 0; motor2_ready_flag = 0; sense_value = 0; - sensor_flag = 1; ready_flag = 0; - debug_uart.write("*******************************\r\n",33); - debug_uart.write("**********LAIWU TECH***********\r\n",33); - debug_uart.write("*******************************\r\n",33); - debug_uart.write("system init done, wait 3 seconds to start\r\n",43); + debug_uart.write("************************************************\r\n",50); + debug_uart.write("******************LAIWU TECH********************\r\n",50); + debug_uart.write("************************************************\r\n",50); + debug_uart.write("****system init done, wait 3 seconds to start***\r\n",50); wait(3); } int main() { - int length; - led1 = 1; - char len[50]; - - debug_uart.write("hello world",11); - WIFI_PWREN = 1; + int length; + char len[50]; + system_init(); + WIFI_PWREN = 1; + InterruptIn Hall1(PA_14); + Hall1.fall(callback(sensor_capture_cb)); // Attach ISR to handle button press event + DebugKey.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event + debug_uart.write("************************************************\r\n",50); + debug_uart.write("************Hall sensor init done***************\r\n",50); + + //Thread thread0(osPriorityNormal, 512, nullptr, nullptr); + Thread thread1(osPriorityNormal, 512, nullptr, nullptr); + //Thread thread2(osPriorityNormal, 512, nullptr, nullptr); + + Thread thread2(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ + //debug_uart.printf("thread1~~~~~~~~~~~~~~~~\r\n"); + Thread thread3(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ + //debug_uart.printf("thread2~~~~~~~~~~~~~~~~\r\n"); + Thread thread4(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ + //debug_uart.printf("thread3~~~~~~~~~~~~~~~~\r\n"); + + //thread0.start(led0_thread); + thread1.start(led1_thread); + thread2.start(Power_thread); + thread3.start(Motor1_thread); + thread4.start(Motor2_thread); + + debug_uart.write("************************************************\r\n",50); + debug_uart.write("************Four Threads created****************\r\n",50); + + while(1){ + if(g_isCloud){ + debug_uart.write("***********WIFI Status: connected***************\r\n",50); + } + if (button1_pressed) { // Set when button is pressed + button1_pressed = false; + debug_uart.write("-------------restore wifi module----------------\r\n",50); + wifi_uart.write("restore\r\n", 9); + } + #if 0 + if(!open_flag&&!close_flag){ + if(sensor_cnt>(pre_sensor_cnt+4)){ + if(cur_cnt<5){ + open_flag = 1; + tar_cnt = cal_cnt; + cur_cnt = cur_cnt+sensor_cnt; + debug_uart.write("main_thread: ---------------open----------------\r\n", 50); + }else{ + close_flag = 1; + tar_cnt = 0; + cur_cnt = cur_cnt-sensor_cnt; + debug_uart.write("main_thread: ---------------close---------------\r\n", 50); + } + sprintf(len, "main_thread: cur_cnt = %d. \r\n", cur_cnt); + debug_uart.write(len, sizeof(len)); + } + pre_sensor_cnt = sensor_cnt; + } + #endif wait(3); - system_init(); - InterruptIn Hall1(PA_14); - //InterruptIn Hall2(PB_3); - Hall1.fall(callback(sensor_capture_cb)); // Attach ISR to handle button press event - DebugKey.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event - //Hall2.fall(callback(sensor_capture_cb)); // Attach ISR to handle button press event - debug_uart.write("Hall sensor init done\r\n", 23); - - - //Thread thread0(osPriorityNormal, 512, nullptr, nullptr); - Thread thread1(osPriorityNormal, 512, nullptr, nullptr); - //Thread thread2(osPriorityNormal, 512, nullptr, nullptr); - - Thread thread2(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ - //debug_uart.printf("thread1~~~~~~~~~~~~~~~~\r\n"); - Thread thread3(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ - //debug_uart.printf("thread2~~~~~~~~~~~~~~~~\r\n"); - Thread thread4(osPriorityNormal, 512, nullptr, nullptr); /*check the real-time current*/ - //debug_uart.printf("thread3~~~~~~~~~~~~~~~~\r\n"); - - //thread0.start(led0_thread); - thread1.start(led1_thread); - thread2.start(Power_thread); - thread3.start(Motor1_thread); - thread4.start(Motor2_thread); - - debug_uart.write("four threads created\r\n",22); - - while(1) - { - if(g_isCloud) - { - debug_uart.write("connected\r\n",11); - } - if(ready_flag){ - m_val += sense_value; - if(sense_value>0.5){ - motor2_move(STOP); - } - } - if (button1_pressed) { // Set when button is pressed - button1_pressed = false; - debug_uart.write("restore wifi module\r\n",21); - wifi_uart.write("restore\r\n", 9); - } - if(!open_flag&&!close_flag){ - if(sensor_cnt>(pre_sensor_cnt+5)){ - if(cur_cnt<5){open_flag = 1;tar_cnt = 90;}else{ - close_flag = 1; - tar_cnt = 0; - } - } - sprintf(len, "main_thread: sensor_cnt = %d. \r\n", sensor_cnt); - debug_uart.write(len, sizeof(len)); - sprintf(len, "main_thread: pre_sensor_cnt = %d. \r\n", pre_sensor_cnt); - debug_uart.write(len, sizeof(len)); - pre_sensor_cnt = sensor_cnt; - } - wait(3); - - //sprintf(len, "main_thread: current sense value is %.4f. \r\n", sense_value); - //debug_uart.write(len, sizeof(len)); - - - } + sprintf(len, "main_thread: cur_cnt = %d. \r\n", cur_cnt); + debug_uart.write(len, sizeof(len)); + } } \ No newline at end of file