こんにちは。
このロボットについて、Blynkを用いてラジコン対応した話です。
昔、メカナムホイールのロボットをBlynkでラジコン化した事がありましたが、その際はwifiを用いていました。そのため、コントール時に通信のラグが生じ、思うように操作できない問題がありました。
そこで今回はBTを用いたラジコン化に挑戦しました。
プログラム
#include "IoTBoardUtility.h" // 自作基板用のライブラリ #define BLYNK_PRINT Serial #define BLYNK_USE_DIRECT_CONNECT #include <BlynkSimpleEsp32_BT.h> /* GPIO definition */ #include <Adafruit_NeoPixel.h> #include <Wire.h> char auth[] = "hogehogehogehogehogehogehoehoge"; int count=0; unsigned long startTime=0; unsigned long timer=0; bool timerFlag = false; #define FRONT 0 #define BACK 1 // 前後左右移動 BLYNK_WRITE(V1) { int x = param[0].asInt(); int y = param[1].asInt(); double rad = atan2(x*2-1023,y*2-1023) - 0.7854; double speed = 255*sqrt((y*2-1023)*(y*2-1023)+(x*2-1023)*(x*2-1023))/1023; actMotorFB(FRONT,speed*cos(rad),speed*sin(rad)); actMotorFB(BACK,speed*sin(rad),speed*cos(rad)); Serial.println(speed); } // 回転 BLYNK_WRITE(V0) { int direction = param[0].asInt(); actMotorFB(FRONT,direction/2,-direction/2); actMotorFB(BACK,direction/2,-direction/2); } void setup(){ Serial.begin(115200); Serial.println("run!!"); init(); // 自作基板用の初期化 pixels.clear(); // Set all pixel colors to 'off' pixels.show(); Blynk.setDeviceName("Blynk"); Blynk.begin(auth); } void loop(){ Blynk.run(); } void actMotorFB(int ForB,int speedL,int speedR){ int R1,R2,L1,L2; if(ForB == FRONT){ R1=4; R2=5; L1=2; L2=3; }else{ R1=8; R2=9; L1=6; L2=7; } if(speedL >= 0){ ledcWrite(L1,255-speedL); ledcWrite(L2,255); }else{ ledcWrite(L1,255); ledcWrite(L2,255+speedL); } if(speedR >= 0){ ledcWrite(R1,255-speedR); ledcWrite(R2,255); }else{ ledcWrite(R1,255); ledcWrite(R2,255+speedR); } }
wifiの場合と違い、ライブラリが異なる点が大きな違いでしょうか。SSIDやパスワードをプログラム中に入れなくて済むのが楽ですね。BTに変更することで、通信時のラグが無くなり操作が楽になりました!
Blynkアプリ側の接続方法は ↓ 図の通りです。
動作映像
おわりに
ちなみに、BLEの方も試してみようと思ったのですが、おそらくまだ未対応のためうまく接続できませんでした。