linetrace

Dependencies:   RemoteIR TextLCD

Revision:
49:178dcafb4f0e
Parent:
48:3003ea51c619
Child:
50:8843bee40df5
--- a/main.cpp	Mon Aug 31 01:22:48 2020 +0000
+++ b/main.cpp	Tue Sep 01 01:43:17 2020 +0000
@@ -196,7 +196,7 @@
     deco_thread -> set_priority(osPriorityRealtime);
     motor_thread = new Thread(motor);
     motor_thread -> set_priority(osPriorityHigh);
-//    wifi_thread -> set_priority(osPriorityRealtime);
+    wifi_thread -> set_priority(osPriorityRealtime);
     display();
 }
 
@@ -775,33 +775,52 @@
     getreply();
     esp.baud(115200);   // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
     startserver();
-
+ 
     while(1) {
         if(DataRX==1) {
+            pc.printf("\f\n\r------------ main while > if --------------\n\r");
             click_flag = 1;
             ReadWebData();
-            if (servreq == 1 && weberror == 0) {    // ページ表示のリクエストがあったとき
+            pc.printf("\f\n\r------------ click_flag=%d --------------\n\r",click_flag);
+            //if ((servreq == 1 && weberror == 0) && click_flag == 1) {
+            if (servreq == 1 && weberror == 0) {
+                pc.printf("\f\n\r------------ befor send page --------------\n\r");
                 sendpage();
             }
+            pc.printf("\f\n\r------------ send_check begin --------------\n\r");
+ 
+            //sendcheck();
+            pc.printf("\f\n\r------------ ssend_check end--------------\n\r");
+ 
             esp.attach(&callback);
+            pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type);
+            pc.printf("\n\n  HTTP Packet: \n\n%s\n", webdata);
+            pc.printf("  Web Characters sent : %d\n\n", bufl);
+            pc.printf("  -------------------------------------\n\n");
             servreq=0;
         }
-        run = beforeRun;
         ThisThread::sleep_for(100);
-        beforeRun = run;
-        run = STOP;
     }
 }
 // Static WEB page
 void sendpage()
 {
 // WEB page data
+ 
     strcpy(webbuff, "<!DOCTYPE html>");
     strcat(webbuff, "<html><head><title>RobotCar</title><meta name='viewport' content='width=device-width'/>");
+    //strcat(webbuff, "<meta http-equiv=\"refresh\" content=\"5\"; >");
     strcat(webbuff, "<style type=\"text/css\">.noselect{ width:100px;height:60px;}.light{ width:100px;height:60px;background-color:#00ff66;}.load{ width: 50px; height: 30px;font-size:10px}</style>");
     strcat(webbuff, "</head><body><center><p><strong>Robot Car Remote Controller");
     strcat(webbuff, "</strong></p><td style='vertical-align:top;'><strong>Battery level ");
-    strcat(webbuff, "<input type=\"text\" id=\"leftms\" size=4 value=250>%</strong>");
+    if(b > 30) {    //残電量表示
+        sprintf(webbuff, "%s%3d", webbuff, b);
+    } else {        //30%より下の場合残電量を赤文字
+        strcat(webbuff, "<font color=\"red\">");
+        sprintf(webbuff, "%s%3d", webbuff, b);
+        strcat(webbuff, "</font>");
+    }
+    strcat(webbuff, "%</strong>");
     strcat(webbuff, "<button id=\"reloadbtn\" type=\"button\" class=\"load\" onclick=\"rel()\">RELOAD</button>");
     strcat(webbuff, "</td></p>");
     strcat(webbuff, "<br>");
@@ -972,47 +991,34 @@
             break;
     }
     strcat(webbuff, "</button></td></tr></table>");
+ 
     strcat(webbuff, "</center>");
     strcat(webbuff, "</body>");
     strcat(webbuff, "</html>");
