feng wang / Mbed OS Hank_Curtain_Sample

Dependencies:   mbed-STM32F103C8T6_new

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.h"
00004 #include "main.h"
00005 #include "stdio.h"
00006 #include "stdlib.h"
00007 
00008 DigitalOut MOTOA1(PB_4);
00009 DigitalOut MOTOB1(PB_5);
00010 
00011 DigitalOut MOTOA2(PB_8);
00012 DigitalOut MOTOB2(PB_9);
00013 
00014 AnalogIn SensorCurrent(PA_0);
00015 
00016 void motor1_move(uint8_t dir);
00017 void motor2_move(uint8_t dir);
00018 
00019 void system_init();
00020 
00021 uint8_t sensor_cnt,cal_cnt, cal_cnt2, cur_cnt, tar_cnt;
00022 //uint8_t dir;
00023 float sense_value;
00024 uint8_t ov_flag, init_flag, motor1_ready_flag, motor2_ready_flag, sensor_flag, ready_flag;
00025 uint8_t open_flag,close_flag;
00026 float m_val = 0;
00027 
00028 void sensor_capture_cb(void){
00029     sensor_cnt++;
00030     cur_cnt++;
00031 }
00032 
00033 void Power_thread(){/*detect current*/
00034     char len[50];
00035   while(true){
00036     Thread::wait(500); /*unit millisec*/
00037     sense_value = SensorCurrent.read();
00038     if((sense_value>0.5)&&sensor_flag){
00039         sprintf(len, "position:%d", position);
00040         debug_uart.write("Power_thread: sense_value = %0.4f > 0.5 \r\n", sense_value);
00041         ov_flag = 1;
00042     }   
00043   }
00044 }
00045 
00046 void Motor1_thread(void const *argument){/*detect current*/
00047     while(true){
00048         Thread::wait(300); /*unit millisec*/
00049         if(!init_flag){
00050             while(1){
00051                 if(motor2_ready_flag){break;}else{
00052                     wait(1);debug_uart.printf("Motor1_thread: --------------------------------- \r\n");
00053                 }
00054             }
00055             motor2_ready_flag = 0;
00056             sensor_cnt = 0;
00057             motor1_move(MOVING_FORWARD);
00058             wait(1);
00059             sensor_flag = 1; /*enable current monitoring*/
00060             while(!ov_flag){debug_uart.printf("Motor1_thread: waiting for ov_flag = 1 \r\n"); wait(1);}
00061             motor1_move(STOP);
00062             //debug_uart.printf("overcurrent detected \r\n");     
00063             ov_flag = 0;
00064             motor2_ready_flag = 0;
00065             cal_cnt = sensor_cnt;
00066             //debug_uart.printf("calibration done \r\n");     
00067             debug_uart.printf("Motor1_thread: calibrated cnt is %d \r\n", cal_cnt);  
00068             wait(1);
00069             //debug_uart.printf("back to origianl position, motor1_ready_flag = 1\r\n"); 
00070             motor1_ready_flag = 1;
00071             while(1){
00072                 if(motor2_ready_flag){break;}else{
00073                     wait(1);debug_uart.printf("Motor1_thread: --------------------------------- \r\n");
00074                 }
00075             }
00076             motor2_ready_flag = 0;
00077             sensor_cnt = 0;
00078             //sensor_flag = 0;
00079             motor1_move(MOVING_BACKWARD);
00080             debug_uart.printf("Motor1_thread: target position is %d\r\n", cal_cnt-10);
00081             while(1){
00082                 //if((sensor_cnt>(cal_cnt-5))||ov_flag){break;}else{wait_ms(10);}
00083                 if(sensor_cnt>(cal_cnt-5)){break;}else{wait_ms(10);}
00084             }
00085             motor1_move(STOP);
00086             #if 0
00087             if(ov_flag){
00088                 debug_uart.printf("Motor1_thread: over current happens\r\n");
00089                 cal_cnt2 = sensor_cnt;
00090                 debug_uart.printf("Motor1_thread: cal_cnt2 = %d\r\n", cal_cnt2);
00091                 sensor_cnt = 0;
00092                 ov_flag = 0;            
00093             }else{
00094                 debug_uart.printf("Motor1_thread: arrive position %d \r\n", sensor_cnt); 
00095             }
00096             #endif
00097             debug_uart.printf("Motor1_thread: arrive position %d \r\n", sensor_cnt); 
00098             wait(1);
00099             motor1_ready_flag = 1;
00100             debug_uart.printf("Motor1_thread: motor1 thread done\r\n");
00101         }
00102     }
00103 }
00104 
00105 void Motor2_thread(void const *argument){/*detect current*/
00106     uint8_t sta1,sta2;
00107     DigitalIn Stopper1(PA_13);
00108     DigitalIn Stopper2(PA_15);
00109 
00110     while(true){
00111         Thread::wait(300); /*unit millisec*/
00112         if(!init_flag){
00113             wait(1);
00114             debug_uart.printf("Motor2_thread: motor2 move up\r\n");    
00115             motor2_move(MOVING_UP);
00116             while(Stopper1){;}
00117             motor2_move(STOP);
00118             debug_uart.printf("Motor2_thread: Up stopper triggered \r\n");        
00119             motor2_ready_flag = 1;
00120             wait(1);
00121             while(1){
00122                 if(motor1_ready_flag){break;}else{
00123                     wait(1);debug_uart.printf("Motor2_thread: --------------------------------- \r\n"); 
00124                 }
00125             }
00126             motor1_ready_flag = 0;
00127             motor2_move(MOVING_DOWN);
00128             while(Stopper2){;}
00129             motor2_move(STOP);
00130             debug_uart.printf("Motor2_thread: Down stopper triggered \r\n");      
00131             motor2_ready_flag = 1;
00132             while(1){
00133                 if(motor1_ready_flag){break;}else{
00134                     wait(1);debug_uart.printf("Motor2_thread: --------------------------------- \r\n"); 
00135                 }
00136             }
00137             debug_uart.printf("Motor2_thread: motor2 move to center\r\n"); 
00138             ov_flag = 0;
00139             ready_flag = 1;
00140             motor2_move(MOVING_UP);
00141             wait(2.3);
00142             motor2_move(STOP);
00143             ready_flag = 0;
00144             debug_uart.printf("Motor2_thread: motor2 thread done\r\n");
00145             debug_uart.printf("Main_thread: mean current is =%2.4f \r\n", m_val);  
00146             init_flag = 1;          
00147         }
00148     }
00149 }
00150 
00151 
00152 
00153 DigitalOut led1(PC_13);
00154 
00155 //Serial pc(PB_10,PB_11);
00156 
00157 UARTSerial *_serial;
00158 
00159 UARTSerial debug_uart(PB_10, PB_11, 115200);
00160 UARTSerial wifi_uart(PA_2, PA_3, 115200);
00161 
00162 //Serial wifi_uart(PA_2, PA_3, 115200);
00163 //Serial debug_uart(PB_10, PB_11, 115200);
00164 
00165 char      rxBuf[32];
00166 char      wifi_rxBuf[32];  //receive msg from xiaomi cloud
00167 short     g_isCloud = 0;   //flag for connected xiaomi cloud
00168 
00169 //char      set_property[] = {"down set_properties "}
00170 //char      get_property[] = {"down get_properties "}
00171 int       i = 0;
00172 
00173 ATCmdParser *_parser;
00174      
00175 void led1_thread() {
00176     int length;
00177     char len[20];
00178     char l;
00179     int position = 0;
00180     int error = 0;
00181     
00182     while (true) 
00183     {
00184         wifi_uart.write("get_down\r", 9);
00185         if(wifi_uart.readable())
00186         {
00187             length = wifi_uart.read(wifi_rxBuf, sizeof(wifi_rxBuf));
00188             if(!(strncmp(wifi_rxBuf,"down none",9)))
00189             {
00190                 //if return "down none"
00191                 debug_uart.write("--- none",8);
00192             }
00193             else if(!(strncmp(wifi_rxBuf,"down set_properties ",20)))
00194             {
00195                 //if return "down set_properties"
00196                 debug_uart.write("--- set:", 8);
00197                 debug_uart.write(wifi_rxBuf, length);
00198                 
00199                 //set properties
00200                 if(wifi_rxBuf[22] == '7')
00201                 {
00202                     //set target-position
00203                     position = atoi(&wifi_rxBuf[24]);
00204                     sprintf(len, "position:%d", position);
00205                     debug_uart.write(len, sizeof(len));
00206                 }
00207                 
00208                 //report result to cloud
00209                 wifi_uart.write("result 2 7 0\r", 13);
00210                 if(wifi_uart.readable())
00211                 {
00212                     length = wifi_uart.read(wifi_rxBuf, sizeof(wifi_rxBuf));
00213                     debug_uart.write(wifi_rxBuf, length);
00214                 }
00215             }
00216             else if(!(strncmp(wifi_rxBuf,"down get_properties ",20)))
00217             {
00218                 //if return "down get_properties"
00219                 debug_uart.write("--- get:", 8);
00220                 debug_uart.write(wifi_rxBuf, length);
00221                 
00222                 //report result to cloud
00223             }
00224             else if(!(strncmp(wifi_rxBuf,"down MIIO_net_change ",21)))
00225             {
00226                 //if return "down MIIO_net_change"
00227                 debug_uart.write(wifi_rxBuf, length);
00228                 debug_uart.write("--- net:",8);
00229                 if((!strncmp(&wifi_rxBuf[21], "offline", 7)))
00230                 {
00231                     //连接中
00232                     debug_uart.write("offline\r", 8);
00233                 }
00234                 else if((!strncmp(&wifi_rxBuf[21], "local", 5)))
00235                 {
00236                     //连上路由器但未连上服务器
00237                     debug_uart.write("local\r", 6);
00238                 }
00239                 else if((!strncmp(&wifi_rxBuf[21], "cloud", 5)))
00240                 {
00241                     //连上小米云服务器
00242                     debug_uart.write("cloud\r", 6);
00243                     g_isCloud = 1;
00244                 }
00245             }
00246             else
00247             {
00248                 debug_uart.write(wifi_rxBuf, length);
00249             }
00250             
00251          }
00252         thread_sleep_for(400);
00253     }
00254 }
00255 
00256 
00257 void led0_thread() {
00258     int length;
00259     while (1) {
00260         if(debug_uart.readable())
00261         {
00262               length = debug_uart.read(rxBuf, sizeof(rxBuf)); 
00263               debug_uart.write(rxBuf, length); 
00264               wifi_uart.write(rxBuf, length);
00265               debug_uart.write("111\r",4);
00266         }
00267         if(wifi_uart.readable())
00268         {
00269             length = wifi_uart.read(rxBuf, sizeof(rxBuf));
00270             debug_uart.write(rxBuf, length); 
00271             debug_uart.write("222\r",4);
00272               
00273         }
00274         wait(0.5);
00275     }
00276 }
00277 
00278 
00279 int main() {
00280     int length;
00281     led1 = 1;
00282     
00283     wait(1);
00284     
00285     debug_uart.write("hello world",11);
00286     
00287     WIFI_PWREN = 1;   
00288  
00289     //Thread thread0(osPriorityNormal, 512, nullptr, nullptr);
00290     Thread thread1(osPriorityNormal, 512, nullptr, nullptr);
00291     //Thread thread2(osPriorityNormal, 512, nullptr, nullptr);
00292     
00293     //thread0.start(led0_thread);
00294     thread1.start(led1_thread);
00295     
00296     //wifi_serial.printf("model\r\n");
00297     //thread1.start(led1_thread);
00298     //thread2.start(led2_thread);
00299     while(1)
00300     {
00301         debug_uart.write("--main--\r\n",10);
00302         if(g_isCloud)
00303         {
00304             debug_uart.write("connect to xiaomi cloud\r\n",25);
00305         }
00306         wait(3);
00307     }
00308     
00309     
00310        system_init();  
00311     InterruptIn Hall1(PA_14);
00312     //InterruptIn Hall2(PB_3);    
00313     Hall1.fall(callback(sensor_capture_cb)); // Attach ISR to handle button press event
00314     //Hall2.fall(callback(sensor_capture_cb)); // Attach ISR to handle button press event
00315     debug_uart.printf("Hall sensor init done\r\n");
00316 
00317     Thread thread1(Power_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the real-time current*/
00318     //debug_uart.printf("thread1~~~~~~~~~~~~~~~~\r\n");
00319     Thread thread2(Motor1_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the real-time current*/
00320     //debug_uart.printf("thread2~~~~~~~~~~~~~~~~\r\n");
00321     Thread thread3(Motor2_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); /*check the real-time current*/
00322     //debug_uart.printf("thread3~~~~~~~~~~~~~~~~\r\n");
00323     
00324     debug_uart.printf("three threads created\r\n");
00325 
00326     while(1){        
00327         //debug_uart.printf("~~~~~~~~~~~~~~\r\n");
00328         if(ready_flag){
00329             m_val += sense_value;   
00330             if(sense_value>0.5){
00331                 motor2_move(STOP);              
00332             }
00333         }
00334         //sense_value = SensorCurrent.read(); 
00335         //if(sense_value > 0.7){
00336             //debug_uart.printf("Main_thread: WARNING WARNING WARNING current data is higher than 0.7\r\n");
00337         //}
00338     }
00339 
00340 }
00341 
00342 void motor1_move(uint8_t dir){/*main motor*/
00343   if(dir==1){/*forward*/
00344     MOTOA1 = 0;
00345     MOTOB1 = 1;
00346   }else if(dir==2){/*backward*/
00347     MOTOA1 = 1;
00348     MOTOB1 = 0;
00349   }else{ /*stop*/
00350     MOTOA1 = 0;
00351     MOTOB1 = 0;
00352   }
00353 }
00354 
00355 void motor2_move(uint8_t dir){/*assistant motor*/
00356   if(dir==1){/*up*/
00357     MOTOA2 = 0;
00358     MOTOB2 = 1;
00359   }else if(dir==2){/*down*/
00360     MOTOA2 = 1;
00361     MOTOB2 = 0;
00362   }else{ /*stop*/
00363     MOTOA2 = 0;
00364     MOTOB2 = 0;
00365   }
00366 }
00367 
00368 void system_init(){
00369   debug_uart.baud(115200);
00370   MOTOA1 = 0;
00371   MOTOB1 = 0;
00372   MOTOA2 = 0;
00373   MOTOB2 = 0;
00374   init_flag = 0;
00375   motor1_ready_flag = 0;
00376   motor2_ready_flag = 0;
00377   sense_value = 0;
00378   sensor_flag = 0;
00379   ready_flag = 0; 
00380   debug_uart.printf("*******************************\r\n");
00381   debug_uart.printf("**********LAIWU TECH***********\r\n");  
00382   debug_uart.printf("*******************************\r\n");
00383   debug_uart.printf("system init done, wait 3 seconds to start\r\n");
00384   wait(3);
00385 }