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