Dependencies:   AmmoPusher AmmoSupplier Shooter

Dependents:   OBROT_ALL

Committer:
inst
Date:
Wed Oct 14 03:52:45 2015 +0000
Revision:
1:3b0b583eba77
Parent:
0:987c53b6a212
Child:
3:bf4ae302724c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
inst 1:3b0b583eba77 1 #include "mbed.h"
inst 0:987c53b6a212 2 #include "ShootingSystem.h"
inst 0:987c53b6a212 3 #include "Command.h"
inst 0:987c53b6a212 4 #include "I2CServo.h"
inst 0:987c53b6a212 5 #include "Shooter.h"
inst 1:3b0b583eba77 6 #include "AmmoPusher.h"
inst 1:3b0b583eba77 7 #include "AmmoSupplier.h"
inst 1:3b0b583eba77 8
inst 1:3b0b583eba77 9 bool ShootingSystem::mIsShootable = false;
inst 0:987c53b6a212 10
inst 0:987c53b6a212 11 ShootingSystem::ShootingSystem(
inst 1:3b0b583eba77 12 I2CServo* angleManagerServo, AmmoPusher* ammoPusher,
inst 1:3b0b583eba77 13 AmmoSupplier* ammoSupplier, Shooter* shooter,
inst 1:3b0b583eba77 14 I2CServo* positionManager ){
inst 0:987c53b6a212 15
inst 1:3b0b583eba77 16 mShooterAngleServo = angleManagerServo;
inst 1:3b0b583eba77 17 mShooter = shooter;
inst 1:3b0b583eba77 18 mAmmoPusher = ammoPusher;
inst 1:3b0b583eba77 19 mAmmoSupplier = ammoSupplier;
inst 1:3b0b583eba77 20 mPositionManager = positionManager;
inst 1:3b0b583eba77 21 mState = SHOOT_PREPARATION;
inst 0:987c53b6a212 22 }
inst 0:987c53b6a212 23
inst 0:987c53b6a212 24 void ShootingSystem::update( Command command ){
inst 1:3b0b583eba77 25 bool isChangeable = ( mState != SHOOTING );
inst 1:3b0b583eba77 26 isChangeable &= ( mState != SHOOT_PREPARATION );
inst 1:3b0b583eba77 27
inst 1:3b0b583eba77 28 if ( isChangeable ){
inst 1:3b0b583eba77 29 // 発射中でないなら角度と電圧が変更可能
inst 1:3b0b583eba77 30 mShooterAngleServo->setTargetPosition( command.getShootingAngleAnalog() );
inst 1:3b0b583eba77 31 mPositionManager->setTargetPosition( command.getShooterPosition() );
inst 1:3b0b583eba77 32 mShooter->setVoltage( command.getShootVoltage() );
inst 0:987c53b6a212 33 }
inst 0:987c53b6a212 34
inst 1:3b0b583eba77 35 mIsShootable = ( mState == WAITING );
inst 1:3b0b583eba77 36
inst 1:3b0b583eba77 37 switch ( mState ){
inst 1:3b0b583eba77 38 case WAITING:
inst 1:3b0b583eba77 39 waiting( command );
inst 1:3b0b583eba77 40 break;
inst 1:3b0b583eba77 41
inst 1:3b0b583eba77 42 case SHOOTING:
inst 1:3b0b583eba77 43 shooting();
inst 1:3b0b583eba77 44 break;
inst 1:3b0b583eba77 45
inst 1:3b0b583eba77 46 case SHOOT_WAITING:
inst 1:3b0b583eba77 47 shootWaiting();
inst 1:3b0b583eba77 48 break;
inst 1:3b0b583eba77 49
inst 1:3b0b583eba77 50 case SHOOT_PREPARATION:
inst 1:3b0b583eba77 51 shootPreparation();
inst 1:3b0b583eba77 52 break;
inst 1:3b0b583eba77 53
inst 1:3b0b583eba77 54 case AMMO_SUPPLYING:
inst 1:3b0b583eba77 55 ammoSupply();
inst 1:3b0b583eba77 56 break;
inst 1:3b0b583eba77 57
inst 1:3b0b583eba77 58 default:
inst 1:3b0b583eba77 59 break;
inst 1:3b0b583eba77 60 }
inst 0:987c53b6a212 61 }
inst 0:987c53b6a212 62
inst 1:3b0b583eba77 63 void ShootingSystem::waiting( Command command ){
inst 1:3b0b583eba77 64 if ( command.isSupplying() ){
inst 1:3b0b583eba77 65 mAmmoSupplier->supply();
inst 1:3b0b583eba77 66 mState = AMMO_SUPPLYING;
inst 1:3b0b583eba77 67 } else if ( command.isShooting() ){
inst 1:3b0b583eba77 68 mState = SHOOTING;
inst 1:3b0b583eba77 69 }
inst 1:3b0b583eba77 70 }
inst 1:3b0b583eba77 71
inst 1:3b0b583eba77 72 void ShootingSystem::shooting(){
inst 1:3b0b583eba77 73 mShooter->launch();
inst 1:3b0b583eba77 74 mAmmoPusher->push();
inst 1:3b0b583eba77 75
inst 1:3b0b583eba77 76 if ( mAmmoPusher->hasPushed() ){
inst 1:3b0b583eba77 77 mState = SHOOT_WAITING;
inst 1:3b0b583eba77 78 mTimer.reset();
inst 1:3b0b583eba77 79 mTimer.start();
inst 1:3b0b583eba77 80 }
inst 1:3b0b583eba77 81 }
inst 1:3b0b583eba77 82
inst 1:3b0b583eba77 83 void ShootingSystem::shootWaiting(){
inst 0:987c53b6a212 84 mAmmoPusher->draw();
inst 0:987c53b6a212 85
inst 1:3b0b583eba77 86 if ( mTimer.read() > 0.7f ){
inst 1:3b0b583eba77 87 mShooter->stop();
inst 1:3b0b583eba77 88 mState = SHOOT_PREPARATION;
inst 0:987c53b6a212 89 }
inst 0:987c53b6a212 90 }
inst 1:3b0b583eba77 91
inst 1:3b0b583eba77 92 void ShootingSystem::shootPreparation(){
inst 1:3b0b583eba77 93 if ( mAmmoPusher->hasDrawn() ){
inst 1:3b0b583eba77 94 mState = WAITING;
inst 1:3b0b583eba77 95 }
inst 1:3b0b583eba77 96 }
inst 1:3b0b583eba77 97
inst 1:3b0b583eba77 98 void ShootingSystem::ammoSupply(){
inst 1:3b0b583eba77 99 if ( mAmmoSupplier->hasSupplied() ){
inst 1:3b0b583eba77 100 mState = WAITING;
inst 1:3b0b583eba77 101 }
inst 1:3b0b583eba77 102 }