藍牙遙控機器人實作

This project is to implement a bluetooth remote controlled robotcar.


藍牙遙控機器人

應用功能說明

  1. Bluetooth remote control App
  2. two-wheel robocar

設計考量與相關技術

系統設計考量:

  1. 操作方式:藍牙遙控手機App
  2. 移動方式:兩輪
  3. 供電方式:鋰電池 3.7V x2
  4. 聯網方式:藍牙

所需相關技術:

  1. MIT App Inventor 2 手機程式設計
  2. Arduino程式設計

所需相關套件:

系統方塊圖:

手機藍牙遙控, 或WebUI 遙控(利用手機熱點WiFi連線)

程式碼

  1. // PWM to DRV8833 dual H-bridge motor driver, PWM freq. = 1000 Hz(設定頻率1K) // ESP32 Webserver to receive commands to control RoboCar(接收control RoboCar的指令)

#include #include #include

// DRV8833 pin connection(定義腳位) #define IN1pin 16
#define IN2pin 17 #define IN3pin 18 #define IN4pin 19

#define motorR 0 #define motorL 1 #define FULLSPEED 100 #define HALFSPEED 50

ESP32MotorControl motor;

/* Set these to your desired credentials. */ const char *ssid = “Your_SSID”;(設定自己熱點名字) const char *password = “Your_Password”;(輸入熱點密碼)

WebServer server(80); // Set web server port number to 80 (設定如何移動) const String HTTP_PAGE_HEAD = “<!DOCTYPE html><html lang="en"><head><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>{v}”; const String HTTP_PAGE_STYLE = “”; const String HTTP_PAGE_SCRIPT = “”; const String HTTP_PAGE_BODY= “</head><body><div style='text-align:left;display:inline-block;min-width:260px;'>”; const String HTTP_PAGE_FORM = “<form action="/cmd1" method="get"><button class="button1">Forward</button></form></br><form action="/cmd2" method="get"><button class="button2">Backward</button></form></br><form action="/cmd3" method="get"><button class="button3">Right</button></form></br><form action="/cmd4" method="get"><button class="button4">Left</button></form></br><form action="/cmd5" method="get"><button class="button5">Stop</button></form></br></div>”; const String HTTP_WEBPAGE = HTTP_PAGE_HEAD + HTTP_PAGE_STYLE + HTTP_PAGE_SCRIPT + HTTP_PAGE_BODY + HTTP_PAGE_FORM; const String HTTP_PAGE_END = “</div></body></html>”; (以上為接上網路設定)

(控制小車方向和速度,若失去連接超過2000ms後會停止) // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000;

int speed = HALFSPEED;

void handleRoot() { String s = HTTP_WEBPAGE; s += HTTP_PAGE_END; server.send(200, “text/html”, s); }

void cmd1() { String s = HTTP_WEBPAGE; s += HTTP_PAGE_END;
server.send(200, “text/html”, s); motor.motorForward(motorR, speed);
motor.motorForward(motorL, speed); Serial.println(“Move Forward”);
}

void cmd2() { String s = HTTP_WEBPAGE; s += HTTP_PAGE_END;
server.send(200, “text/html”, s); motor.motorReverse(motorR, speed); motor.motorReverse(motorL, speed); Serial.println(“Move Backward”);
}

void cmd3() { String s = HTTP_WEBPAGE; s += HTTP_PAGE_END;
server.send(200, “text/html”, s); motor.motorReverse(motorR, speed);
motor.motorForward(motorL, speed); Serial.println(“Turn Right”);
}

void cmd4() { String s = HTTP_WEBPAGE; s += HTTP_PAGE_END;
server.send(200, “text/html”, s); motor.motorForward(motorR, speed); motor.motorReverse(motorL, speed); Serial.println(“Turn Left”); }

void cmd5() { String s = HTTP_WEBPAGE; s += HTTP_PAGE_END;
server.send(200, “text/html”, s); motor.motorStop(motorR); motor.motorStop(motorL); Serial.println(“Motor Stop”); }

void setup() { Serial.begin(115200); Serial.println(“Motor Pins assigned…”); motor.attachMotors(IN1pin, IN2pin, IN3pin, IN4pin);

// Connect to Wi-Fi network with SSID and password Serial.print(“Connecting to “); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(“.”); } // Print local IP address and start web server Serial.println(“”); Serial.println(“WiFi connected.”); Serial.println(“IP address: “); Serial.println(WiFi.localIP());

server.on(“/”, handleRoot); server.on(“/cmd1”, cmd1); server.on(“/cmd2”, cmd2); server.on(“/cmd3”, cmd3); server.on(“/cmd4”, cmd4);
server.on(“/cmd5”, cmd5);

Serial.println(“HTTP server started”); server.begin();

motor.motorStop(motorR); motor.motorStop(motorL); }

void loop() { server.handleClient(); }



This site was last updated June 01, 2023.