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