program to shoot beenbags

Dependencies:   mbed MOTOR

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

Committer:
gennnisi
Date:
Wed Jan 19 14:34:32 2022 +0000
Revision:
11:62c85df03747
Parent:
10:5e3c789e07dc
Child:
12:aef2a6626ea5
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 9:6008a52fb6ef 8 Motor motor_shoot(PB_13,PB_14);//発射機構モーター
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 0:0128d59ac729 12 ScrpSlave slave(PC_12,PD_2,PH_1,SERIAL_TX,SERIAL_RX,0x0807f800);//f446re
gennnisi 0:0128d59ac729 13
gennnisi 9:6008a52fb6ef 14 RotaryInc v(PA_14,PA_15,1,1024,4);//リボルバーロリコン
gennnisi 0:0128d59ac729 15
gennnisi 9:6008a52fb6ef 16 DigitalIn limit_1(PA_9);//リミットスイッチ1
gennnisi 9:6008a52fb6ef 17 DigitalIn limit_2(PA_8);//リミットスイッチ2
gennnisi 6:7dcc827bbe96 18
gennnisi 10:5e3c789e07dc 19 int shoot_phase;//発射フェーズ 0:待機 1:装填動作 2:リボルバー回転 3:モーター復帰 4:発射 
gennnisi 10:5e3c789e07dc 20 bool turn_able;//回転可否 0:待機 1:回転
gennnisi 0:0128d59ac729 21
gennnisi 0:0128d59ac729 22 double revolver_pwm = 0.1;//リボルバー回転速度
gennnisi 9:6008a52fb6ef 23 double load_pwm = -0.3;//装填モーター速度
gennnisi 9:6008a52fb6ef 24 double back_pwm = 0.3;//復帰モーター速度
gennnisi 9:6008a52fb6ef 25
gennnisi 9:6008a52fb6ef 26 int theta_0 = 1450;//0度
gennnisi 10:5e3c789e07dc 27 int theta_90 = 1875;//45度
gennnisi 9:6008a52fb6ef 28 int roricon = 0;//ロリコン
gennnisi 0:0128d59ac729 29
gennnisi 0:0128d59ac729 30 bool interrupt(int rx_data,int &tx_data){
gennnisi 10:5e3c789e07dc 31 if(shoot_phase == 0){//フェーズ0以外は、発射できないように
gennnisi 10:5e3c789e07dc 32 shoot_phase = rx_data;//1を受け取って装填動作開始
gennnisi 0:0128d59ac729 33 }
gennnisi 0:0128d59ac729 34 return true;
gennnisi 0:0128d59ac729 35 }
gennnisi 0:0128d59ac729 36
gennnisi 9:6008a52fb6ef 37 void revolver(int count){
gennnisi 9:6008a52fb6ef 38 if(turn_able == 1){
gennnisi 9:6008a52fb6ef 39 while(roricon < 171 * count){
gennnisi 9:6008a52fb6ef 40 roricon = v.get();
gennnisi 9:6008a52fb6ef 41 motor_revolver.output(revolver_pwm);
gennnisi 9:6008a52fb6ef 42 printf("roricon = %d\n",roricon);
gennnisi 9:6008a52fb6ef 43 wait(0.1);
gennnisi 9:6008a52fb6ef 44 }
gennnisi 9:6008a52fb6ef 45 motor_revolver.output(0.0);
gennnisi 9:6008a52fb6ef 46 turn_able = 0;
gennnisi 9:6008a52fb6ef 47 }
gennnisi 9:6008a52fb6ef 48 }
gennnisi 9:6008a52fb6ef 49
gennnisi 9:6008a52fb6ef 50 int main(){
gennnisi 9:6008a52fb6ef 51 int count = 1;//何発目か
gennnisi 9:6008a52fb6ef 52
gennnisi 9:6008a52fb6ef 53 servo.period_us(20000);
gennnisi 10:5e3c789e07dc 54 limit_1.mode(PullUp);
gennnisi 10:5e3c789e07dc 55 limit_2.mode(PullUp);
gennnisi 9:6008a52fb6ef 56
gennnisi 9:6008a52fb6ef 57 slave.addCMD(2,interrupt);
gennnisi 9:6008a52fb6ef 58 while(1){
gennnisi 10:5e3c789e07dc 59 switch (shoot_phase){
gennnisi 9:6008a52fb6ef 60
gennnisi 9:6008a52fb6ef 61 case 0://待機
gennnisi 10:5e3c789e07dc 62 while(shoot_phase == 0){
gennnisi 9:6008a52fb6ef 63 wait(0.1);
gennnisi 9:6008a52fb6ef 64 printf("waiting\n");
gennnisi 9:6008a52fb6ef 65 }
gennnisi 10:5e3c789e07dc 66 shoot_phase = 1;
gennnisi 9:6008a52fb6ef 67 printf("finished waiting\n");
gennnisi 9:6008a52fb6ef 68 break;
gennnisi 9:6008a52fb6ef 69
gennnisi 9:6008a52fb6ef 70 case 1://装填動作
gennnisi 9:6008a52fb6ef 71 while(limit_1.read() == 0){
gennnisi 10:5e3c789e07dc 72 motor_shoot.output(load_pwm);
gennnisi 11:62c85df03747 73 printf("loading\n");
gennnisi 11:62c85df03747 74 wait(0.1);
gennnisi 11:62c85df03747 75 }
gennnisi 9:6008a52fb6ef 76 motor_shoot.output(0.0);
gennnisi 10:5e3c789e07dc 77 wait(1);
gennnisi 10:5e3c789e07dc 78 //shoot_phase = 2;
gennnisi 10:5e3c789e07dc 79 shoot_phase = 3;
gennnisi 9:6008a52fb6ef 80 printf("finished loading\n");
gennnisi 9:6008a52fb6ef 81 break;
gennnisi 10:5e3c789e07dc 82 /*
gennnisi 9:6008a52fb6ef 83 case 2://リボルバー回転
gennnisi 9:6008a52fb6ef 84 turn_able = 1;
gennnisi 9:6008a52fb6ef 85 revolver(count);
gennnisi 9:6008a52fb6ef 86 if(turn_able == 0){
gennnisi 10:5e3c789e07dc 87 shoot_phase = 3;
gennnisi 9:6008a52fb6ef 88 }
gennnisi 9:6008a52fb6ef 89 printf("finished turnning\n");
gennnisi 9:6008a52fb6ef 90 break;
gennnisi 10:5e3c789e07dc 91 */
gennnisi 9:6008a52fb6ef 92 case 3://モーター復帰
gennnisi 9:6008a52fb6ef 93 while(limit_2.read() == 0){
gennnisi 9:6008a52fb6ef 94 motor_shoot.output(back_pwm);
gennnisi 11:62c85df03747 95 printf("backing\n");
gennnisi 11:62c85df03747 96 wait(0.1);
gennnisi 9:6008a52fb6ef 97 }
gennnisi 9:6008a52fb6ef 98 motor_shoot.output(0.0);
gennnisi 10:5e3c789e07dc 99 shoot_phase = 4;
gennnisi 9:6008a52fb6ef 100 printf("finished backing\n");
gennnisi 9:6008a52fb6ef 101 break;
gennnisi 9:6008a52fb6ef 102
gennnisi 9:6008a52fb6ef 103 case 4://発射動作
gennnisi 9:6008a52fb6ef 104 servo.pulsewidth_us(theta_90);
gennnisi 11:62c85df03747 105 printf("finished shooting %d shot\n",count);
gennnisi 9:6008a52fb6ef 106 count ++;
gennnisi 9:6008a52fb6ef 107 wait(1);
gennnisi 9:6008a52fb6ef 108 servo.pulsewidth_us(theta_0);
gennnisi 10:5e3c789e07dc 109 shoot_phase = 0;
gennnisi 11:62c85df03747 110 printf("finished init\n");
gennnisi 9:6008a52fb6ef 111 break;
gennnisi 9:6008a52fb6ef 112
gennnisi 11:62c85df03747 113 }
gennnisi 9:6008a52fb6ef 114 }
gennnisi 9:6008a52fb6ef 115 }
gennnisi 9:6008a52fb6ef 116
gennnisi 0:0128d59ac729 117 //リボルバーP制御関数
gennnisi 0:0128d59ac729 118 //double k_p = 0.001;
gennnisi 0:0128d59ac729 119 //double target = 171.0;
gennnisi 0:0128d59ac729 120 /*void revolver(int count){
gennnisi 0:0128d59ac729 121 if(turn_able == 1){
gennnisi 0:0128d59ac729 122 roricon = v.get() - (171 * (count - 1));
gennnisi 0:0128d59ac729 123
gennnisi 0:0128d59ac729 124 while(roricon != 171){
gennnisi 0:0128d59ac729 125 pwm = k_p * (target - roricon);
gennnisi 0:0128d59ac729 126 motor_revolver.output(pwm);
gennnisi 0:0128d59ac729 127 printf("roricon = %lf\n",roricon);
gennnisi 0:0128d59ac729 128 printf("pwm = %lf\n",pwm);
gennnisi 0:0128d59ac729 129 wait(interval);
gennnisi 0:0128d59ac729 130 }
gennnisi 0:0128d59ac729 131 motor_revolver.output(0.0);
gennnisi 0:0128d59ac729 132 turn_able = 0;
gennnisi 0:0128d59ac729 133 }
gennnisi 9:6008a52fb6ef 134 }*/