+    
     strcat(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");       //機能
+    
     strcat(webbuff, "function rel(){");
     strcat(webbuff, "location.reload();");
     strcat(webbuff, "}");
+    
+    strcat(webbuff, "var button_9 = document.getElementsByTagName(\"button\");");
+    
+//ボタン入力時それぞれの関数から呼び出されサーバーとの通信を行う
     strcat(webbuff, "function htmlacs(url) {");
-    strcat(webbuff, "var xhr = new XMLHttpRequest();");
-    strcat(webbuff, "xhr.open(\"GET\", url);");
-    strcat(webbuff, "xhr.send(\"\");");
+        strcat(webbuff, "for(var m=0;m<11;m++){button_9[m].disabled=true;}");  
+        strcat(webbuff, "var xhr = new XMLHttpRequest();");
+        strcat(webbuff, "xhr.open(\"GET\", url);");
+        strcat(webbuff, "xhr.onreadystatechange = function(){");
+            //strcat(webbuff, "if(this.readyState == 4 && this.status == 200){");
+            strcat(webbuff, "if(this.readyState == 4){");
+                strcat(webbuff, "for(var m=0;m<10;m++){button_9[m].disabled=false;}");
+            strcat(webbuff, "}");
+        strcat(webbuff, "};");
+        strcat(webbuff, "xhr.send(\"\");");
     strcat(webbuff, "}");
-//0825
-//0824 battery update auto
-    strcat(webbuff, "function battery_update() {");
-    strcat(webbuff, "var url1 =  \"http://\" + window.location.hostname+ \"/cargo?a=responseBattery\";");
-    strcat(webbuff, "var xhr1 = new XMLHttpRequest();");
-    strcat(webbuff, "xhr1.open(\"GET\", url1);");
-//0820
-    strcat(webbuff, "xhr1.onreadystatechange = function(){");
-    strcat(webbuff, "if(this.readyState == 4 && this.status == 200){");
-    strcat(webbuff, "var res1 = xhr1.responseText;");
-//color                
-    strcat(webbuff, "var battery_num=res1;");
-    strcat(webbuff, "if(battery_num>0 && battery_num<31){");
-    strcat(webbuff, "document.getElementById('leftms').style.color=\"red\";");
-    strcat(webbuff, "}");
-    strcat(webbuff, "if(battery_num>39 && battery_num<61){");
-    strcat(webbuff, "document.getElementById('leftms').style.color=\"orange\";");
-    strcat(webbuff, "}");
-    strcat(webbuff, "if(battery_num>69 && battery_num<101){");
-    strcat(webbuff, "document.getElementById('leftms').style.color=\"blue\";");
-    strcat(webbuff, "}");
-//color
-    strcat(webbuff, "document.getElementById('leftms').value=res1;}};");
-//0820
-    strcat(webbuff, "xhr1.send();");
-    strcat(webbuff, "}");
-    strcat(webbuff, "setInterval(battery_update,15000);");
-//0824 battery update auto
-//0825
+    
+//mode変更ボタン入力時動作
     strcat(webbuff, "function send_mes(btnmes,btnval){");       //mode変更ボタン入力時の点灯消灯判定
     strcat(webbuff, "console.log(btnval);");
     strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
@@ -1027,6 +1033,7 @@
     strcat(webbuff, "}");
     strcat(webbuff, "}");
     strcat(webbuff, "}");
+    
     strcat(webbuff, "function send_mes_spe(btnmes,btnval){");       //speed変更ボタン入力時の点灯消灯判定
     strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
     strcat(webbuff, "htmlacs(url);");
@@ -1041,16 +1048,26 @@
     strcat(webbuff, "}");
     strcat(webbuff, "}");
     strcat(webbuff, "</script>");
+    
+    
 // end of WEB page data
     bufl = strlen(webbuff); // get total page buffer length
+    //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
  
     sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl>2048?2048:bufl)); // send IPD link channel and buffer character length.
     timeout=500;
     getcount=40;
     SendCMD();
     getreply();
+    pc.printf(replybuff);
+    //pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl));
+ 
+    pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
+ 
     //pastthrough mode
     SendWEB();  // send web page
+    pc.printf("\n++++++++++ webbuff clear  ++++++++++\r\n");
+ 
     memset(webbuff, '\0', sizeof(webbuff));
     sendcheck();
 }
