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アドレスが上に表示されます。
PCとPS3コントローラが接続されている状態でSixaxisPairToolを起動すると現在のBlueToothアドレスが上に表示されます。

この様にPS3コントローラのMACアドレスを書き換え、スケッチの中でも同じアドレスを書き込んでおくと、一つのコントローラで違うユニットを動かす事が出来て便利です。
勿論ユニット(ESP32やPICO)の本来のMACアドレスは変更されていません。
あくまでもこのスケッチで動かしている間だけの暫定的なものです。
▼回路図
▼【スケッチ】
- /* PS3_M5PICO_tank_DRV8835_basic01b
- 最大1310720バイトのフラッシュメモリのうち、スケッチが954986バイト(72%)を使っています。
- 最大327680バイトのRAMのうち、グローバル変数が32116バイト(9%)を使っていて、
- ローカル変数で295564バイト使うことができます。
- 2022-10-15
- M5Stamp Pico
- ボードはESP32 Pico Kit を選択すること。
- STAMP PICOではBluetoothが接続できなかった。
- ボードマネージャESP32ライブラリはV1.0.4、V1.0.6でもOK
- 【操作手順】
- PSボタンでペアリング。
- 【走行コントロール】
- コントローラーの左スティック上下で前後進、
- 右スティックの左右でステアリングを制御。
- ステアリングを振っていくと回転軸側が減速していき(緩旋回から信地旋回)、
- 一杯に倒すと回転軸側は停止。(信地旋回)
- 左スティック中央で右ステアリングを一杯に振ると超信地旋回(速度80%) ←ver1.0a
- */
- /*
- G0・・・IN/OUT
- G1・・・IN/OUT *** NG
- G3・・・IN/OUT *** NG
- G26・・・IN/OUT DAC2
- G36・・・IN ONLY ADC
- G18・・・OIN/OUT
- G19・・・IN/OUT
- G21・・・IN/OUT
- G22・・・IN/OUT
- G25・・・IN/OUT DAC1
- G32・・・IN/OUT ADC Grove
- G33・・・IN/OUT ADC Grove
- */
- #include <Arduino.h> // Arduino ヘッダインクルード
- #include "M5Atom.h"
- #include <Ps3Controller.h>
- #include <FastLED.h> // LED操作用ライブラリインクルード(3.5.0を使用)
- #define NUM_LEDS 1
- #define DATA_PIN 27
- CRGB leds[NUM_LEDS];
- int cnt = 0 ;
- int motor_speed;
- int steering;
- int AIN1 = 22; // A入力1/APHASE 左モータ AIN1
- int BIN1 = 21; // B入力1/BPHASE 右モータ BIN1
- int PWMApin = 25; // A入力2/AENABLE 左モータ AIN2
- int PWMBpin = 26; // B入力2/BENABLE 右モータ BIN2
- int PWMA = 2; //PWMAチャンネル 0=NG 1=NG 0〜15
- int PWMB = 3; //PWMAチャンネル 0=NG 1=NG 0〜15
- int pos_y;
- int pos_x;
- int LED_8 = 19 ; // ヘッドランプ
- void onConnect() {
- Serial.println("Connected.");
- for (int cnt = 0; cnt < 2; cnt++) {
- digitalWrite(LED_8, HIGH);
- delay(500);
- digitalWrite(LED_8, LOW);
- delay(500);
- }
- }
- void setup() {
- Serial.begin(115200);
- M5.begin(false, false , true); //SerialEnable, I2CEnable ,DisplayEnable
- FastLED.addLeds<SK6812, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
- leds[0] = 0xf00000; //green
- FastLED.show();
- ledcSetup(PWMA, 5000, 8); //チャンネル,周波数,解像度(8bit=256段階)
- ledcAttachPin(PWMApin, PWMA); //ledPinをPWMCHチャンネルに接続
- ledcSetup(PWMB, 5000, 8); //チャンネル,周波数,解像度(8bit=256段階)
- ledcAttachPin(PWMBpin, PWMB); //ledPinをPWMCHチャンネルに接続
- //pinMode(PWMApin, OUTPUT);
- //pinMode(PWMBpin, OUTPUT);
- pinMode(AIN1, OUTPUT);
- pinMode(BIN1, OUTPUT);
- pinMode(LED_8, OUTPUT);
- Ps3.attachOnConnect(onConnect);
- //Ps3.begin("00:11:22:33:44:55"); //SixaxisPairToolで調べたmac adresに修正
- Ps3.begin("00:00:00:00:00:26"); // PS3-sony
- Serial.println("PS3 Ready");
- }
- void loop() {
- if (Ps3.isConnected()) {
- //走行コントロール
- pos_y = Ps3.data.analog.stick.ly;
- pos_x = Ps3.data.analog.stick.rx;
- pos_y = pos_y + 128;
- pos_x = pos_x + 128;
- //左スティックがセンター付近は停止(ブレーキ)
- if (pos_x >= 117 && pos_x <= 137 && pos_y >= 117 && pos_y <= 137) {
- motor_run(0, 0, 0, 0, 1);
- }
- //前進
- if (pos_y <= 102 && pos_x >= 102 && pos_x <= 152) {
- motor_speed = map(pos_y, 102, 0, 0, 255);
- motor_run(motor_speed, 0, motor_speed, 0, 0);
- }
- //後進
- else if ( pos_y >= 152 && pos_x >= 102 && pos_x <= 152) {
- motor_speed = map(pos_y, 152, 255, 0, 255) ;
- motor_run(motor_speed, 1, motor_speed, 1, 0);
- }
- //前進右緩旋回、信地旋回
- //else if ( pos_y <= 102 && pos_x > 152 && pos_x <= 245) {
- else if ( pos_y <= 102 && pos_x >= 152) {
- motor_speed = map(pos_y, 102, 0, 0, 255);
- steering = motor_speed * (1.00 - (map(pos_x, 152, 255, 0, 255) / 255.00));
- motor_run(motor_speed, 0, steering, 0, 0);
- }
- //前進左緩旋回、信地旋回
- //else if ( pos_y <= 102 && pos_x < 102 && pos_x >= 10) {
- else if ( pos_y <= 102 && pos_x <= 102) {
- motor_speed = map(pos_y, 102, 0, 0, 255);
- steering = motor_speed * (1.00 - (map(pos_x, 102, 0, 0, 255) / 255.00));
- motor_run(steering, 0, motor_speed, 0, 0);
- }
- //後進右緩旋回、信地旋回
- //else if ( pos_y >= 152 && pos_x > 152 && pos_x <= 245) {
- else if ( pos_y >= 152 && pos_x >= 152 ) {
- motor_speed = map(pos_y, 152, 255, 0, 255);
- steering = motor_speed * (1.00 - (map(pos_x, 152, 255, 0, 255) / 255.00));
- motor_run(motor_speed, 1, steering, 1, 0);
- }
- //後進左緩旋回、信地旋回
- //else if ( pos_y >= 152 && pos_x < 102 && pos_x >= 10) {
- else if ( pos_y >= 152 && pos_x <= 102 ) {
- motor_speed = map(pos_y, 152, 255, 0, 255);
- steering = motor_speed * (1.00 - (map(pos_x, 102, 0, 0, 255) / 255.00));
- motor_run(steering, 1, motor_speed, 1, 0);
- }
- //右超信地旋回
- else if (pos_x >= 245 && pos_y >= 102 && pos_y <= 152) {
- motor_speed = 165;
- motor_run(motor_speed, 0, motor_speed, 1, 0);
- }
- //左超信地旋回
- else if (pos_x <= 10 && pos_y >= 102 && pos_y <= 152) {
- motor_speed = 165;
- motor_run(motor_speed, 1, motor_speed, 0, 0);
- }
- else { //停止(ブレーキ)
- motor_run(0, 0, 0, 0, 1);
- }
- }
- }
- void motor_run(int D0, int D1, int D2, int D3, int D4) {
- /* D0 : モータスピード(左)
- D1 : モータA(左)1 = HIGH / 0 = LOW
- D2 : モータスピード(右)
- D3 : モータB(右)1 = HIGH / 0 = LOW
- D4 : LED_3 ON/OFF 1 = HIGH / 0 = LOW
- */
- //analogWrite(PWMA, D0);
- ledcWrite(PWMA, D0); //(チャンネル,解像度)
- digitalWrite(AIN1, D1);
- //analogWrite(PWMB, D2);
- ledcWrite(PWMB, D2); //(チャンネル,解像度)
- digitalWrite(BIN1, D3);
- digitalWrite(LED_8, D4);
- /*Serial.print(" D0="); Serial.print(D0);
- Serial.print(" D1="); Serial.print(D1);
- Serial.print(" D2="); Serial.print(D2);
- Serial.print(" D3="); Serial.print(D3);
- Serial.print(" D4="); Serial.println(D4);
- */
- }
※コメント投稿者のブログIDはブログ作成者のみに通知されます