Dependencies:   AmmoPusher AmmoSupplier Shooter

Dependents:   OBROT_ALL

Revision:
1:3b0b583eba77
Parent:
0:987c53b6a212
Child:
3:bf4ae302724c
--- a/ShootingSystem.cpp	Fri Aug 21 04:52:50 2015 +0000
+++ b/ShootingSystem.cpp	Wed Oct 14 03:52:45 2015 +0000
@@ -1,62 +1,102 @@
+#include "mbed.h"
 #include "ShootingSystem.h"
-#include "mbed.h"
-#include "AmmoPusher.h"
-#include "AmmoSupplier.h"
 #include "Command.h"
 #include "I2CServo.h"
 #include "Shooter.h"
+#include "AmmoPusher.h"
+#include "AmmoSupplier.h"
+
+bool ShootingSystem::mIsShootable = false;
 
 ShootingSystem::ShootingSystem(
-        I2CServo* angleManagerServo, I2CServo* positionManagerServo,
-        I2CMotor* ammoPusherMotor, I2CMotor** ammoSupplierMotor,
-        Shooter* shooter ){
+        I2CServo* angleManagerServo, AmmoPusher* ammoPusher,
+        AmmoSupplier* ammoSupplier, Shooter* shooter,
+        I2CServo* positionManager ){
     
-    mShooterAngleManager    = angleManagerServo;
-    mShooterPositionManager = positionManagerServo;
-    mShooter                = shooter;
-    mAmmoPusher             = new AmmoPusher( ammoPusherMotor );
-    mAmmoSupplier           = new AmmoSupplier( ammoSupplierMotor );
-    mIsShooting             = false;
-    mIsResetting            = false;
+    mShooterAngleServo  = angleManagerServo;
+    mShooter            = shooter;
+    mAmmoPusher         = ammoPusher;
+    mAmmoSupplier       = ammoSupplier;
+    mPositionManager    = positionManager;
+    mState              = SHOOT_PREPARATION;
 }
 
 void ShootingSystem::update( Command command ){
-    if ( mIsResetting ){
-        reset();
-    } else {
-        if ( command.isShootable() && ( !mIsShooting ) ){
-            // 射撃態勢に移行し,発射装置を目指すポールにあった角度・位置にする
-            mIsShooting = true;
-            mShooterAngleManager->setTargetPosition( command.getAngleOfShooting() );
-            mShooterPositionManager->setTargetPosition( command.getPositionOfShooting() );
-            mShooter->launch();
-        }
-        
-        // 発射装置が目標の角度・位置に移動したら発射
-        bool canShooterFire = mIsShooting;
-        canShooterFire &= mShooterAngleManager->isStop();
-        canShooterFire &= mShooterPositionManager->isStop();
-        if ( canShooterFire ){
-            mAmmoPusher->push();
-            // 発射が終わったら輪を装填するためにリセット状態に遷移する
-            mIsResetting = mAmmoPusher->hasPusherFinishedPushing();
-        }
+    bool isChangeable = ( mState != SHOOTING );
+    isChangeable &= ( mState != SHOOT_PREPARATION );
+    
+    if ( isChangeable ){
+        // 発射中でないなら角度と電圧が変更可能
+        mShooterAngleServo->setTargetPosition( command.getShootingAngleAnalog() );
+        mPositionManager->setTargetPosition( command.getShooterPosition() );
+        mShooter->setVoltage( command.getShootVoltage() );
     }
     
-    mAmmoSupplier->update();
-    mAmmoPusher->update();
+    mIsShootable = ( mState == WAITING );
+    
+    switch ( mState ){
+        case WAITING:
+            waiting( command );
+            break;
+            
+        case SHOOTING:
+            shooting();
+            break;
+            
+        case SHOOT_WAITING:
+            shootWaiting();
+            break;
+            
+        case SHOOT_PREPARATION:
+            shootPreparation();
+            break;
+            
+        case AMMO_SUPPLYING:
+            ammoSupply();
+            break;
+            
+        default:
+            break;
+    }
 }
 
-void ShootingSystem::reset(){
-    mShooter->stop();
-    // 次の弾の装填のため押出機構は引いておく
+void ShootingSystem::waiting( Command command ){
+    if ( command.isSupplying() ){
+        mAmmoSupplier->supply();
+        mState = AMMO_SUPPLYING;
+    } else if ( command.isShooting() ){
+        mState = SHOOTING;
+    }
+}
+
+void ShootingSystem::shooting(){
+    mShooter->launch();
+    mAmmoPusher->push();
+    
+    if ( mAmmoPusher->hasPushed() ){
+        mState = SHOOT_WAITING;
+        mTimer.reset();
+        mTimer.start();
+    }
+}
+
+void ShootingSystem::shootWaiting(){
     mAmmoPusher->draw();
     
-    if ( mAmmoPusher->hasPusherFinishedDrawing() ){
-        mAmmoSupplier->supply();
-        
-        if ( mAmmoSupplier->isSupplying() ){
-            mIsShooting = false;
-        }
+    if ( mTimer.read() > 0.7f ){
+        mShooter->stop();
+        mState = SHOOT_PREPARATION;
     }
 }
+
+void ShootingSystem::shootPreparation(){
+    if ( mAmmoPusher->hasDrawn() ){
+        mState = WAITING;
+    }
+}
+
+void ShootingSystem::ammoSupply(){
+    if ( mAmmoSupplier->hasSupplied() ){
+        mState = WAITING;
+    }
+}