feng wang / Bob_Curtain_Sample_0506

Dependencies:   mbed-STM32F103C8T6 mbed-rtos mbed-dev

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "stm32f103c8t6.h"
00003 #include <string>
00004 #include "stdio.h"
00005 #include "stdlib.h"
00006 #include "rtos.h"
00007 
00008 #define MAX_LENGTH_STEPS 55
00009 #define MIN_LENGTH_STEPS 10
00010 #define MOVING_UP 1
00011 #define MOVING_DOWN 2
00012 #define MOVING_FORWARD 1
00013 #define MOVING_BACKWARD 2
00014 #define STOP 0
00015 #define DEBOUNCE 4   /*4*20ms*/
00016 
00017 DigitalOut MOTOA1(PB_4);
00018 DigitalOut MOTOB1(PB_5);
00019 
00020 DigitalOut MOTOA2(PB_8);
00021 DigitalOut MOTOB2(PB_9);
00022 
00023 AnalogIn SensorCurrent(PA_0);
00024 
00025 void motor1_move(uint8_t dir);
00026 void motor2_move(uint8_t dir);
00027 
00028 uint8_t sensor_cnt,cal_cnt, cur_cnt, tar_cnt, pre_sensor_cnt;
00029 float sense_value;
00030 uint8_t ov_flag, init_flag;
00031 uint8_t open_flag,close_flag;
00032 
00033 #if 1 /*WIFI related*/
00034 DigitalOut led1(PC_13);
00035 Serial debug_uart(PB_10, PB_11, 115200);
00036 Serial wifi_uart(PA_2, PA_3, 115200);
00037 Timer timer;  /*used by led0_thread*/
00038 string a(""); /*reserved*/
00039 string b(""); /*used by led0_thread*/
00040 char  g_isCloud = 0;   //flag for connected xiaomi cloud
00041 #endif
00042 
00043 Timer timer_m;
00044 int flag = 0;
00045 
00046 void Power_thread(void const *argument){/*detect current*/
00047   char i = 0;
00048   while(true){
00049     Thread::wait(200); /*unit millisec*/
00050     sense_value = SensorCurrent.read();
00051         //debug_uart.printf("dfdfdfdfddfdf\r\n");
00052     if(sense_value>0.5){
00053       debug_uart.printf("Power_thread: sense_value = %0.4f > 0.5 \r\n", sense_value);
00054       i++;
00055       if(i>1){
00056         ov_flag = 1;  
00057       }
00058     }else{
00059         i = 0;
00060     }    
00061   }
00062 }
00063 
00064 char CheckStopper(int ch){
00065     DigitalIn Stopper1(PA_13);
00066     DigitalIn Stopper2(PA_15);
00067     timer_m.reset();    
00068     char i = 0;
00069     while(ch == MOVING_UP){
00070         if(!Stopper1){i++;};
00071         if(i>DEBOUNCE){break;}
00072         if(timer_m.read_ms()>5000){return 1;}
00073         wait_ms(20);
00074     }
00075     while(ch == MOVING_DOWN){
00076         if(!Stopper2){i++;};
00077         if(i>DEBOUNCE){break;}
00078         if(timer_m.read_ms()>5000){return 1;}
00079         wait_ms(20);
00080     }
00081     return 0;
00082 }
00083 
00084 void Motor_thread(void const *argument){
00085   while(true){
00086     Thread::wait(200); /*unit millisec*/
00087         if(!init_flag){
00088       wait(1);
00089       debug_uart.printf("Motor_thread: motor2 move up\r\n");   
00090       motor2_move(MOVING_UP);
00091             if(CheckStopper(MOVING_UP)){
00092                 debug_uart.printf("Motor_thread: motor2 timeout, stop motor2 and finish the calibration\r\n");
00093                 init_flag = 1;
00094                 motor2_move(STOP);
00095                 continue;
00096             }
00097             motor2_move(STOP); 
00098             wait(1);
00099             debug_uart.printf("Motor_thread: motor1 move forward\r\n"); 
00100             sensor_cnt = 0;         
00101       motor1_move(MOVING_FORWARD);
00102       while(1){   
00103                 if(ov_flag){
00104                     motor1_move(STOP);
00105                     cal_cnt = sensor_cnt;
00106                     ov_flag = 0;
00107                     break;
00108                 }
00109       }     
00110       wait(1);
00111             debug_uart.printf("Motor_thread: motor2 move down\r\n");  
00112       motor2_move(MOVING_DOWN);
00113             if(CheckStopper(MOVING_DOWN)){
00114                 debug_uart.printf("Motor_thread: motor2 timeout, stop motor2 and finish the calibration\r\n");
00115                 init_flag = 1;
00116                 motor2_move(STOP);
00117                 continue;
00118             }
00119       motor2_move(STOP);
00120             wait(1);
00121             debug_uart.printf("Motor_thread: motor1 move backward\r\n");  
00122             sensor_cnt = 0;
00123       motor1_move(MOVING_BACKWARD);
00124       while(1){   
00125                 if(ov_flag){
00126                     motor1_move(STOP);
00127                     ov_flag = 0;
00128                     debug_uart.printf("Motor_thread: cal_cnt = %d, 1/2(cal_cnt+sensor_cnt)\r\n", (cal_cnt+sensor_cnt)/2); 
00129                     break;
00130                 }
00131                 if(sensor_cnt>=cal_cnt){
00132                     motor1_move(STOP);
00133                     debug_uart.printf("Motor_thread: cal_cnt = %d\r\n", cal_cnt);  
00134                     break;
00135                 }
00136       }
00137             cur_cnt = 0;            
00138       wait(1);
00139       debug_uart.printf("Motor_thread: motor2 move to center\r\n"); 
00140       motor2_move(MOVING_UP);
00141       wait(1.6);
00142       motor2_move(STOP);
00143             init_flag = 1;
00144             debug_uart.printf("Motor_thread: auto-curtain length was calibrated sucessfully\r\n");          
00145     }else{
00146             debug_uart.printf("Motor_thread: auto-curtain is waiting for command\r\n"); 
00147             wait(3);            
00148         }    
00149   }
00150 }
00151 
00152 void led0_thread(void const *argument) {
00153     int err;
00154     char rxBuf[32];
00155     char wifi_rxBuf[32];
00156     char i; 
00157     int position;
00158     char dat[2];
00159 
00160     while (1) {
00161         wifi_uart.printf("get_down\r");
00162         timer.reset();  
00163         b.clear();
00164         while(timer.read_ms()<5000){
00165             if(wifi_uart.readable()){
00166                 char ch = wifi_uart.getc();
00167                 if(ch!=0x0d){
00168                     b += ch;
00169                 }else{
00170                     debug_uart.printf("get string: %s \r\n", b.c_str());
00171                     #if 1
00172                     if(!strncmp(b.c_str(),"down none",9))
00173                     {
00174                         //debug_uart.printf("--- none\r\n");
00175                     }
00176                     if(!(strncmp(b.c_str(),"down set_properties",19)))  
00177                     {
00178                         //debug_uart.printf("--- set_properties\r\n");
00179                         b.erase(0,b.length()-3);
00180                         position = atoi(b.c_str());
00181                         debug_uart.printf("position = %d\r\n",position);
00182                         wifi_uart.printf("result 2 7 0\r\n");
00183                         b.clear();
00184                         continue;
00185                     }       
00186                     if(!(strncmp(b.c_str(),"down get_properties",19)))  
00187                     {
00188                         //debug_uart.printf("--- get_properties\r\n");
00189                         wifi_uart.printf("result 2 4 0 10 2 6 0 %d 2 7 30\r\n", position);
00190                         b.clear();
00191                         continue;
00192                     }               
00193                     if(!strncmp(b.c_str(),"down MIIO_net_change",20))
00194                     {
00195                         if((!strncmp(b.c_str()+21, "offline", 7)))
00196                         {
00197                             //debug_uart.printf("offline\r\n");
00198                         }
00199                         if((!strncmp(b.c_str()+21, "local", 5)))
00200                         {
00201                             //debug_uart.printf("local\r\n");
00202                         }
00203                         if((!strncmp(b.c_str()+21, "cloud", 5)))
00204                         {
00205                             //debug_uart.printf("cloud\r\n");
00206                         }
00207                     }
00208                     #endif                                      
00209                     break;
00210                 }
00211             }
00212         }
00213         while(wifi_uart.readable()){wifi_uart.getc();}  
00214         wait_ms(400);
00215     }
00216 }
00217 
00218 void serialprocess(void const *argument){
00219     while(1){
00220         if(debug_uart.readable()){
00221             char ch = debug_uart.getc();
00222             if(ch!=0x0d){
00223                 a += ch;
00224             }else{
00225                 debug_uart.printf("get command from uart: '%s'\r\n", a.c_str());
00226                 if(!strncmp(a.c_str(),"stp1",4)){
00227                     debug_uart.printf("stepper1 ready\r\n");
00228                 }
00229                 if(!strncmp(a.c_str(),"stp2",4)){
00230                     debug_uart.printf("stepper2 ready\r\n");
00231                 }
00232                 if(!strncmp(a.c_str(),"ovflag",6)){
00233                     debug_uart.printf("ovflag = 1\r\n");
00234                 }
00235                 a.clear(); 
00236             }
00237         }
00238     }
00239 }
00240 
00241 void serialIRQ(void){
00242     debug_uart.putc(debug_uart.getc());
00243 }
00244 
00245 void sensor_capture_cb(void){
00246   sensor_cnt++;
00247   if(open_flag){
00248         if(cur_cnt<tar_cnt){
00249             cur_cnt++;  
00250         }    
00251   }
00252   if(close_flag){
00253     if(cur_cnt>0){
00254       cur_cnt--;  
00255     }
00256   }
00257 }
00258 
00259 void motor1_move(uint8_t dir){/*main motor*/
00260   if(dir==1){/*forward*/
00261     MOTOA1 = 0;
00262     MOTOB1 = 1;
00263   }else if(dir==2){/*backward*/
00264     MOTOA1 = 1;
00265     MOTOB1 = 0;
00266   }else{ /*stop*/
00267     MOTOA1 = 0;
00268     MOTOB1 = 0;
00269   }
00270 }
00271 
00272 void motor2_move(uint8_t dir){/*assistant motor*/
00273   if(dir==1){/*up*/
00274     MOTOA2 = 0;
00275     MOTOB2 = 1;
00276   }else if(dir==2){/*down*/
00277     MOTOA2 = 1;
00278     MOTOB2 = 0;
00279   }else{ /*stop*/
00280     MOTOA2 = 0;
00281     MOTOB2 = 0;
00282   }
00283 }
00284 
00285 
00286 void system_init(){
00287   MOTOA1 = 0;
00288   MOTOB1 = 0;
00289   MOTOA2 = 0;
00290   MOTOB2 = 0;
00291   init_flag = 0; 
00292   cur_cnt = 0;
00293   cal_cnt = 0; 
00294   sense_value = 0;
00295     debug_uart.printf("\r\n");
00296   debug_uart.printf("************************************************\r\n");
00297   debug_uart.printf("******************LAIWU TECH********************\r\n");  
00298   debug_uart.printf("************************************************\r\n");
00299   debug_uart.printf("****system init done, wait 3 seconds to start***\r\n");
00300   wait(3);
00301 }
00302 
00303 int main() {
00304 
00305         system_init();
00306 
00307     led1 = 1; 
00308     flag = 0;
00309         
00310     wait(3);
00311         
00312         InterruptIn Hall1(PA_14);
00313         Hall1.fall(callback(sensor_capture_cb)); // Attach ISR to handle button press event
00314         
00315         debug_uart.printf("************************************************\r\n");
00316         debug_uart.printf("***************hall sensor init*****************\r\n");
00317 
00318 
00319     //Thread thread2(Power_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the real-time current*/
00320         debug_uart.printf("**********************2*************************\r\n");
00321         //Thread thread3(Motor_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the real-time current*/     
00322         debug_uart.printf("**********************3*************************\r\n");
00323         Thread thread1(led0_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the wifi connection*/
00324         debug_uart.printf("**********************1*************************\r\n");
00325         
00326         debug_uart.printf("**************three threads init****************\r\n");
00327         
00328     timer.start();      
00329         timer_m.start();
00330         
00331         debug_uart.printf("************************************************\r\n");
00332         debug_uart.printf("****************two timer init*****************\r\n");
00333         
00334         debug_uart.attach(&serialIRQ, Serial::RxIrq);
00335 
00336     while(1)
00337     {
00338        wait(1);
00339              //wifi_uart.printf("get_down\r");
00340       //debug_uart.printf("************************************************\r\n");
00341             //debug_uart.printf("********************%%%%%***********************\r\n");
00342     }
00343 }