程式是來自前一篇文章:[OTTO Ninja] 把使用RemoteXY控制arduino程式改成Web控制
程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <WebSocketsServer.h> #include <Servo.h> /* ========== WiFi AP ========== */ const char* ssid = "OTTO NINJA"; const char* password = "12345678"; /* ========== Web ========== */ ESP8266WebServer server(80); WebSocketsServer webSocket(81); /* ========== Calibration SETTINGS(沿用你原本) ========== */ // Feet forward/back speed int LFFWRS= 20; int RFFWRS= 20; int LFBWRS= 20; int RFBWRS= 20; // Walk stand (legs) int LA0= 60; int RA0= 120; // Walk tilt positions int LATL= 100; int RATL= 175; int LATR= 5; int RATR= 80; // Roll (legs) int LA1= 180; int RA1= 0; /* ========== Timing ========== */ unsigned long currentmillis1 = 0; // Mode: 0=WALK, 1=ROLL int ModeCounter = 0; /* ========== Pinout(沿用你原本) ========== */ const uint8_t ServoLeftFootPin = 13; // D7 const uint8_t ServoLeftLegPin = 15; // D8 const uint8_t ServoRightFootPin = 0; // D3 const uint8_t ServoRightLegPin = 2; // D4 // 如果你「沒有手」:下面三個可以不接、不用管(程式不會主動動它們) const uint8_t ServoLeftArmPin = 16; // D0 const uint8_t ServoRightArmPin = 3; // RX (GPIO3) const uint8_t ServoHeadPin = 1; // TX (GPIO1) Servo myservoLeftFoot; Servo myservoLeftLeg; Servo myservoRightFoot; Servo myservoRightLeg; Servo myservoLeftArm; Servo myservoRightArm; Servo myservoHead; /* ========== Web 控制輸入(取代 RemoteXY) ========== */ int CTRL_JX = 0; // -100..100 int CTRL_JY = 0; // -100..100 /* ========== HTML Page(保留原本按鈕 + 新增虛擬搖桿) ========== */ const char MAIN_page[] PROGMEM = R"=====(<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>OTTO NINJA Web</title> <style> body{font-family:Arial;text-align:center;background:#f4f6f8;margin:0;padding:16px;} .card{max-width:560px;margin:0 auto;background:#fff;border-radius:14px;padding:16px;box-shadow:0 8px 24px rgba(0,0,0,.08);} button{width:150px;height:54px;font-size:16px;margin:6px;border:none;border-radius:12px;background:#2d7ef7;color:#fff} button.gray{background:#6c757d} button.green{background:#00a86b} button.red{background:#e74c3c} .row{display:flex;justify-content:center;flex-wrap:wrap} .box{margin:10px;padding:8px;border-radius:8px;font-weight:bold;background:#eee;} .small{font-size:12px;color:#666;line-height:1.6} /* ===== 虛擬搖桿樣式 ===== */ .joystick-wrap{ margin:20px auto; width:200px; height:200px; position:relative; user-select:none; -webkit-user-select:none; } .joystick-base{ width:200px; height:200px; border-radius:50%; background:#ddd; position:absolute; left:0; top:0; } .joystick-stick{ width:80px; height:80px; border-radius:50%; background:#2d7ef7; position:absolute; left:60px; top:60px; touch-action:none; /* 重要:避免手機滑動造成頁面捲動 */ } </style> </head> <body> <div class="card"> <h2>OTTO NINJA(Web 控制)</h2> <div id="statusBox" class="box">狀態:未連線</div> <div id="modeBox" class="box">模式:未知</div> <div id="actBox" class="box">動作:未知</div> <div class="row"> <button class="gray" onclick="connectWS()">連線</button> </div> <div class="row"> <button class="green" onclick="sendCmd('mode_walk')">Walk(人形)</button> <button class="green" onclick="sendCmd('mode_roll')">Roll(車形)</button> </div> <!-- 原本按鈕:保留 --> <div class="row"> <button onclick="move(0, 100)">前進</button> </div> <div class="row"> <button onclick="move(-60, 0)">左轉</button> <button class="red" onclick="move(0,0)">停止</button> <button onclick="move(60, 0)">右轉</button> </div> <div class="row"> <button onclick="move(0, -100)">後退</button> </div> <!-- 新增:虛擬搖桿 --> <div class="joystick-wrap"> <div class="joystick-base"></div> <div id="joy" class="joystick-stick"></div> </div> <p class="small"> 連 Wi-Fi:OTTO NINJA / 12345678<br> 瀏覽器:192.168.4.1 </p> </div> <script> let ws=null; const statusBox=document.getElementById("statusBox"); const modeBox=document.getElementById("modeBox"); const actBox=document.getElementById("actBox"); function setStatus(t,c){statusBox.innerText="狀態:"+t; statusBox.style.background=c;} function connectWS(){ ws=new WebSocket("ws://"+location.hostname+":81/"); setStatus("連線中…","#ffeaa7"); ws.onopen=()=>setStatus("已連線","#55efc4"); ws.onclose=()=>setStatus("中斷","#fab1a0"); ws.onerror=()=>setStatus("錯誤","#ff7675"); ws.onmessage=(e)=>handleMsg(e.data); } function sendCmd(cmd){ if(ws && ws.readyState===1) ws.send(cmd); else alert("請先按「連線」"); } function move(x,y){ sendCmd("move "+x+" "+y); } function handleMsg(m){ if(m.startsWith("MODE:")) modeBox.innerText="模式:"+m.substring(5); if(m.startsWith("ACT:")) actBox.innerText ="動作:"+m.substring(4); } /* ===== 虛擬搖桿(Virtual Joystick) ===== */ const joy = document.getElementById("joy"); const joyWrap = joy.parentElement; let dragging = false; let center = { x: 100, y: 100 }; let maxDist = 70; function sendJoy(x, y){ x = Math.round(x); y = Math.round(y); sendCmd("move " + x + " " + y); } function resetJoy(){ joy.style.left = "60px"; joy.style.top = "60px"; sendJoy(0,0); } joy.addEventListener("pointerdown", e=>{ dragging = true; joy.setPointerCapture(e.pointerId); }); window.addEventListener("pointermove", e=>{ if(!dragging) return; const rect = joyWrap.getBoundingClientRect(); let x = e.clientX - rect.left - center.x; let y = e.clientY - rect.top - center.y; const dist = Math.sqrt(x*x + y*y); if(dist > maxDist){ x = x / dist * maxDist; y = y / dist * maxDist; } joy.style.left = (center.x + x - 40) + "px"; joy.style.top = (center.y + y - 40) + "px"; // 轉換成 -100 ~ 100(Y 軸反向) let jx = (x / maxDist) * 100; let jy = (-y / maxDist) * 100; sendJoy(jx, jy); }); window.addEventListener("pointerup", e=>{ if(!dragging) return; dragging = false; resetJoy(); }); </script> </body> </html>)====="; /* ========== 工具:WebSocket 廣播(避免 String& 暫時值編譯錯) ========== */ void wsBroadcast(const String& s){ String msg = s; webSocket.broadcastTXT(msg); } /* ========== 動作函式(沿用你的結構) ========== */ void NinjaStop(){ myservoLeftFoot.detach(); myservoRightFoot.detach(); myservoLeftLeg.detach(); myservoRightLeg.detach(); } void NinjaSetWalk(){ myservoLeftArm.attach(ServoLeftArmPin, 544, 2400); myservoRightArm.attach(ServoRightArmPin, 544, 2400); myservoLeftArm.write(90); myservoRightArm.write(90); delay(200); myservoLeftArm.detach(); myservoRightArm.detach(); myservoLeftLeg.attach(ServoLeftLegPin, 544, 2400); myservoRightLeg.attach(ServoRightLegPin, 544, 2400); myservoLeftLeg.write(LA0); myservoRightLeg.write(RA0); delay(300); myservoLeftLeg.detach(); myservoRightLeg.detach(); myservoLeftArm.attach(ServoLeftArmPin, 544, 2400); myservoRightArm.attach(ServoRightArmPin, 544, 2400); myservoLeftArm.write(180); myservoRightArm.write(0); delay(300); myservoLeftArm.detach(); myservoRightArm.detach(); } void NinjaSetRoll(){ myservoLeftArm.attach(ServoLeftArmPin, 544, 2400); myservoRightArm.attach(ServoRightArmPin, 544, 2400); myservoLeftArm.write(90); myservoRightArm.write(90); delay(200); myservoLeftArm.detach(); myservoRightArm.detach(); myservoLeftLeg.attach(ServoLeftLegPin, 544, 2400); myservoRightLeg.attach(ServoRightLegPin, 544, 2400); myservoLeftLeg.write(LA1); myservoRightLeg.write(RA1); delay(300); myservoLeftLeg.detach(); myservoRightLeg.detach(); myservoLeftArm.attach(ServoLeftArmPin, 544, 2400); myservoRightArm.attach(ServoRightArmPin, 544, 2400); myservoLeftArm.write(180); myservoRightArm.write(0); delay(300); myservoLeftArm.detach(); myservoRightArm.detach(); } /* 重要:停止姿勢時要確保有 attach,否則 detach 後 write 不會動 */ void NinjaWalkStop(){ myservoLeftFoot.attach(ServoLeftFootPin, 544, 2400); myservoRightFoot.attach(ServoRightFootPin, 544, 2400); myservoLeftLeg.attach(ServoLeftLegPin, 544, 2400); myservoRightLeg.attach(ServoRightLegPin, 544, 2400); myservoLeftFoot.write(90); myservoRightFoot.write(90); myservoLeftLeg.write(LA0); myservoRightLeg.write(RA0); } void NinjaRollStop(){ myservoLeftFoot.attach(ServoLeftFootPin, 544, 2400); myservoRightFoot.attach(ServoRightFootPin, 544, 2400); myservoLeftFoot.write(90); myservoRightFoot.write(90); myservoLeftFoot.detach(); myservoRightFoot.detach(); } /* ========== WebSocket event:接收控制命令 ========== */ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length){ if(type == WStype_CONNECTED){ wsBroadcast("MODE:" + String(ModeCounter==0 ? "WALK" : "ROLL")); wsBroadcast("ACT:STOP"); return; } if(type != WStype_TEXT) return; String cmd = String((char*)payload).substring(0, length); cmd.trim(); if(cmd == "mode_walk"){ NinjaSetWalk(); ModeCounter = 0; wsBroadcast("MODE:WALK"); return; } if(cmd == "mode_roll"){ NinjaSetRoll(); ModeCounter = 1; wsBroadcast("MODE:ROLL"); return; } if(cmd.startsWith("move")){ int sp1 = cmd.indexOf(' '); int sp2 = cmd.lastIndexOf(' '); if(sp1 > 0 && sp2 > sp1){ CTRL_JX = cmd.substring(sp1+1, sp2).toInt(); CTRL_JY = cmd.substring(sp2+1).toInt(); CTRL_JX = constrain(CTRL_JX, -100, 100); CTRL_JY = constrain(CTRL_JY, -100, 100); } return; } } /* ========== Setup ========== */ void setup(){ Serial.begin(115200); // AP mode WiFi.mode(WIFI_AP); WiFi.softAP(ssid, password); // Web server.on("/", [](){ server.send_P(200, "text/html", MAIN_page); }); server.begin(); webSocket.begin(); webSocket.onEvent(webSocketEvent); // 初始站立(人形) NinjaWalkStop(); delay(300); NinjaStop(); Serial.println("OTTO NINJA Web Ready"); Serial.println(WiFi.softAPIP()); // 通常是 192.168.4.1 } /* ========== Loop ========== */ void loop(){ server.handleClient(); webSocket.loop(); // 動作狀態判斷(供網頁顯示) static String lastAct = ""; String act; bool idle = (abs(CTRL_JX) <= 10 && abs(CTRL_JY) <= 10); if(idle) act="STOP"; else if(abs(CTRL_JY) >= abs(CTRL_JX)) act = (CTRL_JY > 0) ? "FORWARD" : "BACK"; else act = (CTRL_JX > 0) ? "RIGHT" : "LEFT"; if(act != lastAct){ lastAct = act; wsBroadcast("ACT:" + act); } /* ===== 走路模式(沿用你原本演算法,把 RemoteXY 換成 CTRL_) ===== */ if(ModeCounter == 0){ if(idle){ NinjaWalkStop(); return; } if(CTRL_JY > 0){ int lt= map(CTRL_JX, 100, -100, 200, 700); int rt= map(CTRL_JX, 100, -100, 700, 200); int Interval1 = 250; int Interval2 = 250 + rt; int Interval3 = 250 + rt + 250; int Interval4 = 250 + rt + 250 + lt; int Interval5 = 250 + rt + 250 + lt + 50; if(millis() > currentmillis1 + (unsigned long)Interval5) currentmillis1 = millis(); if(millis() - currentmillis1 <= (unsigned long)Interval1){ myservoLeftLeg.attach(ServoLeftLegPin, 544, 2400); myservoRightLeg.attach(ServoRightLegPin, 544, 2400); myservoRightFoot.attach(ServoRightFootPin, 544, 2400); myservoLeftFoot.attach(ServoLeftFootPin, 544, 2400); myservoLeftLeg.write(LATR); myservoRightLeg.write(RATR); } if((millis() - currentmillis1 >= (unsigned long)Interval1) && (millis() - currentmillis1 <= (unsigned long)Interval2)){ myservoRightFoot.write(90 - RFFWRS); } if((millis() - currentmillis1 >= (unsigned long)Interval2) && (millis() - currentmillis1 <= (unsigned long)Interval3)){ myservoRightFoot.detach(); myservoLeftLeg.write(LATL); myservoRightLeg.write(RATL); } if((millis() - currentmillis1 >= (unsigned long)Interval3) && (millis() - currentmillis1 <= (unsigned long)Interval4)){ myservoLeftFoot.write(90 + LFFWRS); } if((millis() - currentmillis1 >= (unsigned long)Interval4) && (millis() - currentmillis1 <= (unsigned long)Interval5)){ myservoLeftFoot.detach(); } } if(CTRL_JY < 0){ int lt= map(CTRL_JX, 100, -100, 200, 700); int rt= map(CTRL_JX, 100, -100, 700, 200); int Interval1 = 250; int Interval2 = 250 + rt; int Interval3 = 250 + rt + 250; int Interval4 = 250 + rt + 250 + lt; int Interval5 = 250 + rt + 250 + lt + 50; if(millis() > currentmillis1 + (unsigned long)Interval5) currentmillis1 = millis(); if(millis() - currentmillis1 <= (unsigned long)Interval1){ myservoLeftLeg.attach(ServoLeftLegPin, 544, 2400); myservoRightLeg.attach(ServoRightLegPin, 544, 2400); myservoRightFoot.attach(ServoRightFootPin, 544, 2400); myservoLeftFoot.attach(ServoLeftFootPin, 544, 2400); myservoLeftLeg.write(LATR); myservoRightLeg.write(RATR); } if((millis() - currentmillis1 >= (unsigned long)Interval1) && (millis() - currentmillis1 <= (unsigned long)Interval2)){ myservoRightFoot.write(90 + RFBWRS); } if((millis() - currentmillis1 >= (unsigned long)Interval2) && (millis() - currentmillis1 <= (unsigned long)Interval3)){ myservoRightFoot.detach(); myservoLeftLeg.write(LATL); myservoRightLeg.write(RATL); } if((millis() - currentmillis1 >= (unsigned long)Interval3) && (millis() - currentmillis1 <= (unsigned long)Interval4)){ myservoLeftFoot.write(90 - LFBWRS); } if((millis() - currentmillis1 >= (unsigned long)Interval4) && (millis() - currentmillis1 <= (unsigned long)Interval5)){ myservoLeftFoot.detach(); } } } /* ===== 車形模式(ROLL):差速左右轉(沿用你原本) ===== */ if(ModeCounter == 1){ if(idle){ NinjaRollStop(); return; } myservoLeftFoot.attach(ServoLeftFootPin, 544, 2400); myservoRightFoot.attach(ServoRightFootPin, 544, 2400); int LWS= map(CTRL_JY, 100, -100, 135, 45); int RWS= map(CTRL_JY, 100, -100, 45, 135); int LWD= map(CTRL_JX, 100, -100, 45, 0); int RWD= map(CTRL_JX, 100, -100, 0, -45); myservoLeftFoot.write(LWS + LWD); myservoRightFoot.write(RWS + RWD); } } |