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

Dependencies:   RemoteIR TextLCD

Committer:
faker_71
Date:
Tue Aug 25 05:15:48 2020 +0000
Revision:
48:7be823ac7eb8
Parent:
47:8a5a4275480a
Child:
49:14b3dba5bd56
0825

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