TShopping

 找回密碼
 註冊
搜索
查看: 1024|回復: 0

[教學] Android手機如何用Arduino藍芽連線ESP32控制蜘蛛機器人

[複製鏈接]
發表於 2020-7-13 23:36:47 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk  
影片


ESP32 控制 TB6612FNG 直流馬達驅動∕控制板 請看這篇


使用Android手機如何用Arduino藍芽連線ESP32控制蜘蛛機器人
需要使用雙電源
如果使用單電源,電流會被馬達抽走
ESP32晶片電流不足會無法正常運作

材料:
18650電池*2
ESP32*1
TB6612FNG*1
蜘蛛機器人*2


01.jpg 02.jpg


AUDINO 程式碼
  1. // Include necessary libraries
  2. #include <BLEDevice.h>
  3. #include <BLEServer.h>
  4. #include <BLEUtils.h>

  5. // 定義 UUIDs (注意要與App Inventor內容對應)
  6. #define SERVICE_UUID            "C6FBDD3C-7123-4C9E-86AB-005F1A7EDA01"
  7. #define CHARACTERISTIC_UUID_RX  "B88E098B-E464-4B54-B827-79EB2B150A9F"
  8. #define CHARACTERISTIC_UUID_TX  "D769FACF-A4DA-47BA-9253-65359EE480FB"

  9. String BLE_Code;
  10. BLECharacteristic *pCharacteristic;
  11. bool deviceConnected = false;

  12. int directState = 0; // 動作
  13. int speed_c = 192;
  14. // PWM, INA, INB 與LED接腳變數
  15. int PWMA = 13;
  16. int INA1 = 12;
  17. int INA2 = 14;
  18. int STBY = 27;
  19. int INB1 = 25;
  20. int INB2 = 26;
  21. int PWMB = 33;
  22. const int ledPin = 2;

  23. const int freq = 10000;
  24. const int resolution = 8;



  25. // 設定 callbacks onConnect & onDisconnect函數
  26. class MyServerCallbacks: public BLEServerCallbacks {
  27.   void onConnect(BLEServer* pServer) {
  28.     deviceConnected = true;
  29.   };
  30.   void onDisconnect(BLEServer* pServer) {
  31.     deviceConnected = false;
  32.   }
  33. };

  34. // 設定 callback function 當收到新的資訊 (from the Android application)
  35. class MyCallbacks: public BLECharacteristicCallbacks {
  36.   void onWrite(BLECharacteristic *pCharacteristic) {
  37.     std::string rxValue = pCharacteristic->getValue();
  38.     BLE_Code="";
  39.     if(rxValue.length() > 0) {
  40.       Serial.print("接收資料為 : ");
  41.       for(int i = 0; i < rxValue.length(); i++) {
  42.         BLE_Code+=rxValue[i];
  43.         Serial.print(rxValue[i]);
  44.       }
  45.       Serial.println();
  46.       BLE_Code.toUpperCase();
  47.       Serial.println(BLE_Code);
  48.         if(BLE_Code.indexOf("GO")!=-1)
  49.         {
  50.           Serial.println("GO");
  51.           directState=1;
  52.         } else if(BLE_Code.indexOf("BACK")!=-1) {
  53.           Serial.println("BACK");
  54.           directState=2;
  55.         } else if(BLE_Code.indexOf("LEFT")!=-1) {
  56.           Serial.println("LEFT");
  57.           directState=3;
  58.         } else if(BLE_Code.indexOf("RIGHT")!=-1) {
  59.           Serial.println("RIGHT");
  60.           directState=4;
  61.         } else if(BLE_Code.indexOf("RELEASE")!=-1) {
  62.           Serial.println("RELEASE");
  63.           directState=5;
  64.         }
  65.         if(BLE_Code.indexOf("192")!=-1)
  66.         {
  67.           speed_c=192;
  68.         } else if(BLE_Code.indexOf("255")!=-1) {
  69.           speed_c=255;
  70.         }
  71.     }
  72.   }
  73. };

  74. void setup() {
  75.   Serial.begin(115200);
  76.   pinMode(ledPin, OUTPUT); //設定腳位為輸出
  77.   pinMode(INA1,OUTPUT);
  78.   pinMode(INA2,OUTPUT);
  79.   pinMode(PWMA,OUTPUT);
  80.   pinMode(STBY,OUTPUT);
  81.   pinMode(INB1,OUTPUT);
  82.   pinMode(INB2,OUTPUT);
  83.   pinMode(PWMB,OUTPUT);
  84.   //digital output test
  85.   digitalWrite(INA1,HIGH); //設定腳位HIGH LOW
  86.   digitalWrite(INA2,LOW);
  87.   digitalWrite(PWMA,LOW);
  88.   digitalWrite(STBY,HIGH);
  89.   digitalWrite(INB1,HIGH);
  90.   digitalWrite(INB2,LOW);
  91.   digitalWrite(PWMB,LOW);
  92.   delay(1000);

  93.   //analog output(PWM) test 設定LED Channel PWM 頻率
  94.   ledcSetup(0, freq, resolution);
  95.   ledcSetup(1, freq, resolution);
  96.   ledcSetup(2, freq, resolution);
  97.   ledcSetup(3, freq, resolution);
  98.   ledcSetup(4, freq, resolution);
  99.   ledcSetup(5, freq, resolution);
  100.   ledcSetup(6, freq, resolution);
  101.   //設定腳位Channel
  102.   ledcAttachPin(INA1, 0);
  103.   ledcAttachPin(INA2, 1);
  104.   ledcAttachPin(PWMA, 2);
  105.   ledcAttachPin(STBY, 3);
  106.   ledcAttachPin(INB1, 4);
  107.   ledcAttachPin(INB2, 5);
  108.   ledcAttachPin(PWMB, 6);
  109.   
  110.   // 建立BLE Device
  111.   BLEDevice::init("ESP32_WeMos1");

  112.   // 建立BLE Server
  113.   BLEServer *pServer = BLEDevice::createServer();
  114.   pServer->setCallbacks(new MyServerCallbacks());

  115.   // 建立BLE Service
  116.   BLEService *pService = pServer->createService(SERVICE_UUID);

  117.   // 建立BLE Characteristic
  118.   pCharacteristic = pService->createCharacteristic(
  119.                       CHARACTERISTIC_UUID_TX,
  120.                       BLECharacteristic::PROPERTY_NOTIFY);                     
  121. //  pCharacteristic->addDescriptor(new BLE2902());
  122.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  123.                                          CHARACTERISTIC_UUID_RX,
  124.                                          BLECharacteristic::PROPERTY_WRITE);
  125. pCharacteristic->setCallbacks(new MyCallbacks());

  126.   // 開始(起)service
  127.   pService->start();

  128.   // 開始(起)advertising
  129.   pServer->getAdvertising()->start();
  130.   Serial.println("等待BLE手機連線....");
  131.   
  132.   digitalWrite(ledPin,LOW);
  133.   delay(500);
  134.   digitalWrite(ledPin,HIGH);
  135.   delay(500);
  136.   digitalWrite(ledPin,LOW);
  137. }

  138. void loop() {
  139.     // Check received message and control output accordingly
  140.     if (directState==1){
  141.         move(speed_c, 1); // full speed, go
  142.         Serial.println("前進");
  143.     } else if(directState==2) {
  144.         move(speed_c, 2); // full speed, down
  145.         Serial.println("後退");
  146.     } else if(directState==3) {
  147.         move(speed_c, 3); // full speed, left
  148.         Serial.println("左轉");
  149.     } else if(directState==4) {
  150.         move(speed_c, 4); // full speed, right
  151.         Serial.println("右轉");
  152.     } else if(directState==5) {
  153.       stop(); //stop
  154.       Serial.println("stop");
  155.     }
  156.   delay(100); //go for 1 second
  157. }

  158. void move( int speed, int direction){
  159. //Move specific motor at speed and direction
  160. //speed: 0 is off, and 255 is full speed
  161. //direction: 0 clockwise, 1 counter-clockwise

  162.   ledcWrite(3, 255); //STBY disable standby
  163.   Serial.println(direction);
  164.   if(direction == 1){
  165.     //設定馬達1為正轉
  166.     ledcWrite(0, 255); //INA1
  167.     ledcWrite(1, 0);   //INA2
  168.     ledcWrite(2, speed);   //PWMA
  169.     //設定馬達2為正轉
  170.     ledcWrite(4, 255); //INB1
  171.     ledcWrite(5, 0); //INB2
  172.     ledcWrite(6, speed);   //PWMB
  173.     digitalWrite(ledPin,HIGH);
  174.     delay(500);
  175.     digitalWrite(ledPin,LOW);
  176.   } else if (direction == 2){
  177.     //設定馬達1為反轉
  178.     ledcWrite(0, 0);   //INA1
  179.     ledcWrite(1, 255); //INA2
  180.     ledcWrite(2, speed);   //PWMA
  181.     //設定馬達2為反轉
  182.     ledcWrite(4, 0);   //INB1
  183.     ledcWrite(5, 255); //INB2
  184.     ledcWrite(6, speed);   //PWMB
  185.     digitalWrite(ledPin,HIGH);
  186.     delay(500);
  187.     digitalWrite(ledPin,LOW);
  188.   } else if (direction == 3){
  189.     //設定馬達1為正轉
  190.     ledcWrite(0, 255); //INA1
  191.     ledcWrite(1, 0);   //INA2
  192.     ledcWrite(2, speed);   //PWMA
  193.     //設定馬達2為反轉
  194.     ledcWrite(4, 0);   //INB1
  195.     ledcWrite(5, 255); //INB2
  196.     ledcWrite(6, speed);   //PWMB
  197.     digitalWrite(ledPin,HIGH);
  198.     delay(500);
  199.     digitalWrite(ledPin,LOW);
  200.   } else if (direction == 4){
  201.     //設定馬達1為反轉
  202.     ledcWrite(0, 0);   //INA1
  203.     ledcWrite(1, 255); //INA2
  204.     ledcWrite(2, speed);   //PWMA
  205.     //設定馬達2為正轉
  206.     ledcWrite(4, 255); //INB1
  207.     ledcWrite(5, 0); //INB2
  208.     ledcWrite(6, speed);   //PWMB
  209.     digitalWrite(ledPin,HIGH);
  210.     delay(500);
  211.     digitalWrite(ledPin,LOW);
  212.   }
  213.   Serial.println(speed);
  214. }

  215. void stop(){
  216. //enable standby  
  217.   ledcWrite(3, 0); //STBY enable standby
  218.   //Serial.println("stop");
  219. }
複製代碼

1.png 2.png
3.png
4.png
5.png


ARDUINO 程式
ESP32_BLE_car_20200713.rar (2.12 KB, 下載次數: 0, 售價: 10 金T幣)

 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2020-12-3 04:55 , Processed in 0.142754 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表