Ganponブログ

趣味の模型作り、ドライブ、旅行など
since Mar.2017

M5Stamp PicoでRC可動戦車

2022-10-16 10:46:25 | RC可動コントロール

2022-10-16
模型製作の合間に手持ちのM5Stamp Pico(以下 Picoと称す)でアリイのリモコン戦車をRC化してみました。
砲塔旋回も砲身上下、リコイルなどしない走るだけの仕様です。

 

▼M5Stamp Pico
サイズは18 x 24 x 4.4 mmの小さなユニットですが、Wi-FiもBluetoothもできます 。

しかし、PS3コントローラとペアリングできません。
過去にPicoでRCカーを作ったのですが、今回は何か設定がおかしいようです。
PicoをPS3コントローラで無線操縦するスケッチ(プログラム)のポイントを忘れていました。
そんな訳で備忘録として書き留めておくことにしました。

ペアリングできない原因は、Arduino IDEでスケッチを書き込むとき、ボードの設定が違っています。
通常はPicoを使う時のボードはSTAMP-PICOで良いです。

しかし、どう言う訳かは判りませんが、PS3コントローラとペアリングさせるには
ESP32 Pico Kitを選定する必要が有ります。

 

私の場合、PS3コントローラのMACアドレスを書き換えて使用しています。
(あくまでも個人責任です。)
方法は、sixaxispairtool というツールで現在のアドレスの確認、書き換えができます。
PCとPS3コントローラが接続されている状態でSixaxisPairToolを起動すると現在のBlueToothアドレスが上に表示されます。



この様にPS3コントローラのMACアドレスを書き換え、スケッチの中でも同じアドレスを書き込んでおくと、一つのコントローラで違うユニットを動かす事が出来て便利です。
勿論ユニット(ESP32やPICO)の本来のMACアドレスは変更されていません。
あくまでもこのスケッチで動かしている間だけの暫定的なものです。
 

▼回路図

 
 
