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

Dependencies:   RemoteIR TextLCD

Committer:
nishimura_taku_pet
Date:
Wed Aug 05 07:42:57 2020 +0000
Revision:
24:9481c8f56a49
Parent:
23:8c862b55fa1f
Child:
25:8ed98982faa7
wifi add

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