program to shoot beenbags

Dependencies:   mbed MOTOR

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

Committer:
gennnisi
Date:
Tue Feb 01 07:22:36 2022 +0000
Revision:
20:4db5f042a45c
Parent:
19:fbb3b5331641
Child:
21:9f917ee199ff
a;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gennnisi 20:4db5f042a45c 1 d#include "mbed.h"
gennnisi 0:0128d59ac729 2 #include "rotary_inc.hpp"
gennnisi 0:0128d59ac729 3 #include "scrp_slave.hpp"
gennnisi 0:0128d59ac729 4 #include "Motor.hpp"
gennnisi 0:0128d59ac729 5
gennnisi 19:fbb3b5331641 6 //-----インスタンス--------------------------
gennnisi 19:fbb3b5331641 7
gennnisi 20:4db5f042a45c 8 Motor motor_revolver(PA_7,PA_8);//リボルバーモーター
gennnisi 20:4db5f042a45c 9 Motor motor_shoot(PA_1,PA_3);//発射機構モーター
gennnisi 20:4db5f042a45c 10 PwmOut servo(PB_6);//ロック解除用サーボ
gennnisi 9:6008a52fb6ef 11
gennnisi 18:c61ba21bbc67 12 ScrpSlave slave(PA_9,PA_10,PA_12,SERIAL_TX,SERIAL_RX,3);//id=3
gennnisi 0:0128d59ac729 13
gennnisi 18:c61ba21bbc67 14 RotaryInc v(PA_0,PA_4,1,1024,2);//リボルバーロリコン
gennnisi 0:0128d59ac729 15
gennnisi 18:c61ba21bbc67 16 DigitalIn limit_1(PB_0);//リミットスイッチ1
gennnisi 18:c61ba21bbc67 17 DigitalIn limit_2(PB_1);//リミットスイッチ2
gennnisi 6:7dcc827bbe96 18
gennnisi 19:fbb3b5331641 19 //-----変数---------------------------------
gennnisi 19:fbb3b5331641 20
gennnisi 17:9ee4c69d2d8b 21 int shoot_phase = 0;//発射フェーズ 0:待機 1:装填動作 2:リボルバー回転、モーター復帰(同時) 3:発射 
gennnisi 18:c61ba21bbc67 22 bool turn_able = 0;//回転可否
gennnisi 18:c61ba21bbc67 23 bool back_able = 0;//復帰可否 
gennnisi 0:0128d59ac729 24
gennnisi 19:fbb3b5331641 25 bool mode = 0;//0:手動 1:自動
gennnisi 19:fbb3b5331641 26 bool emergency = 0;//0:停止 1:停止
gennnisi 20:4db5f042a45c 27 double revolver_pwm = -0.1;//リボルバー回転速度
gennnisi 20:4db5f042a45c 28 double load_pwm = -0.5;//装填モーター速度
gennnisi 20:4db5f042a45c 29 double back_pwm = 0.5;//復帰モーター速度
gennnisi 9:6008a52fb6ef 30
gennnisi 12:aef2a6626ea5 31 int theta_0 = 1290;//0度
gennnisi 12:aef2a6626ea5 32 int theta_45 = 1875;//45度
gennnisi 9:6008a52fb6ef 33 int roricon = 0;//ロリコン
gennnisi 0:0128d59ac729 34
gennnisi 19:fbb3b5331641 35 //-----関数---------------------------------
gennnisi 19:fbb3b5331641 36
gennnisi 19:fbb3b5331641 37 //発射開始を受信
gennnisi 19:fbb3b5331641 38 bool get_start(int rx_data,int &tx_data){
gennnisi 10:5e3c789e07dc 39 if(shoot_phase == 0){//フェーズ0以外は、発射できないように
gennnisi 18:c61ba21bbc67 40 shoot_phase = 1;
gennnisi 0:0128d59ac729 41 }
gennnisi 0:0128d59ac729 42 return true;
gennnisi 0:0128d59ac729 43 }
gennnisi 0:0128d59ac729 44
gennnisi 19:fbb3b5331641 45 //モード変更を受信
gennnisi 19:fbb3b5331641 46 bool get_changemode(int rx_data,int &tx_data){
gennnisi 19:fbb3b5331641 47 if(mode == 0){
gennnisi 19:fbb3b5331641 48 mode = 1;
gennnisi 19:fbb3b5331641 49 }else{
gennnisi 19:fbb3b5331641 50 mode = 0;
gennnisi 19:fbb3b5331641 51 }
gennnisi 20:4db5f042a45c 52 return 0;
gennnisi 19:fbb3b5331641 53 }
gennnisi 19:fbb3b5331641 54
gennnisi 19:fbb3b5331641 55 //非常停止を受信
gennnisi 19:fbb3b5331641 56 bool get_stop(int rx_data,int &tx_data){
gennnisi 19:fbb3b5331641 57 if(emergency == 0){
gennnisi 19:fbb3b5331641 58 emergency = 1;//停止
gennnisi 20:4db5f042a45c 59 shoot_phase = 10;
gennnisi 19:fbb3b5331641 60 }else{
gennnisi 19:fbb3b5331641 61 emergency = 0;//解除
gennnisi 19:fbb3b5331641 62 }
gennnisi 20:4db5f042a45c 63 return 0;
gennnisi 19:fbb3b5331641 64 }
gennnisi 19:fbb3b5331641 65
gennnisi 19:fbb3b5331641 66 //リボルバー、ラック復帰
gennnisi 13:66e37e0aa786 67 void revolver_back(int count){
gennnisi 16:04f42eeb9561 68 while(turn_able == 1 || back_able == 1){
gennnisi 16:04f42eeb9561 69 //リボルバー
gennnisi 16:04f42eeb9561 70 if(count == 1){
gennnisi 16:04f42eeb9561 71 motor_revolver.output(0);
gennnisi 16:04f42eeb9561 72 turn_able = 0;
gennnisi 17:9ee4c69d2d8b 73 printf("no turn\n");
gennnisi 16:04f42eeb9561 74 }
gennnisi 18:c61ba21bbc67 75 if(count > 1){
gennnisi 16:04f42eeb9561 76 if(roricon < 341 * (count - 1)){
gennnisi 16:04f42eeb9561 77 if(turn_able == 1){
gennnisi 14:81f09ab5ed23 78 roricon = v.get();
gennnisi 14:81f09ab5ed23 79 motor_revolver.output(revolver_pwm);
gennnisi 16:04f42eeb9561 80 printf("roricon = %d\n",roricon);
gennnisi 14:81f09ab5ed23 81 }
gennnisi 13:66e37e0aa786 82 }
gennnisi 16:04f42eeb9561 83 else{
gennnisi 16:04f42eeb9561 84 motor_revolver.output(0);
gennnisi 16:04f42eeb9561 85 turn_able = 0;
gennnisi 16:04f42eeb9561 86 }
gennnisi 16:04f42eeb9561 87 }
gennnisi 16:04f42eeb9561 88 //復帰
gennnisi 16:04f42eeb9561 89 if(limit_2.read() == 0){
gennnisi 16:04f42eeb9561 90 if(back_able == 1){
gennnisi 13:66e37e0aa786 91 motor_shoot.output(back_pwm);
gennnisi 16:04f42eeb9561 92 printf("backing\n");
gennnisi 13:66e37e0aa786 93 }
gennnisi 16:04f42eeb9561 94 }
gennnisi 16:04f42eeb9561 95 if(limit_2.read() == 1){
gennnisi 16:04f42eeb9561 96 motor_shoot.output(0);
gennnisi 16:04f42eeb9561 97 back_able = 0;
gennnisi 16:04f42eeb9561 98 printf("finished backing\n");
gennnisi 9:6008a52fb6ef 99 }
gennnisi 16:04f42eeb9561 100 //ループ抜け出し
gennnisi 18:c61ba21bbc67 101 if(turn_able == 0 && back_able == 0){
gennnisi 16:04f42eeb9561 102 break;
gennnisi 16:04f42eeb9561 103 }
gennnisi 20:4db5f042a45c 104 wait(0.01);
gennnisi 9:6008a52fb6ef 105 }
gennnisi 9:6008a52fb6ef 106 }
gennnisi 9:6008a52fb6ef 107
gennnisi 19:fbb3b5331641 108 //------------------------------------------
gennnisi 19:fbb3b5331641 109
gennnisi 9:6008a52fb6ef 110 int main(){
gennnisi 9:6008a52fb6ef 111 int count = 1;//何発目か
gennnisi 9:6008a52fb6ef 112
gennnisi 9:6008a52fb6ef 113 servo.period_us(20000);
gennnisi 10:5e3c789e07dc 114 limit_1.mode(PullUp);
gennnisi 10:5e3c789e07dc 115 limit_2.mode(PullUp);
gennnisi 9:6008a52fb6ef 116
gennnisi 19:fbb3b5331641 117 slave.addCMD(2,get_start);
gennnisi 19:fbb3b5331641 118 slave.addCMD(50,get_changemode);
gennnisi 20:4db5f042a45c 119 slave.addCMD(51,get_stop);
gennnisi 12:aef2a6626ea5 120
gennnisi 9:6008a52fb6ef 121 while(1){
gennnisi 19:fbb3b5331641 122 if(mode == 1){
gennnisi 19:fbb3b5331641 123 switch (shoot_phase){
gennnisi 19:fbb3b5331641 124 case 0://待機
gennnisi 19:fbb3b5331641 125 while(shoot_phase == 0){
gennnisi 19:fbb3b5331641 126 wait(0.1);
gennnisi 19:fbb3b5331641 127 printf("waiting\n");
gennnisi 15:7c26b01dcefd 128 }
gennnisi 19:fbb3b5331641 129 shoot_phase = 1;
gennnisi 19:fbb3b5331641 130 printf("finished waiting\n");
gennnisi 19:fbb3b5331641 131 break;
gennnisi 19:fbb3b5331641 132
gennnisi 19:fbb3b5331641 133 case 1://装填動作
gennnisi 19:fbb3b5331641 134 while(limit_1.read() == 0){
gennnisi 19:fbb3b5331641 135 motor_shoot.output(load_pwm);
gennnisi 19:fbb3b5331641 136 printf("loading\n");
gennnisi 19:fbb3b5331641 137 if(limit_2.read() == 1){
gennnisi 19:fbb3b5331641 138 break;
gennnisi 19:fbb3b5331641 139 }
gennnisi 19:fbb3b5331641 140 wait(0.1);
gennnisi 19:fbb3b5331641 141 }
gennnisi 19:fbb3b5331641 142 motor_shoot.output(0);
gennnisi 19:fbb3b5331641 143 shoot_phase = 2;
gennnisi 19:fbb3b5331641 144 printf("finished loading\n");
gennnisi 19:fbb3b5331641 145 wait(3);
gennnisi 19:fbb3b5331641 146 break;
gennnisi 19:fbb3b5331641 147
gennnisi 19:fbb3b5331641 148 case 2://リボルバー回転,モーター復帰
gennnisi 19:fbb3b5331641 149 turn_able = 1;
gennnisi 19:fbb3b5331641 150 back_able = 1;
gennnisi 19:fbb3b5331641 151 revolver_back(count);
gennnisi 19:fbb3b5331641 152 shoot_phase = 3;
gennnisi 19:fbb3b5331641 153 printf("finished backing\n");
gennnisi 19:fbb3b5331641 154 wait(3);
gennnisi 19:fbb3b5331641 155 break;
gennnisi 19:fbb3b5331641 156
gennnisi 19:fbb3b5331641 157 case 3://発射動作
gennnisi 19:fbb3b5331641 158 servo.pulsewidth_us(theta_45);
gennnisi 19:fbb3b5331641 159 printf("%d shot\n",count);
gennnisi 19:fbb3b5331641 160 wait(3);
gennnisi 20:4db5f042a45c 161 servo.pulsewidth_us(theta_0);
gennnisi 19:fbb3b5331641 162 motor_shoot.output(load_pwm);
gennnisi 19:fbb3b5331641 163 wait(0.15);
gennnisi 19:fbb3b5331641 164 motor_shoot.output(0);
gennnisi 19:fbb3b5331641 165 shoot_phase = 0;
gennnisi 19:fbb3b5331641 166 printf("finished init\n");
gennnisi 19:fbb3b5331641 167 count ++;
gennnisi 19:fbb3b5331641 168 break;
gennnisi 19:fbb3b5331641 169
gennnisi 20:4db5f042a45c 170 case 10://非常停止
gennnisi 19:fbb3b5331641 171 while(emergency == 1){
gennnisi 19:fbb3b5331641 172 motor_shoot.output(0);
gennnisi 19:fbb3b5331641 173 motor_revolver.output(0);
gennnisi 20:4db5f042a45c 174 printf("emergency\n");
gennnisi 19:fbb3b5331641 175 wait(0.1);
gennnisi 19:fbb3b5331641 176 }
gennnisi 19:fbb3b5331641 177 break;
gennnisi 19:fbb3b5331641 178 }
gennnisi 11:62c85df03747 179 }
gennnisi 9:6008a52fb6ef 180 }
gennnisi 12:aef2a6626ea5 181 }