program to shoot beenbags

Dependencies:   mbed MOTOR

arrcmbedをインクルードしてください。

Committer:
gennnisi
Date:
Tue Jan 25 23:56:51 2022 +0000
Revision:
15:7c26b01dcefd
Parent:
14:81f09ab5ed23
Child:
16:04f42eeb9561
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gennnisi 0:0128d59ac729 1 //発射、リボルバープログラム
gennnisi 0:0128d59ac729 2 #include "mbed.h"
gennnisi 0:0128d59ac729 3 #include "rotary_inc.hpp"
gennnisi 0:0128d59ac729 4 #include "scrp_slave.hpp"
gennnisi 0:0128d59ac729 5 #include "Motor.hpp"
gennnisi 0:0128d59ac729 6
gennnisi 9:6008a52fb6ef 7 Motor motor_revolver(PB_4,PB_5);//リボルバーモーター
gennnisi 12:aef2a6626ea5 8 Motor motor_shoot(PB_1,PA_11);//発射機構モーター
gennnisi 9:6008a52fb6ef 9 PwmOut servo(PB_8);//ロック解除用サーボ
gennnisi 9:6008a52fb6ef 10
gennnisi 0:0128d59ac729 11 //ScrpSlave slave(PA_9,PA_10,PA_12,SERIAL_TX,SERIAL_RX,0x0803f800);//l432kc
gennnisi 12:aef2a6626ea5 12 ScrpSlave slave(PC_12,PD_2,PH_1,SERIAL_TX,SERIAL_RX,0x0807f800);
gennnisi 0:0128d59ac729 13
gennnisi 12:aef2a6626ea5 14 //RotaryInc v(PA_14,PA_15,1,1024,4);//リボルバーロリコン
gennnisi 12:aef2a6626ea5 15 RotaryInc v(PA_14,PA_15,1,1024,2);
gennnisi 0:0128d59ac729 16
gennnisi 9:6008a52fb6ef 17 DigitalIn limit_1(PA_9);//リミットスイッチ1
gennnisi 9:6008a52fb6ef 18 DigitalIn limit_2(PA_8);//リミットスイッチ2
gennnisi 6:7dcc827bbe96 19
gennnisi 13:66e37e0aa786 20 int shoot_phase;//発射フェーズ 0:待機 1:装填動作 2:リボルバー回転、モーター復帰(同時) 3:発射 
gennnisi 10:5e3c789e07dc 21 bool turn_able;//回転可否 0:待機 1:回転
gennnisi 0:0128d59ac729 22
gennnisi 12:aef2a6626ea5 23 double revolver_pwm = -0.03;//リボルバー回転速度
gennnisi 9:6008a52fb6ef 24 double load_pwm = -0.3;//装填モーター速度
gennnisi 9:6008a52fb6ef 25 double back_pwm = 0.3;//復帰モーター速度
gennnisi 9:6008a52fb6ef 26
gennnisi 12:aef2a6626ea5 27 double turn = 0.0;
gennnisi 12:aef2a6626ea5 28
gennnisi 12:aef2a6626ea5 29 int theta_0 = 1290;//0度
gennnisi 12:aef2a6626ea5 30 int theta_45 = 1875;//45度
gennnisi 9:6008a52fb6ef 31 int roricon = 0;//ロリコン
gennnisi 0:0128d59ac729 32
gennnisi 0:0128d59ac729 33 bool interrupt(int rx_data,int &tx_data){
gennnisi 10:5e3c789e07dc 34 if(shoot_phase == 0){//フェーズ0以外は、発射できないように
gennnisi 10:5e3c789e07dc 35 shoot_phase = rx_data;//1を受け取って装填動作開始
gennnisi 0:0128d59ac729 36 }
gennnisi 0:0128d59ac729 37 return true;
gennnisi 0:0128d59ac729 38 }
gennnisi 0:0128d59ac729 39
gennnisi 13:66e37e0aa786 40 void revolver_back(int count){
gennnisi 9:6008a52fb6ef 41 if(turn_able == 1){
gennnisi 14:81f09ab5ed23 42 while(roricon < 341 * (count - 1) || limit_2.read() == 0){
gennnisi 14:81f09ab5ed23 43 if(count != 1){
gennnisi 14:81f09ab5ed23 44 if(roricon < 341 * (count - 1)){
gennnisi 14:81f09ab5ed23 45 roricon = v.get();
gennnisi 14:81f09ab5ed23 46 motor_revolver.output(revolver_pwm);
gennnisi 14:81f09ab5ed23 47 printf("roricon = %d\n",roricon);
gennnisi 14:81f09ab5ed23 48 }else{
gennnisi 14:81f09ab5ed23 49 motor_revolver.output(0);
gennnisi 14:81f09ab5ed23 50 turn_able = 0;
gennnisi 14:81f09ab5ed23 51 printf("finished turnning\n");
gennnisi 14:81f09ab5ed23 52 }
gennnisi 13:66e37e0aa786 53 }
gennnisi 13:66e37e0aa786 54
gennnisi 13:66e37e0aa786 55 if(limit_2.read() == 0){
gennnisi 13:66e37e0aa786 56 motor_shoot.output(back_pwm);
gennnisi 13:66e37e0aa786 57 printf("backing\n");
gennnisi 13:66e37e0aa786 58 }
gennnisi 13:66e37e0aa786 59
gennnisi 15:7c26b01dcefd 60 wait(0.01);
gennnisi 9:6008a52fb6ef 61 }
gennnisi 9:6008a52fb6ef 62 }
gennnisi 9:6008a52fb6ef 63 }
gennnisi 9:6008a52fb6ef 64
gennnisi 9:6008a52fb6ef 65 int main(){
gennnisi 9:6008a52fb6ef 66 int count = 1;//何発目か
gennnisi 12:aef2a6626ea5 67 shoot_phase = 0;
gennnisi 9:6008a52fb6ef 68
gennnisi 9:6008a52fb6ef 69 servo.period_us(20000);
gennnisi 10:5e3c789e07dc 70 limit_1.mode(PullUp);
gennnisi 10:5e3c789e07dc 71 limit_2.mode(PullUp);
gennnisi 9:6008a52fb6ef 72
gennnisi 9:6008a52fb6ef 73 slave.addCMD(2,interrupt);
gennnisi 12:aef2a6626ea5 74
gennnisi 9:6008a52fb6ef 75 while(1){
gennnisi 10:5e3c789e07dc 76 switch (shoot_phase){
gennnisi 9:6008a52fb6ef 77
gennnisi 9:6008a52fb6ef 78 case 0://待機
gennnisi 10:5e3c789e07dc 79 while(shoot_phase == 0){
gennnisi 9:6008a52fb6ef 80 wait(0.1);
gennnisi 9:6008a52fb6ef 81 printf("waiting\n");
gennnisi 9:6008a52fb6ef 82 }
gennnisi 10:5e3c789e07dc 83 shoot_phase = 1;
gennnisi 9:6008a52fb6ef 84 printf("finished waiting\n");
gennnisi 9:6008a52fb6ef 85 break;
gennnisi 9:6008a52fb6ef 86
gennnisi 9:6008a52fb6ef 87 case 1://装填動作
gennnisi 9:6008a52fb6ef 88 while(limit_1.read() == 0){
gennnisi 10:5e3c789e07dc 89 motor_shoot.output(load_pwm);
gennnisi 11:62c85df03747 90 printf("loading\n");
gennnisi 11:62c85df03747 91 wait(0.1);
gennnisi 15:7c26b01dcefd 92 if(limit_2.read() == 1){
gennnisi 15:7c26b01dcefd 93 break;
gennnisi 15:7c26b01dcefd 94 }
gennnisi 11:62c85df03747 95 }
gennnisi 12:aef2a6626ea5 96 motor_shoot.output(0);
gennnisi 12:aef2a6626ea5 97 shoot_phase = 2;
gennnisi 12:aef2a6626ea5 98 printf("finished loading\n");
gennnisi 12:aef2a6626ea5 99 wait(5);
gennnisi 9:6008a52fb6ef 100 break;
gennnisi 12:aef2a6626ea5 101
gennnisi 13:66e37e0aa786 102 case 2://リボルバー回転,モーター復帰
gennnisi 9:6008a52fb6ef 103 turn_able = 1;
gennnisi 13:66e37e0aa786 104 revolver_back(count);
gennnisi 13:66e37e0aa786 105 printf("finished backing\n");
gennnisi 13:66e37e0aa786 106 shoot_phase = 3;
gennnisi 9:6008a52fb6ef 107 break;
gennnisi 12:aef2a6626ea5 108
gennnisi 13:66e37e0aa786 109 case 3://発射動作
gennnisi 13:66e37e0aa786 110 wait(5);
gennnisi 12:aef2a6626ea5 111 servo.pulsewidth_us(theta_45);
gennnisi 11:62c85df03747 112 printf("finished shooting %d shot\n",count);
gennnisi 9:6008a52fb6ef 113 count ++;
gennnisi 12:aef2a6626ea5 114 wait(5);
gennnisi 9:6008a52fb6ef 115 servo.pulsewidth_us(theta_0);
gennnisi 15:7c26b01dcefd 116 motor_shoot.output(load_pwm);
gennnisi 15:7c26b01dcefd 117 wait(0.1);
gennnisi 15:7c26b01dcefd 118 motor_shoot.output(0);
gennnisi 10:5e3c789e07dc 119 shoot_phase = 0;
gennnisi 11:62c85df03747 120 printf("finished init\n");
gennnisi 12:aef2a6626ea5 121 break;
gennnisi 11:62c85df03747 122 }
gennnisi 9:6008a52fb6ef 123 }
gennnisi 12:aef2a6626ea5 124 }