Dependencies:   AmmoPusher AmmoSupplier Shooter

Dependents:   OBROT_ALL

Committer:
inst
Date:
Thu Oct 15 08:45:53 2015 +0000
Revision:
3:bf4ae302724c
Parent:
1:3b0b583eba77

        

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 3:bf4ae302724c 63 ShootingSystem::~ShootingSystem(){
inst 3:bf4ae302724c 64 delete mShooterAngleServo;
inst 3:bf4ae302724c 65 delete mAmmoPusher;
inst 3:bf4ae302724c 66 delete mAmmoSupplier;
inst 3:bf4ae302724c 67 delete mShooter;
inst 3:bf4ae302724c 68 delete mPositionManager;
inst 3:bf4ae302724c 69 }
inst 3:bf4ae302724c 70
inst 1:3b0b583eba77 71 void ShootingSystem::waiting( Command command ){
inst 1:3b0b583eba77 72 if ( command.isSupplying() ){
inst 1:3b0b583eba77 73 mAmmoSupplier->supply();
inst 1:3b0b583eba77 74 mState = AMMO_SUPPLYING;
inst 1:3b0b583eba77 75 } else if ( command.isShooting() ){
inst 1:3b0b583eba77 76 mState = SHOOTING;
inst 1:3b0b583eba77 77 }
inst 1:3b0b583eba77 78 }
inst 1:3b0b583eba77 79
inst 1:3b0b583eba77 80 void ShootingSystem::shooting(){
inst 1:3b0b583eba77 81 mShooter->launch();
inst 1:3b0b583eba77 82 mAmmoPusher->push();
inst 1:3b0b583eba77 83
inst 1:3b0b583eba77 84 if ( mAmmoPusher->hasPushed() ){
inst 1:3b0b583eba77 85 mState = SHOOT_WAITING;
inst 1:3b0b583eba77 86 mTimer.reset();
inst 1:3b0b583eba77 87 mTimer.start();
inst 1:3b0b583eba77 88 }
inst 1:3b0b583eba77 89 }
inst 1:3b0b583eba77 90
inst 1:3b0b583eba77 91 void ShootingSystem::shootWaiting(){
inst 0:987c53b6a212 92 mAmmoPusher->draw();
inst 0:987c53b6a212 93
inst 1:3b0b583eba77 94 if ( mTimer.read() > 0.7f ){
inst 1:3b0b583eba77 95 mShooter->stop();
inst 1:3b0b583eba77 96 mState = SHOOT_PREPARATION;
inst 0:987c53b6a212 97 }
inst 0:987c53b6a212 98 }
inst 1:3b0b583eba77 99
inst 1:3b0b583eba77 100 void ShootingSystem::shootPreparation(){
inst 1:3b0b583eba77 101 if ( mAmmoPusher->hasDrawn() ){
inst 1:3b0b583eba77 102 mState = WAITING;
inst 1:3b0b583eba77 103 }
inst 1:3b0b583eba77 104 }
inst 1:3b0b583eba77 105
inst 1:3b0b583eba77 106 void ShootingSystem::ammoSupply(){
inst 1:3b0b583eba77 107 if ( mAmmoSupplier->hasSupplied() ){
inst 1:3b0b583eba77 108 mState = WAITING;
inst 1:3b0b583eba77 109 }
inst 1:3b0b583eba77 110 }