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

Ganponブログ

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

ESP32 Dev ModuleでRC戦車コントロール(IR対戦仕様)

2022-06-08 22:40:35 | RC可動コントロール

2022-06-08
ESP32 Dev ModuleでRC可動戦車コントロールの第2段、今回はIR対戦仕様の実験です。

Arduino用に作成したスケッチをESP32用に変更します。
ESP32用のIR送受信のライブラリは IRremoteESP8266.h を使います。
コマンドは少し変更するだけで利用できました。
注意が必要なのは、自分の送信した赤外線信号の反射を誤受信しない様に、
送信の前後は受信中止にしておくことが必要ということです。
Arduino用は受信再開コマンドだけでよかったのですが、ESP32用は受信中止コマンドも必要でした。

▼M26実装基板と同じ形に組上げました。
左下がモータドライバーDVR8835(秋月電子)、右下は5V出力DC昇圧コンバータ(Amazon)。
ESP32の左側にJQ6500(MP3プレーヤー)のUSBコネクタが覗いています。

▼ギヤードモータx2、サーボモータx3(内1個は連続回転仕様)、
LED、赤外線LED、赤外線受光素子、スピーカなどを接続。

▼IR対戦用送受信チェッカ

▼テスト機器全景

 

▼実験中の動画です。
ESP32 Dev ModuleでRC戦車コントロール(IR対戦仕様)  

追伸:画像撮影後ですが、技適取得済の正規PS3コントローラの中古を○○カリで入手しました。

 

▼回路
回路図中でブレーキ、ヘッドライトのLEDは1個で書いてありますが、
実態は太線枠内の様に左右2個です。
IR発光LEDの電流はトランジスタ(2SC1815など)を介して増幅しています。

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

