goo blog サービス終了のお知らせ 

Ganponブログ

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

ミニ四駆 ランドクルーザー 製作&RC化 (M5Stamp PICO編)

2022-05-16 22:00:37 | RC可動コントロール

2022-05-16

またまた寄り道です。

▼下記ブログでで書いた様にRC化の新しいツールを勉強中です。
いろいろ試している中で、何とかRCカーに利用できるところまでできました。

 

1/72 T-2 ブルーインパルス 製作(その3) - Ganponブログ

2022-05-11早い方なら1日で完成させる1/72ジェット機ですが、あまり進んでおりません。まさに牛の歩みです。▼主翼と水平尾翼を取付け、接合部を均し、不要な穴(赤丸)をイ...

goo blog

 

 

▼という事で、2年前にArduino pro miniで完成しているミニ四駆ランクルに換装しました。

 

「RC化ミニ四駆」のブログ記事一覧-Ganponブログ

「RC化ミニ四駆」のブログ記事一覧です。趣味の模型作り、ドライブ、旅行などsince Mar.2017【Ganponブログ】

goo blog

 

▼画像の上がArduino pro mini+ミニUSBホストシールド(Bluetooth USBアダプタ装着)、
下がM5Stamp PICOです。

▼M5Stamp PICOをセット。

換装して動作確認すると動かない。
プログラムや接続などチェックすると、リード線がはんだ付け部であちこち切れていました。
こんな事を繰り返して、やっと完成。

以下に、備忘録としてプログラムと回路を残します。

 

【スケッチを書き込む準備】

(注1)サーボを制御するために「ESP32Servo.h」をインストール。

(注2)ESP32ライブラリV1.0.4をインストール。

(注3)下記スケッチをM5Stamp PICOに書き込み実行。

void setup(void) {
Serial.begin(115200);
uint8_t btmac[6];
delay(500);
esp_read_mac(btmac, ESP_MAC_BT);
Serial.printf("[Bluetooth] Mac Address = %02X:%02X:%02X:%02X:%02X:%02X\r\n", btmac[0], btmac[1], btmac[2], btmac[3], btmac[4], btmac[5]);
}
void loop() {
}

シリアルモニタにアドレスが表示されるのでメモしておく。

次に、SixaxisPairTool でPS3のコントローラにMac Addressを書き込む
1. PS3コントローラをMiniUSBケーブルでPCに繋ぐ。
2. SixaxisPairToolを実行し、メモしたアドレスを入力してUpdateボタンを押すと書き込みが完了。

(参考)「ESP32でPS3コントローラを使う」をキーにネット検索すると参考になる記述が見つかります。

 