@@ -1062,45 +1079,30 @@
     if(esp.writeable()) {
         while(webbuff[i]!='\0') {
             esp.putc(webbuff[i]);
+ 
             //****
             //output at command when 2000
             if(((i%2047)==0) && (i>0)) {
-                run = beforeRun;
+                //wait_ms(10);
                 ThisThread::sleep_for(100);
-                beforeRun = run;
-                run = STOP;
-                sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl-2048*(i/2047))>2048?2048:(bufl-2048*(i/2047))); // send IPD link channel and buffer character length.
+                sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048)); // send IPD link channel and buffer character length.
+                //pc.printf("\r\n++++++++++ AT+CIPSENDBUF=%d,%d ++++++++++\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048));
                 timeout=600;
                 getcount=50;
                 SendCMD();
                 getreply();
+                //pc.printf(replybuff);
+                //pc.printf("\r\n+++++++++++++++++++\r\n");
             }
             //****
             i++;
+            //pc.printf("%c",webbuff[i]);
         }
     }
+    pc.printf("\n++++++++++ send web i= %dinfo ++++++++++\r\n",i);
 }
  
  
-void sendpage2()
-{
-// WEB page data
-    sprintf(battery_ch,"%d",b);
-    strcpy(webbuff, battery_ch);
-// end of WEB page data
-    bufl = strlen(webbuff); // get total page buffer length
-    
-    sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl>2048?2048:bufl)); // send IPD link channel and buffer character length.
-    timeout=500;
-    getcount=40;
-    SendCMD();
-    getreply();
-    //pastthrough mode
-    SendWEB();  // send web page
-    memset(webbuff, '\0', sizeof(webbuff));
-    sendcheck();
-}
- 
 void sendcheck()
 {
     weberror=1;
@@ -1109,21 +1111,32 @@
     time2.reset();
     time2.start();
  
+    /*
+    while(weberror==1 && time2.read() <5) {
+        getreply();
+        if (strstr(replybuff, "SEND OK") != NULL) {
+            weberror=0;   // wait for valid SEND OK
+        }
+    }
+    */
     if(weberror==1) { // restart connection
         strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
         timeout=500;
         getcount=10;
         SendCMD();
         getreply();
+        pc.printf(replybuff);
         sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
         timeout=500;
         getcount=10;
         SendCMD();
         getreply();
+        pc.printf(replybuff);
     } else {
         sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
         SendCMD();
         getreply();
+        pc.printf(replybuff);
     }
     time2.reset();
 }
