程式碼:
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 | #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); /* ========= Servo Pins(不改你原本) ========= */ #define PIN_LF D7 // 左腳 #define PIN_LL D8 // 左腿 #define PIN_RF D3 // 右腳 #define PIN_RL D4 // 右腿 Servo servoLF, servoLL, servoRF, servoRL; /* ========= 校正 / 姿態參數(已調到接近影片) ========= */ int LA0 = 60; // 人形站立左腿 int RA0 = 120; // 人形站立右腿 int LA1 = 180; // 車形左腿 int RA1 = 0; // 車形右腿 // 走路側傾(關鍵) int LATL = 80; int RATL = 150; int LATR = 40; int RATR = 100; /* ========= 控制輸入 ========= */ int CTRL_JX = 0; // -100 ~ 100 int CTRL_JY = 0; // -100 ~ 100 /* ========= 模式 ========= */ enum MODE { WALK, ROLL }; MODE currentMode = WALK; /* ========= 三段走路速度 ========= */ enum WALK_SPEED { SLOW, NORMAL, FAST }; WALK_SPEED walkSpeed = NORMAL; /* ========= 計時 ========= */ unsigned long stepTimer = 0; /* ========= 依速度模式取得步態參數 ========= */ void getWalkParams(int &baseTime, int &stepMin, int &stepMax, int &holdTime){ switch(walkSpeed){ case SLOW: baseTime = 380; stepMin = 14; stepMax = 26; holdTime = 160; break; case NORMAL: baseTime = 260; stepMin = 18; stepMax = 40; holdTime = 120; break; case FAST: baseTime = 190; stepMin = 22; stepMax = 45; holdTime = 70; break; } } /* ========= HTML(虛擬搖桿+死區+三段速度) ========= */ 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</title> <style> body{font-family:Arial;background:#f4f6f8;text-align:center;margin:0;padding:16px} .card{max-width:420px;margin:auto;background:#fff;border-radius:16px;padding:16px} canvas{background:#ecf0f1;border-radius:50%;touch-action:none} button{width:120px;height:42px;margin:4px;border:none;border-radius:10px;background:#2d7ef7;color:#fff} .green{background:#00a86b} .gray{background:#6c757d} </style> </head> <body> <div class="card"> <h2>OTTO NINJA</h2> <div id="status">未連線</div> <canvas id="joy" width="220" height="220"></canvas> <div> <button class="gray" onclick="connectWS()">連線</button> </div> <div> <button onclick="sendCmd('mode_walk')">人形</button> <button onclick="sendCmd('mode_roll')">車形</button> </div> <div> <button onclick="sendCmd('walk_slow')">慢走</button> <button onclick="sendCmd('walk_normal')">正常</button> <button onclick="sendCmd('walk_fast')">快走</button> </div> </div> <script> let ws=null; const canvas=document.getElementById("joy"); const ctx=canvas.getContext("2d"); const cx=canvas.width/2, cy=canvas.height/2; const R=cx-10, knobR=26; const DEAD=20; let dragging=false,x=0,y=0; function connectWS(){ ws=new WebSocket("ws://"+location.hostname+":81/"); ws.onopen=()=>document.getElementById("status").innerText="已連線"; ws.onclose=()=>document.getElementById("status").innerText="中斷"; } function sendCmd(c){ if(ws&&ws.readyState===1) ws.send(c); } function draw(){ ctx.clearRect(0,0,canvas.width,canvas.height); ctx.beginPath();ctx.arc(cx,cy,R,0,Math.PI*2);ctx.fillStyle="#ddd";ctx.fill(); ctx.beginPath();ctx.arc(cx+x,cy+y,knobR,0,Math.PI*2);ctx.fillStyle="#0984e3";ctx.fill(); } function update(px,py){ let dx=px-cx,dy=py-cy; let d=Math.hypot(dx,dy); if(d>R){dx*=R/d;dy*=R/d;} x=dx;y=dy; let X=Math.round(x/R*100); let Y=Math.round(-y/R*100); let m=Math.hypot(X,Y); if(m<DEAD){X=0;Y=0;} else{ let s=(m-DEAD)/(100-DEAD); X=Math.round(X/m*s*100); Y=Math.round(Y/m*s*100); } if(ws&&ws.readyState===1) ws.send(`move ${X} ${Y}`); draw(); } function reset(){ x=0;y=0; if(ws&&ws.readyState===1) ws.send("move 0 0"); draw(); } canvas.addEventListener("pointerdown",e=>{dragging=true;update(e.offsetX,e.offsetY);}); canvas.addEventListener("pointermove",e=>{if(dragging)update(e.offsetX,e.offsetY);}); canvas.addEventListener("pointerup",e=>{dragging=false;reset();}); canvas.addEventListener("pointercancel",reset); draw(); </script> </body> </html>)====="; /* ========= 動作基礎 ========= */ void stand(){ servoLL.attach(PIN_LL); servoRL.attach(PIN_RL); servoLF.attach(PIN_LF); servoRF.attach(PIN_RF); servoLL.write(LA0); servoRL.write(RA0); servoLF.write(90); servoRF.write(90); } void setRoll(){ servoLL.attach(PIN_LL); servoRL.attach(PIN_RL); servoLL.write(LA1); servoRL.write(RA1); } /* ========= WebSocket ========= */ void webSocketEvent(uint8_t n,WStype_t t,uint8_t* p,size_t l){ if(t!=WStype_TEXT) return; String cmd=String((char*)p).substring(0,l); if(cmd=="mode_walk"){currentMode=WALK;stand();} else if(cmd=="mode_roll"){currentMode=ROLL;setRoll();} else if(cmd=="walk_slow") walkSpeed=SLOW; else if(cmd=="walk_normal") walkSpeed=NORMAL; else if(cmd=="walk_fast") walkSpeed=FAST; else if(cmd.startsWith("move")){ int a=cmd.indexOf(' '),b=cmd.lastIndexOf(' '); CTRL_JX=cmd.substring(a+1,b).toInt(); CTRL_JY=cmd.substring(b+1).toInt(); } } /* ========= SETUP ========= */ void setup(){ Serial.begin(115200); stand(); WiFi.mode(WIFI_AP); WiFi.softAP(ssid,password); server.on("/",[](){server.send_P(200,"text/html",MAIN_PAGE);}); server.begin(); webSocket.begin(); webSocket.onEvent(webSocketEvent); } /* ========= LOOP ========= */ void loop(){ server.handleClient(); webSocket.loop(); bool idle = abs(CTRL_JX)<10 && abs(CTRL_JY)<10; if(currentMode==WALK){ if(idle){stand();return;} int baseTime,stepMin,stepMax,hold; getWalkParams(baseTime,stepMin,stepMax,hold); int STEP=map(abs(CTRL_JY),0,100,stepMin,stepMax); unsigned long now=millis(); int base=baseTime; int t=(now-stepTimer)%(base*2+hold); servoLL.attach(PIN_LL); servoRL.attach(PIN_RL); servoLF.attach(PIN_LF); servoRF.attach(PIN_RF); if(t<base){ servoLL.write(LATR); servoRL.write(RATR); servoRF.write(90-STEP); }else if(t<base+hold){ }else{ servoLL.write(LATL); servoRL.write(RATL); servoLF.write(90+STEP); } } else{ if(idle){servoLF.write(90);servoRF.write(90);return;} servoLF.attach(PIN_LF); servoRF.attach(PIN_RF); int l=map(CTRL_JY+CTRL_JX,-200,200,45,135); int r=map(CTRL_JY-CTRL_JX,-200,200,45,135); servoLF.write(l); servoRF.write(r); } } |
沒有留言:
張貼留言