▼スケッチ(プログラム)

  1. /*PS3_ESP32_tank_JQ6500mp3_DRV8835_IR_v6.0a
  2.   2022-06-07
  3.    【操作方法】
  4.   PS3コントローラで操縦します。
  5.   車体の電源をONし、コントローラのPSボタンでペアリング開始。
  6.   インジケータランプ4個高速点滅後に1個点灯でペアリング完了。
  7.   コントローラーの左スティック上下で前後進、右スティックの左右でステアリングを制御。
  8.   ステアリングを切っていくと緩旋回、一杯切るとで信地旋回する。
  9.   また、左スティック中央でステアリング左右一杯で超信地旋回。
  10.   △ボタンで砲身UP、?ボタンで砲身DOWN、□ボタンでセンター位置になる。
  11.   左スティック左右で砲塔旋回。離した位置で止まる。
  12.   R1ボタンで主砲発射。L1ボタンで機銃射撃。
  13.   RIGHTボタンでヘッドライト点灯、LEFTボタンで消灯。
  14.   砲撃時のリコイルアクション車体+砲身
  15.   IR発光、受光装置を付けるとIR対戦可能。
  16.   メインCPU:  ESP32 DevKitC
  17.   モータドライバ:DRV8835
  18.   MP3プレイヤー:JQ6500 MP3プレイヤーモジュール
  19. */
  20.  
  21. #include <Arduino.h>
  22. //#include "SoftwareSerial.h"
  23. //#include "DFRobotDFPlayerMini.h"
  24. #include <JQ6500_Serial.h>
  25. #include <IRremoteESP8266.h>
  26. #include <IRsend.h>
  27. const uint16_t kIrLed = 4;
  28. IRsend irsend(kIrLed);
  29. uint16_t rawData[5] = {4150, 1000, 2050, 2000, 1000};
  30.  
  31. #include <IRrecv.h>
  32. //#include <IRac.h>
  33. //#include <IRtext.h>
  34. #include <IRutils.h>
  35.  
  36. const uint16_t kRecvPin = 13;
  37. IRrecv irrecv(kRecvPin);
  38.  
  39. decode_results results;
  40. //const long sign_hit = 0x4CB0FADD;
  41. const uint32_t value = 0x4CB0FADD;
  42.  
  43. HardwareSerial Serial_df(2);
  44. JQ6500_Serial mp3(Serial_df);
  45. void printDetail(uint8_t type, int value);
  46. #include <Ps3Controller.h>
  47. #include <ESP32Servo.h>
  48. Servo servo1;
  49. int servo1Pin = 27;
  50. Servo servo2;
  51. int servo2Pin = 14;
  52. Servo servo3;
  53. int servo3Pin = 12;
  54.  
  55. int LED_1 = 23; //ブレーキランプ
  56. int LED_2 = 22; //ヘッドライト
  57. int LED_3 = 19; //砲撃
  58. int LED_4 = 18; //銃撃
  59. int k = 0;
  60. int s = 0;
  61. int d_time = 30;
  62. int angle_servo1 = 95; // 上下
  63. int anglenow = angle_servo1 ;
  64. int angle_servo3 = 90; // リコイル
  65. int motor_speed;
  66. float steering;
  67. int pos_y;
  68. int pos_x;
  69. int pos_lx;
  70.  
  71. int AIN1 = 32; // A入力1/APHASE 左モータ AIN1
  72. int BIN1 = 25; // B入力1/BPHASE 右モータ BIN1
  73. int PWMApin = 33; //A5; // A入力2/AENABLE 左モータ AIN2 IO33 A5
  74. int PWMBpin = 26; //A19; // B入力2/BENABLE 右モータ BIN2 IO26 (IO0は起動時不安定)A19
  75. int PWMA = 2; //PWMAチャンネル 0=NG 1=NG 0〜15
  76. int PWMB = 3; //PWMBチャンネル 0=NG 1=NG 0〜15 
  77. int cnt = 0;
  78.  
  79. void setup() {
  80.   irsend.begin();
  81.   irrecv.enableIRIn();
  82.  
  83.   servo1.setPeriodHertz(50);
  84.   servo1.attach(servo1Pin, 500, 2400);
  85.   servo2.setPeriodHertz(50);
  86.   servo2.attach(servo2Pin, 900, 2100);
  87.   servo3.setPeriodHertz(50);
  88.   servo3.attach(servo3Pin, 500, 2400);
  89.   Serial.begin(115200);
  90.   Serial_df.begin(9600); // RX2 = 16, TX2 = 17
  91.   mp3.reset();
  92.   mp3.setVolume(25);
  93.  
  94.   Ps3.begin("00:11:22:33:44:55"); // PS3-Eコントローラ SixaxisPairToolで調べたmac adresに修正
  95.   Serial.println("PS3 Ready");
  96.  
  97.   ledcSetup(PWMA, 12000, 8); //チャンネル,周波数,解像度(8bit=256段階)
  98.   ledcAttachPin(PWMApin, PWMA); //ledPinをPWMCHチャンネルに接続
  99.   ledcSetup(PWMB, 12000, 8); //チャンネル,周波数,解像度(8bit=256段階)
  100.   ledcAttachPin(PWMBpin, PWMB); //ledPinをPWMCHチャンネルに接続
  101.   pinMode(AIN1, OUTPUT);
  102.   pinMode(BIN1, OUTPUT);
  103.   pinMode(LED_1, OUTPUT);
  104.   pinMode(LED_2, OUTPUT);
  105.   pinMode(LED_3, OUTPUT);
  106.   pinMode(LED_4, OUTPUT);
  107.  
  108.   LEDtenmetsu(LED_2);
  109.  
  110.   servo1.write(angle_servo1); // 0 - 180
  111.   delay(100);
  112.   servo2.write(90); // 0 - 180
  113.   delay(100);
  114.   servo3.write(angle_servo3); // 0 - 180
  115.   delay(100);
  116.  
  117.   //myDFPlayer.play(3); //エンジン始動
  118.   mp3.playFileByIndexNumber(3);
  119.   delay(1000);
  120.  
  121. }
  122.  
  123. void loop() {
  124.   //被弾チェック
  125.   HitCHK();
  126.  
  127.   if (Ps3.isConnected()) {
  128.     pos_lx = Ps3.data.analog.stick.lx;
  129.     pos_lx = pos_lx + 128;
  130.  
  131.     //砲塔旋回
  132.     if ( pos_lx > 230) { //右旋回
  133.       servo2.write(80);
  134.     } else if (pos_lx < 25) { //左旋回
  135.       servo2.write(100);
  136.     } else { //停止
  137.       servo2.write(90);
  138.     }
  139.  
  140.     //砲身上下
  141.     if ( Ps3.event.button_down.square) { // center
  142.       if (anglenow > angle_servo1) {
  143.         for (anglenow = anglenow; anglenow >= angle_servo1; anglenow -= 1) {
  144.           servo1.write(anglenow);
  145.           delay(40);
  146.         }
  147.       }
  148.       else {
  149.         for (anglenow = anglenow; anglenow <= angle_servo1; anglenow += 1) {
  150.           servo1.write(anglenow);
  151.           delay(40);
  152.         }
  153.       }
  154.     }
  155.  
  156.     if ( Ps3.event.button_down.triangle) { // up
  157.       for (anglenow = anglenow; anglenow <= (angle_servo1+60); anglenow += 1 ) {
  158.         servo1.write(anglenow);
  159.         delay(40);
  160.       }
  161.     }
  162.  
  163.     if ( Ps3.event.button_down.cross) { // down
  164.       for (anglenow = anglenow; anglenow >= (angle_servo1-15); anglenow -= 1 ) {
  165.         servo1.write(anglenow);
  166.         delay(40);
  167.       }
  168.     }
  169.  
  170.     if (Ps3.event.button_down.circle) {
  171.       if (s == 0) {
  172.         s = 1;
  173.         //myDFPlayer.play(3); //エンジン始動
  174.         mp3.playFileByIndexNumber(3);
  175.         delay(1000);
  176.         mp3.playFileByIndexNumber(5);
  177.         mp3.setLoopMode(MP3_LOOP_ONE);
  178.         delay(d_time);
  179.       } else {
  180.         mp3.setLoopMode(MP3_LOOP_ONE_STOP);
  181.         s = 0;
  182.         delay(100);
  183.       }
  184.     }
  185.  
  186.     if (Ps3.event.button_down.r3) {
  187.       mp3.setLoopMode(MP3_LOOP_ONE_STOP);
  188.       s = 0;
  189.     }
  190.  
  191.     if (Ps3.event.button_down.right) {
  192.       digitalWrite(LED_2, HIGH); // Headlight ON
  193.     }
  194.  
  195.     if (Ps3.event.button_down.left) {
  196.       digitalWrite(LED_2, LOW); // Headlight off
  197.     }
  198.  
  199.     if (Ps3.event.button_down.r1) { //砲撃 + リコイル(砲身&車体)
  200.       digitalWrite(LED_3, HIGH); //主砲発光
  201.       fire();
  202.       //myDFPlayer.play(26);
  203.       mp3.playFileByIndexNumber(2);
  204.       delay(50);
  205.       servo3.write(angle_servo3 - 40); // 砲身リコイル
  206.       delay(20);
  207.       motor_run(180, 1, 180, 1, 1); // 車体リコイル
  208.       delay(60);
  209.       motor_run(0, 0, 0, 0, 1);
  210.       digitalWrite(LED_3, LOW);
  211.       for (int ang3 = (angle_servo3 - 30); ang3 <= angle_servo3; ang3 += 1 ) {
  212.         servo3.write(ang3);
  213.         delay(20);
  214.       }
  215.       //digitalWrite(LED_3, LOW);
  216.       motor_run(100, 0, 100, 0, 1);
  217.       delay(80);
  218.       motor_run(0, 0, 0, 0, 1);
  219.       delay(100);
  220.       if (s == 1) {
  221.         //myDFPlayer.loop(5); //Loop 5th mp3 アイドリング
  222.         mp3.playFileByIndexNumber(5);
  223.         mp3.setLoopMode(MP3_LOOP_ONE);
  224.         delay(d_time);
  225.       }
  226.     }
  227.  
  228.     if (Ps3.event.button_down.l1) { //銃撃
  229.       mp3.playFileByIndexNumber(24);
  230.       for ( k = 0; k < 6; k++) {
  231.         digitalWrite(LED_4, HIGH); //発光
  232.         delay(80);
  233.         digitalWrite(LED_4, LOW);
  234.         delay(120);
  235.       }
  236.       if (s == 1) {
  237.         mp3.playFileByIndexNumber(5);
  238.         mp3.setLoopMode(MP3_LOOP_ONE);
  239.         delay(d_time);
  240.       }
  241.     }
  242.  
  243.     //走行コントロール
  244.     pos_y = Ps3.data.analog.stick.ly;
  245.     pos_x = Ps3.data.analog.stick.rx;
  246.     pos_y = pos_y + 128;
  247.     pos_x = pos_x + 128;
  248.  
  249.     //前進
  250.     if (pos_y <= 102 && pos_x >= 102 && pos_x <= 152) {
  251.       motor_speed = map(pos_y, 102, 0, 0, 255);
  252.       motor_run(motor_speed, 0, motor_speed, 0, 0);
  253.     }
  254.  
  255.     //後進
  256.     else if ( pos_y >= 152 && pos_x >= 102 && pos_x <= 152) {
  257.       motor_speed = map(pos_y, 152, 255, 0, 255) ;
  258.       motor_run(motor_speed, 1, motor_speed, 1, 0);
  259.     }
  260.  
  261.     //前進右緩旋回、信地旋回
  262.     //else if ( pos_y <= 102 && pos_x > 152 && pos_x <= 245) {
  263.     else if ( pos_y <= 102 && pos_x >= 152) {
  264.       motor_speed = map(pos_y, 102, 0, 0, 255);
  265.       steering = motor_speed * (1.00 - (map(pos_x, 152, 255, 0, 255) / 255.00));
  266.       motor_run(motor_speed, 0, steering, 0, 0);
  267.     }
  268.  
  269.     //前進左緩旋回、信地旋回
  270.     //else if ( pos_y <= 102 && pos_x < 102 && pos_x >= 10) {
  271.     else if ( pos_y <= 102 && pos_x <= 102) {
  272.       motor_speed = map(pos_y, 102, 0, 0, 255);
  273.       steering = motor_speed * (1.00 - (map(pos_x, 102, 0, 0, 255) / 255.00));
  274.       motor_run(steering, 0, motor_speed, 0, 0);
  275.     }
  276.  
  277.     //後進右緩旋回、信地旋回
  278.     //else if ( pos_y >= 152 && pos_x > 152 && pos_x <= 245) {
  279.     else if ( pos_y >= 152 && pos_x >= 152 ) {
  280.       motor_speed = map(pos_y, 152, 255, 0, 255);
  281.       steering = motor_speed * (1.00 - (map(pos_x, 152, 255, 0, 255) / 255.00));
  282.       motor_run(motor_speed, 1, steering, 1, 0);
  283.     }
  284.  
  285.     //後進左緩旋回、信地旋回
  286.     //else if ( pos_y >= 152 && pos_x < 102 && pos_x >= 10) {
  287.     else if ( pos_y >= 152 && pos_x <= 102 ) {
  288.       motor_speed = map(pos_y, 152, 255, 0, 255);
  289.       steering = motor_speed * (1.00 - (map(pos_x, 102, 0, 0, 255) / 255.00));
  290.       motor_run(steering, 1, motor_speed, 1, 0);
  291.     }
  292.  
  293.     //右超信地旋回
  294.     else if (pos_x >= 245 && pos_y >= 102 && pos_y <= 152) {
  295.       motor_speed = 204;
  296.       motor_run(motor_speed, 0, motor_speed, 1, 0);
  297.     }
  298.  
  299.     //左超信地旋回
  300.     else if (pos_x <= 10 && pos_y >= 102 && pos_y <= 152) {
  301.       motor_speed = 204;
  302.       motor_run(motor_speed, 1, motor_speed, 0, 0);
  303.     }
  304.  
  305.     else { //停止(ブレーキ)
  306.       motor_run(0, 0, 0, 0, 1);
  307.     }
  308.   }
  309. }
  310.  
  311.  
  312. void motor_run(int D0, int D1, int D2, int D3, int D4) {
  313.  
  314.   ledcWrite(PWMA, D0); //(チャンネル,解像度)
  315.   digitalWrite(AIN1, D1);
  316.   ledcWrite(PWMB, D2); //(チャンネル,解像度)
  317.   digitalWrite(BIN1, D3);
  318.   digitalWrite(LED_1, D4);
  319. }
  320.  
  321. void LEDtenmetsu(int LED) {
  322.   for (int i = 0; i < 2; i++) {
  323.     digitalWrite(LED, HIGH);
  324.     delay(500);
  325.     digitalWrite(LED, LOW);
  326.     delay(500);
  327.   }
  328. }
  329.  
  330. void fire() {
  331.   irrecv.disableIRIn(); // 自らの送信を誤受信しないように受信を停止  
  332.   irsend.sendRaw(rawData, 5, 38); //irsend.sendRaw(data buf, length, hertz)
  333.   delay(10);
  334.   irrecv.enableIRIn(); // 受信を再開する  
  335. }
  336.  
  337. void HitCHK() {
  338.   if (irrecv.decode(&results)) { // 受信コードの値が
  339.     if (results.value == value) { // 0x4CB0FADDだったら被弾
  340.       mp3.playFileByIndexNumber(17);
  341.       Hit(); //被弾
  342.     }
  343.     irrecv.resume();
  344.     delay(10);
  345.   }
  346. }
  347.  
  348. // ====被弾====
  349. void Hit() {
  350.   motor_run(180, 1, 180, 0, 0);
  351.   delay(100);
  352.   motor_run(0, 0, 0, 0, 1);
  353.   delay(50);
  354.   motor_run(180, 0, 180, 1, 0);
  355.   delay(80);
  356.   motor_run(0, 0, 0, 0, 1);
  357.   delay(50);
  358.   cnt++;
  359.   if ( cnt >= 5 ) {
  360.     mp3.playFileByIndexNumber(18);
  361.     motor_run(0, 0, 0, 0, 0);
  362.     for ( int i = 0; i < 15 ; i++) {
  363.       digitalWrite(LED_2, HIGH);
  364.       delay(1000);
  365.       digitalWrite(LED_2, LOW);
  366.       delay(1000);
  367.     } //復活までに15秒
  368.     cnt = 0;
  369.   } else {
  370.     delay(1000);
  371.   }
  372. }


コメントを投稿