▼スケッチ(プログラム)/Arduino IDE用 

  1. /*PS4BT_Arduino_DRV8835_RCCAR_04a
  2.    2020/05/14
  3.   PS4BT_Arduino_DRV8835_RCCAR_02a
  4.   左十字キーTRIANGLEで高速モード、CROSSで低速(70%)モード
  5.   ステアリング角度をスティック角比例にする
  6.   モータドライバはDRV8835
  7.    3ピンでステアリングサーボを動かす
  8. */
  9.  
  10. #include <Arduino.h> // Arduino ヘッダインクルード
  11. #include "M5Atom.h"
  12.  
  13. #include <FastLED.h> // LED操作用ライブラリインクルード(3.5.0を使用)
  14. #define NUM_LEDS 1
  15. #define DATA_PIN 27
  16. CRGB leds[NUM_LEDS];
  17.  
  18. #include <Ps3Controller.h>
  19.  
  20. #include <ESP32Servo.h>
  21. Servo servo1; // create four servo objects
  22. int servo1Pin = 26;
  23.  
  24. int servo_pos = 90;
  25. int ang = 40;
  26.  
  27. int d_time = 10;
  28. float ratio = 0.6;
  29. int pos_y;
  30. int pos_x;
  31. float motor_speed;
  32. float steering;
  33.  
  34. int PWMApin = 25; // A入力2/AENABLE 左モータ AIN2
  35. int PWMA = 11; //PWMAチャンネル 11
  36. int AIN1 = 22; // A入力1/APHASE 左モータ AIN1
  37.  
  38. int LED_3 = 19 ; // ブレーキランプ
  39. int LED_2 = 18 ; // フォグランプ
  40. int LED_8 = 21 ; // ヘッドランプ
  41.  
  42. void onConnect() {
  43.   Serial.println("Connected.");
  44.   for (int cnt = 0; cnt
  45.     digitalWrite(LED_8, HIGH);
  46.     delay(500);
  47.     digitalWrite(LED_8, LOW);
  48.     delay(500);
  49.   }
  50. }
  51.  
  52. void setup() {
  53.   Serial.begin(115200);
  54.   M5.begin(false, false , true); //SerialEnable, I2CEnable ,DisplayEnable
  55.   FastLED.addLeds<SK6812, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
  56.   leds[0] = 0xf00000; //green
  57.  
  58.   ledcSetup(PWMA, 5000, 8); //チャンネル,周波数,解像度(8bit=256段階)
  59.   ledcAttachPin(PWMApin, PWMA); //ledPinをPWMCHチャンネルに接続
  60.  
  61.   pinMode(AIN1, OUTPUT);
  62.   pinMode(LED_2, OUTPUT);
  63.   pinMode(LED_3, OUTPUT);
  64.   pinMode(LED_8, OUTPUT);
  65.  
  66.   servo1.setPeriodHertz(50); // Standard 50hz servo PWMサイクル:20mS
  67.   servo1.attach(servo1Pin, 500, 2400); // (servo1Pin, minUs, maxUs)
  68.   servo1.write(servo_pos);
  69.  
  70.   Ps3.attachOnConnect(onConnect);
  71.   Ps3.begin("00:11:22:33:44:55"); //SixaxisPairToolで調べたmac adresに修正
  72.   Serial.println("Ready");
  73. }
  74.  
  75. void loop() {
  76.   if (Ps3.isConnected()) {
  77.     pos_y = Ps3.data.analog.stick.ly;
  78.     pos_x = Ps3.data.analog.stick.rx;
  79.     pos_y = pos_y + 128;
  80.     pos_x = pos_x + 128;
  81.  
  82.     if (Ps3.event.button_down.triangle) {
  83.       ratio = 1; //高速モード
  84.     }
  85.  
  86.     if (Ps3.event.button_down.cross) {
  87.       ratio = 0.7; //低速モード
  88.     }
  89.  
  90.     if (pos_x >= 152 ) { // R
  91.       //steering = map(pos_x, 152, 255, 0, 45);
  92.       steering = map(pow(pos_x, 2), 23104, 65025, 0, 55 );
  93.       servo1.write(servo_pos - steering);
  94.       //delay(d_time);
  95.     } else {
  96.       if (pos_x <= 103 ) { //L
  97.         //steering = map(pos_x, 103, 0, 0, 35);
  98.         steering = map(pow(pos_x, 2), 10609, 0, 0, 55 );
  99.         servo1.write(servo_pos + steering);
  100.         //delay(d_time);
  101.       }
  102.       else {
  103.         servo1.write(servo_pos);
  104.       }
  105.     }
  106.  
  107.     //左スティックがセンター付近は停止(ブレーキ)
  108.     if ( pos_y > 117 && pos_y
  109.       motor_run(0, 0, 1);
  110.     }
  111.  
  112.     //前進
  113.     else if (pos_y
  114.       //左スティック中央(117)から最上部(0)の値をモーターのスピード0から255に変換
  115.       // motor_speed = ratio * map(pos_y, 117, 0, 0, 255);
  116.       motor_speed = ratio * map(pow(pos_y, 2), 13689, 0, 0, 255 );
  117.       motor_run(motor_speed, 1, 0);
  118.     }
  119.  
  120.     //後進
  121.     else if ( pos_y > 137) {
  122.       //motor_speed = ratio * map(pos_y, 137, 255, 0, 255);
  123.       motor_speed = ratio * map(pow(pos_y, 2), 18769, 65025, 0, 255 );
  124.       motor_run(motor_speed, 0, 0);
  125.     }
  126.  
  127.     //ヘッドライト
  128.     if (Ps3.event.button_down.right) { //点灯
  129.       digitalWrite(LED_8, HIGH);
  130.     }
  131.  
  132.     if (Ps3.event.button_down.left) { //消灯
  133.       digitalWrite(LED_8, LOW);
  134.     }
  135.  
  136.     //フォグランプ
  137.     if (Ps3.event.button_down.up) { //点灯
  138.       digitalWrite(LED_2, HIGH);
  139.     }
  140.  
  141.     if (Ps3.event.button_down.down) { //消灯
  142.       digitalWrite(LED_2, LOW);
  143.     }
  144.   }
  145. }
  146.  
  147. void motor_run(float D0, int D1, int D4) {
  148.   //SPrint();
  149.   /* D0 : モータスピード(左)
  150.     D1 : モータA(左)1 = HIGH / 0 = LOW
  151.     D4 : LED_3 ON/OFF 1 = HIGH / 0 = LOW
  152.   */
  153.   //analogWrite(PWMA, D0);
  154.   ledcWrite(PWMA, D0); //(チャンネル,解像度)
  155.   digitalWrite(AIN1, D1);
  156.   digitalWrite(LED_3, D4);
  157. }

▼回路図

画像をクリックすると拡大します

▼Arduino IDE ボードの設定