@@ -1131,10 +1144,8 @@
 // Reads and processes GET and POST web data
 void ReadWebData()
 {
-    run = beforeRun;
+    pc.printf("+++++++++++++++++Read Web Data+++++++++++++++++++++\r\n");
     ThisThread::sleep_for(200);
-    beforeRun = run;
-    run = STOP;
     esp.attach(NULL);
     ount=0;
     DataRX=0;
@@ -1145,22 +1156,24 @@
         strcpy(webdata, webbuff + x);
         weberror=0;
         int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
-         if( strstr(webdata, "responseBattery") != NULL ) {
-             click_flag = 0;
-             led4=!led4;
-             sendpage2();
-        }
+        //int i=0;
+        pc.printf("+++++++++++++++++succed rec begin+++++++++++++++++++++\r\n");
+        pc.printf("%s",webdata);
+        pc.printf("+++++++++++++++++succed rec end+++++++++++++++++++++\r\n");
         if( strstr(webdata, "Normal") != NULL ) {
+            pc.printf("++++++++++++++++++Normal++++++++++++++++++++");
             mode = SPEED;       // スピードモード
             flag_sp = 0;
             display();          // ディスプレイ表示
             mode = beforeMode;  // 現在のモードに前回のモードを設定
         }else if( strstr(webdata, "VeryFast") != NULL ) {
+            pc.printf("+++++++++++++++++++VeryFast+++++++++++++++++++");
             mode = SPEED;       // スピードモード 
             flag_sp = 2;
             display();          // ディスプレイ表示
             mode = beforeMode;  // 現在のモードに前回のモードを設定
         }else if( strstr(webdata, "Fast") != NULL ) {
+            pc.printf("++++++++++++++++++++Fast++++++++++++++++++");
             mode = SPEED;       // スピードモード
             flag_sp = 1;
             display();          // ディスプレイ表示
@@ -1169,14 +1182,16 @@
             beforeMode = mode;
         }
         if( strstr(webdata, "GO") != NULL ) {
+            pc.printf("+++++++++++++++++前進+++++++++++++++++++++\r\n");
             //delete avoi_thread;     //障害物回避スレッド停止
             //delete trace_thread;    //ライントレーススレッド停止
             run = ADVANCE;          // 前進
             mode = ADVANCE;         // モード変更
             display();              // ディスプレイ表示
         }
- 
+
         if( strstr(webdata, "LEFT") != NULL ) {
+            pc.printf("+++++++++++++++++左折+++++++++++++++++++++\r\n");
             //delete avoi_thread;     //障害物回避スレッド停止
             //delete trace_thread;    //ライントレーススレッド停止
             run = LEFT;             // 左折
@@ -1185,6 +1200,7 @@
         }
  
         if( strstr(webdata, "STOP") != NULL ) {
+            pc.printf("+++++++++++++++++停止+++++++++++++++++++++\r\n");
             //delete avoi_thread;     //障害物回避スレッド停止
             //delete trace_thread;    //ライントレーススレッド停止
             run = STOP;             // 停止
@@ -1193,6 +1209,7 @@
         }
  
         if( strstr(webdata, "RIGHT") != NULL ) {
+            pc.printf("+++++++++++++++++右折+++++++++++++++++++++\r\n");
             //delete avoi_thread;     //障害物回避スレッド停止
             //delete trace_thread;    //ライントレーススレッド停止
             run = RIGHT;            // 右折
@@ -1201,14 +1218,17 @@
         }
  
         if( strstr(webdata, "BACK") != NULL ) {
+            pc.printf("+++++++++++++++++後進+++++++++++++++++++++\r\n");
             //delete avoi_thread;     //障害物回避スレッド停止
             //delete trace_thread;    //ライントレーススレッド停止
             run = BACK;             // 後進
             mode = BACK;            // モード変更
             display();              // ディスプレイ表示
         }
+        pc.printf("+++++++++++++++++succed+++++++++++++++++++++");
  
         if( strstr(webdata, "AVOIDANCE") != NULL ) {
+            pc.printf("+++++++++++++++++AVOIDANCE+++++++++++++++++++++");
             if(avoi_thread->get_state() == Thread::Deleted) {
                 delete avoi_thread;     //障害物回避スレッド停止
                 avoi_thread = new Thread(avoidance);
@@ -1219,6 +1239,8 @@
             display();          // ディスプレイ表示
         }
         if( strstr(webdata, "LINE_TRACE") != NULL ) {
+            pc.printf("+++++++++++++++++LINET RACE+++++++++++++++++++++");
+            pc.printf("mode = LINE_TRACE\r\n");
             if(trace_thread->get_state() == Thread::Deleted) {
                 delete trace_thread;    //ライントレーススレッド停止
                 trace_thread = new Thread(trace);
@@ -1296,10 +1318,12 @@
         pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
         pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
         replybuff[strlen(replybuff)-1] = '\0';
+        //char* IP = replybuff + 5;
         sprintf(webdata,"%s", replybuff);
         pc.printf(webdata);
         led2=1;
         bufflen=200;
+        //bufflen=100;
         ount=0;
         pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
         setup();
@@ -1345,9 +1369,9 @@
 /* mainスレッド */
 int main() {
     /* 初期設定 */
-//    wifi_thread = new Thread(wifi);
-//    wifi_thread -> set_priority(osPriorityHigh);
-    setup();
+    wifi_thread = new Thread(wifi);
+    wifi_thread -> set_priority(osPriorityHigh);
+//    setup();
     avoi_thread = new Thread(avoidance);
     avoi_thread->terminate();
     trace_thread = new Thread(trace);
@@ -1359,7 +1383,7 @@
     lcd.setBacklight(TextLCD::LightOn);  // バックライトON
     lcd.setAddress(0,1);
     lcd.printf("Mode:SetUp");
-    display();              // ディスプレイ表示
+//    display();              // ディスプレイ表示
     
     while(1){
         bChange();          // バッテリー残量更新