tarou yamada / ShootingSystem

Dependencies:   AmmoPusher AmmoSupplier Shooter

Dependents:   OBROT_ALL

Files at this revision

API Documentation at this revision

Comitter:
inst
Date:
Wed Oct 14 03:52:45 2015 +0000
Parent:
0:987c53b6a212
Child:
2:4972946a6dfc
Commit message:

Changed in this revision

AmmoPusher.lib Show annotated file Show diff for this revision Revisions of this file
AmmoSupplier.lib Show annotated file Show diff for this revision Revisions of this file
Shooter.lib Show annotated file Show diff for this revision Revisions of this file
ShootingSystem.cpp Show annotated file Show diff for this revision Revisions of this file
ShootingSystem.h Show annotated file Show diff for this revision Revisions of this file
ShootingSystemDoc.txt Show diff for this revision Revisions of this file
--- a/AmmoPusher.lib	Fri Aug 21 04:52:50 2015 +0000
+++ b/AmmoPusher.lib	Wed Oct 14 03:52:45 2015 +0000
@@ -1,1 +1,1 @@
-AmmoPusher#42d2772575c5
+AmmoPusher#5ec2a3097d4c
--- a/AmmoSupplier.lib	Fri Aug 21 04:52:50 2015 +0000
+++ b/AmmoSupplier.lib	Wed Oct 14 03:52:45 2015 +0000
@@ -1,1 +1,1 @@
-AmmoSupplier#49a747f09bd2
+AmmoSupplier#2a43c09cc3be
--- a/Shooter.lib	Fri Aug 21 04:52:50 2015 +0000
+++ b/Shooter.lib	Wed Oct 14 03:52:45 2015 +0000
@@ -1,1 +1,1 @@
-Shooter#d660b49b71e0
+Shooter#215e05be641b
--- 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;
+    }
+}
--- a/ShootingSystem.h	Fri Aug 21 04:52:50 2015 +0000
+++ b/ShootingSystem.h	Wed Oct 14 03:52:45 2015 +0000
@@ -6,25 +6,53 @@
 class Command;
 class AmmoPusher;
 class AmmoSupplier;
+class AmmoSupplierManager;
 class Shooter;
+#include "mbed.h"
 
 class ShootingSystem{
 public:
-    ShootingSystem( 
-        I2CServo* angleManagerServo, I2CServo* positionManagerServo,
-        I2CMotor* ammoPusherMotor, I2CMotor** ammoSupplier,
-        Shooter* shooter );
+    enum AimState{
+        AIM_OWN_POLE            = 0x00,
+        AIM_CENTER_MIDDLE_POLE  = 0x40,
+        AIM_CENTER_SIDE_POLE    = 0x80,
+        AIM_ENEMYS_POLE         = 0xC0
+    };
+    
+    enum State{
+        WAITING,
+        SHOOTING,
+        SHOOT_PREPARATION,
+        SHOOT_WAITING,
+        AMMO_SUPPLYING
+    };
+    
+    static bool isShootable(){
+        return mIsShootable;
+    }
+    
+    ShootingSystem( I2CServo* angleManagerServo, AmmoPusher* ammoPusher,
+                    AmmoSupplier* ammoSupplier, Shooter* shooter,
+                    I2CServo* positoinManager );
+        
     void update( Command command );
+    
 private:
-    void reset();
+    void waiting( Command command );
+    void shooting();
+    void shootWaiting();
+    void shootPreparation();
+    void ammoSupply();
 
-    I2CServo* mShooterAngleManager;
-    I2CServo* mShooterPositionManager;
+    static bool mIsShootable;
+    
+    I2CServo* mShooterAngleServo;
     AmmoPusher* mAmmoPusher;
     AmmoSupplier* mAmmoSupplier;
     Shooter* mShooter;
-    bool mIsShooting;
-    bool mIsResetting;
+    I2CServo* mPositionManager;
+    State mState;
+    Timer mTimer;
 };
 
 #endif
--- a/ShootingSystemDoc.txt	Fri Aug 21 04:52:50 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,92 +0,0 @@
-MD
-(I2CServo)ShooterAngleManager	 	: 発射角度変更機構	: ポテメ 					: I2CServo
-(I2CServo)ShooterPositionManager	: 発射機構上下機構	: ポテメ					: I2CServo
-AmmoPusher			 				: 押出機構			: 2入力スイッチ			: DigitalIn * 2, I2CMotor * 1
-AmmoSupplier	 					: 補給機構			: 2モータ, レーザーON/OFF	: DigitalIn, InterruptIn, I2CMotor * 2
-
-リレーMD
-Shooter 							: 発射機構			: 出力3つ					: *I2C ( LPC1114側のプログラム : RelayYMotorDriver )
-
-About Shooter{
-	LPC1114{
-		DigitalOut( dp11, dp13, dp14 );
-	}
-}
-
-
-全統一制御ShootingSystem
-interface:
-	void update( Command command ){
-		if ( command.getIsShootable() && ( !mIsShooting ) ){
-			mIsShooting = true;
-			mShooterAngleManager->setTargetPosition( command.getAngleOfShooting() );
-			mShooterPositionManager->setTargetPosition( command.getPositionOfShooting() );
-			mAmmoPusher->draw();	// 補給された時に受け取れるように引いておく
-		}
-		
-		// 弾押し出し機構が補給された弾を受け取る位置に来たら補給開始
-		if ( mAmmoPusher->hasPusherFinishedDrawing() ){
-			mAmmoSupplier->supply();
-		}
-		
-		// 射撃作業で
-		//  ・角度調整
-		//  ・位置調整
-		//  ・弾補給
-		//  ・押出機構の準備
-		// が完了したら発射
-		bool canShooterFire = mIsShooting;
-		canShooterFire &= mShooterAngleManager->isStop();
-		canShooterFire &= mShooterPositionManager->isSop();
-		canShooterFire &= mAmmoPusher->hasPusherFinishedDrawing();
-		canShooterFire &= mAmmoSupplier->hasSupplied();
-		if ( canShooterFire ){
-			mShooter->launch();
-			mAmmoPusher->push();
-			
-			if ( mAmmoPresser->hasPresserFinishedPushing() ){
-				mAmmoPresser->draw();
-			}
-		}
-		/*
-			hasWorkFinished系は仕事の受注でフラグリセット、仕事の完了でフラグセット.
-			つまり受注前に使用すると前回の仕事のフラグを返されるため正しく動作しない可能性がある.
-		*/
-	}
-	void update( Command command ){
-		if ( command.getIsShootable() && ( !mIsShooting ) ){
-			mIsShooting 		= true;
-			mShooterAngleManager->setAngle( command.getAngleOfShooting() );
-			mShooterPositionManager->setPosition( command.getPositionOfShooting() );
-			mAmmoPusher->draw();
-		}
-		
-		// 弾押し出し機構が補給された弾を受け取る位置に来たら補給開始
-		if ( mAmmoPusher->hasPusherFinishedDrawing() ){
-			mAmmoSupplier->supply();
-		}
-		
-		// 射撃作業で
-		//  ・角度調整
-		//  ・位置調整
-		//  ・弾補給
-		//  ・押出機構の準備
-		// が完了したら発射
-		bool canShooterFire = mIsShooting;
-		canShooterFire &= mShooterAngleManager->hasWorkFinished();
-		canShooterFire &= mShooterPositionManager->hasWorkFinished();
-		canShooterFire &= mAmmoPusher->hasPusherFinishedDrawing();
-		canShooterFire &= mAmmoSupplier->hasSupplied();
-		if ( canShooterFire ){
-			mShooter->launch();
-			mAmmoPusher->push();
-			
-			if ( mAmmoPresser->hasPresserFinishedPushing() ){
-				mAmmoPresser->draw();
-			}
-		}
-		/*
-			hasWorkFinished系は仕事の受注でフラグリセット、仕事の完了でフラグセット.
-			つまり受注前に使用すると前回の仕事のフラグを返されるため正しく動作しない可能性がある.
-		*/
-	}