Dependencies:   AmmoPusher AmmoSupplier Shooter

Dependents:   OBROT_ALL

Revision:
0:987c53b6a212
Child:
1:3b0b583eba77
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ShootingSystem.cpp	Fri Aug 21 04:52:50 2015 +0000
@@ -0,0 +1,62 @@
+#include "ShootingSystem.h"
+#include "mbed.h"
+#include "AmmoPusher.h"
+#include "AmmoSupplier.h"
+#include "Command.h"
+#include "I2CServo.h"
+#include "Shooter.h"
+
+ShootingSystem::ShootingSystem(
+        I2CServo* angleManagerServo, I2CServo* positionManagerServo,
+        I2CMotor* ammoPusherMotor, I2CMotor** ammoSupplierMotor,
+        Shooter* shooter ){
+    
+    mShooterAngleManager    = angleManagerServo;
+    mShooterPositionManager = positionManagerServo;
+    mShooter                = shooter;
+    mAmmoPusher             = new AmmoPusher( ammoPusherMotor );
+    mAmmoSupplier           = new AmmoSupplier( ammoSupplierMotor );
+    mIsShooting             = false;
+    mIsResetting            = false;
+}
+
+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();
+        }
+    }
+    
+    mAmmoSupplier->update();
+    mAmmoPusher->update();
+}
+
+void ShootingSystem::reset(){
+    mShooter->stop();
+    // 次の弾の装填のため押出機構は引いておく
+    mAmmoPusher->draw();
+    
+    if ( mAmmoPusher->hasPusherFinishedDrawing() ){
+        mAmmoSupplier->supply();
+        
+        if ( mAmmoSupplier->isSupplying() ){
+            mIsShooting = false;
+        }
+    }
+}