▼【スケッチ】

  1. /* PS3_M5PICO_tank_DRV8835_basic01b
  2.   最大1310720バイトのフラッシュメモリのうち、スケッチが954986バイト(72%)を使っています。
  3.   最大327680バイトのRAMのうち、グローバル変数が32116バイト(9%)を使っていて、
  4.   ローカル変数で295564バイト使うことができます。
  5.  
  6.   2022-10-15
  7.   M5Stamp Pico
  8.   ボードはESP32 Pico Kit を選択すること。
  9.   STAMP PICOではBluetoothが接続できなかった。
  10.   ボードマネージャESP32ライブラリはV1.0.4、V1.0.6でもOK
  11.  
  12.    【操作手順】
  13.     PSボタンでペアリング。
  14.  
  15.    【走行コントロール】
  16.     コントローラーの左スティック上下で前後進、
  17.     右スティックの左右でステアリングを制御。
  18.     ステアリングを振っていくと回転軸側が減速していき(緩旋回から信地旋回)、
  19.     一杯に倒すと回転軸側は停止。(信地旋回)
  20.     左スティック中央で右ステアリングを一杯に振ると超信地旋回(速度80%) ←ver1.0a
  21.  
  22. */
  23.  
  24. /*
  25.   G0・・・IN/OUT
  26.   G1・・・IN/OUT *** NG
  27.   G3・・・IN/OUT *** NG
  28.   G26・・・IN/OUT DAC2
  29.   G36・・・IN ONLY ADC
  30.   G18・・・OIN/OUT
  31.   G19・・・IN/OUT
  32.   G21・・・IN/OUT
  33.   G22・・・IN/OUT
  34.   G25・・・IN/OUT DAC1
  35.   G32・・・IN/OUT ADC Grove
  36.   G33・・・IN/OUT ADC Grove
  37. */
  38.  
  39. #include <Arduino.h> // Arduino ヘッダインクルード
  40. #include "M5Atom.h"
  41. #include <Ps3Controller.h>
  42.  
  43. #include <FastLED.h> // LED操作用ライブラリインクルード(3.5.0を使用)
  44. #define NUM_LEDS 1
  45. #define DATA_PIN 27
  46. CRGB leds[NUM_LEDS];
  47.  
  48. int cnt = 0 ;
  49. int motor_speed;
  50. int steering;
  51.  
  52. int AIN1 = 22; // A入力1/APHASE 左モータ AIN1
  53. int BIN1 = 21; // B入力1/BPHASE 右モータ BIN1
  54. int PWMApin = 25; // A入力2/AENABLE 左モータ AIN2
  55. int PWMBpin = 26; // B入力2/BENABLE 右モータ BIN2
  56. int PWMA = 2; //PWMAチャンネル 0=NG 1=NG 0〜15
  57. int PWMB = 3; //PWMAチャンネル 0=NG 1=NG 0〜15
  58.  
  59. int pos_y;
  60. int pos_x;
  61. int LED_8 = 19 ; // ヘッドランプ
  62.  
  63. void onConnect() {
  64.   Serial.println("Connected.");
  65.   for (int cnt = 0; cnt < 2; cnt++) {
  66.     digitalWrite(LED_8, HIGH);
  67.     delay(500);
  68.     digitalWrite(LED_8, LOW);
  69.     delay(500);
  70.   }
  71. }
  72.  
  73. void setup() {
  74.   Serial.begin(115200);
  75.   M5.begin(false, false , true); //SerialEnable, I2CEnable ,DisplayEnable
  76.   FastLED.addLeds<SK6812, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
  77.   leds[0] = 0xf00000; //green
  78.   FastLED.show();
  79.  
  80.   ledcSetup(PWMA, 5000, 8); //チャンネル,周波数,解像度(8bit=256段階)
  81.   ledcAttachPin(PWMApin, PWMA); //ledPinをPWMCHチャンネルに接続
  82.   ledcSetup(PWMB, 5000, 8); //チャンネル,周波数,解像度(8bit=256段階)
  83.   ledcAttachPin(PWMBpin, PWMB); //ledPinをPWMCHチャンネルに接続
  84.  
  85.   //pinMode(PWMApin, OUTPUT);
  86.   //pinMode(PWMBpin, OUTPUT);
  87.   pinMode(AIN1, OUTPUT);
  88.   pinMode(BIN1, OUTPUT);
  89.   pinMode(LED_8, OUTPUT);
  90.  
  91.   Ps3.attachOnConnect(onConnect);
  92.   //Ps3.begin("00:11:22:33:44:55"); //SixaxisPairToolで調べたmac adresに修正
  93.   Ps3.begin("00:00:00:00:00:26"); // PS3-sony
  94.   Serial.println("PS3 Ready");
  95.  
  96. }
  97.  
  98. void loop() {
  99.   if (Ps3.isConnected()) {
  100.     //走行コントロール
  101.     pos_y = Ps3.data.analog.stick.ly;
  102.     pos_x = Ps3.data.analog.stick.rx;
  103.     pos_y = pos_y + 128;
  104.     pos_x = pos_x + 128;
  105.  
  106.     //左スティックがセンター付近は停止(ブレーキ)
  107.     if (pos_x >= 117 && pos_x <= 137 && pos_y >= 117 && pos_y <= 137) {
  108.       motor_run(0, 0, 0, 0, 1);
  109.     }
  110.  
  111.     //前進
  112.     if (pos_y <= 102 && pos_x >= 102 && pos_x <= 152) {
  113.       motor_speed = map(pos_y, 102, 0, 0, 255);
  114.       motor_run(motor_speed, 0, motor_speed, 0, 0);
  115.     }
  116.  
  117.     //後進
  118.     else if ( pos_y >= 152 && pos_x >= 102 && pos_x <= 152) {
  119.       motor_speed = map(pos_y, 152, 255, 0, 255) ;
  120.       motor_run(motor_speed, 1, motor_speed, 1, 0);
  121.     }
  122.  
  123.     //前進右緩旋回、信地旋回
  124.     //else if ( pos_y <= 102 && pos_x > 152 && pos_x <= 245) {
  125.     else if ( pos_y <= 102 && pos_x >= 152) {
  126.       motor_speed = map(pos_y, 102, 0, 0, 255);
  127.       steering = motor_speed * (1.00 - (map(pos_x, 152, 255, 0, 255) / 255.00));
  128.       motor_run(motor_speed, 0, steering, 0, 0);
  129.     }
  130.  
  131.     //前進左緩旋回、信地旋回
  132.     //else if ( pos_y <= 102 && pos_x < 102 && pos_x >= 10) {
  133.     else if ( pos_y <= 102 && pos_x <= 102) {
  134.       motor_speed = map(pos_y, 102, 0, 0, 255);
  135.       steering = motor_speed * (1.00 - (map(pos_x, 102, 0, 0, 255) / 255.00));
  136.       motor_run(steering, 0, motor_speed, 0, 0);
  137.     }
  138.  
  139.     //後進右緩旋回、信地旋回
  140.     //else if ( pos_y >= 152 && pos_x > 152 && pos_x <= 245) {
  141.     else if ( pos_y >= 152 && pos_x >= 152 ) {
  142.       motor_speed = map(pos_y, 152, 255, 0, 255);
  143.       steering = motor_speed * (1.00 - (map(pos_x, 152, 255, 0, 255) / 255.00));
  144.       motor_run(motor_speed, 1, steering, 1, 0);
  145.     }
  146.  
  147.     //後進左緩旋回、信地旋回
  148.     //else if ( pos_y >= 152 && pos_x < 102 && pos_x >= 10) {
  149.     else if ( pos_y >= 152 && pos_x <= 102 ) {
  150.       motor_speed = map(pos_y, 152, 255, 0, 255);
  151.       steering = motor_speed * (1.00 - (map(pos_x, 102, 0, 0, 255) / 255.00));
  152.       motor_run(steering, 1, motor_speed, 1, 0);
  153.     }
  154.  
  155.     //右超信地旋回
  156.     else if (pos_x >= 245 && pos_y >= 102 && pos_y <= 152) {
  157.       motor_speed = 165;
  158.       motor_run(motor_speed, 0, motor_speed, 1, 0);
  159.     }
  160.  
  161.     //左超信地旋回
  162.     else if (pos_x <= 10 && pos_y >= 102 && pos_y <= 152) {
  163.       motor_speed = 165;
  164.       motor_run(motor_speed, 1, motor_speed, 0, 0);
  165.     }
  166.  
  167.     else { //停止(ブレーキ)
  168.       motor_run(0, 0, 0, 0, 1);
  169.     }
  170.   }
  171. }
  172.  
  173.  
  174. void motor_run(int D0, int D1, int D2, int D3, int D4) {
  175.   /* D0 : モータスピード(左)
  176.      D1 : モータA(左)1 = HIGH / 0 = LOW
  177.      D2 : モータスピード(右)
  178.      D3 : モータB(右)1 = HIGH / 0 = LOW
  179.      D4 : LED_3 ON/OFF 1 = HIGH / 0 = LOW
  180.   */
  181.   //analogWrite(PWMA, D0);
  182.   ledcWrite(PWMA, D0); //(チャンネル,解像度)
  183.   digitalWrite(AIN1, D1);
  184.   //analogWrite(PWMB, D2);
  185.   ledcWrite(PWMB, D2); //(チャンネル,解像度)
  186.   digitalWrite(BIN1, D3);
  187.   digitalWrite(LED_8, D4);
  188.  
  189.   /*Serial.print(" D0="); Serial.print(D0);
  190.   Serial.print(" D1="); Serial.print(D1);
  191.   Serial.print(" D2="); Serial.print(D2);
  192.   Serial.print(" D3="); Serial.print(D3);
  193.   Serial.print(" D4="); Serial.println(D4);
  194.   */
  195. }
 


コメントを投稿