2021-07-26
要望があったので、2年前に製作したRC可動化キングタイガーの回路図を掲載します。
当時の資料を探しましたが、時間がたっていますので、違っているところがあるかもしれません。
- * PS4BT_arduino_pro mini_tank_DRV8835_KING_TIGER_ser_ver04c
- 【走行コントロール】
- コントローラーの左スティック上下で前後進、
- 右スティックの左右でステアリングを制御。
- ステアリングを振っていくと回転軸側が減速していき(緩旋回から信地旋回)、
- 一杯に倒すと回転軸側は停止。(信地旋回)
- 左スティック中央で右ステアリングを一杯に振ると超信地旋回(速度50%)
- 【砲身上下】 右スティック上下で砲身上下。R3ボタンでセンター。
- // 右側の△ボタンで砲身上、×ボタンで砲身下、□ボタンでセンター位置になる。
- 【砲塔旋回】 左スティック左右で砲塔旋回。離した位置で止まる。
- 【主砲】 R1ボタンで主砲発射
- 【機銃】 L1ボタンで機銃射撃
- 【ヘッドライト】RIGHTボタンで点灯、LEFTボタンで消灯。
- 【車間表示灯】UPボタンで点灯、DOWNボタンで消灯。
- モータドライバはDRX8835
- 砲撃時のリコイルアクション 車体
- 主砲発光はミニUSBシールドのGPOUTピン0を使用
- サブシステムで実行
- irremote PIN3 赤外線発光(砲撃)システム
- 赤外線受光モジュール出力を接続し、被弾処理を追加
- MP3プレーヤーで効果音を発生
- シリアル通信データ
- エンジン始動 100
- 砲撃 101
- 銃撃 102
- 被弾 103
- 破壊 105
- エンジン停止/始動 110
- */
- #include <PS4BT.h>
- #include <usbhub.h>
- #ifdef dobogusinclude
- #include <spi4teensy3.h>
- #endif
- #include <SPI.h>
- USB Usb;
- BTD Btd(&Usb);
- PS4BT PS4(&Btd, PAIR);
- bool printAngle, printTouch;
- uint8_t oldL2Value, oldR2Value;
- #include <USB_Host_Shield_GPIO.h> // GPOUTピンで主砲LEDを発光させるため
- MaxGPIO max;
- int cnt = 0 ;
- byte ser_deta;
- #include <VarSpeedServo.h>
- VarSpeedServo myservo_1;
- VarSpeedServo myservo_2;
- VarSpeedServo myservo_3;
- int servo1_pos = 60;
- int servo_pos = servo1_pos;
- const int servo1_sp = 5;
- const int servo2_sp = 1;
- int servo3_pos = 50;
- int ch = 0;
- int pos_y;
- int pos_x;
- int pos_ry;
- int pos_rx;
- int motor_speed;
- float steering;
- int PWMA = 5;
- int PWMB = 6;
- int AIN1 = 4;
- int BIN1 = 7;
- const int LED_2 = 2;
- const int LED_3 = 8;
- const int LED_4 = A2;
- void setup() {
- Serial.begin (115200);
- #if !defined(__MIPSEL__)
- while (!Serial);
- #endif
- if (Usb.Init() == -1) {
- Serial.print(F("\r\nOSC did not start"));
- while (1); // Halt
- }
- Serial.print(F("\r\nPS4 Bluetooth Library Started"));
- pinMode(PWMA, OUTPUT);
- pinMode(PWMB, OUTPUT);
- pinMode(AIN1, OUTPUT);
- pinMode(BIN1, OUTPUT);
- pinMode(LED_2, OUTPUT);
- pinMode(LED_3, OUTPUT);
- pinMode(LED_4, OUTPUT);
- pinMode(A4, OUTPUT);
- pinMode(A5, OUTPUT);
- pinMode(A1, OUTPUT);
- myservo_1.attach(A4);
- myservo_1.write(servo_pos);
- myservo_2.attach(A5);
- myservo_2.write(90);
- //myservo_2.detach();
- myservo_3.attach(A1);
- myservo_3.write(servo3_pos);
- }
- void loop() {
- Usb.Task();
- if (PS4.connected()) {
- if ( Serial.available() >= 2 ) {
- if ( Serial.read() == 'H' ) {
- ser_deta = Serial.read() ;
- }
- }
- if (ser_deta == 103) {
- Hit();
- }
- //エンジン音停止
- if (PS4.getButtonClick(CIRCLE)) {
- sendData(110);
- }
- // 砲身 UP/DOWN
- if ( PS4.getButtonClick(R3)) { //CENTER
- myservo_1.attach(A4);
- myservo_1.write(servo1_pos, servo1_sp, true);
- // myservo_1.detach();
- }
- pos_ry = PS4.getAnalogHat(RightHatY);
- if (pos_ry < 10) { // UP
- myservo_1.attach(A4);
- myservo_1.write(servo1_pos - 40, servo1_sp, true);
- // myservo_1.detach();
- } else if (pos_ry > 245) { //DOWN
- myservo_1.attach(A4);
- myservo_1.write(servo1_pos +25, servo1_sp, true);
- // myservo_1.detach();
- } else { //停止
- myservo_1.detach();
- }
- //砲塔旋回
- //pos_rx = PS4.getAnalogHat(LeftHatX);
- if ( PS4.getAnalogHat(LeftHatX) > 230) {
- myservo_2.attach(A5);
- myservo_2.write(95, servo2_sp);
- } else if (PS4.getAnalogHat(LeftHatX) < 26) {
- myservo_2.attach(A5);
- myservo_2.write(85, servo2_sp);
- } else {
- myservo_2.write(90, 255);
- myservo_2.detach();
- }
- //砲撃 + リコイル
- if (PS4.getButtonClick(R1) && ch == 0 ) {
- sendData(101);
- delay(100);
- max.write(0, HIGH);
- //myservo_3.attach(A1);
- myservo_3.write(servo3_pos+50, 255);
- delay(100);
- motor_run(200, 1, 200, 1, 1);
- ch = 2000;
- myservo_3.write(servo3_pos, 15);
- delay(100);
- motor_run(0, 0, 0, 0, 1);
- // myservo_3.write(servo3_pos, 20);
- delay(40);
- max.write(0, LOW);
- motor_run(100, 0, 100, 0, 1);
- delay(260);
- motor_run(0, 0, 0, 0, 1);
- delay(140);
- //myservo_3.detach();
- } else {
- if (ch > 0) {
- ch--;
- }
- }
- //銃撃
- if ( PS4.getButtonClick(L1)) {
- sendData(102);
- delay(100);
- for ( int i = 0; i < 5 ; i++) {
- digitalWrite(LED_2, HIGH);
- delay(80);
- digitalWrite(LED_2, LOW);
- delay(120);
- }
- }
- //ヘッドライト
- if (PS4.getButtonClick(RIGHT)) {
- max.write(1, HIGH);
- }
- if (PS4.getButtonClick(LEFT)) {
- max.write(1, LOW);
- }
- //車間表示灯
- if (PS4.getButtonClick(UP)) {
- digitalWrite(LED_4, HIGH);
- }
- if (PS4.getButtonClick(DOWN)) {
- digitalWrite(LED_4, LOW);
- }
- //走行コントロール
- pos_y = PS4.getAnalogHat(LeftHatY);
- pos_x = PS4.getAnalogHat(RightHatX);
- //前進
- 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 = 128;
- motor_run(motor_speed, 0, motor_speed, 1, 0);
- }
- //左超信地旋回
- else if (pos_x < 10 && pos_y >= 102 && pos_y <= 152) {
- motor_speed = 128;
- 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) {
- analogWrite(PWMA, D0);
- digitalWrite(AIN1, D1);
- analogWrite(PWMB, D2);
- digitalWrite(BIN1, D3);
- digitalWrite(LED_3, D4);
- }
- // ====被弾====
- void Hit() {
- ser_deta = 0;
- motor_run(255, 1, 255, 0, 0);
- delay(150);
- motor_run(0, 0, 0, 0, 1);
- delay(100);
- motor_run(200, 0, 200, 1, 0);
- delay(200);
- motor_run(0, 0, 0, 0, 1);
- delay(50);
- cnt++;
- if ( cnt >= 5 ) {
- sendData(105);
- motor_run(0, 0, 0, 0, 0);
- for ( int i = 0; i < 15 ; i++) {
- max.write(1, HIGH);
- delay(1000);
- max.write(1, LOW);
- delay(1000);
- }
- //delay(30000);
- cnt = 0;
- } else {
- delay(1000);
- }
- }
- void sendData(byte val) {
- Serial.write('H');
- Serial.write(val);
- }
- /* arduino_pro mini_tank_IRw_MP3_sub_ver03b
- 2019-06-15
- 本サブシステムの内容
- irremote PIN3 赤外線発光(砲撃)システム
- 赤外線受光モジュール出力を接続し、被弾処理を追加
- MP3プレーヤーで効果音を発生
- エンジン始動信号受信でエンジン始動、アイドリング音発生。
- シリアル通信データ
- エンジン始動 100
- 砲撃 101
- 銃撃 102
- 被弾 103
- 破壊 105
- エンジン停止/始動 110
- */
- #include
- #include
- SoftwareSerial mySerial(A2, A3);
- DFRobotDFPlayerMini myDFPlayer;
- #include
- IRsend irsend;
- int RECV_PIN = A0 ; /
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- const long sign_hit = 0x4CB0FADD;
- byte recv_data;
- const int LED_1 = 8;
- int silent = 1;
- int khz = 38;
- unsigned int irSignal[] = {4100, 1000, 2050, 2050, 1000};
- int d_time = 200;
- void setup() {
- irrecv.enableIRIn(); // Start the receiver
- pinMode(RECV_PIN, INPUT) ;
- Serial.begin (115200);
- mySerial.begin (9600);
- myDFPlayer.begin(mySerial);
- delay(100);
- myDFPlayer.volume(22);
- //pinMode(LED_1, OUTPUT);
- }
- void loop() {
- if (irrecv.decode(&results)) {
- if (results.value == sign_hit) {
- sendData(103);
- myDFPlayer.play(17);
- delay(1000);
- }
- irrecv.resume();
- }
- if ( Serial.available() >= 2 ) {
- if ( Serial.read() == 'H' ) {
- recv_data = Serial.read() ;
- }
- }
- if (recv_data == 110) {
- silent = 1 - silent;
- // recv_data = 0;
- }
- if (silent == 1) {
- myDFPlayer.disableLoop();
- } else if ( (silent == 0) && (recv_data == 110)) {
- myDFPlayer.play(3);
- delay(2000);
- myDFPlayer.loop(5);
- delay(d_time);
- } else {
- myDFPlayer.loop(5);
- delay(d_time);
- }
- if ( recv_data == 101) {
- irsend.sendRaw(irSignal, (sizeof(irSignal) / sizeof(irSignal[0])), khz);
- //irsend.sendRaw(data buf, length, hertz)
- irrecv.enableIRIn();
- PLAY_MP3(15);
- } else if ( recv_data == 102 ) {
- PLAY_MP3(10);
- } else if ( recv_data == 105 ) {
- PLAY_MP3(18);
- delay(30000);
- PLAY_MP3(3);
- delay(2000);
- }
- recv_data = 0;
- }
- void sendData(byte val) {
- Serial.write('H');
- Serial.write(val);
- }
- void PLAY_MP3(int LT) {
- recv_data = 0;
- //digitalWrite(LED_1, HIGH);
- myDFPlayer.play(LT);
- delay(900);
- //digitalWrite(LED_1, LOW);
- }