Arduino程式:
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 | /* * Otto Starter + Web Bluetooth Controller * Add: Ultrasound interactive features (auto-avoid + stream distance) */ #include <Otto.h> #include <EEPROM.h> #define LEFTLEG 2 #define RIGHTLEG 3 #define LEFTFOOT 4 #define RIGHTFOOT 5 // 你指定的接腳 #define TRIG 9 #define ECHO 10 #define BLE_TX 11 #define BLE_RX 12 #define BUZZER 13 #if defined(ARDUINO_ARCH_ESP32) #include "BluetoothSerial.h" String device_name = "Forest_Elf_OTTO_20"; BluetoothSerial bluetooth; #else #include <SoftwareSerial.h> String device_name = "Forest_Elf_OTTO_20"; SoftwareSerial bluetooth(BLE_TX, BLE_RX); #endif int move_speed[] = {3000, 2000, 1000, 750, 500, 250}; int n = 2; int ultrasound_threeshold = 15; // cm String command = ""; int v; int i; int positions[] = {90, 90, 90, 90}; int8_t trims[4] = {0,0,0,0}; unsigned long sync_time = 0; bool calibration = false; int isavailable = 1; Otto Ottobot; // ===== 新增:超音波互動狀態 ===== bool autoAvoidEnabled = false; // BLE 指令開關: autoavoid_on / autoavoid_off bool distanceStreamEnabled = false;// BLE 指令開關: us_stream_on / us_stream_off unsigned long usMeasureDue = 0; unsigned long usStreamDue = 0; const unsigned long US_MEASURE_INTERVAL_MS = 120; // 自動避障量測頻率 const unsigned long US_STREAM_INTERVAL_MS = 250; // 距離串流回報頻率 // 防止 pulseIn 卡死:30ms 約可量到 5m (HC-SR04 常見) long ultrasound_distance_once_cm() { long duration; digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); duration = pulseIn(ECHO, HIGH, 30000UL); // timeout 30,000 us if (duration == 0) return 999; // 超時視為很遠 long distance = duration / 58; // us -> cm return distance; } // 簡單去雜訊:看 3 次取中位數 long ultrasound_distance_cm() { long a = ultrasound_distance_once_cm(); long b = ultrasound_distance_once_cm(); long c = ultrasound_distance_once_cm(); // median of 3 if ((a <= b && b <= c) || (c <= b && b <= a)) return b; if ((b <= a && a <= c) || (c <= a && a <= b)) return a; return c; } // ===== 新增:避障互動邏輯 ===== void ultrasoundInteractionTick() { unsigned long now = millis(); // 1) 距離串流回報(不影響自動避障開關) if (distanceStreamEnabled && now >= usStreamDue) { usStreamDue = now + US_STREAM_INTERVAL_MS; long d = ultrasound_distance_cm(); bluetooth.print("US:"); bluetooth.println(d); } // 2) 自動避障 if (!autoAvoidEnabled) return; if (now < usMeasureDue) return; usMeasureDue = now + US_MEASURE_INTERVAL_MS; long d = ultrasound_distance_cm(); // 太近:做互動(停止/後退/轉向/表情/聲音) if (d <= ultrasound_threeshold) { // 先停 Ottobot.home(); Ottobot.sing(S_surprise); Ottobot.playGesture(OttoConfused); // 後退 2 步 Ottobot.walk(2, move_speed[n], BACKWARD); // 左右掃描判斷轉向(用轉頭概念:實際用 turn 旋轉身體) // 左量測 Ottobot.turn(1, 800, LEFT); long dl = ultrasound_distance_cm(); // 右量測(回到原位再往右) Ottobot.turn(2, 800, RIGHT); long dr = ultrasound_distance_cm(); // 回正(往左轉回來) Ottobot.turn(1, 800, LEFT); // 選較遠的方向轉開 if (dl >= dr) { Ottobot.turn(2, move_speed[n], LEFT); } else { Ottobot.turn(2, move_speed[n], RIGHT); } // 回報一次距離(可選) bluetooth.print("AVOID:"); bluetooth.println(d); // 避完障後不強制前進,交給你原本指令/模式控制 } } void setup() { Serial.begin(9600); Ottobot.init(LEFTLEG, RIGHTLEG, LEFTFOOT, RIGHTFOOT, true, BUZZER); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); #if defined(ARDUINO_ARCH_ESP32) Serial.print("ESP32"); bluetooth.begin(device_name); #else Serial.print("No ESP32"); Serial.print(device_name); bluetooth.begin(9600); bluetooth.print("AT+NAME" + device_name + "\r\n"); #endif Ottobot.home(); v = 0; } void loop() { checkBluetooth(); // 允許從序列埠輸入簡單指令測試 if (Serial.available() > 0) { command = Serial.readStringUntil('\n'); command.trim(); Serial.print("Received: "); Serial.println(command); } // ===== 新增:超音波互動 tick(非阻塞)===== ultrasoundInteractionTick(); // 既有命令處理 if (command == "forward") { Forward(); } else if (command == "backward") { Backward(); } else if (command == "right") { Right(); } else if (command == "left") { Left(); } else if (command == "avoidance") { Avoidance(); } else if (command == "force") { UseForce(); } } void checkBluetooth() { char charBuffer[30]; // 放大一點避免新指令溢出 if (bluetooth.available() > 0) { isavailable = 1; int numberOfBytesReceived = bluetooth.readBytesUntil('\n', charBuffer, 29); if (numberOfBytesReceived <= 0) return; charBuffer[numberOfBytesReceived] = '\0'; Serial.print("Received by BLE: "); Serial.println(charBuffer); // 最後一個字元當作速度 index char last = charBuffer[numberOfBytesReceived - 1]; if (last >= '0' && last <= '9') { n = last - '0'; if (n < 0) n = 0; if (n > 5) n = 5; } // ===== 行走指令 ===== if (strstr(charBuffer, "forward") == &charBuffer[0]) { command = "forward"; } else if (strstr(charBuffer, "backward") == &charBuffer[0]) { command = "backward"; } else if (strstr(charBuffer, "right") == &charBuffer[0]) { command = "right"; } else if (strstr(charBuffer, "left") == &charBuffer[0]) { command = "left"; } else if (strstr(charBuffer, "stop") == &charBuffer[0]) { command = "stop"; Stop(); } // ===== 超音波:單次回報距離 ===== else if (strstr(charBuffer, "ultrasound") == &charBuffer[0]) { Stop(); long d = ultrasound_distance_cm(); bluetooth.print("US:"); bluetooth.println(d); } // ===== 新增:自動避障開關 ===== else if (strstr(charBuffer, "autoavoid_on") == &charBuffer[0]) { autoAvoidEnabled = true; command = ""; bluetooth.println("AUTOAVOID:ON"); } else if (strstr(charBuffer, "autoavoid_off") == &charBuffer[0]) { autoAvoidEnabled = false; command = ""; bluetooth.println("AUTOAVOID:OFF"); } // ===== 新增:距離串流回報開關 ===== else if (strstr(charBuffer, "us_stream_on") == &charBuffer[0]) { distanceStreamEnabled = true; usStreamDue = millis(); // 立即開始 command = ""; bluetooth.println("US_STREAM:ON"); } else if (strstr(charBuffer, "us_stream_off") == &charBuffer[0]) { distanceStreamEnabled = false; command = ""; bluetooth.println("US_STREAM:OFF"); } // ===== 既有:模式 ===== else if (strstr(charBuffer, "avoidance") == &charBuffer[0]) { command = "avoidance"; } else if (strstr(charBuffer, "force") == &charBuffer[0]) { command = "force"; } // ===== 表情 / 手勢指令區 ===== else if (strstr(charBuffer, "happy") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoHappy); } else if (strstr(charBuffer, "superhappy") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoSuperHappy); } else if (strstr(charBuffer, "sad") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoSad); } else if (strstr(charBuffer, "sleeping") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoSleeping); } else if (strstr(charBuffer, "confused") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoConfused); } else if (strstr(charBuffer, "fretful") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoFretful); } else if (strstr(charBuffer, "love") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoLove); } else if (strstr(charBuffer, "angry") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoAngry); } else if (strstr(charBuffer, "magic") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoMagic); } else if (strstr(charBuffer, "wave") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoWave); } else if (strstr(charBuffer, "victory") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoVictory); } else if (strstr(charBuffer, "fail") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoFail); } else if (strstr(charBuffer, "fart") == &charBuffer[0]) { command = ""; Ottobot.playGesture(OttoFart); } // 校正與測試 else if (strstr(charBuffer, "C") == &charBuffer[0]) { if (calibration == false) { Ottobot._moveServos(10, positions); calibration = true; delay(50); } command = "calibration"; Calibration(String(charBuffer)); } else if (strstr(charBuffer, "walk_test") == &charBuffer[0]) { command = ""; Ottobot.walk(3, 1000, FORWARD); } else if (strstr(charBuffer, "save_calibration") == &charBuffer[0]) { command = ""; readChar('s'); } } else { if (isavailable == 1) { Serial.println("No Bluetooth is available."); isavailable = 0; } } } void Forward() { Ottobot.walk(1, move_speed[n], FORWARD); } void Backward(){ Ottobot.walk(1, move_speed[n], BACKWARD); } void Right() { Ottobot.walk(1, move_speed[n], RIGHT); } void Left() { Ottobot.walk(1, move_speed[n], LEFT); } void Stop() { Ottobot.home(); } void Avoidance() { if (ultrasound_distance_cm() <= ultrasound_threeshold) { Ottobot.playGesture(OttoConfused); for (int count=0 ; count<2 ; count++) { Ottobot.walk(1, move_speed[n], BACKWARD); } for (int count=0 ; count<4 ; count++) { Ottobot.turn(1, move_speed[n], LEFT); } } Ottobot.walk(1, move_speed[n], FORWARD); } void UseForce() { long d = ultrasound_distance_cm(); if (d <= ultrasound_threeshold) { Ottobot.walk(1, move_speed[n], BACKWARD); } if ((d > 10) && (d < 15)) { Ottobot.home(); } if ((d > 15) && (d < 30)) { Ottobot.walk(1, move_speed[n], FORWARD); } if (d > 30) { Ottobot.home(); } } void Settings(String ts_ultrasound) { ultrasound_threeshold = ts_ultrasound.toInt(); } void Calibration(String c) { if (sync_time < millis()) { sync_time = millis() + 50; for (int k = 1; k < c.length(); k++) { readChar((c[k])); } } } void readChar(char ch) { switch (ch) { case '0'...'9': v = (v * 10 + ch) - 48; break; case 'a': trims[0] = v-90; setTrims(); v = 0; break; case 'b': trims[1] = v-90; setTrims(); v = 0; break; case 'c': trims[2] = v-90; setTrims(); v = 0; break; case 'd': trims[3] = v-90; setTrims(); v = 0; break; case 's': for (i=0 ; i<=3 ; i=i+1) EEPROM.write(i,trims[i]); delay(500); Ottobot.sing(S_superHappy); Ottobot.crusaito(1, 1000, 25, -1); Ottobot.crusaito(1, 1000, 25, 1); Ottobot.sing(S_happy_short); break; } } void setTrims() { Ottobot.setTrims(trims[0],trims[1],trims[2],trims[3]); Ottobot._moveServos(10, positions); } |

