Advertisement
firtel

Mecanum-dualshock3

Nov 4th, 2022
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //ESP32 Arduino Core 2.0.5
  2. //PS3 Controller Host library 1.1.0
  3.  
  4. #include <Ps3Controller.h>
  5.  
  6. //#define NUM_LEDS 12
  7. //#define LED_PIN 27
  8. //#define BTN_PIN 39
  9. //CRGB leds[NUM_LEDS];
  10.  
  11. #define DEBUG
  12.  
  13. //Bluetooth MAC変更(変更しない場合はコメント)
  14. //#define BTMAC "1a:2b:3c:01:01:01"
  15.  
  16. //ピン設定
  17. #define M1p 17
  18. #define M1n 18
  19. #define M2p 4
  20. #define M2n 5
  21. #define M3p 22
  22. #define M3n 23
  23. #define M4p 19
  24. #define M4n 21
  25.  
  26. //パラメータ
  27. #define PWMfreq 2000
  28. #define PWMreso 8 //8bit 0-255
  29. #define PWMmax 255 //2^PWMreso - 1
  30. #define DEADZone 8
  31.  
  32. //進行方向用各要素定義
  33. int8_t MoveY[4] = { 50, 50, 50, 50 };
  34. int8_t MoveX[4] = { -50, 50, 50, -50 };
  35. int8_t ROTATE[4] = { 30, -30, 30, -30 };
  36.  
  37. //コントローラーの値代入用
  38. int lx = 0;
  39. int ly = 0;
  40. int rx = 0;
  41. int ry = 0;
  42. int lt = 0;
  43. int rt = 0;
  44. boolean BTconnected = false;
  45.  
  46. //モーター出力設定
  47. int8_t speed_sendbuff[4] = { 0 };
  48.  
  49. //ポーリング用
  50. unsigned long lastMotor = 0;
  51. unsigned long lastupdate = 0;
  52.  
  53. void notify()
  54. {
  55.   lx = Ps3.data.analog.stick.lx;
  56.   ly = Ps3.data.analog.stick.ly;
  57.   rx = Ps3.data.analog.stick.rx;
  58.   ry = Ps3.data.analog.stick.ry;
  59.   if (abs(lx) < DEADZone) {
  60.     lx = 0;
  61.   }
  62.   if (abs(ly) < DEADZone) {
  63.     ly = 0;
  64.   }
  65.   if (abs(rx) < DEADZone) {
  66.     rx = 0;
  67.   }
  68.   if (abs(ry) < DEADZone) {
  69.     ry = 0;
  70.   }
  71.   lt = Ps3.data.analog.button.l2;
  72.   rt = Ps3.data.analog.button.r2;
  73.  
  74.   /*
  75.     Serial.print(lx);
  76.     Serial.print(",");
  77.     Serial.print(ly);
  78.     Serial.print(",");
  79.     Serial.print(rx);
  80.     Serial.print(",");
  81.     Serial.println(ry);
  82.   */
  83. }
  84.  
  85. void btdisconn() {
  86.   BTconnected = false;
  87. #ifdef DEBUG
  88.   Serial.println("Controller Disconnected");
  89. #endif
  90. }
  91.  
  92. void btconn() {
  93.   BTconnected = true;
  94. #ifdef DEBUG
  95.   Serial.println("Controller Connected");
  96. #endif
  97. }
  98.  
  99. void setup() {
  100. #ifdef DEBUG
  101.   Serial.begin(115200);
  102. #endif
  103.  
  104.   //PWMタイマ設定
  105.   ledcSetup(0, PWMfreq, PWMreso); //ch,kHz,bit
  106.   ledcSetup(1, PWMfreq, PWMreso);
  107.   ledcSetup(2, PWMfreq, PWMreso);
  108.   ledcSetup(3, PWMfreq, PWMreso);
  109.   ledcSetup(4, PWMfreq, PWMreso);
  110.   ledcSetup(5, PWMfreq, PWMreso);
  111.   ledcSetup(6, PWMfreq, PWMreso);
  112.   ledcSetup(7, PWMfreq, PWMreso);
  113.  
  114.   //PWMタイマに出力PIN関連付け
  115.   ledcAttachPin(M1p, 0);
  116.   ledcAttachPin(M1n, 1);
  117.   ledcAttachPin(M2p, 2);
  118.   ledcAttachPin(M2n, 3);
  119.   ledcAttachPin(M3p, 4);
  120.   ledcAttachPin(M3n, 5);
  121.   ledcAttachPin(M4p, 6);
  122.   ledcAttachPin(M4n, 7);
  123.  
  124.   //DUALSHOCK3初期化
  125.   Ps3.attach(notify);
  126.   Ps3.attachOnDisconnect(btdisconn);
  127.   Ps3.attachOnConnect(btconn);
  128. #ifdef BTMAC
  129.   Ps3.begin(BTMAC);
  130. #else
  131.   Ps3.begin();
  132. #endif
  133.  
  134.   //Wire.begin(26,32);//SDA SCL
  135. }
  136.  
  137.  
  138.  
  139. void loop() {
  140.   /* if (lastupdate + 2000 <= millis()) {
  141.      lastupdate = millis();//次の実行のために時間更新
  142.      Serial.println("time");
  143.      if (!Ps3.isConnected()) {
  144.        setspeed(0, 0, 0); //切断されたら2秒以内に停止
  145.      }
  146.     }*/
  147.   if (lastMotor + 50 <= millis()) {
  148.     lastMotor = millis();//次の実行のために時間更新
  149.     if (BTconnected == true) {
  150.       setspeed(lx / 127.00, ly / -127.00, rx / 127.00);
  151.     } else {
  152.       setspeed(0, 0, 0);
  153.     }
  154.  
  155.   }
  156.   // Serial.println("scanning:" + String(scanning) + " connected:" + String(connected) + " advDevice is nullptr:" + String(advDevice == nullptr));
  157.   //delay(2000);
  158. }
  159.  
  160. void setspeed(float x, float y, float r) {  // 前後左右回転それぞれを係数と掛けて足す
  161.   for (int i = 0; i < 4; i++) {
  162.     speed_sendbuff[i] = MoveY[i] * y;
  163.     speed_sendbuff[i] += MoveX[i] * x;
  164.     speed_sendbuff[i] += ROTATE[i] * r;
  165.   }
  166.   float limit = 0.0;
  167.   for (int i = 0; i < 4; i++) {
  168.     // speedが100を超えないようにリミッターをかける
  169.     limit = 100.0 / max(abs(speed_sendbuff[3]), max(abs(speed_sendbuff[2]), max(abs(speed_sendbuff[1]), abs(speed_sendbuff[0]))));
  170.   }
  171.   //    printf("limit = %f\n", limit);
  172.   if (limit < 1.0) {
  173.     for (int i = 0; i < 4; i++) {
  174.       speed_sendbuff[i] = speed_sendbuff[i] * limit;
  175.     }
  176.   }
  177.  
  178.   int motorpwm[4] = {0};
  179.   for (uint8_t i = 0; i < 4; i++) {
  180.     motorpwm[i] = int(speed_sendbuff[i] * 2.55);
  181.   }
  182.  
  183.   setMotor(motorpwm);
  184. }
  185.  
  186. void setMotor(int *pwm) {
  187.   for (uint8_t i = 0; i <= 3; i++) {
  188.     if (pwm[i] == 0) { //フリー
  189.       ledcWrite((i * 2), 0);
  190.       ledcWrite(((i * 2) + 1), 0);
  191.     } else if (pwm[i] > 0 && pwm[i] <= 255) {
  192.       ledcWrite((i * 2), PWMmax - pwm[i]);
  193.       ledcWrite(((i * 2) + 1), PWMmax);
  194.     } else if (pwm[i] < 0 && pwm[i] >= -255) {
  195.       ledcWrite((i * 2), PWMmax);
  196.       ledcWrite(((i * 2) + 1), PWMmax + pwm[i]);
  197.     }
  198.   }
  199. #ifdef DEBUG
  200.   for (uint8_t i = 0; i <= 3; i++) {
  201.     Serial.print(pwm[i]);
  202.     Serial.print(",");
  203.   }
  204.   Serial.println("");
  205. #endif
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement