NerfUS mobile node that manages a target for the Nerf gun firing range

Dependencies:   LedController mbed-rtos mbed NerfUSXbee Servomotor TargetManager

Fork of NerfUS by NerfUS

Committer:
Maxime Dupuis
Date:
Wed Apr 12 10:33:32 2017 -0400
Revision:
51:6bf268cd1a1b
Parent:
50:8cf0be9a61db
Update tests to ignore target hits before 750ms threshold

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Maxime Dupuis 28:8fc53a870691 1 #include "TargetManager.hpp"
GaiSensei 39:a98413395b20 2 #include <cassert>
Maxime Dupuis 28:8fc53a870691 3
Maxime Dupuis 30:412a779cf607 4 TargetManager::TargetManager(std::vector<TargetInterface*>& targets,
Maxime Dupuis 38:70a1c8bd2877 5 std::vector<NerfusTickerInterface*>& nerfus_tickers) :
Maxime Dupuis 30:412a779cf607 6 targets(targets),
Maxime Dupuis 38:70a1c8bd2877 7 nerfus_tickers(nerfus_tickers)
Maxime Dupuis 30:412a779cf607 8 {
Maxime Dupuis 32:50b777bab5a4 9 for(int i=0; i<targets.size(); i++)
Maxime Dupuis 32:50b777bab5a4 10 {
Maxime Dupuis 32:50b777bab5a4 11 is_active_target.push_back(false);
Maxime Dupuis 32:50b777bab5a4 12 }
Maxime Dupuis 32:50b777bab5a4 13 }
Maxime Dupuis 30:412a779cf607 14
Maxime Dupuis 34:85994e0501fb 15 void TargetManager::execute(const TargetInfo& target_info)
Maxime Dupuis 32:50b777bab5a4 16 {
GaiSensei 39:a98413395b20 17 assert(0 <= target_info.id && target_info.id < targets.size() && "ID is out of range");
Maxime Dupuis 34:85994e0501fb 18 TargetInterface& target = *(targets[target_info.id]);
Maxime Dupuis 32:50b777bab5a4 19
Maxime Dupuis 34:85994e0501fb 20 if(target_info.type == TARGET_TYPE_ALLY)
Maxime Dupuis 34:85994e0501fb 21 {
Maxime Dupuis 34:85994e0501fb 22 target.ally_command();
Maxime Dupuis 34:85994e0501fb 23 }
GaiSensei 39:a98413395b20 24 else if(target_info.type == TARGET_TYPE_ENEMY)
GaiSensei 39:a98413395b20 25 {
GaiSensei 39:a98413395b20 26 target.enemy_command();
GaiSensei 39:a98413395b20 27 }
Maxime Dupuis 34:85994e0501fb 28 else
Maxime Dupuis 34:85994e0501fb 29 {
GaiSensei 39:a98413395b20 30 assert(false && "Target type invalid");
Maxime Dupuis 34:85994e0501fb 31 }
Maxime Dupuis 32:50b777bab5a4 32
Maxime Dupuis 34:85994e0501fb 33 is_active_target[target_info.id] = true;
Maxime Dupuis 38:70a1c8bd2877 34 nerfus_tickers[target_info.id]->start(target_info.timeout_ms);
Maxime Dupuis 30:412a779cf607 35 }
Maxime Dupuis 30:412a779cf607 36
Maxime Dupuis 37:8fc7845ae95b 37 void TargetManager::execute(const std::vector<uint8_t>& target_info_bytes, int *address)
Maxime Dupuis 35:7ee45f6d6484 38 {
GaiSensei 36:f55ce07292c9 39 const int id = target_info_bytes[0];
GaiSensei 36:f55ce07292c9 40 const TargetType type = (target_info_bytes[1] == 0) ? TARGET_TYPE_ALLY : TARGET_TYPE_ENEMY;
GaiSensei 36:f55ce07292c9 41 const int timeout_msb = target_info_bytes[2];
GaiSensei 36:f55ce07292c9 42 const int timeout_lsb = target_info_bytes[3];
Maxime Dupuis 35:7ee45f6d6484 43 const int timeout = (timeout_msb << 8) + timeout_lsb;
Maxime Dupuis 35:7ee45f6d6484 44 const TargetInfo target_info = make_TargetInfo(id, type, timeout);
Maxime Dupuis 35:7ee45f6d6484 45 execute(target_info);
Maxime Dupuis 35:7ee45f6d6484 46 }
Maxime Dupuis 35:7ee45f6d6484 47
Maxime Dupuis 28:8fc53a870691 48 void TargetManager::target_hit(int target_number)
Maxime Dupuis 28:8fc53a870691 49 {
dupm2216 50:8cf0be9a61db 50 const int time_ms = nerfus_tickers[target_number]->get_time_ms();
dupm2216 50:8cf0be9a61db 51 if(is_active_target[target_number] && time_ms > 750)
Maxime Dupuis 32:50b777bab5a4 52 {
GaiSensei 44:2e186038862c 53 is_active_target[target_number] = false;
dupm2216 50:8cf0be9a61db 54 targets[target_number]->hit(time_ms);
Maxime Dupuis 38:70a1c8bd2877 55 nerfus_tickers[target_number]->stop();
Maxime Dupuis 32:50b777bab5a4 56 }
Maxime Dupuis 28:8fc53a870691 57 }
Maxime Dupuis 28:8fc53a870691 58
Maxime Dupuis 28:8fc53a870691 59 void TargetManager::target_missed(int target_number)
Maxime Dupuis 28:8fc53a870691 60 {
Maxime Dupuis 32:50b777bab5a4 61 if(is_active_target[target_number])
Maxime Dupuis 32:50b777bab5a4 62 {
GaiSensei 44:2e186038862c 63 is_active_target[target_number] = false;
Maxime Dupuis 38:70a1c8bd2877 64 targets[target_number]->timeout(nerfus_tickers[target_number]->get_time_ms());
Maxime Dupuis 38:70a1c8bd2877 65 nerfus_tickers[target_number]->stop();
Maxime Dupuis 32:50b777bab5a4 66 }
Maxime Dupuis 28:8fc53a870691 67 }