ボタンを押すと、 バッテリ更新を停止し、 他のボタンもロックさせる

Dependencies:   RemoteIR TextLCD

Committer:
faker_71
Date:
Tue Sep 01 05:36:30 2020 +0000
Revision:
51:622df065f7ff
Parent:
50:6c25bf1c39d7
0901

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yangtzuli 0:0d0037aabe41 1 /* mbed Microcontroller Library
yangtzuli 0:0d0037aabe41 2 * Copyright (c) 2019 ARM Limited
yangtzuli 0:0d0037aabe41 3 * SPDX-License-Identifier: Apache-2.0
yangtzuli 0:0d0037aabe41 4 */
yangtzuli 0:0d0037aabe41 5
yangtzuli 0:0d0037aabe41 6 #include "mbed.h"
yangtzuli 0:0d0037aabe41 7 #include "ReceiverIR.h"
yangtzuli 0:0d0037aabe41 8 #include "rtos.h"
yangtzuli 0:0d0037aabe41 9 #include <stdint.h>
yangtzuli 0:0d0037aabe41 10 #include "platform/mbed_thread.h"
yangtzuli 2:38825726cb1b 11 #include "TextLCD.h"
yangtzuli 0:0d0037aabe41 12
nishimura_taku_pet 27:90962c01bfeb 13 RawSerial pc(USBTX, USBRX);
yangtzuli 0:0d0037aabe41 14
yangtzuli 3:2ae6218973be 15 /* マクロ定義、列挙型定義 */
nishimura_taku_pet 27:90962c01bfeb 16 #define MIN_V 2.0 // 電圧の最小値
nishimura_taku_pet 27:90962c01bfeb 17 #define MAX_V 2.67 // 電圧の最大値
nishimura_taku_pet 27:90962c01bfeb 18 #define LOW 0 // モーターOFF
nishimura_taku_pet 27:90962c01bfeb 19 #define HIGH 1 // モーターON
nishimura_taku_pet 27:90962c01bfeb 20 #define NORMAL 0 // 普通
nishimura_taku_pet 27:90962c01bfeb 21 #define FAST 1 // 速い
tomotsugu 8:a47dbf4fa455 22 #define VERYFAST 2 // とても速い
yangtzuli 2:38825726cb1b 23
tomotsugu 8:a47dbf4fa455 24 /* 操作モード定義 */
yangtzuli 3:2ae6218973be 25 enum MODE{
tomotsugu 8:a47dbf4fa455 26 READY = -1, // -1:待ち
tomotsugu 8:a47dbf4fa455 27 ADVANCE = 1, // 1:前進
tomotsugu 8:a47dbf4fa455 28 RIGHT, // 2:右折
tomotsugu 8:a47dbf4fa455 29 LEFT, // 3:左折
tomotsugu 8:a47dbf4fa455 30 BACK, // 4:後退
tomotsugu 8:a47dbf4fa455 31 STOP, // 5:停止
tomotsugu 8:a47dbf4fa455 32 LINE_TRACE, // 6:ライントレース
tomotsugu 8:a47dbf4fa455 33 AVOIDANCE, // 7:障害物回避
tomotsugu 8:a47dbf4fa455 34 SPEED, // 8:スピード制御
yangtzuli 2:38825726cb1b 35 };
yangtzuli 2:38825726cb1b 36
yangtzuli 3:2ae6218973be 37 /* ピン配置 */
yangtzuli 3:2ae6218973be 38 ReceiverIR ir(p5); // リモコン操作
yangtzuli 3:2ae6218973be 39 DigitalOut trig(p6); // 超音波センサtrigger
faker_71 49:14b3dba5bd56 40 DigitalOut pred(p16);
faker_71 49:14b3dba5bd56 41 DigitalOut pblue(p19);
faker_71 49:14b3dba5bd56 42
yangtzuli 5:3fffb364744b 43 DigitalIn echo(p7); // 超音波センサecho
yangtzuli 3:2ae6218973be 44 DigitalIn ss1(p8); // ライントレースセンサ(左)
yangtzuli 3:2ae6218973be 45 DigitalIn ss2(p9); // ライントレースセンサ
yangtzuli 3:2ae6218973be 46 DigitalIn ss3(p10); // ライントレースセンサ
yangtzuli 3:2ae6218973be 47 DigitalIn ss4(p11); // ライントレースセンサ
yangtzuli 3:2ae6218973be 48 DigitalIn ss5(p12); // ライントレースセンサ(右)
nishimura_taku_pet 24:9481c8f56a49 49 RawSerial esp(p13, p14); // Wi-Fiモジュール(tx, rx)
yangtzuli 3:2ae6218973be 50 AnalogIn battery(p15); // 電池残量読み取り(Max 3.3V)
faker_71 51:622df065f7ff 51 PwmOut motorR2(p22); // 右モーター後退
faker_71 51:622df065f7ff 52 PwmOut motorR1(p21); // 右モーター前進
yangtzuli 3:2ae6218973be 53 PwmOut motorL2(p23); // 左モーター後退
yangtzuli 3:2ae6218973be 54 PwmOut motorL1(p24); // 左モーター前進
yangtzuli 3:2ae6218973be 55 PwmOut servo(p25); // サーボ
yangtzuli 3:2ae6218973be 56 I2C i2c_lcd(p28,p27); // LCD(tx, rx)
yangtzuli 2:38825726cb1b 57
yangtzuli 3:2ae6218973be 58 /* 変数宣言 */
yangtzuli 3:2ae6218973be 59 int mode; // 操作モード
yangtzuli 3:2ae6218973be 60 int run; // 走行状態
tomotsugu 8:a47dbf4fa455 61 int beforeMode; // 前回のモード
tomotsugu 8:a47dbf4fa455 62 int flag_sp = 0; // スピード変化フラグ
tomotsugu 8:a47dbf4fa455 63 Timer viewTimer; // スピ―ド変更時に3秒計測タイマー
nishimura_taku_pet 40:75e1ad7c27e4 64 float motorSpeed[9] = {0.4, 0.7, 0.8, 0.7, 0.8, 0.9, 0.8, 0.9, 1.0};
yangtzuli 3:2ae6218973be 65 // モーター速度設定(後半はライントレース用)
tomotsugu 8:a47dbf4fa455 66
tomotsugu 8:a47dbf4fa455 67 Mutex mutex; // ミューテックス
tomotsugu 8:a47dbf4fa455 68
tomotsugu 8:a47dbf4fa455 69 /* decodeIR用変数 */
yangtzuli 3:2ae6218973be 70 RemoteIR::Format format;
yangtzuli 3:2ae6218973be 71 uint8_t buf[32];
yangtzuli 3:2ae6218973be 72 uint32_t bitcount;
yangtzuli 3:2ae6218973be 73 uint32_t code;
yangtzuli 3:2ae6218973be 74
tomotsugu 8:a47dbf4fa455 75 /* bChange, lcdbacklight用変数 */
yangtzuli 3:2ae6218973be 76 TextLCD_I2C lcd(&i2c_lcd, (0x27 << 1), TextLCD::LCD16x2, TextLCD::HD44780);
tomotsugu 8:a47dbf4fa455 77 int b = 0; // バッテリー残量
tomotsugu 8:a47dbf4fa455 78 int flag_b = 0; // バックライト点滅フラグ
tomotsugu 8:a47dbf4fa455 79 int flag_t = 0; // バックライトタイマーフラグ
yangtzuli 3:2ae6218973be 80
tomotsugu 8:a47dbf4fa455 81 /* trace用変数 */
takuminomura 39:054c17d10c55 82 int sensArray[32] = {0,6,2,4,1,1,2,2, // ライントレースセンサパターン
nishimura_taku_pet 28:cb51cafca490 83 3,1,1,1,3,1,1,2,
nishimura_taku_pet 28:cb51cafca490 84 7,1,1,1,1,1,1,1,
nishimura_taku_pet 28:cb51cafca490 85 5,1,1,1,3,1,3,1};
yangtzuli 0:0d0037aabe41 86
tomotsugu 8:a47dbf4fa455 87 /* avoidance用変数 */
yangtzuli 3:2ae6218973be 88 Timer timer; // 距離計測用タイマ
yangtzuli 3:2ae6218973be 89 int DT; // 距離
yangtzuli 2:38825726cb1b 90 int SC; // 正面
yangtzuli 2:38825726cb1b 91 int SL; // 左
yangtzuli 2:38825726cb1b 92 int SR; // 右
yangtzuli 2:38825726cb1b 93 int SLD; // 左前
yangtzuli 2:38825726cb1b 94 int SRD; // 右前
tomotsugu 8:a47dbf4fa455 95 int flag_a = 0; // 障害物有無のフラグ
yangtzuli 2:38825726cb1b 96 const int limit = 20; // 障害物の距離のリミット(単位:cm)
yangtzuli 3:2ae6218973be 97 int far; // 最も遠い距離
yangtzuli 2:38825726cb1b 98 int houkou; // 進行方向(1:前 2:左 3:右)
nishimura_taku_pet 29:600e4b9b5c5b 99 int t1 = 0;
yangtzuli 2:38825726cb1b 100
nishimura_taku_pet 24:9481c8f56a49 101 /*WiFi用変数*/
nishimura_taku_pet 24:9481c8f56a49 102 Timer time1;
nishimura_taku_pet 24:9481c8f56a49 103 Timer time2;
nishimura_taku_pet 24:9481c8f56a49 104 int bufflen, DataRX, ount, getcount, replycount, servreq, timeout;
nishimura_taku_pet 24:9481c8f56a49 105 int bufl, ipdLen, linkID, weberror, webcounter,click_flag;
nishimura_taku_pet 24:9481c8f56a49 106 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
nishimura_taku_pet 24:9481c8f56a49 107 char webcount[8];
nishimura_taku_pet 24:9481c8f56a49 108 char type[16];
nishimura_taku_pet 24:9481c8f56a49 109 char channel[2];
nishimura_taku_pet 24:9481c8f56a49 110 char cmdbuff[32];
nishimura_taku_pet 24:9481c8f56a49 111 char replybuff[1024];
nishimura_taku_pet 24:9481c8f56a49 112 char webdata[1024]; // This may need to be bigger depending on WEB browser used
nishimura_taku_pet 27:90962c01bfeb 113 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
nishimura_taku_pet 24:9481c8f56a49 114 int port =80; // set server port
nishimura_taku_pet 24:9481c8f56a49 115 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
nishimura_taku_pet 24:9481c8f56a49 116 char ssid[32] = "mbed02"; // enter WiFi router ssid inside the quotes
nishimura_taku_pet 24:9481c8f56a49 117 char pwd [32] = "0123456789a"; // enter WiFi router password inside the quotes
yangtzuli 3:2ae6218973be 118
yangtzuli 3:2ae6218973be 119
yangtzuli 3:2ae6218973be 120 /* プロトタイプ宣言 */
nishimura_taku_pet 33:a6f1090e0174 121 void decodeIR(/*void const *argument*/);
nishimura_taku_pet 33:a6f1090e0174 122 void motor(/*void const *argument*/);
yangtzuli 3:2ae6218973be 123 void changeSpeed();
nishimura_taku_pet 16:ffc732a3cf92 124 void avoidance(/*void const *argument*/);
nishimura_taku_pet 16:ffc732a3cf92 125 void trace(/*void const *argument*/);
nishimura_taku_pet 40:75e1ad7c27e4 126 void watchsurrounding3();
nishimura_taku_pet 40:75e1ad7c27e4 127 void watchsurrounding5();
yangtzuli 2:38825726cb1b 128 int watch();
faker_71 48:7be823ac7eb8 129 char battery_ch[8];
yangtzuli 5:3fffb364744b 130 void bChange();
yangtzuli 3:2ae6218973be 131 void display();
yangtzuli 3:2ae6218973be 132 void lcdBacklight(void const *argument);
nishimura_taku_pet 45:76e8c07d76ef 133 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
nishimura_taku_pet 43:243c1455f88a 134 void wifi(/*void const *argument*/);
nishimura_taku_pet 33:a6f1090e0174 135 Thread *deco_thread; // decodeIRをスレッド化 :+3
nishimura_taku_pet 43:243c1455f88a 136 Thread *wifi_thread;
nishimura_taku_pet 45:76e8c07d76ef 137 //wifi_thread(wifi,NULL,osPriorityHigh); // wifiをスレッド化
nishimura_taku_pet 33:a6f1090e0174 138 Thread *motor_thread; // motorをスレッド化 :+2
nishimura_taku_pet 16:ffc732a3cf92 139 //Thread avoi_thread(avoidance, NULL, osPriorityHigh); // avoidanceをスレッド化:+2
nishimura_taku_pet 16:ffc732a3cf92 140 //Thread trace_thread(trace, NULL, osPriorityHigh); // traceをスレッド化 :+2
nishimura_taku_pet 33:a6f1090e0174 141 RtosTimer bTimer(lcdBacklight, osTimerPeriodic); // lcdBacklightをタイマー割り込みで設定
nishimura_taku_pet 43:243c1455f88a 142 //Ticker bTimer;
nishimura_taku_pet 16:ffc732a3cf92 143 Thread *avoi_thread;
nishimura_taku_pet 16:ffc732a3cf92 144 Thread *trace_thread;
yangtzuli 2:38825726cb1b 145
nishimura_taku_pet 24:9481c8f56a49 146 DigitalOut led1(LED1);
nishimura_taku_pet 24:9481c8f56a49 147 DigitalOut led2(LED2);
nishimura_taku_pet 24:9481c8f56a49 148 DigitalOut led3(LED3);
nishimura_taku_pet 24:9481c8f56a49 149 DigitalOut led4(LED4);
yangtzuli 3:2ae6218973be 150
nishimura_taku_pet 33:a6f1090e0174 151 void setup(){
nishimura_taku_pet 33:a6f1090e0174 152 deco_thread = new Thread(decodeIR);
nishimura_taku_pet 33:a6f1090e0174 153 deco_thread -> set_priority(osPriorityRealtime);
nishimura_taku_pet 33:a6f1090e0174 154 motor_thread = new Thread(motor);
nishimura_taku_pet 33:a6f1090e0174 155 motor_thread -> set_priority(osPriorityHigh);
nishimura_taku_pet 43:243c1455f88a 156 wifi_thread -> set_priority(osPriorityRealtime);
nishimura_taku_pet 33:a6f1090e0174 157 display();
nishimura_taku_pet 33:a6f1090e0174 158 }
nishimura_taku_pet 33:a6f1090e0174 159
tomotsugu 8:a47dbf4fa455 160 /* リモコン受信スレッド */
nishimura_taku_pet 33:a6f1090e0174 161 void decodeIR(/*void const *argument*/){
yangtzuli 5:3fffb364744b 162 while(1){
yangtzuli 0:0d0037aabe41 163 // 受信待ち
tomotsugu 8:a47dbf4fa455 164 if (ir.getState() == ReceiverIR::Received){ // コード受信
yangtzuli 3:2ae6218973be 165 bitcount = ir.getData(&format, buf, sizeof(buf) * 8);
tomotsugu 8:a47dbf4fa455 166 if(bitcount > 1){ // 受信成功
yangtzuli 1:5bb497a38344 167 code=0;
tomotsugu 15:5eef1955f6c2 168 for(int j = 0; j < 4; j++){
yangtzuli 1:5bb497a38344 169 code+=(buf[j]<<(8*(3-j)));
yangtzuli 1:5bb497a38344 170 }
tomotsugu 8:a47dbf4fa455 171 if(mode != SPEED){ // スピードモード以外なら
tomotsugu 8:a47dbf4fa455 172 beforeMode=mode; // 前回のモードに現在のモードを設定
yangtzuli 3:2ae6218973be 173 }
yangtzuli 0:0d0037aabe41 174 switch(code){
tomotsugu 8:a47dbf4fa455 175 case 0x40bf27d8: // クイック
yangtzuli 17:f7259ab2fe86 176 //pc.printf("mode = SPEED\r\n");
tomotsugu 8:a47dbf4fa455 177 mode = SPEED; // スピードモード
tomotsugu 8:a47dbf4fa455 178 changeSpeed(); // 速度変更
tomotsugu 8:a47dbf4fa455 179 display(); // ディスプレイ表示
tomotsugu 8:a47dbf4fa455 180 mode = beforeMode; // 現在のモードに前回のモードを設定
yangtzuli 1:5bb497a38344 181 break;
tomotsugu 8:a47dbf4fa455 182 case 0x40be34cb: // レグザリンク
yangtzuli 17:f7259ab2fe86 183 //pc.printf("mode = LINE_TRACE\r\n");
nishimura_taku_pet 16:ffc732a3cf92 184 if(trace_thread->get_state() == Thread::Deleted){
nishimura_taku_pet 16:ffc732a3cf92 185 delete trace_thread;
nishimura_taku_pet 16:ffc732a3cf92 186 trace_thread = new Thread(trace);
nishimura_taku_pet 16:ffc732a3cf92 187 trace_thread -> set_priority(osPriorityHigh);
nishimura_taku_pet 16:ffc732a3cf92 188 }
tomotsugu 8:a47dbf4fa455 189 mode=LINE_TRACE; // ライントレースモード
tomotsugu 8:a47dbf4fa455 190 display(); // ディスプレイ表示
yangtzuli 1:5bb497a38344 191 break;
tomotsugu 8:a47dbf4fa455 192 case 0x40bf6e91: // 番組表
yangtzuli 17:f7259ab2fe86 193 //pc.printf("mode = AVOIDANCE\r\n");
nishimura_taku_pet 16:ffc732a3cf92 194 if(avoi_thread->get_state() == Thread::Deleted){
nishimura_taku_pet 16:ffc732a3cf92 195 delete avoi_thread;
nishimura_taku_pet 16:ffc732a3cf92 196 avoi_thread = new Thread(avoidance);
nishimura_taku_pet 16:ffc732a3cf92 197 avoi_thread -> set_priority(osPriorityHigh);
nishimura_taku_pet 16:ffc732a3cf92 198 }
tomotsugu 18:6cca64c7dbc3 199 flag_a = 0;
tomotsugu 8:a47dbf4fa455 200 mode=AVOIDANCE; // 障害物回避モード
tomotsugu 13:1a7667d0aa78 201 run = ADVANCE; // 前進
tomotsugu 8:a47dbf4fa455 202 display(); // ディスプレイ表示
yangtzuli 1:5bb497a38344 203 break;
tomotsugu 8:a47dbf4fa455 204 case 0x40bf3ec1: // ↑
yangtzuli 17:f7259ab2fe86 205 //pc.printf("mode = ADVANCE\r\n");
tomotsugu 8:a47dbf4fa455 206 mode = ADVANCE; // 前進モード
tomotsugu 8:a47dbf4fa455 207 run = ADVANCE; // 前進
tomotsugu 8:a47dbf4fa455 208 display(); // ディスプレイ表示
yangtzuli 1:5bb497a38344 209 break;
tomotsugu 8:a47dbf4fa455 210 case 0x40bf3fc0: // ↓
yangtzuli 17:f7259ab2fe86 211 //pc.printf("mode = BACK\r\n");
tomotsugu 8:a47dbf4fa455 212 mode = BACK; // 後退モード
tomotsugu 8:a47dbf4fa455 213 run = BACK; // 後退
tomotsugu 8:a47dbf4fa455 214 display(); // ディスプレイ表示
yangtzuli 0:0d0037aabe41 215 break;
tomotsugu 8:a47dbf4fa455 216 case 0x40bf5fa0: // ←
yangtzuli 17:f7259ab2fe86 217 //pc.printf("mode = LEFT\r\n");
tomotsugu 8:a47dbf4fa455 218 mode = LEFT; // 左折モード
tomotsugu 8:a47dbf4fa455 219 run = LEFT; // 左折
tomotsugu 8:a47dbf4fa455 220 display(); // ディスプレイ表示
yangtzuli 1:5bb497a38344 221 break;
tomotsugu 8:a47dbf4fa455 222 case 0x40bf5ba4: // →
yangtzuli 17:f7259ab2fe86 223 //pc.printf("mode = RIGHT\r\n");
tomotsugu 8:a47dbf4fa455 224 mode = RIGHT; // 右折モード
tomotsugu 8:a47dbf4fa455 225 run = RIGHT; // 右折
tomotsugu 8:a47dbf4fa455 226 display(); // ディスプレイ表示
yangtzuli 1:5bb497a38344 227 break;
tomotsugu 8:a47dbf4fa455 228 case 0x40bf3dc2: // 決定
yangtzuli 17:f7259ab2fe86 229 //pc.printf("mode = STOP\r\n");
tomotsugu 8:a47dbf4fa455 230 mode = STOP; // 停止モード
tomotsugu 8:a47dbf4fa455 231 run = STOP; // 停止
tomotsugu 8:a47dbf4fa455 232 display(); // ディスプレイ表示
yangtzuli 1:5bb497a38344 233 break;
yangtzuli 0:0d0037aabe41 234 default:
yangtzuli 0:0d0037aabe41 235 ;
yangtzuli 0:0d0037aabe41 236 }
nishimura_taku_pet 16:ffc732a3cf92 237 if(mode != LINE_TRACE && trace_thread->get_state() != Thread::Deleted){
nishimura_taku_pet 16:ffc732a3cf92 238 trace_thread->terminate();
nishimura_taku_pet 16:ffc732a3cf92 239 }
nishimura_taku_pet 16:ffc732a3cf92 240 if(mode != AVOIDANCE && avoi_thread->get_state() != Thread::Deleted){
nishimura_taku_pet 16:ffc732a3cf92 241 avoi_thread->terminate();
nishimura_taku_pet 28:cb51cafca490 242 servo.pulsewidth_us(1450); // サーボを中央位置に戻す
nishimura_taku_pet 16:ffc732a3cf92 243 }
yangtzuli 0:0d0037aabe41 244 }
yangtzuli 0:0d0037aabe41 245 }
tomotsugu 8:a47dbf4fa455 246 if(viewTimer.read_ms()>=3000){ // スピードモードのまま3秒経過
tomotsugu 8:a47dbf4fa455 247 viewTimer.stop(); // タイマーストップ
tomotsugu 8:a47dbf4fa455 248 viewTimer.reset(); // タイマーリセット
tomotsugu 8:a47dbf4fa455 249 display(); // ディスプレイ表示
yangtzuli 4:3f80c0180e2f 250 }
tomotsugu 18:6cca64c7dbc3 251 ThisThread::sleep_for(90); // 90ms待つ
yangtzuli 4:3f80c0180e2f 252 }
yangtzuli 2:38825726cb1b 253 }
tomotsugu 8:a47dbf4fa455 254
tomotsugu 8:a47dbf4fa455 255 /* モーター制御スレッド */
nishimura_taku_pet 33:a6f1090e0174 256 void motor(/*void const *argument*/){
tomotsugu 8:a47dbf4fa455 257 while(1){
tomotsugu 8:a47dbf4fa455 258 /* 走行状態の場合分け */
yangtzuli 3:2ae6218973be 259 switch(run){
tomotsugu 8:a47dbf4fa455 260 /* 前進 */
yangtzuli 3:2ae6218973be 261 case ADVANCE:
tomotsugu 8:a47dbf4fa455 262 motorR1 = motorSpeed[flag_sp]; // 右前進モーターON
tomotsugu 8:a47dbf4fa455 263 motorR2 = LOW; // 右後退モーターOFF
tomotsugu 8:a47dbf4fa455 264 motorL1 = motorSpeed[flag_sp]; // 左前進モーターON
tomotsugu 8:a47dbf4fa455 265 motorL2 = LOW; // 左後退モーターOFF
yangtzuli 3:2ae6218973be 266 break;
tomotsugu 8:a47dbf4fa455 267 /* 右折 */
yangtzuli 3:2ae6218973be 268 case RIGHT:
tomotsugu 8:a47dbf4fa455 269 motorR1 = LOW; // 右前進モーターOFF
tomotsugu 8:a47dbf4fa455 270 motorR2 = motorSpeed[flag_sp]; // 右後退モーターON
tomotsugu 8:a47dbf4fa455 271 motorL1 = motorSpeed[flag_sp]; // 左前進モーターON
tomotsugu 8:a47dbf4fa455 272 motorL2 = LOW; // 左後退モーターOFF
yangtzuli 3:2ae6218973be 273 break;
tomotsugu 8:a47dbf4fa455 274 /* 左折 */
yangtzuli 3:2ae6218973be 275 case LEFT:
tomotsugu 8:a47dbf4fa455 276 motorR1 = motorSpeed[flag_sp]; // 右前進モーターON
tomotsugu 8:a47dbf4fa455 277 motorR2 = LOW; // 右後退モーターOFF
tomotsugu 8:a47dbf4fa455 278 motorL1 = LOW; // 左前進モーターOFF
tomotsugu 8:a47dbf4fa455 279 motorL2 = motorSpeed[flag_sp]; // 左後退モーターON
yangtzuli 3:2ae6218973be 280 break;
tomotsugu 8:a47dbf4fa455 281 /* 後退 */
yangtzuli 3:2ae6218973be 282 case BACK:
tomotsugu 8:a47dbf4fa455 283 motorR1 = LOW; // 右前進モーターOFF
tomotsugu 8:a47dbf4fa455 284 motorR2 = motorSpeed[flag_sp]; // 右後退モーターON
tomotsugu 8:a47dbf4fa455 285 motorL1 = LOW; // 左前進モーターOFF
tomotsugu 8:a47dbf4fa455 286 motorL2 = motorSpeed[flag_sp]; // 左後退モーターON
yangtzuli 3:2ae6218973be 287 break;
tomotsugu 8:a47dbf4fa455 288 /* 停止 */
yangtzuli 3:2ae6218973be 289 case STOP:
tomotsugu 8:a47dbf4fa455 290 motorR1 = LOW; // 右前進モーターOFF
tomotsugu 8:a47dbf4fa455 291 motorR2 = LOW; // 右後退モーターOFF
tomotsugu 8:a47dbf4fa455 292 motorL1 = LOW; // 左前進モーターOFF
tomotsugu 8:a47dbf4fa455 293 motorL2 = LOW; // 左後退モーターOFF
yangtzuli 3:2ae6218973be 294 break;
yangtzuli 3:2ae6218973be 295 }
tomotsugu 8:a47dbf4fa455 296 if(flag_sp > VERYFAST){ // スピード変更フラグが2より大きいなら
nishimura_taku_pet 47:8a5a4275480a 297 flag_sp %= 3; // スピード変更フラグ調整
yangtzuli 6:800a745c7f2e 298 }
tomotsugu 8:a47dbf4fa455 299 ThisThread::sleep_for(30); // 30ms待つ
yangtzuli 2:38825726cb1b 300 }
yangtzuli 2:38825726cb1b 301 }
tomotsugu 8:a47dbf4fa455 302
tomotsugu 8:a47dbf4fa455 303 /* スピード変更関数 */
yangtzuli 3:2ae6218973be 304 void changeSpeed(){
tomotsugu 8:a47dbf4fa455 305 if(flag_sp%3 == 2){ // スピード変更フラグを3で割った余りが2なら
tomotsugu 8:a47dbf4fa455 306 flag_sp -= 2; // スピード変更フラグを-2
yangtzuli 3:2ae6218973be 307
tomotsugu 8:a47dbf4fa455 308 }else{ // それ以外
tomotsugu 8:a47dbf4fa455 309 flag_sp = flag_sp + 1; // スピード変更フラグを+1
yangtzuli 3:2ae6218973be 310 }
yangtzuli 3:2ae6218973be 311 }
tomotsugu 8:a47dbf4fa455 312
tomotsugu 8:a47dbf4fa455 313 /* ライントレーススレッド */
tomotsugu 20:02bb875a9b13 314 void trace(){
tomotsugu 8:a47dbf4fa455 315 while(1){
tomotsugu 8:a47dbf4fa455 316 /* 各センサー値読み取り */
yangtzuli 3:2ae6218973be 317 int sensor1 = ss1;
yangtzuli 3:2ae6218973be 318 int sensor2 = ss2;
yangtzuli 3:2ae6218973be 319 int sensor3 = ss3;
yangtzuli 3:2ae6218973be 320 int sensor4 = ss4;
yangtzuli 3:2ae6218973be 321 int sensor5 = ss5;
yangtzuli 6:800a745c7f2e 322 pc.printf("%d %d %d %d %d \r\n",sensor1,sensor2,sensor3,sensor4,sensor5);
yangtzuli 3:2ae6218973be 323 int sensD = 0;
tomotsugu 8:a47dbf4fa455 324
tomotsugu 8:a47dbf4fa455 325 /* センサー値の決定 */
yangtzuli 3:2ae6218973be 326 if(sensor1 > 0) sensD |= 0x10;
yangtzuli 3:2ae6218973be 327 if(sensor2 > 0) sensD |= 0x08;
yangtzuli 3:2ae6218973be 328 if(sensor3 > 0) sensD |= 0x04;
yangtzuli 3:2ae6218973be 329 if(sensor4 > 0) sensD |= 0x02;
yangtzuli 3:2ae6218973be 330 if(sensor5 > 0) sensD |= 0x01;
tomotsugu 8:a47dbf4fa455 331
tomotsugu 8:a47dbf4fa455 332 /* センサー値によって場合分け */
nishimura_taku_pet 34:040474bf6fe0 333 switch(sensArray[sensD]){
yangtzuli 3:2ae6218973be 334 case 1:
tomotsugu 18:6cca64c7dbc3 335 run = ADVANCE; // 低速で前進
yangtzuli 3:2ae6218973be 336 break;
yangtzuli 3:2ae6218973be 337 case 2:
takuminomura 39:054c17d10c55 338 // flag_sp = flag_sp % 3 + 6;
tomotsugu 18:6cca64c7dbc3 339 run = RIGHT; // 低速で右折
yangtzuli 3:2ae6218973be 340 break;
yangtzuli 3:2ae6218973be 341 case 3:
takuminomura 39:054c17d10c55 342 // flag_sp = flag_sp % 3 + 6;
tomotsugu 18:6cca64c7dbc3 343 run = LEFT; // 低速で左折
yangtzuli 3:2ae6218973be 344 break;
yangtzuli 3:2ae6218973be 345 case 4:
takuminomura 39:054c17d10c55 346 flag_sp = flag_sp % 3 + 3;
tomotsugu 18:6cca64c7dbc3 347 run = RIGHT; // 中速で右折
yangtzuli 3:2ae6218973be 348 break;
yangtzuli 3:2ae6218973be 349 case 5:
takuminomura 39:054c17d10c55 350 flag_sp = flag_sp % 3 + 3;
tomotsugu 18:6cca64c7dbc3 351 run = LEFT; // 中速で左折
yangtzuli 6:800a745c7f2e 352 break;
yangtzuli 6:800a745c7f2e 353 case 6:
tomotsugu 18:6cca64c7dbc3 354 flag_sp = flag_sp % 3 + 6;
tomotsugu 18:6cca64c7dbc3 355 run = RIGHT; // 高速で右折
yangtzuli 6:800a745c7f2e 356 break;
yangtzuli 6:800a745c7f2e 357 case 7:
tomotsugu 18:6cca64c7dbc3 358 flag_sp = flag_sp % 3 + 6;
tomotsugu 18:6cca64c7dbc3 359 run = LEFT; // 高速で左折
yangtzuli 3:2ae6218973be 360 break;
takuminomura 39:054c17d10c55 361 default:
takuminomura 39:054c17d10c55 362 break; // 前回動作を継続
yangtzuli 3:2ae6218973be 363 }
tomotsugu 8:a47dbf4fa455 364 ThisThread::sleep_for(30); // 30ms待つ
tomotsugu 18:6cca64c7dbc3 365 }
yangtzuli 3:2ae6218973be 366 }
yangtzuli 3:2ae6218973be 367
tomotsugu 8:a47dbf4fa455 368 /* 障害物回避走行スレッド */
tomotsugu 20:02bb875a9b13 369 void avoidance(){
nishimura_taku_pet 46:c6deb699160b 370 int i;
yangtzuli 3:2ae6218973be 371 while(1){
nishimura_taku_pet 40:75e1ad7c27e4 372 watchsurrounding3();
tomotsugu 18:6cca64c7dbc3 373 pc.printf("%d %d %d %d %d \r\n",SL,SLD,SC,SRD,SR);
tomotsugu 18:6cca64c7dbc3 374 if(flag_a == 0){ // 障害物がない場合
tomotsugu 18:6cca64c7dbc3 375 run = ADVANCE; // 前進
nishimura_taku_pet 28:cb51cafca490 376 }else{ // 障害物がある場合
tomotsugu 18:6cca64c7dbc3 377 i = 0;
tomotsugu 18:6cca64c7dbc3 378 if(SC < 15){ // 正面15cm以内に障害物が現れた場合
nishimura_taku_pet 41:3c58a4be1199 379 servo.pulsewidth_us(1450); // サーボを中央位置に戻す
nishimura_taku_pet 41:3c58a4be1199 380 ThisThread::sleep_for(100); // 100ms待つ
tomotsugu 18:6cca64c7dbc3 381 run = BACK; // 後退
nishimura_taku_pet 40:75e1ad7c27e4 382 int cnt_kyori=0;
nishimura_taku_pet 40:75e1ad7c27e4 383 int kyori = watch();
nishimura_taku_pet 40:75e1ad7c27e4 384 while(kyori < limit){ // 正面20cm以内に障害物がある間
nishimura_taku_pet 40:75e1ad7c27e4 385 if(kyori==-1){
nishimura_taku_pet 40:75e1ad7c27e4 386 cnt_kyori++;
nishimura_taku_pet 40:75e1ad7c27e4 387 if(cnt_kyori>15){
nishimura_taku_pet 40:75e1ad7c27e4 388 cnt_kyori=0;
nishimura_taku_pet 40:75e1ad7c27e4 389 break;
nishimura_taku_pet 40:75e1ad7c27e4 390 }
nishimura_taku_pet 40:75e1ad7c27e4 391 }
nishimura_taku_pet 40:75e1ad7c27e4 392 kyori = watch();
tomotsugu 18:6cca64c7dbc3 393 }
nishimura_taku_pet 40:75e1ad7c27e4 394 /*while(i < 30){ // 正面20cm以内に障害物がある間
nishimura_taku_pet 40:75e1ad7c27e4 395 if(watch() < limit){
nishimura_taku_pet 40:75e1ad7c27e4 396 break;
nishimura_taku_pet 40:75e1ad7c27e4 397 }
nishimura_taku_pet 40:75e1ad7c27e4 398 i++;
nishimura_taku_pet 40:75e1ad7c27e4 399 }
nishimura_taku_pet 40:75e1ad7c27e4 400 i = 0;*/
nishimura_taku_pet 40:75e1ad7c27e4 401 run = STOP; // 停止
yangtzuli 3:2ae6218973be 402 }
nishimura_taku_pet 40:75e1ad7c27e4 403 watchsurrounding5();
nishimura_taku_pet 28:cb51cafca490 404 if(SC < limit && SLD < limit && SL < limit && SRD < limit && SR < limit){ // 全方向に障害物がある場合
tomotsugu 18:6cca64c7dbc3 405 run = LEFT; // 左折
nishimura_taku_pet 28:cb51cafca490 406 while(i < 1){ // 進行方向確認
tomotsugu 18:6cca64c7dbc3 407 if(watch() > limit){
tomotsugu 18:6cca64c7dbc3 408 i++;
nishimura_taku_pet 28:cb51cafca490 409 }else{
nishimura_taku_pet 28:cb51cafca490 410 i = 0;
yangtzuli 2:38825726cb1b 411 }
yangtzuli 2:38825726cb1b 412 }
nishimura_taku_pet 28:cb51cafca490 413 run = STOP; // 停止
nishimura_taku_pet 28:cb51cafca490 414 }else { // 全方向以外
nishimura_taku_pet 28:cb51cafca490 415 far = SC; // 正面を最も遠い距離に設定
nishimura_taku_pet 28:cb51cafca490 416 houkou = 1; // 進行方向を前に設定
nishimura_taku_pet 28:cb51cafca490 417 if(far < SLD || far < SL){ // 左または左前がより遠い場合
nishimura_taku_pet 28:cb51cafca490 418 if(SL < SLD){ // 左前が左より遠い場合
nishimura_taku_pet 28:cb51cafca490 419 far = SLD; // 左前を最も遠い距離に設定
nishimura_taku_pet 28:cb51cafca490 420 }else{ // 左が左前より遠い場合
nishimura_taku_pet 28:cb51cafca490 421 far = SL; // 左を最も遠い距離に設定
nishimura_taku_pet 28:cb51cafca490 422 }
nishimura_taku_pet 28:cb51cafca490 423 houkou = 2; // 進行方向を左に設定
nishimura_taku_pet 28:cb51cafca490 424 }
nishimura_taku_pet 28:cb51cafca490 425 if(far < SRD || far < SR){ // 右または右前がより遠い場合
nishimura_taku_pet 28:cb51cafca490 426 if(SR < SRD){ // 右前が右より遠い場合
nishimura_taku_pet 28:cb51cafca490 427 far = SRD; // 右前を最も遠い距離に設定
nishimura_taku_pet 28:cb51cafca490 428 }else{ // 右が右前よりも遠い場合
nishimura_taku_pet 28:cb51cafca490 429 far = SR; // 右を最も遠い距離に設定
nishimura_taku_pet 28:cb51cafca490 430 }
nishimura_taku_pet 28:cb51cafca490 431 houkou = 3; // 進行方向を右に設定
nishimura_taku_pet 28:cb51cafca490 432 }
nishimura_taku_pet 28:cb51cafca490 433 switch(houkou){ // 進行方向の場合分け
nishimura_taku_pet 28:cb51cafca490 434 case 1: // 前の場合
nishimura_taku_pet 28:cb51cafca490 435 run = ADVANCE; // 前進
nishimura_taku_pet 40:75e1ad7c27e4 436 ThisThread::sleep_for(500); // 0.5秒待つ
nishimura_taku_pet 28:cb51cafca490 437 break;
nishimura_taku_pet 40:75e1ad7c27e4 438 case 2: // 左の場合
nishimura_taku_pet 28:cb51cafca490 439 run = LEFT; // 左折
nishimura_taku_pet 40:75e1ad7c27e4 440 //int kyori = watch();
nishimura_taku_pet 40:75e1ad7c27e4 441 //int kyori_f=0;
nishimura_taku_pet 40:75e1ad7c27e4 442 while(i < 20){ // 進行方向確認
nishimura_taku_pet 40:75e1ad7c27e4 443 /*if(kyori > (far - 2) || kyori_f == 2){ // 正面の計測距離と最も遠い距離が一致したら(誤差-2cm)
nishimura_taku_pet 40:75e1ad7c27e4 444 break; // ループ+
nishimura_taku_pet 40:75e1ad7c27e4 445 }else if(kyori==-1){
nishimura_taku_pet 40:75e1ad7c27e4 446 kyori_f++;
nishimura_taku_pet 40:75e1ad7c27e4 447 }else{
nishimura_taku_pet 40:75e1ad7c27e4 448 kyori_f = 0;
nishimura_taku_pet 40:75e1ad7c27e4 449 i++;
nishimura_taku_pet 40:75e1ad7c27e4 450 }*/
nishimura_taku_pet 28:cb51cafca490 451 if(watch() > (far - 2)){ // 正面の計測距離と最も遠い距離が一致したら(誤差-2cm)
nishimura_taku_pet 40:75e1ad7c27e4 452 break; // ループ+
nishimura_taku_pet 28:cb51cafca490 453 }else{
nishimura_taku_pet 40:75e1ad7c27e4 454 i++;
nishimura_taku_pet 28:cb51cafca490 455 }
nishimura_taku_pet 28:cb51cafca490 456 }
nishimura_taku_pet 28:cb51cafca490 457 run = STOP; // 停止
nishimura_taku_pet 28:cb51cafca490 458 break;
nishimura_taku_pet 40:75e1ad7c27e4 459 case 3: // 右の場合
nishimura_taku_pet 28:cb51cafca490 460 run = RIGHT; // 右折
nishimura_taku_pet 45:76e8c07d76ef 461 //int kyori = watch();
nishimura_taku_pet 45:76e8c07d76ef 462 //int kyori_f=0;
nishimura_taku_pet 40:75e1ad7c27e4 463 while(i < 20){ // 進行方向確認
nishimura_taku_pet 40:75e1ad7c27e4 464 /*if(kyori > (far - 2) || kyori_f == 2){ // 正面の計測距離と最も遠い距離が一致したら(誤差-2cm)
nishimura_taku_pet 40:75e1ad7c27e4 465 break; // ループ+
nishimura_taku_pet 40:75e1ad7c27e4 466 }else if(kyori==-1){
nishimura_taku_pet 40:75e1ad7c27e4 467 kyori_f++;
nishimura_taku_pet 40:75e1ad7c27e4 468 }else{
nishimura_taku_pet 40:75e1ad7c27e4 469 kyori_f = 0;
nishimura_taku_pet 40:75e1ad7c27e4 470 i++;
nishimura_taku_pet 40:75e1ad7c27e4 471 }*/
nishimura_taku_pet 28:cb51cafca490 472 if(watch() > (far - 2)){ // 正面の計測距離と最も遠い距離が一致したら(誤差-2cm)
nishimura_taku_pet 40:75e1ad7c27e4 473 break; // ループ+
nishimura_taku_pet 28:cb51cafca490 474 }else{
nishimura_taku_pet 40:75e1ad7c27e4 475 i++;
nishimura_taku_pet 28:cb51cafca490 476 }
nishimura_taku_pet 28:cb51cafca490 477 }
nishimura_taku_pet 28:cb51cafca490 478 run = STOP; // 停止
nishimura_taku_pet 28:cb51cafca490 479 break;
nishimura_taku_pet 28:cb51cafca490 480 }
yangtzuli 5:3fffb364744b 481 }
nishimura_taku_pet 28:cb51cafca490 482 }
nishimura_taku_pet 28:cb51cafca490 483 flag_a = 0; // 障害物有無フラグを0にセット
nishimura_taku_pet 40:75e1ad7c27e4 484 if(SLD < 29){ // 正面15cm以内に障害物が現れた場合
nishimura_taku_pet 40:75e1ad7c27e4 485 run = RIGHT; // 右折
nishimura_taku_pet 41:3c58a4be1199 486 ThisThread::sleep_for(200); // 100ms待つ
nishimura_taku_pet 40:75e1ad7c27e4 487 run = STOP; // 停止
nishimura_taku_pet 40:75e1ad7c27e4 488 }else if(SRD < 29){
nishimura_taku_pet 40:75e1ad7c27e4 489 run = LEFT; // 左折
nishimura_taku_pet 41:3c58a4be1199 490 ThisThread::sleep_for(200); // 100ms待つ
nishimura_taku_pet 40:75e1ad7c27e4 491 run = STOP; // 停止
nishimura_taku_pet 40:75e1ad7c27e4 492 }
yangtzuli 2:38825726cb1b 493 }
yangtzuli 2:38825726cb1b 494 }
tomotsugu 8:a47dbf4fa455 495
tomotsugu 8:a47dbf4fa455 496 /* 距離計測関数 */
yangtzuli 2:38825726cb1b 497 int watch(){
tomotsugu 21:68d38e8f64b5 498 do{
tomotsugu 21:68d38e8f64b5 499 trig = 0;
tomotsugu 21:68d38e8f64b5 500 ThisThread::sleep_for(5); // 5ms待つ
tomotsugu 21:68d38e8f64b5 501 trig = 1;
tomotsugu 21:68d38e8f64b5 502 ThisThread::sleep_for(15); // 15ms待つ
tomotsugu 21:68d38e8f64b5 503 trig = 0;
tomotsugu 21:68d38e8f64b5 504 timer.start();
yangtzuli 17:f7259ab2fe86 505 t1=timer.read_ms();
nishimura_taku_pet 29:600e4b9b5c5b 506 while(echo.read() == 0 && t1<10){
tomotsugu 21:68d38e8f64b5 507 t1=timer.read_ms();
tomotsugu 21:68d38e8f64b5 508 led1 = 1;
tomotsugu 21:68d38e8f64b5 509 }
tomotsugu 21:68d38e8f64b5 510 timer.stop();
tomotsugu 21:68d38e8f64b5 511 timer.reset();
nishimura_taku_pet 28:cb51cafca490 512 /*if((t1-t2) >= 10){
nishimura_taku_pet 28:cb51cafca490 513 run = STOP;*/
nishimura_taku_pet 29:600e4b9b5c5b 514 }while(t1 >= 10);
tomotsugu 21:68d38e8f64b5 515 timer.start(); // 距離計測タイマースタート
tomotsugu 21:68d38e8f64b5 516 while(echo.read() == 1){
yangtzuli 5:3fffb364744b 517 }
tomotsugu 21:68d38e8f64b5 518 timer.stop(); // 距離計測タイマーストップ
tomotsugu 21:68d38e8f64b5 519 DT = (int)(timer.read_us()*0.01657); // 距離計算
nishimura_taku_pet 28:cb51cafca490 520 if(DT > 1000){
nishimura_taku_pet 28:cb51cafca490 521 DT = -1;
nishimura_taku_pet 28:cb51cafca490 522 }else if(DT > 150){ // 検知範囲外なら100cmに設定
tomotsugu 22:c6e2a3b9aa14 523 DT = 150;
yangtzuli 2:38825726cb1b 524 }
tomotsugu 21:68d38e8f64b5 525 timer.reset(); // 距離計測タイマーリセット
tomotsugu 8:a47dbf4fa455 526 led1 = 0;
yangtzuli 2:38825726cb1b 527 return DT;
yangtzuli 0:0d0037aabe41 528 }
yangtzuli 0:0d0037aabe41 529
tomotsugu 8:a47dbf4fa455 530 /* 障害物検知関数 */
nishimura_taku_pet 40:75e1ad7c27e4 531 void watchsurrounding3(){
nishimura_taku_pet 28:cb51cafca490 532 //servo.pulsewidth_us(1450); // サーボを中央位置に戻す
nishimura_taku_pet 28:cb51cafca490 533 //ThisThread::sleep_for(200); // 100ms待つ
yangtzuli 2:38825726cb1b 534 SC = watch();
nishimura_taku_pet 28:cb51cafca490 535 if(SC < limit){ // 正面20cm以内に障害物がある場合
nishimura_taku_pet 28:cb51cafca490 536 if(SC!=-1){
nishimura_taku_pet 28:cb51cafca490 537 run = STOP; // 停止
nishimura_taku_pet 40:75e1ad7c27e4 538 flag_a = 1;
nishimura_taku_pet 40:75e1ad7c27e4 539 return;
nishimura_taku_pet 28:cb51cafca490 540 }
yangtzuli 2:38825726cb1b 541 }
yangtzuli 2:38825726cb1b 542 servo.pulsewidth_us(1925); // サーボを左に40度回転
nishimura_taku_pet 28:cb51cafca490 543 ThisThread::sleep_for(100); // 250ms待つ
yangtzuli 2:38825726cb1b 544 SLD = watch();
yangtzuli 2:38825726cb1b 545 if(SLD < limit){ // 左前20cm以内に障害物がある場合
tomotsugu 20:02bb875a9b13 546 run = STOP; // 停止
nishimura_taku_pet 28:cb51cafca490 547 flag_a = 1;
nishimura_taku_pet 40:75e1ad7c27e4 548 return;
yangtzuli 2:38825726cb1b 549 }
tomotsugu 19:c6f9f010bd9e 550 servo.pulsewidth_us(1450);
nishimura_taku_pet 42:56022456ef23 551 ThisThread::sleep_for(150);
tomotsugu 19:c6f9f010bd9e 552 SC = watch();
tomotsugu 19:c6f9f010bd9e 553 if(SC < limit){
nishimura_taku_pet 28:cb51cafca490 554 if(SC!=-1){
nishimura_taku_pet 28:cb51cafca490 555 run = STOP; // 停止
nishimura_taku_pet 40:75e1ad7c27e4 556 flag_a = 1;
nishimura_taku_pet 40:75e1ad7c27e4 557 return;
nishimura_taku_pet 28:cb51cafca490 558 }
tomotsugu 19:c6f9f010bd9e 559 }
yangtzuli 2:38825726cb1b 560 servo.pulsewidth_us(925); // サーボを右に40度回転
nishimura_taku_pet 28:cb51cafca490 561 ThisThread::sleep_for(100); // 250ms待つ
yangtzuli 2:38825726cb1b 562 SRD = watch();
yangtzuli 2:38825726cb1b 563 if(SRD < limit){ // 右前20cm以内に障害物がある場合
tomotsugu 20:02bb875a9b13 564 run = STOP; // 停止
nishimura_taku_pet 28:cb51cafca490 565 flag_a = 1;
nishimura_taku_pet 40:75e1ad7c27e4 566 return;
yangtzuli 2:38825726cb1b 567 }
nishimura_taku_pet 40:75e1ad7c27e4 568 servo.pulsewidth_us(1450); // サーボを中央位置に戻す
nishimura_taku_pet 40:75e1ad7c27e4 569 ThisThread::sleep_for(100); // 100ms待つ
nishimura_taku_pet 40:75e1ad7c27e4 570 /*if(SC < limit || SLD < limit || SL < limit || SRD < limit || SR < limit){ // 20cm以内に障害物を検知した場合
nishimura_taku_pet 40:75e1ad7c27e4 571 flag_a = 1; // 障害物有無フラグに1をセット
nishimura_taku_pet 40:75e1ad7c27e4 572 }*/
nishimura_taku_pet 40:75e1ad7c27e4 573 }
nishimura_taku_pet 40:75e1ad7c27e4 574
nishimura_taku_pet 40:75e1ad7c27e4 575 /* 障害物検知関数 */
nishimura_taku_pet 40:75e1ad7c27e4 576 void watchsurrounding5(){
nishimura_taku_pet 40:75e1ad7c27e4 577 //servo.pulsewidth_us(1450); // サーボを中央位置に戻す
nishimura_taku_pet 40:75e1ad7c27e4 578 //ThisThread::sleep_for(200); // 100ms待つ
nishimura_taku_pet 40:75e1ad7c27e4 579 SC = watch();
nishimura_taku_pet 40:75e1ad7c27e4 580 servo.pulsewidth_us(1925); // サーボを左に40度回転
nishimura_taku_pet 40:75e1ad7c27e4 581 ThisThread::sleep_for(100); // 250ms待つ
nishimura_taku_pet 40:75e1ad7c27e4 582 SLD = watch();
nishimura_taku_pet 40:75e1ad7c27e4 583 servo.pulsewidth_us(2400); // サーボを左に90度回転
nishimura_taku_pet 40:75e1ad7c27e4 584 ThisThread::sleep_for(100); // 250ms待つ
nishimura_taku_pet 40:75e1ad7c27e4 585 SL = watch();
nishimura_taku_pet 40:75e1ad7c27e4 586 servo.pulsewidth_us(1450);
nishimura_taku_pet 40:75e1ad7c27e4 587 ThisThread::sleep_for(250);
nishimura_taku_pet 40:75e1ad7c27e4 588 SC = watch();
nishimura_taku_pet 40:75e1ad7c27e4 589 servo.pulsewidth_us(925); // サーボを右に40度回転
nishimura_taku_pet 40:75e1ad7c27e4 590 ThisThread::sleep_for(100); // 250ms待つ
nishimura_taku_pet 40:75e1ad7c27e4 591 SRD = watch();
yangtzuli 2:38825726cb1b 592 servo.pulsewidth_us(500); // サーボを右に90度回転
nishimura_taku_pet 28:cb51cafca490 593 ThisThread::sleep_for(100); // 250ms待つ
yangtzuli 2:38825726cb1b 594 SR = watch();
yangtzuli 2:38825726cb1b 595 servo.pulsewidth_us(1450); // サーボを中央位置に戻す
nishimura_taku_pet 28:cb51cafca490 596 ThisThread::sleep_for(250); // 100ms待つ
yangtzuli 3:2ae6218973be 597 }
yangtzuli 3:2ae6218973be 598
tomotsugu 8:a47dbf4fa455 599 /* ディスプレイ表示関数 */
yangtzuli 3:2ae6218973be 600 void display(){
tomotsugu 8:a47dbf4fa455 601 mutex.lock(); // ミューテックスロック
yangtzuli 3:2ae6218973be 602 lcd.setAddress(0,1);
tomotsugu 8:a47dbf4fa455 603
tomotsugu 8:a47dbf4fa455 604 /* 操作モードによる場合分け */
yangtzuli 3:2ae6218973be 605 switch(mode){
tomotsugu 8:a47dbf4fa455 606 /* 前進 */
yangtzuli 3:2ae6218973be 607 case ADVANCE:
yangtzuli 3:2ae6218973be 608 lcd.printf("Mode:Advance ");
yangtzuli 3:2ae6218973be 609 break;
tomotsugu 8:a47dbf4fa455 610 /* 右折 */
yangtzuli 3:2ae6218973be 611 case RIGHT:
yangtzuli 5:3fffb364744b 612 lcd.printf("Mode:TurnRight ");
yangtzuli 3:2ae6218973be 613 break;
tomotsugu 8:a47dbf4fa455 614 /* 左折 */
yangtzuli 3:2ae6218973be 615 case LEFT:
yangtzuli 5:3fffb364744b 616 lcd.printf("Mode:TurnLeft ");
yangtzuli 3:2ae6218973be 617 break;
tomotsugu 8:a47dbf4fa455 618 /* 後退 */
yangtzuli 3:2ae6218973be 619 case BACK:
yangtzuli 3:2ae6218973be 620 lcd.printf("Mode:Back ");
yangtzuli 3:2ae6218973be 621 break;
tomotsugu 8:a47dbf4fa455 622 /* 停止 */
yangtzuli 3:2ae6218973be 623 case STOP:
yangtzuli 3:2ae6218973be 624 lcd.printf("Mode:Stop ");
yangtzuli 3:2ae6218973be 625 break;
tomotsugu 8:a47dbf4fa455 626 /* 待ち */
yangtzuli 3:2ae6218973be 627 case READY:
yangtzuli 3:2ae6218973be 628 lcd.printf("Mode:Ready ");
yangtzuli 3:2ae6218973be 629 break;
tomotsugu 8:a47dbf4fa455 630 /* ライントレース */
yangtzuli 3:2ae6218973be 631 case LINE_TRACE:
yangtzuli 3:2ae6218973be 632 lcd.printf("Mode:LineTrace ");
yangtzuli 3:2ae6218973be 633 break;
tomotsugu 8:a47dbf4fa455 634 /* 障害物回避 */
yangtzuli 3:2ae6218973be 635 case AVOIDANCE:
yangtzuli 3:2ae6218973be 636 lcd.printf("Mode:Avoidance ");
yangtzuli 3:2ae6218973be 637 break;
tomotsugu 8:a47dbf4fa455 638 /* スピード制御 */
yangtzuli 3:2ae6218973be 639 case SPEED:
tomotsugu 8:a47dbf4fa455 640 /* スピードの状態で場合分け */
yangtzuli 3:2ae6218973be 641 switch(flag_sp){
tomotsugu 8:a47dbf4fa455 642 /* 普通 */
yangtzuli 3:2ae6218973be 643 case(NORMAL):
yangtzuli 3:2ae6218973be 644 lcd.printf("Speed:Normal ");
yangtzuli 3:2ae6218973be 645 break;
tomotsugu 8:a47dbf4fa455 646 /* 速い */
yangtzuli 3:2ae6218973be 647 case(FAST):
yangtzuli 3:2ae6218973be 648 lcd.printf("Speed:Fast ");
yangtzuli 3:2ae6218973be 649 break;
tomotsugu 8:a47dbf4fa455 650 /* とても速い */
yangtzuli 3:2ae6218973be 651 case(VERYFAST):
yangtzuli 3:2ae6218973be 652 lcd.printf("Speed:VeryFast ");
yangtzuli 3:2ae6218973be 653 break;
yangtzuli 3:2ae6218973be 654 }
tomotsugu 8:a47dbf4fa455 655 viewTimer.reset(); // タイマーリセット
tomotsugu 8:a47dbf4fa455 656 viewTimer.start(); // タイマースタート
yangtzuli 3:2ae6218973be 657 break;
yangtzuli 3:2ae6218973be 658 }
tomotsugu 8:a47dbf4fa455 659 mutex.unlock(); // ミューテックスアンロック
yangtzuli 3:2ae6218973be 660 }
yangtzuli 3:2ae6218973be 661
tomotsugu 8:a47dbf4fa455 662 /* バックライト点滅 */
yangtzuli 3:2ae6218973be 663 void lcdBacklight(void const *argument){
tomotsugu 8:a47dbf4fa455 664 if(flag_b == 1){ // バックライト点滅フラグが1なら
tomotsugu 8:a47dbf4fa455 665 lcd.setBacklight(TextLCD::LightOn); // バックライトON
tomotsugu 8:a47dbf4fa455 666 }else{ // バックライト点滅フラグが0なら
tomotsugu 8:a47dbf4fa455 667 lcd.setBacklight(TextLCD::LightOff); // バックライトOFF
yangtzuli 3:2ae6218973be 668 }
tomotsugu 8:a47dbf4fa455 669 flag_b = !flag_b; // バックライト点滅フラグ切り替え
yangtzuli 3:2ae6218973be 670 }
yangtzuli 2:38825726cb1b 671
tomotsugu 8:a47dbf4fa455 672 /* バッテリー残量更新関数 */
yangtzuli 5:3fffb364744b 673 void bChange(){
yangtzuli 17:f7259ab2fe86 674 //pc.printf(" bChange1\r\n");
tomotsugu 10:d193030ce672 675 b = (int)(((battery.read() * 3.3 - MIN_V)/0.67)*10+0.5)*10;
tomotsugu 10:d193030ce672 676 if(b <= 0){ // バッテリー残量0%なら全ての機能停止
tomotsugu 10:d193030ce672 677 b = 0;
tomotsugu 10:d193030ce672 678 //lcd.setBacklight(TextLCD::LightOff);
tomotsugu 10:d193030ce672 679 //run = STOP;
tomotsugu 10:d193030ce672 680 //exit(1); // 電池残量が5%未満の時、LCDを消灯し、モーターを停止し、プログラムを終了する。
tomotsugu 10:d193030ce672 681 }
tomotsugu 10:d193030ce672 682 mutex.lock(); // ミューテックスロック
tomotsugu 10:d193030ce672 683 lcd.setAddress(0,0);
tomotsugu 10:d193030ce672 684 lcd.printf("Battery:%3d%%",b); // バッテリー残量表示
tomotsugu 10:d193030ce672 685 mutex.unlock(); // ミューテックスアンロック
tomotsugu 10:d193030ce672 686 if(b <= 30){ // バッテリー残量30%以下なら
tomotsugu 10:d193030ce672 687 if(flag_t == 0){ // バックライトタイマーフラグが0なら
nishimura_taku_pet 43:243c1455f88a 688 //bTimer.attach(lcdBacklight,0.5);
tomotsugu 10:d193030ce672 689 bTimer.start(500); // 0.5秒周期でバックライトタイマー割り込み
tomotsugu 10:d193030ce672 690 flag_t = 1; // バックライトタイマーフラグを1に切り替え
yangtzuli 3:2ae6218973be 691 }
tomotsugu 10:d193030ce672 692 }else{
tomotsugu 10:d193030ce672 693 if(flag_t == 1){ // バックライトタイマーフラグが1なら
nishimura_taku_pet 43:243c1455f88a 694 //bTimer.detach();
tomotsugu 10:d193030ce672 695 bTimer.stop(); // バックライトタイマーストップ
tomotsugu 10:d193030ce672 696 lcd.setBacklight(TextLCD::LightOn); // バックライトON
tomotsugu 10:d193030ce672 697 flag_t = 0; // バックライトタイマーフラグを0に切り替え
yangtzuli 3:2ae6218973be 698 }
tomotsugu 10:d193030ce672 699 }
yangtzuli 2:38825726cb1b 700 }
tomotsugu 8:a47dbf4fa455 701
nishimura_taku_pet 24:9481c8f56a49 702 // Serial Interrupt read ESP data
nishimura_taku_pet 24:9481c8f56a49 703 void callback()
nishimura_taku_pet 24:9481c8f56a49 704 {
nishimura_taku_pet 24:9481c8f56a49 705 //pc.printf("\n\r------------ callback is being called --------------\n\r");
nishimura_taku_pet 24:9481c8f56a49 706 led3=1;
nishimura_taku_pet 24:9481c8f56a49 707 while (esp.readable()) {
nishimura_taku_pet 24:9481c8f56a49 708 webbuff[ount] = esp.getc();
nishimura_taku_pet 24:9481c8f56a49 709 ount++;
nishimura_taku_pet 24:9481c8f56a49 710 }
nishimura_taku_pet 24:9481c8f56a49 711 if(strlen(webbuff)>bufflen) {
nishimura_taku_pet 24:9481c8f56a49 712 pc.printf("\f\n\r------------ webbuff over bufflen --------------\n\r");
nishimura_taku_pet 24:9481c8f56a49 713 DataRX=1;
nishimura_taku_pet 24:9481c8f56a49 714 led3=0;
nishimura_taku_pet 24:9481c8f56a49 715 }
nishimura_taku_pet 24:9481c8f56a49 716 }
nishimura_taku_pet 24:9481c8f56a49 717
nishimura_taku_pet 43:243c1455f88a 718 void wifi(/*void const *argument*/)
nishimura_taku_pet 24:9481c8f56a49 719 {
nishimura_taku_pet 24:9481c8f56a49 720 pc.printf("\f\n\r------------ ESP8266 Hardware Reset psq --------------\n\r");
nishimura_taku_pet 33:a6f1090e0174 721 ThisThread::sleep_for(100);
nishimura_taku_pet 24:9481c8f56a49 722 led1=1,led2=0,led3=0;
nishimura_taku_pet 24:9481c8f56a49 723 timeout=6000;
nishimura_taku_pet 24:9481c8f56a49 724 getcount=500;
nishimura_taku_pet 24:9481c8f56a49 725 getreply();
nishimura_taku_pet 24:9481c8f56a49 726 esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
nishimura_taku_pet 24:9481c8f56a49 727 startserver();
nishimura_taku_pet 24:9481c8f56a49 728
nishimura_taku_pet 24:9481c8f56a49 729 while(1) {
nishimura_taku_pet 24:9481c8f56a49 730 if(DataRX==1) {
nishimura_taku_pet 24:9481c8f56a49 731 pc.printf("\f\n\r------------ main while > if --------------\n\r");
nishimura_taku_pet 24:9481c8f56a49 732 click_flag = 1;
nishimura_taku_pet 24:9481c8f56a49 733 ReadWebData();
nishimura_taku_pet 24:9481c8f56a49 734 pc.printf("\f\n\r------------ click_flag=%d --------------\n\r",click_flag);
nishimura_taku_pet 24:9481c8f56a49 735 //if ((servreq == 1 && weberror == 0) && click_flag == 1) {
nishimura_taku_pet 24:9481c8f56a49 736 if (servreq == 1 && weberror == 0) {
nishimura_taku_pet 24:9481c8f56a49 737 pc.printf("\f\n\r------------ befor send page --------------\n\r");
nishimura_taku_pet 24:9481c8f56a49 738 sendpage();
nishimura_taku_pet 24:9481c8f56a49 739 }
nishimura_taku_pet 24:9481c8f56a49 740 pc.printf("\f\n\r------------ send_check begin --------------\n\r");
nishimura_taku_pet 24:9481c8f56a49 741
nishimura_taku_pet 24:9481c8f56a49 742 //sendcheck();
nishimura_taku_pet 24:9481c8f56a49 743 pc.printf("\f\n\r------------ ssend_check end--------------\n\r");
nishimura_taku_pet 24:9481c8f56a49 744
nishimura_taku_pet 24:9481c8f56a49 745 esp.attach(&callback);
nishimura_taku_pet 24:9481c8f56a49 746 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);
nishimura_taku_pet 24:9481c8f56a49 747 pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata);
nishimura_taku_pet 24:9481c8f56a49 748 pc.printf(" Web Characters sent : %d\n\n", bufl);
nishimura_taku_pet 24:9481c8f56a49 749 pc.printf(" -------------------------------------\n\n");
nishimura_taku_pet 24:9481c8f56a49 750 servreq=0;
nishimura_taku_pet 24:9481c8f56a49 751 }
nishimura_taku_pet 24:9481c8f56a49 752 ThisThread::sleep_for(100);
nishimura_taku_pet 24:9481c8f56a49 753 }
nishimura_taku_pet 24:9481c8f56a49 754 }
nishimura_taku_pet 24:9481c8f56a49 755 // Static WEB page
nishimura_taku_pet 24:9481c8f56a49 756 void sendpage()
nishimura_taku_pet 24:9481c8f56a49 757 {
nishimura_taku_pet 24:9481c8f56a49 758 // WEB page data
nishimura_taku_pet 24:9481c8f56a49 759
nishimura_taku_pet 24:9481c8f56a49 760 strcpy(webbuff, "<!DOCTYPE html>");
nishimura_taku_pet 24:9481c8f56a49 761 strcat(webbuff, "<html><head><title>RobotCar</title><meta name='viewport' content='width=device-width'/>");
faker_71 48:7be823ac7eb8 762 //strcat(webbuff, "<meta http-equiv=\"refresh\" content=\"5\"; >");
molberry 38:39db3f7450c2 763 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>");
nishimura_taku_pet 24:9481c8f56a49 764 strcat(webbuff, "</head><body><center><p><strong>Robot Car Remote Controller");
nishimura_taku_pet 24:9481c8f56a49 765 strcat(webbuff, "</strong></p><td style='vertical-align:top;'><strong>Battery level ");
faker_71 48:7be823ac7eb8 766 /*
molberry 35:4cda290bdb87 767 if(b > 30) { //残電量表示
nishimura_taku_pet 33:a6f1090e0174 768 sprintf(webbuff, "%s%3d", webbuff, b);
molberry 35:4cda290bdb87 769 } else { //30%より下の場合残電量を赤文字
nishimura_taku_pet 33:a6f1090e0174 770 strcat(webbuff, "<font color=\"red\">");
nishimura_taku_pet 33:a6f1090e0174 771 sprintf(webbuff, "%s%3d", webbuff, b);
nishimura_taku_pet 43:243c1455f88a 772 strcat(webbuff, "</font>");
faker_71 48:7be823ac7eb8 773 }*/
faker_71 48:7be823ac7eb8 774 strcat(webbuff, "<input type=\"text\" id=\"leftms\" size=4 value=250>%</strong>");
molberry 38:39db3f7450c2 775 strcat(webbuff, "<button id=\"reloadbtn\" type=\"button\" class=\"load\" onclick=\"rel()\">RELOAD</button>");
nishimura_taku_pet 24:9481c8f56a49 776 strcat(webbuff, "</td></p>");
nishimura_taku_pet 24:9481c8f56a49 777 strcat(webbuff, "<br>");
nishimura_taku_pet 24:9481c8f56a49 778 strcat(webbuff, "<table><tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 779
molberry 35:4cda290bdb87 780 switch(mode) { //ブラウザ更新時の現在の車の状態からボタンの点灯判定
molberry 35:4cda290bdb87 781 case ADVANCE: //前進
nishimura_taku_pet 24:9481c8f56a49 782 strcat(webbuff, "<button id='gobtn' type='button' class=\"light\" value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
nishimura_taku_pet 24:9481c8f56a49 783 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 784 strcat(webbuff, "<button id='leftbtn' type='button' class=\"noselect\" value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
nishimura_taku_pet 24:9481c8f56a49 785 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 786 strcat(webbuff, "<button id='stopbtn' type='button' class=\"noselect\" value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
nishimura_taku_pet 24:9481c8f56a49 787 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 788 strcat(webbuff, "<button id='rightbtn' type='button' class=\"noselect\" value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
nishimura_taku_pet 24:9481c8f56a49 789 strcat(webbuff, "</button></td></tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 790 strcat(webbuff, "<button id='backbtn' type='button' class=\"noselect\" value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
nishimura_taku_pet 24:9481c8f56a49 791 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 792 strcat(webbuff, "<strong>Mode</strong>");
nishimura_taku_pet 24:9481c8f56a49 793 strcat(webbuff, "<table><tr><td><button id='avoidbtn' type='button' class=\"noselect\" value=\"AVOIDANCE\" onClick='send_mes(this.id,this.value)' >");
nishimura_taku_pet 24:9481c8f56a49 794 strcat(webbuff, "AVOIDANCE</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 795 strcat(webbuff, "<button id='tracebtn' type='button' class=\"noselect\" value=\"LINE_TRACE\" onClick='send_mes(this.id,this.value)' >LINE_TRACE");
nishimura_taku_pet 24:9481c8f56a49 796 break;
molberry 35:4cda290bdb87 797 case LEFT: //左折
nishimura_taku_pet 24:9481c8f56a49 798 strcat(webbuff, "<button id='gobtn' type='button' class=\"noselect\" value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
nishimura_taku_pet 24:9481c8f56a49 799 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 800 strcat(webbuff, "<button id='leftbtn' type='button' class=\"light\" value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
nishimura_taku_pet 24:9481c8f56a49 801 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 802 strcat(webbuff, "<button id='stopbtn' type='button' class=\"noselect\" value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
nishimura_taku_pet 24:9481c8f56a49 803 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 804 strcat(webbuff, "<button id='rightbtn' type='button' class=\"noselect\" value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
nishimura_taku_pet 24:9481c8f56a49 805 strcat(webbuff, "</button></td></tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 806 strcat(webbuff, "<button id='backbtn' type='button' class=\"noselect\" value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
nishimura_taku_pet 24:9481c8f56a49 807 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 808 strcat(webbuff, "<strong>Mode</strong>");
nishimura_taku_pet 24:9481c8f56a49 809 strcat(webbuff, "<table><tr><td><button id='avoidbtn' type='button' class=\"noselect\" value=\"AVOIDANCE\" onClick='send_mes(this.id,this.value)' >");
nishimura_taku_pet 24:9481c8f56a49 810 strcat(webbuff, "AVOIDANCE</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 811 strcat(webbuff, "<button id='tracebtn' type='button' class=\"noselect\" value=\"LINE_TRACE\" onClick='send_mes(this.id,this.value)' >LINE_TRACE");
nishimura_taku_pet 24:9481c8f56a49 812 break;
molberry 35:4cda290bdb87 813 case STOP: //停止
nishimura_taku_pet 24:9481c8f56a49 814 strcat(webbuff, "<button id='gobtn' type='button' class=\"noselect\" value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
nishimura_taku_pet 24:9481c8f56a49 815 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 816 strcat(webbuff, "<button id='leftbtn' type='button' class=\"noselect\" value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
nishimura_taku_pet 24:9481c8f56a49 817 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 818 strcat(webbuff, "<button id='stopbtn' type='button' class=\"light\" value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
nishimura_taku_pet 24:9481c8f56a49 819 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 820 strcat(webbuff, "<button id='rightbtn' type='button' class=\"noselect\" value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
nishimura_taku_pet 24:9481c8f56a49 821 strcat(webbuff, "</button></td></tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 822 strcat(webbuff, "<button id='backbtn' type='button' class=\"noselect\" value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
nishimura_taku_pet 24:9481c8f56a49 823 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 824 strcat(webbuff, "<strong>Mode</strong>");
nishimura_taku_pet 24:9481c8f56a49 825 strcat(webbuff, "<table><tr><td><button id='avoidbtn' type='button' class=\"noselect\" value=\"AVOIDANCE\" onClick='send_mes(this.id,this.value)' >");
nishimura_taku_pet 24:9481c8f56a49 826 strcat(webbuff, "AVOIDANCE</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 827 strcat(webbuff, "<button id='tracebtn' type='button' class=\"noselect\" value=\"LINE_TRACE\" onClick='send_mes(this.id,this.value)' >LINE_TRACE");
nishimura_taku_pet 24:9481c8f56a49 828 break;
molberry 35:4cda290bdb87 829 case RIGHT: //右折
nishimura_taku_pet 24:9481c8f56a49 830 strcat(webbuff, "<button id='gobtn' type='button' class=\"noselect\" value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
nishimura_taku_pet 24:9481c8f56a49 831 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 832 strcat(webbuff, "<button id='leftbtn' type='button' class=\"noselect\" value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
nishimura_taku_pet 24:9481c8f56a49 833 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 834 strcat(webbuff, "<button id='stopbtn' type='button' class=\"noselect\" value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
nishimura_taku_pet 24:9481c8f56a49 835 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 836 strcat(webbuff, "<button id='rightbtn' type='button' class=\"light\" value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
nishimura_taku_pet 24:9481c8f56a49 837 strcat(webbuff, "</button></td></tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 838 strcat(webbuff, "<button id='backbtn' type='button' class=\"noselect\" value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
nishimura_taku_pet 24:9481c8f56a49 839 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 840 strcat(webbuff, "<strong>Mode</strong>");
nishimura_taku_pet 24:9481c8f56a49 841 strcat(webbuff, "<table><tr><td><button id='avoidbtn' type='button' class=\"noselect\" value=\"AVOIDANCE\" onClick='send_mes(this.id,this.value)' >");
nishimura_taku_pet 24:9481c8f56a49 842 strcat(webbuff, "AVOIDANCE</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 843 strcat(webbuff, "<button id='tracebtn' type='button' class=\"noselect\" value=\"LINE_TRACE\" onClick='send_mes(this.id,this.value)' >LINE_TRACE");
nishimura_taku_pet 24:9481c8f56a49 844 break;
molberry 35:4cda290bdb87 845 case BACK: //後進
nishimura_taku_pet 24:9481c8f56a49 846 strcat(webbuff, "<button id='gobtn' type='button' class=\"noselect\" value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
nishimura_taku_pet 24:9481c8f56a49 847 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 848 strcat(webbuff, "<button id='leftbtn' type='button' class=\"noselect\" value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
nishimura_taku_pet 24:9481c8f56a49 849 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 850 strcat(webbuff, "<button id='stopbtn' type='button' class=\"noselect\" value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
nishimura_taku_pet 24:9481c8f56a49 851 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 852 strcat(webbuff, "<button id='rightbtn' type='button' class=\"noselect\" value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
nishimura_taku_pet 24:9481c8f56a49 853 strcat(webbuff, "</button></td></tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 854 strcat(webbuff, "<button id='backbtn' type='button' class=\"light\" value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
nishimura_taku_pet 24:9481c8f56a49 855 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr><td>");
nishimura_taku_pet 24:9481c8f56a49 856 strcat(webbuff, "<strong>Mode</strong>");
nishimura_taku_pet 24:9481c8f56a49 857 strcat(webbuff, "<table><tr><td><button id='avoidbtn' type='button' class=\"noselect\" value=\"AVOIDANCE\" onClick='send_mes(this.id,this.value)' >");
nishimura_taku_pet 24:9481c8f56a49 858 strcat(webbuff, "AVOIDANCE</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 859 strcat(webbuff, "<button id='tracebtn' type='button' class=\"noselect\" value=\"LINE_TRACE\" onClick='send_mes(this.id,this.value)' >LINE_TRACE");
nishimura_taku_pet 24:9481c8f56a49 860 break;
molberry 35:4cda290bdb87 861 case AVOIDANCE: //障害物回避
nishimura_taku_pet 24:9481c8f56a49 862 strcat(webbuff, "<button id='gobtn' type='button' class=\"noselect\" value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
nishimura_taku_pet 24:9481c8f56a49 863 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 864 strcat(webbuff, "<button id='leftbtn' type='button' class=\"noselect\" value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
nishimura_taku_pet 24:9481c8f56a49 865 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 866 strcat(webbuff, "<button id='stopbtn' type='button' class=\"noselect\" value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
nishimura_taku_pet 24:9481c8f56a49 867 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 868 strcat(webbuff, "<button id='rightbtn' type='button' class=\"noselect\" value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
nishimura_taku_pet 24:9481c8f56a49 869 strcat(webbuff, "</button></td></tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 870 strcat(webbuff, "<button id='backbtn' type='button' class=\"noselect\" value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
nishimura_taku_pet 24:9481c8f56a49 871 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 872 strcat(webbuff, "<strong>Mode</strong>");
nishimura_taku_pet 24:9481c8f56a49 873 strcat(webbuff, "<table><tr><td><button id='avoidbtn' type='button' class=\"light\" value=\"AVOIDANCE\" onClick='send_mes(this.id,this.value)' >");
nishimura_taku_pet 24:9481c8f56a49 874 strcat(webbuff, "AVOIDANCE</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 875 strcat(webbuff, "<button id='tracebtn' type='button' class=\"noselect\" value=\"LINE_TRACE\" onClick='send_mes(this.id,this.value)' >LINE_TRACE");
nishimura_taku_pet 24:9481c8f56a49 876 break;
molberry 35:4cda290bdb87 877 case LINE_TRACE: //ライントレース
nishimura_taku_pet 24:9481c8f56a49 878 strcat(webbuff, "<button id='gobtn' type='button' class=\"noselect\" value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
nishimura_taku_pet 24:9481c8f56a49 879 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 880 strcat(webbuff, "<button id='leftbtn' type='button' class=\"noselect\" value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
nishimura_taku_pet 24:9481c8f56a49 881 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 882 strcat(webbuff, "<button id='stopbtn' type='button' class=\"noselect\" value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
nishimura_taku_pet 24:9481c8f56a49 883 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 884 strcat(webbuff, "<button id='rightbtn' type='button' class=\"noselect\" value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
nishimura_taku_pet 24:9481c8f56a49 885 strcat(webbuff, "</button></td></tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 886 strcat(webbuff, "<button id='backbtn' type='button' class=\"noselect\" value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
nishimura_taku_pet 24:9481c8f56a49 887 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 888 strcat(webbuff, "<strong>Mode</strong>");
nishimura_taku_pet 24:9481c8f56a49 889 strcat(webbuff, "<table><tr><td><button id='avoidbtn' type='button' class=\"noselect\" value=\"AVOIDANCE\" onClick='send_mes(this.id,this.value)' >");
nishimura_taku_pet 24:9481c8f56a49 890 strcat(webbuff, "AVOIDANCE</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 891 strcat(webbuff, "<button id='tracebtn' type='button' class=\"light\" value=\"LINE_TRACE\" onClick='send_mes(this.id,this.value)' >LINE_TRACE");
nishimura_taku_pet 24:9481c8f56a49 892 break;
molberry 35:4cda290bdb87 893 default: //その他
nishimura_taku_pet 24:9481c8f56a49 894 strcat(webbuff, "<button id='gobtn' type='button' class=\"noselect\" value=\"GO\" onClick='send_mes(this.id,this.value)'>GO");
nishimura_taku_pet 24:9481c8f56a49 895 strcat(webbuff, "</button></td><td></td></tr><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 896 strcat(webbuff, "<button id='leftbtn' type='button' class=\"noselect\" value=\"LEFT\" onClick='send_mes(this.id,this.value)' >LEFT");
nishimura_taku_pet 24:9481c8f56a49 897 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 898 strcat(webbuff, "<button id='stopbtn' type='button' class=\"noselect\" value=\"STOP\" onClick='send_mes(this.id,this.value)' >STOP");
nishimura_taku_pet 24:9481c8f56a49 899 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 900 strcat(webbuff, "<button id='rightbtn' type='button' class=\"noselect\" value=\"RIGHT\" onClick='send_mes(this.id,this.value)' >RIGHT");
nishimura_taku_pet 24:9481c8f56a49 901 strcat(webbuff, "</button></td></tr><td></td><td>");
nishimura_taku_pet 24:9481c8f56a49 902 strcat(webbuff, "<button id='backbtn' type='button' class=\"noselect\" value=\"BACK\" onClick='send_mes(this.id,this.value)' >BACK");
nishimura_taku_pet 24:9481c8f56a49 903 strcat(webbuff, "</button></td><td style='vertical-align:top; text-align:right;'></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 904 strcat(webbuff, "<strong>Mode</strong>");
nishimura_taku_pet 24:9481c8f56a49 905 strcat(webbuff, "<table><tr><td><button id='avoidbtn' type='button' class=\"noselect\" value=\"AVOIDANCE\" onClick='send_mes(this.id,this.value)' >");
nishimura_taku_pet 24:9481c8f56a49 906 strcat(webbuff, "AVOIDANCE</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 907 strcat(webbuff, "<button id='tracebtn' type='button' class=\"noselect\" value=\"LINE_TRACE\" onClick='send_mes(this.id,this.value)' >LINE_TRACE");
nishimura_taku_pet 24:9481c8f56a49 908 break;
nishimura_taku_pet 24:9481c8f56a49 909 }
nishimura_taku_pet 24:9481c8f56a49 910 strcat(webbuff, "</button></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 911 strcat(webbuff, "<strong>Speed</strong>");
nishimura_taku_pet 24:9481c8f56a49 912 strcat(webbuff, "<table><tr><td>");
nishimura_taku_pet 24:9481c8f56a49 913 //ready示速度だけ点灯
molberry 35:4cda290bdb87 914 switch (flag_sp) { //現在の速度のボタン表示
molberry 35:4cda290bdb87 915 case 0: //ノーマル
nishimura_taku_pet 24:9481c8f56a49 916 strcat(webbuff, "<button id='sp1btn' type='button' class=\"light\" value=\"Normal\" onClick='send_mes_spe(this.id,this.value)' >Normal");
nishimura_taku_pet 24:9481c8f56a49 917 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 918 strcat(webbuff, "<button id='sp2btn' type='button' class=\"noselect\" value=\"Fast\" onClick='send_mes_spe(this.id,this.value)' >Fast");
nishimura_taku_pet 24:9481c8f56a49 919 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 920 strcat(webbuff, "<button id='sp3btn' type='button' class=\"noselect\" value=\"VeryFast\" onClick='send_mes_spe(this.id,this.value)' >VeryFast");
nishimura_taku_pet 24:9481c8f56a49 921 break;
molberry 35:4cda290bdb87 922 case 1: //ファスト
nishimura_taku_pet 24:9481c8f56a49 923 strcat(webbuff, "<button id='sp1btn' type='button' class=\"noselect\" value=\"Normal\" onClick='send_mes_spe(this.id,this.value)' >Normal");
nishimura_taku_pet 24:9481c8f56a49 924 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 925 strcat(webbuff, "<button id='sp2btn' type='button' class=\"light\" value=\"Fast\" onClick='send_mes_spe(this.id,this.value)' >Fast");
nishimura_taku_pet 24:9481c8f56a49 926 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 927 strcat(webbuff, "<button id='sp3btn' type='button' class=\"noselect\" value=\"VeryFast\" onClick='send_mes_spe(this.id,this.value)' >VeryFast");
nishimura_taku_pet 24:9481c8f56a49 928 break;
molberry 35:4cda290bdb87 929 case 2: //ベリーファスト
nishimura_taku_pet 24:9481c8f56a49 930 strcat(webbuff, "<button id='sp1btn' type='button' class=\"noselect\" value=\"Normal\" onClick='send_mes_spe(this.id,this.value)' >Normal");
nishimura_taku_pet 24:9481c8f56a49 931 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 932 strcat(webbuff, "<button id='sp2btn' type='button' class=\"noselect\" value=\"Fast\" onClick='send_mes_spe(this.id,this.value)' >Fast");
nishimura_taku_pet 24:9481c8f56a49 933 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 934 strcat(webbuff, "<button id='sp3btn' type='button' class=\"light\" value=\"VeryFast\" onClick='send_mes_spe(this.id,this.value)' >VeryFast");
nishimura_taku_pet 24:9481c8f56a49 935 break;
molberry 35:4cda290bdb87 936 default: //その他
nishimura_taku_pet 24:9481c8f56a49 937 strcat(webbuff, "<button id='sp1btn' type='button' class=\"noselect\" value=\"Normal\" onClick='send_mes_spe(this.id,this.value)' >Normal");
nishimura_taku_pet 24:9481c8f56a49 938 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 939 strcat(webbuff, "<button id='sp2btn' type='button' class=\"noselect\" value=\"Fast\" onClick='send_mes_spe(this.id,this.value)' >Fast");
nishimura_taku_pet 24:9481c8f56a49 940 strcat(webbuff, "</button></td><td>");
nishimura_taku_pet 24:9481c8f56a49 941 strcat(webbuff, "<button id='sp3btn' type='button' class=\"noselect\" value=\"VeryFast\" onClick='send_mes_spe(this.id,this.value)' >VeryFast");
nishimura_taku_pet 24:9481c8f56a49 942 break;
nishimura_taku_pet 24:9481c8f56a49 943 }
nishimura_taku_pet 24:9481c8f56a49 944 strcat(webbuff, "</button></td></tr></table>");
nishimura_taku_pet 24:9481c8f56a49 945
nishimura_taku_pet 24:9481c8f56a49 946 strcat(webbuff, "</center>");
nishimura_taku_pet 24:9481c8f56a49 947 strcat(webbuff, "</body>");
nishimura_taku_pet 24:9481c8f56a49 948 strcat(webbuff, "</html>");
molberry 35:4cda290bdb87 949 strcat(webbuff, "<script language=\"javascript\" type=\"text/javascript\">"); //機能
nishimura_taku_pet 24:9481c8f56a49 950
molberry 38:39db3f7450c2 951 strcat(webbuff, "function rel(){");
molberry 38:39db3f7450c2 952 strcat(webbuff, "location.reload();");
molberry 38:39db3f7450c2 953 strcat(webbuff, "}");
molberry 38:39db3f7450c2 954
faker_71 50:6c25bf1c39d7 955 //
faker_71 50:6c25bf1c39d7 956 strcat(webbuff, "var button_9 = document.getElementsByTagName(\"button\");");
faker_71 50:6c25bf1c39d7 957
faker_71 50:6c25bf1c39d7 958
faker_71 50:6c25bf1c39d7 959
nishimura_taku_pet 24:9481c8f56a49 960 strcat(webbuff, "function htmlacs(url) {");
faker_71 49:14b3dba5bd56 961 strcat(webbuff, "clearInterval(si);");
faker_71 50:6c25bf1c39d7 962 //0827
faker_71 50:6c25bf1c39d7 963 strcat(webbuff, "for(var m=0;m<11;m++){");
faker_71 50:6c25bf1c39d7 964 strcat(webbuff, "button_9[m].disabled=true;}");
faker_71 50:6c25bf1c39d7 965 //0827
nishimura_taku_pet 24:9481c8f56a49 966 strcat(webbuff, "var xhr = new XMLHttpRequest();");
nishimura_taku_pet 24:9481c8f56a49 967 strcat(webbuff, "xhr.open(\"GET\", url);");
faker_71 49:14b3dba5bd56 968
faker_71 49:14b3dba5bd56 969 //0826
faker_71 49:14b3dba5bd56 970 strcat(webbuff, "xhr.onreadystatechange = function(){");
faker_71 49:14b3dba5bd56 971 strcat(webbuff, "if(this.readyState == 4 && this.status == 200){");
faker_71 49:14b3dba5bd56 972 strcat(webbuff, "console.log(\"apologize for\");");
faker_71 50:6c25bf1c39d7 973 ////0827
faker_71 50:6c25bf1c39d7 974 strcat(webbuff, "for(var m=0;m<10;m++){");
faker_71 50:6c25bf1c39d7 975 strcat(webbuff, "button_9[m].disabled=false;}");
faker_71 50:6c25bf1c39d7 976 //0827
faker_71 49:14b3dba5bd56 977 strcat(webbuff, "var si = setInterval(battery_update,15000);");
faker_71 49:14b3dba5bd56 978
faker_71 49:14b3dba5bd56 979 strcat(webbuff, "}");
faker_71 49:14b3dba5bd56 980 strcat(webbuff, "};");
faker_71 49:14b3dba5bd56 981
faker_71 49:14b3dba5bd56 982
faker_71 49:14b3dba5bd56 983 //0826
faker_71 49:14b3dba5bd56 984
faker_71 49:14b3dba5bd56 985
nishimura_taku_pet 24:9481c8f56a49 986 strcat(webbuff, "xhr.send(\"\");");
nishimura_taku_pet 24:9481c8f56a49 987 strcat(webbuff, "}");
nishimura_taku_pet 24:9481c8f56a49 988
faker_71 48:7be823ac7eb8 989
faker_71 48:7be823ac7eb8 990 //0825
faker_71 48:7be823ac7eb8 991 //
faker_71 48:7be823ac7eb8 992 //0824 battery update auto
faker_71 48:7be823ac7eb8 993 strcat(webbuff, "function battery_update() {");
faker_71 48:7be823ac7eb8 994 strcat(webbuff, "var url1 = \"http://\" + window.location.hostname+ \"/cargo?a=responseBattery\";");
faker_71 48:7be823ac7eb8 995 strcat(webbuff, "var xhr1 = new XMLHttpRequest();");
faker_71 48:7be823ac7eb8 996 strcat(webbuff, "xhr1.open(\"GET\", url1);");
faker_71 48:7be823ac7eb8 997 //0820
faker_71 48:7be823ac7eb8 998 strcat(webbuff, "xhr1.onreadystatechange = function(){");
faker_71 48:7be823ac7eb8 999 //strcat(webbuff, "console.log(\"onready function is being reload!\");");
faker_71 48:7be823ac7eb8 1000 strcat(webbuff, "if(this.readyState == 4 && this.status == 200){");
faker_71 48:7be823ac7eb8 1001 //strcat(webbuff, "console.log(\"state is being reload!\");");
faker_71 48:7be823ac7eb8 1002 strcat(webbuff, "var res1 = xhr1.responseText;");
faker_71 48:7be823ac7eb8 1003
faker_71 48:7be823ac7eb8 1004 //color
faker_71 48:7be823ac7eb8 1005 strcat(webbuff, "var battery_num=res1;");
faker_71 48:7be823ac7eb8 1006 strcat(webbuff, "if(battery_num>0 && battery_num<31){");
faker_71 48:7be823ac7eb8 1007 strcat(webbuff, "document.getElementById('leftms').style.color=\"red\";");
faker_71 48:7be823ac7eb8 1008 strcat(webbuff, "}");
faker_71 48:7be823ac7eb8 1009 strcat(webbuff, "if(battery_num>39 && battery_num<61){");
faker_71 48:7be823ac7eb8 1010 strcat(webbuff, "document.getElementById('leftms').style.color=\"orange\";");
faker_71 48:7be823ac7eb8 1011 strcat(webbuff, "}");
faker_71 48:7be823ac7eb8 1012 strcat(webbuff, "if(battery_num>69 && battery_num<101){");
faker_71 48:7be823ac7eb8 1013 strcat(webbuff, "document.getElementById('leftms').style.color=\"blue\";");
faker_71 48:7be823ac7eb8 1014 strcat(webbuff, "}");
faker_71 48:7be823ac7eb8 1015 //color
faker_71 48:7be823ac7eb8 1016 strcat(webbuff, "document.getElementById('leftms').value=res1;}};");
faker_71 48:7be823ac7eb8 1017
faker_71 48:7be823ac7eb8 1018
faker_71 48:7be823ac7eb8 1019 //strcat(webbuff, "console.log(res);}};");
faker_71 48:7be823ac7eb8 1020 //0820
faker_71 48:7be823ac7eb8 1021
faker_71 48:7be823ac7eb8 1022 strcat(webbuff, "xhr1.send();");
faker_71 48:7be823ac7eb8 1023 strcat(webbuff, "}");
faker_71 49:14b3dba5bd56 1024 strcat(webbuff, "var si = setInterval(battery_update,15000);");
faker_71 48:7be823ac7eb8 1025
faker_71 48:7be823ac7eb8 1026 //0824 battery update auto
faker_71 48:7be823ac7eb8 1027 //0825
faker_71 48:7be823ac7eb8 1028
faker_71 48:7be823ac7eb8 1029
molberry 35:4cda290bdb87 1030 strcat(webbuff, "function send_mes(btnmes,btnval){"); //mode変更ボタン入力時の点灯消灯判定
nishimura_taku_pet 24:9481c8f56a49 1031 strcat(webbuff, "console.log(btnval);");
nishimura_taku_pet 24:9481c8f56a49 1032
nishimura_taku_pet 24:9481c8f56a49 1033 strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
nishimura_taku_pet 24:9481c8f56a49 1034 strcat(webbuff, "htmlacs(url);");
nishimura_taku_pet 24:9481c8f56a49 1035 strcat(webbuff, "console.log(url);");
nishimura_taku_pet 24:9481c8f56a49 1036 strcat(webbuff, "var buttons = document.getElementsByTagName(\"button\");");
nishimura_taku_pet 43:243c1455f88a 1037 strcat(webbuff, "for(var i=1;i<8;i++){");
nishimura_taku_pet 24:9481c8f56a49 1038 strcat(webbuff, "if(buttons[i].value == btnval){");
nishimura_taku_pet 24:9481c8f56a49 1039 strcat(webbuff, "buttons[i].className=\"light\";");
nishimura_taku_pet 24:9481c8f56a49 1040 strcat(webbuff, "}else{");
nishimura_taku_pet 24:9481c8f56a49 1041 strcat(webbuff, "buttons[i].className=\"noselect\";");
nishimura_taku_pet 24:9481c8f56a49 1042 strcat(webbuff, "}");
nishimura_taku_pet 24:9481c8f56a49 1043 strcat(webbuff, "}");
nishimura_taku_pet 24:9481c8f56a49 1044 strcat(webbuff, "}");
nishimura_taku_pet 24:9481c8f56a49 1045
molberry 35:4cda290bdb87 1046 strcat(webbuff, "function send_mes_spe(btnmes,btnval){"); //speed変更ボタン入力時の点灯消灯判定
nishimura_taku_pet 24:9481c8f56a49 1047 strcat(webbuff, "var url = \"http://\" + window.location.hostname + \"/cargo?a=\" + btnval;");
nishimura_taku_pet 24:9481c8f56a49 1048 strcat(webbuff, "htmlacs(url);");
nishimura_taku_pet 24:9481c8f56a49 1049 strcat(webbuff, "console.log(url);");
nishimura_taku_pet 24:9481c8f56a49 1050 strcat(webbuff, "var buttons = document.getElementsByTagName(\"button\");");
nishimura_taku_pet 43:243c1455f88a 1051 strcat(webbuff, "for(var i=8;i<11;i++){");
nishimura_taku_pet 24:9481c8f56a49 1052 strcat(webbuff, "if(buttons[i].value == btnval){");
nishimura_taku_pet 24:9481c8f56a49 1053 strcat(webbuff, "buttons[i].className=\"light\";");
nishimura_taku_pet 24:9481c8f56a49 1054 strcat(webbuff, "}else{");
nishimura_taku_pet 24:9481c8f56a49 1055 strcat(webbuff, "buttons[i].className=\"noselect\";");
nishimura_taku_pet 24:9481c8f56a49 1056 strcat(webbuff, "}");
nishimura_taku_pet 24:9481c8f56a49 1057 strcat(webbuff, "}");
nishimura_taku_pet 24:9481c8f56a49 1058 strcat(webbuff, "}");
nishimura_taku_pet 24:9481c8f56a49 1059 strcat(webbuff, "</script>");
nishimura_taku_pet 24:9481c8f56a49 1060 // end of WEB page data
nishimura_taku_pet 24:9481c8f56a49 1061 bufl = strlen(webbuff); // get total page buffer length
nishimura_taku_pet 24:9481c8f56a49 1062 //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
nishimura_taku_pet 24:9481c8f56a49 1063
nishimura_taku_pet 24:9481c8f56a49 1064 sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl>2048?2048:bufl)); // send IPD link channel and buffer character length.
nishimura_taku_pet 24:9481c8f56a49 1065 timeout=500;
nishimura_taku_pet 24:9481c8f56a49 1066 getcount=40;
nishimura_taku_pet 24:9481c8f56a49 1067 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1068 getreply();
nishimura_taku_pet 24:9481c8f56a49 1069 pc.printf(replybuff);
nishimura_taku_pet 32:049d9ba081d4 1070 //pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl));
nishimura_taku_pet 24:9481c8f56a49 1071
nishimura_taku_pet 24:9481c8f56a49 1072 pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
nishimura_taku_pet 24:9481c8f56a49 1073
nishimura_taku_pet 24:9481c8f56a49 1074 //pastthrough mode
nishimura_taku_pet 24:9481c8f56a49 1075 SendWEB(); // send web page
nishimura_taku_pet 24:9481c8f56a49 1076 pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n");
nishimura_taku_pet 24:9481c8f56a49 1077
nishimura_taku_pet 24:9481c8f56a49 1078 memset(webbuff, '\0', sizeof(webbuff));
nishimura_taku_pet 24:9481c8f56a49 1079 sendcheck();
nishimura_taku_pet 24:9481c8f56a49 1080 }
nishimura_taku_pet 24:9481c8f56a49 1081
nishimura_taku_pet 24:9481c8f56a49 1082 // Large WEB buffer data send
nishimura_taku_pet 24:9481c8f56a49 1083 void SendWEB()
nishimura_taku_pet 24:9481c8f56a49 1084 {
nishimura_taku_pet 24:9481c8f56a49 1085 int i=0;
nishimura_taku_pet 24:9481c8f56a49 1086 if(esp.writeable()) {
nishimura_taku_pet 24:9481c8f56a49 1087 while(webbuff[i]!='\0') {
nishimura_taku_pet 24:9481c8f56a49 1088 esp.putc(webbuff[i]);
nishimura_taku_pet 24:9481c8f56a49 1089
nishimura_taku_pet 24:9481c8f56a49 1090 //****
nishimura_taku_pet 24:9481c8f56a49 1091 //output at command when 2000
nishimura_taku_pet 24:9481c8f56a49 1092 if(((i%2047)==0) && (i>0)) {
nishimura_taku_pet 24:9481c8f56a49 1093 //wait_ms(10);
faker_71 50:6c25bf1c39d7 1094 ThisThread::sleep_for(10); //0827 100
faker_71 48:7be823ac7eb8 1095 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.
nishimura_taku_pet 32:049d9ba081d4 1096 //pc.printf("\r\n++++++++++ AT+CIPSENDBUF=%d,%d ++++++++++\r\n", linkID, (bufl-2048)>2048?2048:(bufl-2048));
nishimura_taku_pet 24:9481c8f56a49 1097 timeout=600;
nishimura_taku_pet 24:9481c8f56a49 1098 getcount=50;
nishimura_taku_pet 24:9481c8f56a49 1099 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1100 getreply();
nishimura_taku_pet 32:049d9ba081d4 1101 //pc.printf(replybuff);
nishimura_taku_pet 32:049d9ba081d4 1102 //pc.printf("\r\n+++++++++++++++++++\r\n");
nishimura_taku_pet 24:9481c8f56a49 1103 }
nishimura_taku_pet 24:9481c8f56a49 1104 //****
nishimura_taku_pet 24:9481c8f56a49 1105 i++;
nishimura_taku_pet 32:049d9ba081d4 1106 //pc.printf("%c",webbuff[i]);
nishimura_taku_pet 24:9481c8f56a49 1107 }
nishimura_taku_pet 24:9481c8f56a49 1108 }
nishimura_taku_pet 24:9481c8f56a49 1109 pc.printf("\n++++++++++ send web i= %dinfo ++++++++++\r\n",i);
nishimura_taku_pet 24:9481c8f56a49 1110 }
nishimura_taku_pet 24:9481c8f56a49 1111
faker_71 48:7be823ac7eb8 1112 void sendpage2()
faker_71 48:7be823ac7eb8 1113 {
faker_71 48:7be823ac7eb8 1114 // WEB page data
faker_71 49:14b3dba5bd56 1115 pred=!pred;
faker_71 48:7be823ac7eb8 1116 //strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
faker_71 48:7be823ac7eb8 1117 //strcat(webbuff, "document.getElementById('leftms').value=\"100\";");
faker_71 48:7be823ac7eb8 1118
faker_71 48:7be823ac7eb8 1119 sprintf(battery_ch,"%d",b);
faker_71 48:7be823ac7eb8 1120 //sprintf(battery_ch,"%d",30);
faker_71 48:7be823ac7eb8 1121 strcpy(webbuff, battery_ch);
faker_71 48:7be823ac7eb8 1122
faker_71 48:7be823ac7eb8 1123 //strcpy(webbuff, "document.getElementById('leftms').value=\"100\";");
faker_71 48:7be823ac7eb8 1124
faker_71 48:7be823ac7eb8 1125 //strcat(webbuff, "</script>");
faker_71 48:7be823ac7eb8 1126 // end of WEB page data
faker_71 48:7be823ac7eb8 1127 bufl = strlen(webbuff); // get total page buffer length
faker_71 48:7be823ac7eb8 1128 //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
faker_71 48:7be823ac7eb8 1129
faker_71 48:7be823ac7eb8 1130 sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl>2048?2048:bufl)); // send IPD link channel and buffer character length.
faker_71 48:7be823ac7eb8 1131 timeout=500;
faker_71 48:7be823ac7eb8 1132 getcount=40;
faker_71 48:7be823ac7eb8 1133 SendCMD();
faker_71 48:7be823ac7eb8 1134 getreply();
faker_71 48:7be823ac7eb8 1135 pc.printf(replybuff);
faker_71 48:7be823ac7eb8 1136 pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl));
faker_71 48:7be823ac7eb8 1137
faker_71 48:7be823ac7eb8 1138 pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
faker_71 48:7be823ac7eb8 1139
faker_71 48:7be823ac7eb8 1140 //pastthrough mode
faker_71 48:7be823ac7eb8 1141 SendWEB(); // send web page
faker_71 48:7be823ac7eb8 1142 pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n");
faker_71 48:7be823ac7eb8 1143
faker_71 48:7be823ac7eb8 1144 memset(webbuff, '\0', sizeof(webbuff));
faker_71 48:7be823ac7eb8 1145 sendcheck();
faker_71 48:7be823ac7eb8 1146 }
faker_71 49:14b3dba5bd56 1147
faker_71 49:14b3dba5bd56 1148
faker_71 49:14b3dba5bd56 1149 void sendpage3()
faker_71 49:14b3dba5bd56 1150 {
faker_71 49:14b3dba5bd56 1151 // WEB page data
faker_71 49:14b3dba5bd56 1152 pblue=!pblue;
faker_71 49:14b3dba5bd56 1153 //strcpy(webbuff, "<script language=\"javascript\" type=\"text/javascript\">");
faker_71 49:14b3dba5bd56 1154 //strcat(webbuff, "document.getElementById('leftms').value=\"100\";");
faker_71 49:14b3dba5bd56 1155
faker_71 51:622df065f7ff 1156 //sprintf(battery_ch,"%d",b);
faker_71 49:14b3dba5bd56 1157 //sprintf(battery_ch,"%d",30);
faker_71 51:622df065f7ff 1158 strcpy(webbuff, "response ok");
faker_71 49:14b3dba5bd56 1159
faker_71 49:14b3dba5bd56 1160 //strcpy(webbuff, "document.getElementById('leftms').value=\"100\";");
faker_71 49:14b3dba5bd56 1161
faker_71 49:14b3dba5bd56 1162 //strcat(webbuff, "</script>");
faker_71 49:14b3dba5bd56 1163 // end of WEB page data
faker_71 49:14b3dba5bd56 1164 bufl = strlen(webbuff); // get total page buffer length
faker_71 49:14b3dba5bd56 1165 //sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
faker_71 49:14b3dba5bd56 1166
faker_71 49:14b3dba5bd56 1167 sprintf(cmdbuff,"AT+CIPSENDBUF=%d,%d\r\n", linkID, (bufl>2048?2048:bufl)); // send IPD link channel and buffer character length.
faker_71 49:14b3dba5bd56 1168 timeout=500;
faker_71 49:14b3dba5bd56 1169 getcount=40;
faker_71 49:14b3dba5bd56 1170 SendCMD();
faker_71 49:14b3dba5bd56 1171 getreply();
faker_71 49:14b3dba5bd56 1172 pc.printf(replybuff);
faker_71 49:14b3dba5bd56 1173 pc.printf("\n++++++++++ AT+CIPSENDBUF=%d,%d+++++++++\r\n", linkID, (bufl>2048?2048:bufl));
faker_71 49:14b3dba5bd56 1174
faker_71 49:14b3dba5bd56 1175 pc.printf("\n++++++++++ bufl is %d ++++++++++\r\n",bufl);
faker_71 49:14b3dba5bd56 1176
faker_71 49:14b3dba5bd56 1177 //pastthrough mode
faker_71 49:14b3dba5bd56 1178 SendWEB(); // send web page
faker_71 49:14b3dba5bd56 1179 pc.printf("\n++++++++++ webbuff clear ++++++++++\r\n");
faker_71 49:14b3dba5bd56 1180
faker_71 49:14b3dba5bd56 1181 memset(webbuff, '\0', sizeof(webbuff));
faker_71 49:14b3dba5bd56 1182 sendcheck();
faker_71 49:14b3dba5bd56 1183 }
faker_71 49:14b3dba5bd56 1184
nishimura_taku_pet 24:9481c8f56a49 1185
nishimura_taku_pet 24:9481c8f56a49 1186 void sendcheck()
nishimura_taku_pet 24:9481c8f56a49 1187 {
nishimura_taku_pet 24:9481c8f56a49 1188 weberror=1;
nishimura_taku_pet 24:9481c8f56a49 1189 timeout=500;
nishimura_taku_pet 24:9481c8f56a49 1190 getcount=24;
nishimura_taku_pet 24:9481c8f56a49 1191 time2.reset();
nishimura_taku_pet 24:9481c8f56a49 1192 time2.start();
nishimura_taku_pet 24:9481c8f56a49 1193
nishimura_taku_pet 24:9481c8f56a49 1194 /*
nishimura_taku_pet 24:9481c8f56a49 1195 while(weberror==1 && time2.read() <5) {
nishimura_taku_pet 24:9481c8f56a49 1196 getreply();
nishimura_taku_pet 24:9481c8f56a49 1197 if (strstr(replybuff, "SEND OK") != NULL) {
nishimura_taku_pet 24:9481c8f56a49 1198 weberror=0; // wait for valid SEND OK
nishimura_taku_pet 24:9481c8f56a49 1199 }
nishimura_taku_pet 24:9481c8f56a49 1200 }
nishimura_taku_pet 24:9481c8f56a49 1201 */
nishimura_taku_pet 24:9481c8f56a49 1202 if(weberror==1) { // restart connection
nishimura_taku_pet 24:9481c8f56a49 1203 strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
nishimura_taku_pet 24:9481c8f56a49 1204 timeout=500;
nishimura_taku_pet 24:9481c8f56a49 1205 getcount=10;
nishimura_taku_pet 24:9481c8f56a49 1206 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1207 getreply();
nishimura_taku_pet 24:9481c8f56a49 1208 pc.printf(replybuff);
nishimura_taku_pet 24:9481c8f56a49 1209 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
nishimura_taku_pet 24:9481c8f56a49 1210 timeout=500;
nishimura_taku_pet 24:9481c8f56a49 1211 getcount=10;
nishimura_taku_pet 24:9481c8f56a49 1212 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1213 getreply();
nishimura_taku_pet 24:9481c8f56a49 1214 pc.printf(replybuff);
nishimura_taku_pet 24:9481c8f56a49 1215 } else {
nishimura_taku_pet 24:9481c8f56a49 1216 sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
nishimura_taku_pet 24:9481c8f56a49 1217 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1218 getreply();
nishimura_taku_pet 24:9481c8f56a49 1219 pc.printf(replybuff);
nishimura_taku_pet 24:9481c8f56a49 1220 }
nishimura_taku_pet 24:9481c8f56a49 1221 time2.reset();
nishimura_taku_pet 24:9481c8f56a49 1222 }
nishimura_taku_pet 24:9481c8f56a49 1223
nishimura_taku_pet 24:9481c8f56a49 1224 // Reads and processes GET and POST web data
nishimura_taku_pet 24:9481c8f56a49 1225 void ReadWebData()
nishimura_taku_pet 24:9481c8f56a49 1226 {
nishimura_taku_pet 24:9481c8f56a49 1227 pc.printf("+++++++++++++++++Read Web Data+++++++++++++++++++++\r\n");
nishimura_taku_pet 30:c80da0ecc260 1228 ThisThread::sleep_for(200);
nishimura_taku_pet 24:9481c8f56a49 1229 esp.attach(NULL);
nishimura_taku_pet 24:9481c8f56a49 1230 ount=0;
nishimura_taku_pet 24:9481c8f56a49 1231 DataRX=0;
nishimura_taku_pet 24:9481c8f56a49 1232 weberror=0;
nishimura_taku_pet 24:9481c8f56a49 1233 memset(webdata, '\0', sizeof(webdata));
nishimura_taku_pet 24:9481c8f56a49 1234 int x = strcspn (webbuff,"+");
nishimura_taku_pet 24:9481c8f56a49 1235 if(x) {
nishimura_taku_pet 24:9481c8f56a49 1236 strcpy(webdata, webbuff + x);
nishimura_taku_pet 24:9481c8f56a49 1237 weberror=0;
nishimura_taku_pet 24:9481c8f56a49 1238 int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
nishimura_taku_pet 24:9481c8f56a49 1239 //int i=0;
nishimura_taku_pet 24:9481c8f56a49 1240 pc.printf("+++++++++++++++++succed rec begin+++++++++++++++++++++\r\n");
nishimura_taku_pet 24:9481c8f56a49 1241 pc.printf("%s",webdata);
nishimura_taku_pet 24:9481c8f56a49 1242 pc.printf("+++++++++++++++++succed rec end+++++++++++++++++++++\r\n");
faker_71 48:7be823ac7eb8 1243
faker_71 48:7be823ac7eb8 1244 if( strstr(webdata, "responseBattery") != NULL ) {
faker_71 48:7be823ac7eb8 1245 click_flag = 0;
faker_71 48:7be823ac7eb8 1246 led4=!led4;
faker_71 48:7be823ac7eb8 1247 pc.printf("\r\n++++++++++++++mode = LEFT++++++++++++++\r\n");
faker_71 48:7be823ac7eb8 1248 sendpage2();
faker_71 48:7be823ac7eb8 1249 }
faker_71 51:622df065f7ff 1250 /*
faker_71 49:14b3dba5bd56 1251 if( strstr(webdata, "GO") != NULL ) {
faker_71 49:14b3dba5bd56 1252 sendpage3();
faker_71 49:14b3dba5bd56 1253
faker_71 49:14b3dba5bd56 1254 pc.printf("+++++++++++++++++前進+++++++++++++++++++++\r\n");
faker_71 49:14b3dba5bd56 1255 //delete avoi_thread; //障害物回避スレッド停止
faker_71 49:14b3dba5bd56 1256 //delete trace_thread; //ライントレーススレッド停止
faker_71 49:14b3dba5bd56 1257 run = ADVANCE; // 前進
faker_71 49:14b3dba5bd56 1258 mode = ADVANCE; // モード変更
faker_71 49:14b3dba5bd56 1259 display(); // ディスプレイ表示
faker_71 49:14b3dba5bd56 1260 //0826
faker_71 49:14b3dba5bd56 1261 led1=!led1;
faker_71 49:14b3dba5bd56 1262 led2=!led2;
faker_71 49:14b3dba5bd56 1263 led3=!led3;
faker_71 49:14b3dba5bd56 1264 click_flag = 0;
faker_71 49:14b3dba5bd56 1265 //0826
faker_71 49:14b3dba5bd56 1266 }
faker_71 51:622df065f7ff 1267 */
faker_71 48:7be823ac7eb8 1268
nishimura_taku_pet 43:243c1455f88a 1269 if( strstr(webdata, "Normal") != NULL ) {
nishimura_taku_pet 43:243c1455f88a 1270 pc.printf("++++++++++++++++++Normal++++++++++++++++++++");
nishimura_taku_pet 43:243c1455f88a 1271 mode = SPEED; // スピードモード
nishimura_taku_pet 43:243c1455f88a 1272 flag_sp = 0;
nishimura_taku_pet 43:243c1455f88a 1273 display(); // ディスプレイ表示
nishimura_taku_pet 43:243c1455f88a 1274 mode = beforeMode; // 現在のモードに前回のモードを設定
nishimura_taku_pet 43:243c1455f88a 1275 }else if( strstr(webdata, "VeryFast") != NULL ) {
nishimura_taku_pet 43:243c1455f88a 1276 pc.printf("+++++++++++++++++++VeryFast+++++++++++++++++++");
nishimura_taku_pet 43:243c1455f88a 1277 mode = SPEED; // スピードモード
nishimura_taku_pet 43:243c1455f88a 1278 flag_sp = 2;
nishimura_taku_pet 43:243c1455f88a 1279 display(); // ディスプレイ表示
nishimura_taku_pet 43:243c1455f88a 1280 mode = beforeMode; // 現在のモードに前回のモードを設定
nishimura_taku_pet 43:243c1455f88a 1281 }else if( strstr(webdata, "Fast") != NULL ) {
nishimura_taku_pet 43:243c1455f88a 1282 pc.printf("++++++++++++++++++++Fast++++++++++++++++++");
nishimura_taku_pet 43:243c1455f88a 1283 mode = SPEED; // スピードモード
nishimura_taku_pet 43:243c1455f88a 1284 flag_sp = 1;
nishimura_taku_pet 43:243c1455f88a 1285 display(); // ディスプレイ表示
nishimura_taku_pet 43:243c1455f88a 1286 mode = beforeMode; // 現在のモードに前回のモードを設定
nishimura_taku_pet 43:243c1455f88a 1287 }else{
nishimura_taku_pet 43:243c1455f88a 1288 beforeMode = mode;
nishimura_taku_pet 43:243c1455f88a 1289 }
faker_71 51:622df065f7ff 1290
nishimura_taku_pet 24:9481c8f56a49 1291 if( strstr(webdata, "GO") != NULL ) {
faker_71 49:14b3dba5bd56 1292 sendpage3();
faker_71 49:14b3dba5bd56 1293
nishimura_taku_pet 24:9481c8f56a49 1294 pc.printf("+++++++++++++++++前進+++++++++++++++++++++\r\n");
yangtzuli 37:0cde453dce7d 1295 //delete avoi_thread; //障害物回避スレッド停止
yangtzuli 37:0cde453dce7d 1296 //delete trace_thread; //ライントレーススレッド停止
molberry 35:4cda290bdb87 1297 run = ADVANCE; // 前進
molberry 35:4cda290bdb87 1298 mode = ADVANCE; // モード変更
molberry 35:4cda290bdb87 1299 display(); // ディスプレイ表示
faker_71 49:14b3dba5bd56 1300 //0826
faker_71 49:14b3dba5bd56 1301 led1=!led1;
faker_71 49:14b3dba5bd56 1302 led2=!led2;
faker_71 49:14b3dba5bd56 1303 led3=!led3;
faker_71 49:14b3dba5bd56 1304 click_flag = 0;
faker_71 49:14b3dba5bd56 1305 //0826
nishimura_taku_pet 24:9481c8f56a49 1306 }
faker_71 51:622df065f7ff 1307
nishimura_taku_pet 24:9481c8f56a49 1308 if( strstr(webdata, "LEFT") != NULL ) {
faker_71 51:622df065f7ff 1309 sendpage3();
faker_71 51:622df065f7ff 1310
nishimura_taku_pet 24:9481c8f56a49 1311 pc.printf("+++++++++++++++++左折+++++++++++++++++++++\r\n");
yangtzuli 37:0cde453dce7d 1312 //delete avoi_thread; //障害物回避スレッド停止
yangtzuli 37:0cde453dce7d 1313 //delete trace_thread; //ライントレーススレッド停止
molberry 35:4cda290bdb87 1314 run = LEFT; // 左折
molberry 35:4cda290bdb87 1315 mode = LEFT; // モード変更
molberry 35:4cda290bdb87 1316 display(); // ディスプレイ表示
nishimura_taku_pet 24:9481c8f56a49 1317 }
nishimura_taku_pet 24:9481c8f56a49 1318
nishimura_taku_pet 24:9481c8f56a49 1319 if( strstr(webdata, "STOP") != NULL ) {
faker_71 51:622df065f7ff 1320 sendpage3();
faker_71 51:622df065f7ff 1321
nishimura_taku_pet 24:9481c8f56a49 1322 pc.printf("+++++++++++++++++停止+++++++++++++++++++++\r\n");
yangtzuli 37:0cde453dce7d 1323 //delete avoi_thread; //障害物回避スレッド停止
yangtzuli 37:0cde453dce7d 1324 //delete trace_thread; //ライントレーススレッド停止
molberry 35:4cda290bdb87 1325 run = STOP; // 停止
molberry 35:4cda290bdb87 1326 mode = STOP; // モード変更
molberry 35:4cda290bdb87 1327 display(); // ディスプレイ表示
nishimura_taku_pet 24:9481c8f56a49 1328 }
nishimura_taku_pet 24:9481c8f56a49 1329
nishimura_taku_pet 24:9481c8f56a49 1330 if( strstr(webdata, "RIGHT") != NULL ) {
faker_71 51:622df065f7ff 1331 sendpage3();
faker_71 51:622df065f7ff 1332
nishimura_taku_pet 24:9481c8f56a49 1333 pc.printf("+++++++++++++++++右折+++++++++++++++++++++\r\n");
yangtzuli 37:0cde453dce7d 1334 //delete avoi_thread; //障害物回避スレッド停止
yangtzuli 37:0cde453dce7d 1335 //delete trace_thread; //ライントレーススレッド停止
molberry 35:4cda290bdb87 1336 run = RIGHT; // 右折
molberry 35:4cda290bdb87 1337 mode = RIGHT; // モード変更
molberry 35:4cda290bdb87 1338 display(); // ディスプレイ表示
nishimura_taku_pet 24:9481c8f56a49 1339 }
nishimura_taku_pet 24:9481c8f56a49 1340
nishimura_taku_pet 24:9481c8f56a49 1341 if( strstr(webdata, "BACK") != NULL ) {
faker_71 51:622df065f7ff 1342 sendpage3();
faker_71 51:622df065f7ff 1343
nishimura_taku_pet 24:9481c8f56a49 1344 pc.printf("+++++++++++++++++後進+++++++++++++++++++++\r\n");
yangtzuli 37:0cde453dce7d 1345 //delete avoi_thread; //障害物回避スレッド停止
yangtzuli 37:0cde453dce7d 1346 //delete trace_thread; //ライントレーススレッド停止
molberry 35:4cda290bdb87 1347 run = BACK; // 後進
molberry 35:4cda290bdb87 1348 mode = BACK; // モード変更
molberry 35:4cda290bdb87 1349 display(); // ディスプレイ表示
nishimura_taku_pet 24:9481c8f56a49 1350 }
nishimura_taku_pet 24:9481c8f56a49 1351 pc.printf("+++++++++++++++++succed+++++++++++++++++++++");
nishimura_taku_pet 24:9481c8f56a49 1352
nishimura_taku_pet 24:9481c8f56a49 1353 if( strstr(webdata, "AVOIDANCE") != NULL ) {
faker_71 51:622df065f7ff 1354 sendpage3();
faker_71 51:622df065f7ff 1355
nishimura_taku_pet 24:9481c8f56a49 1356 pc.printf("+++++++++++++++++AVOIDANCE+++++++++++++++++++++");
nishimura_taku_pet 26:0badbc9f9cb3 1357 if(avoi_thread->get_state() == Thread::Deleted) {
molberry 35:4cda290bdb87 1358 delete avoi_thread; //障害物回避スレッド停止
nishimura_taku_pet 26:0badbc9f9cb3 1359 avoi_thread = new Thread(avoidance);
nishimura_taku_pet 26:0badbc9f9cb3 1360 avoi_thread -> set_priority(osPriorityHigh);
nishimura_taku_pet 26:0badbc9f9cb3 1361 }
nishimura_taku_pet 24:9481c8f56a49 1362 mode=AVOIDANCE;
nishimura_taku_pet 26:0badbc9f9cb3 1363 run = ADVANCE;
nishimura_taku_pet 26:0badbc9f9cb3 1364 display(); // ディスプレイ表示
nishimura_taku_pet 24:9481c8f56a49 1365 }
nishimura_taku_pet 24:9481c8f56a49 1366 if( strstr(webdata, "LINE_TRACE") != NULL ) {
faker_71 51:622df065f7ff 1367 sendpage3();
faker_71 51:622df065f7ff 1368
nishimura_taku_pet 24:9481c8f56a49 1369 pc.printf("+++++++++++++++++LINET RACE+++++++++++++++++++++");
nishimura_taku_pet 24:9481c8f56a49 1370 pc.printf("mode = LINE_TRACE\r\n");
nishimura_taku_pet 26:0badbc9f9cb3 1371 if(trace_thread->get_state() == Thread::Deleted) {
molberry 35:4cda290bdb87 1372 delete trace_thread; //ライントレーススレッド停止
nishimura_taku_pet 26:0badbc9f9cb3 1373 trace_thread = new Thread(trace);
nishimura_taku_pet 26:0badbc9f9cb3 1374 trace_thread -> set_priority(osPriorityHigh);
nishimura_taku_pet 26:0badbc9f9cb3 1375 }
nishimura_taku_pet 24:9481c8f56a49 1376 mode=LINE_TRACE;
nishimura_taku_pet 26:0badbc9f9cb3 1377 display(); // ディスプレイ表示
nishimura_taku_pet 24:9481c8f56a49 1378 }
nishimura_taku_pet 26:0badbc9f9cb3 1379 if(mode != LINE_TRACE && trace_thread->get_state() != Thread::Deleted){
nishimura_taku_pet 26:0badbc9f9cb3 1380 trace_thread->terminate();
nishimura_taku_pet 26:0badbc9f9cb3 1381 }
nishimura_taku_pet 26:0badbc9f9cb3 1382 if(mode != AVOIDANCE && avoi_thread->get_state() != Thread::Deleted){
nishimura_taku_pet 26:0badbc9f9cb3 1383 avoi_thread->terminate();
nishimura_taku_pet 31:3665282561d6 1384 servo.pulsewidth_us(1450); // サーボを中央位置に戻す
nishimura_taku_pet 26:0badbc9f9cb3 1385 }
nishimura_taku_pet 24:9481c8f56a49 1386 sprintf(channel, "%d",linkID);
nishimura_taku_pet 24:9481c8f56a49 1387 if (strstr(webdata, "GET") != NULL) {
nishimura_taku_pet 24:9481c8f56a49 1388 servreq=1;
nishimura_taku_pet 24:9481c8f56a49 1389 }
nishimura_taku_pet 24:9481c8f56a49 1390 if (strstr(webdata, "POST") != NULL) {
nishimura_taku_pet 24:9481c8f56a49 1391 servreq=1;
nishimura_taku_pet 24:9481c8f56a49 1392 }
nishimura_taku_pet 24:9481c8f56a49 1393 webcounter++;
nishimura_taku_pet 24:9481c8f56a49 1394 sprintf(webcount, "%d",webcounter);
nishimura_taku_pet 24:9481c8f56a49 1395 } else {
nishimura_taku_pet 24:9481c8f56a49 1396 memset(webbuff, '\0', sizeof(webbuff));
nishimura_taku_pet 24:9481c8f56a49 1397 esp.attach(&callback);
nishimura_taku_pet 24:9481c8f56a49 1398 weberror=1;
nishimura_taku_pet 24:9481c8f56a49 1399 }
nishimura_taku_pet 24:9481c8f56a49 1400 }
nishimura_taku_pet 24:9481c8f56a49 1401 // Starts and restarts webserver if errors detected.
nishimura_taku_pet 24:9481c8f56a49 1402 void startserver()
nishimura_taku_pet 24:9481c8f56a49 1403 {
nishimura_taku_pet 24:9481c8f56a49 1404 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
nishimura_taku_pet 24:9481c8f56a49 1405 strcpy(cmdbuff,"AT+RST\r\n");
nishimura_taku_pet 24:9481c8f56a49 1406 timeout=8000;
nishimura_taku_pet 24:9481c8f56a49 1407 getcount=1000;
nishimura_taku_pet 24:9481c8f56a49 1408 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1409 getreply();
nishimura_taku_pet 24:9481c8f56a49 1410 pc.printf(replybuff);
nishimura_taku_pet 24:9481c8f56a49 1411 pc.printf("%d",ount);
nishimura_taku_pet 24:9481c8f56a49 1412 if (strstr(replybuff, "OK") != NULL) {
nishimura_taku_pet 24:9481c8f56a49 1413 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
nishimura_taku_pet 24:9481c8f56a49 1414 strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections.
nishimura_taku_pet 24:9481c8f56a49 1415 timeout=500;
nishimura_taku_pet 24:9481c8f56a49 1416 getcount=20;
nishimura_taku_pet 24:9481c8f56a49 1417 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1418 getreply();
nishimura_taku_pet 24:9481c8f56a49 1419 pc.printf(replybuff);
nishimura_taku_pet 24:9481c8f56a49 1420 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
nishimura_taku_pet 24:9481c8f56a49 1421 timeout=500;
nishimura_taku_pet 24:9481c8f56a49 1422 getcount=20;
nishimura_taku_pet 24:9481c8f56a49 1423 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1424 getreply();
nishimura_taku_pet 24:9481c8f56a49 1425 pc.printf(replybuff);
nishimura_taku_pet 33:a6f1090e0174 1426 ThisThread::sleep_for(500);
nishimura_taku_pet 24:9481c8f56a49 1427 sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
nishimura_taku_pet 24:9481c8f56a49 1428 timeout=500;
nishimura_taku_pet 24:9481c8f56a49 1429 getcount=50;
nishimura_taku_pet 24:9481c8f56a49 1430 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1431 getreply();
nishimura_taku_pet 24:9481c8f56a49 1432 pc.printf(replybuff);
nishimura_taku_pet 25:8ed98982faa7 1433 ThisThread::sleep_for(5000);
nishimura_taku_pet 24:9481c8f56a49 1434 pc.printf("\n Getting Server IP \r\n");
nishimura_taku_pet 24:9481c8f56a49 1435 strcpy(cmdbuff, "AT+CIFSR\r\n");
nishimura_taku_pet 24:9481c8f56a49 1436 timeout=2500;
nishimura_taku_pet 24:9481c8f56a49 1437 getcount=200;
nishimura_taku_pet 24:9481c8f56a49 1438 while(weberror==0) {
nishimura_taku_pet 24:9481c8f56a49 1439 SendCMD();
nishimura_taku_pet 24:9481c8f56a49 1440 getreply();
nishimura_taku_pet 24:9481c8f56a49 1441 if (strstr(replybuff, "0.0.0.0") == NULL) {
nishimura_taku_pet 24:9481c8f56a49 1442 weberror=1; // wait for valid IP
nishimura_taku_pet 24:9481c8f56a49 1443 }
nishimura_taku_pet 24:9481c8f56a49 1444 }
nishimura_taku_pet 24:9481c8f56a49 1445 pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
nishimura_taku_pet 24:9481c8f56a49 1446 pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
nishimura_taku_pet 24:9481c8f56a49 1447 replybuff[strlen(replybuff)-1] = '\0';
nishimura_taku_pet 24:9481c8f56a49 1448 //char* IP = replybuff + 5;
nishimura_taku_pet 24:9481c8f56a49 1449 sprintf(webdata,"%s", replybuff);
nishimura_taku_pet 24:9481c8f56a49 1450 pc.printf(webdata);
nishimura_taku_pet 24:9481c8f56a49 1451 led2=1;
nishimura_taku_pet 24:9481c8f56a49 1452 bufflen=200;
nishimura_taku_pet 24:9481c8f56a49 1453 //bufflen=100;
nishimura_taku_pet 24:9481c8f56a49 1454 ount=0;
nishimura_taku_pet 24:9481c8f56a49 1455 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
nishimura_taku_pet 33:a6f1090e0174 1456 setup();
nishimura_taku_pet 24:9481c8f56a49 1457 esp.attach(&callback);
nishimura_taku_pet 24:9481c8f56a49 1458 } else {
nishimura_taku_pet 24:9481c8f56a49 1459 pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
nishimura_taku_pet 24:9481c8f56a49 1460 led1=1;
nishimura_taku_pet 24:9481c8f56a49 1461 led2=1;
nishimura_taku_pet 24:9481c8f56a49 1462 led3=1;
nishimura_taku_pet 24:9481c8f56a49 1463 led4=1;
nishimura_taku_pet 24:9481c8f56a49 1464 while(1) {
nishimura_taku_pet 24:9481c8f56a49 1465 led1=!led1;
nishimura_taku_pet 24:9481c8f56a49 1466 led2=!led2;
nishimura_taku_pet 24:9481c8f56a49 1467 led3=!led3;
nishimura_taku_pet 24:9481c8f56a49 1468 led4=!led4;
nishimura_taku_pet 25:8ed98982faa7 1469 ThisThread::sleep_for(1000);
nishimura_taku_pet 24:9481c8f56a49 1470 }
nishimura_taku_pet 24:9481c8f56a49 1471 }
nishimura_taku_pet 24:9481c8f56a49 1472 time2.reset();
nishimura_taku_pet 24:9481c8f56a49 1473 time2.start();
nishimura_taku_pet 24:9481c8f56a49 1474 }
nishimura_taku_pet 24:9481c8f56a49 1475 // ESP Command data send
nishimura_taku_pet 24:9481c8f56a49 1476 void SendCMD()
nishimura_taku_pet 24:9481c8f56a49 1477 {
nishimura_taku_pet 24:9481c8f56a49 1478 esp.printf("%s", cmdbuff);
nishimura_taku_pet 24:9481c8f56a49 1479 }
nishimura_taku_pet 24:9481c8f56a49 1480 // Get Command and ESP status replies
nishimura_taku_pet 24:9481c8f56a49 1481 void getreply()
nishimura_taku_pet 24:9481c8f56a49 1482 {
nishimura_taku_pet 24:9481c8f56a49 1483 memset(replybuff, '\0', sizeof(replybuff));
nishimura_taku_pet 24:9481c8f56a49 1484 time1.reset();
nishimura_taku_pet 24:9481c8f56a49 1485 time1.start();
nishimura_taku_pet 24:9481c8f56a49 1486 replycount=0;
nishimura_taku_pet 24:9481c8f56a49 1487 while(time1.read_ms()< timeout && replycount < getcount) {
nishimura_taku_pet 24:9481c8f56a49 1488 if(esp.readable()) {
nishimura_taku_pet 24:9481c8f56a49 1489 replybuff[replycount] = esp.getc();
nishimura_taku_pet 24:9481c8f56a49 1490 replycount++;
nishimura_taku_pet 24:9481c8f56a49 1491 }
nishimura_taku_pet 24:9481c8f56a49 1492 }
nishimura_taku_pet 24:9481c8f56a49 1493 time1.stop();
nishimura_taku_pet 24:9481c8f56a49 1494 }
nishimura_taku_pet 24:9481c8f56a49 1495
tomotsugu 8:a47dbf4fa455 1496 /* mainスレッド */
yangtzuli 0:0d0037aabe41 1497 int main() {
tomotsugu 8:a47dbf4fa455 1498 /* 初期設定 */
nishimura_taku_pet 43:243c1455f88a 1499 wifi_thread = new Thread(wifi);
nishimura_taku_pet 43:243c1455f88a 1500 wifi_thread -> set_priority(osPriorityHigh);
faker_71 51:622df065f7ff 1501 // pc.printf("\n++++++++++ begin ++++++++++\r\n");
faker_71 51:622df065f7ff 1502 //setup();
nishimura_taku_pet 16:ffc732a3cf92 1503 avoi_thread = new Thread(avoidance);
nishimura_taku_pet 16:ffc732a3cf92 1504 avoi_thread->terminate();
nishimura_taku_pet 16:ffc732a3cf92 1505 trace_thread = new Thread(trace);
nishimura_taku_pet 16:ffc732a3cf92 1506 trace_thread->terminate();
tomotsugu 8:a47dbf4fa455 1507 mode = READY; // 現在待ちモード
tomotsugu 8:a47dbf4fa455 1508 beforeMode = READY; // 前回待ちモード
tomotsugu 8:a47dbf4fa455 1509 run = STOP; // 停止
tomotsugu 8:a47dbf4fa455 1510 flag_sp = NORMAL; // スピード(普通)
tomotsugu 10:d193030ce672 1511 lcd.setBacklight(TextLCD::LightOn); // バックライトON
nishimura_taku_pet 33:a6f1090e0174 1512 lcd.setAddress(0,1);
nishimura_taku_pet 33:a6f1090e0174 1513 lcd.printf("Mode:SetUp");
nishimura_taku_pet 33:a6f1090e0174 1514 //display(); // ディスプレイ表示
yangtzuli 2:38825726cb1b 1515
yangtzuli 0:0d0037aabe41 1516 while(1){
tomotsugu 8:a47dbf4fa455 1517 bChange(); // バッテリー残量更新
yangtzuli 0:0d0037aabe41 1518 }
yangtzuli 0:0d0037aabe41 1519 }