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
source/TargetManager.cpp@44:2e186038862c, 2017-04-10 (annotated)
- Committer:
- GaiSensei
- Date:
- Mon Apr 10 17:36:16 2017 +0000
- Revision:
- 44:2e186038862c
- Parent:
- 43:b8cda61584a8
- Child:
- 46:853966aab733
Fix bumper
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
Maxime Dupuis |
32:50b777bab5a4 | 50 | if(is_active_target[target_number]) |
Maxime Dupuis |
32:50b777bab5a4 | 51 | { |
GaiSensei | 44:2e186038862c | 52 | is_active_target[target_number] = false; |
Maxime Dupuis |
38:70a1c8bd2877 | 53 | targets[target_number]->hit(nerfus_tickers[target_number]->get_time_ms()); |
Maxime Dupuis |
38:70a1c8bd2877 | 54 | nerfus_tickers[target_number]->stop(); |
Maxime Dupuis |
32:50b777bab5a4 | 55 | } |
Maxime Dupuis |
28:8fc53a870691 | 56 | } |
Maxime Dupuis |
28:8fc53a870691 | 57 | |
Maxime Dupuis |
28:8fc53a870691 | 58 | void TargetManager::target_missed(int target_number) |
Maxime Dupuis |
28:8fc53a870691 | 59 | { |
Maxime Dupuis |
32:50b777bab5a4 | 60 | if(is_active_target[target_number]) |
Maxime Dupuis |
32:50b777bab5a4 | 61 | { |
GaiSensei | 44:2e186038862c | 62 | is_active_target[target_number] = false; |
Maxime Dupuis |
38:70a1c8bd2877 | 63 | targets[target_number]->timeout(nerfus_tickers[target_number]->get_time_ms()); |
Maxime Dupuis |
38:70a1c8bd2877 | 64 | nerfus_tickers[target_number]->stop(); |
Maxime Dupuis |
32:50b777bab5a4 | 65 | } |
Maxime Dupuis |
28:8fc53a870691 | 66 | } |