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/Target.cpp@41:d60c9ced57ab, 2017-04-09 (annotated)
- Committer:
- Maxime Dupuis
- Date:
- Sun Apr 09 18:59:01 2017 -0400
- Revision:
- 41:d60c9ced57ab
- Parent:
- 40:1f0a5e5f24f6
- Child:
- 43:b8cda61584a8
Pass length as parameter
Otherwise it is impossible to test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Maxime Dupuis |
18:353fb432c03c | 1 | #include "Target.hpp" |
Maxime Dupuis |
18:353fb432c03c | 2 | |
GaiSensei | 36:f55ce07292c9 | 3 | int coordinator_address[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
Maxime Dupuis |
18:353fb432c03c | 4 | |
Maxime Dupuis |
22:d9df16225df3 | 5 | Target::Target(ServomotorInterface& servomotor, |
Maxime Dupuis |
22:d9df16225df3 | 6 | LedControllerInterface& ally_leds, |
Maxime Dupuis |
22:d9df16225df3 | 7 | LedControllerInterface& enemy_leds, |
Maxime Dupuis |
23:fb47bceb61da | 8 | XbeeTransmitterInterface& xbee_transmitter, |
Maxime Dupuis |
23:fb47bceb61da | 9 | const int target_number): |
Maxime Dupuis |
18:353fb432c03c | 10 | servomotor(servomotor), |
Maxime Dupuis |
22:d9df16225df3 | 11 | ally_leds(ally_leds), |
Maxime Dupuis |
22:d9df16225df3 | 12 | enemy_leds(enemy_leds), |
Maxime Dupuis |
23:fb47bceb61da | 13 | xbee_transmitter(xbee_transmitter), |
Maxime Dupuis |
23:fb47bceb61da | 14 | target_number(target_number) |
Maxime Dupuis |
18:353fb432c03c | 15 | { |
Maxime Dupuis |
18:353fb432c03c | 16 | } |
Maxime Dupuis |
18:353fb432c03c | 17 | |
Maxime Dupuis |
18:353fb432c03c | 18 | void Target::execute_command(Mode mode, int timeout_ms) |
Maxime Dupuis |
18:353fb432c03c | 19 | { |
Maxime Dupuis |
18:353fb432c03c | 20 | if(mode == Ally) |
Maxime Dupuis |
18:353fb432c03c | 21 | { |
Maxime Dupuis |
18:353fb432c03c | 22 | ally_command(); |
Maxime Dupuis |
18:353fb432c03c | 23 | } |
Maxime Dupuis |
18:353fb432c03c | 24 | else |
Maxime Dupuis |
18:353fb432c03c | 25 | { |
Maxime Dupuis |
18:353fb432c03c | 26 | enemy_command(); |
Maxime Dupuis |
18:353fb432c03c | 27 | } |
Maxime Dupuis |
18:353fb432c03c | 28 | } |
Maxime Dupuis |
18:353fb432c03c | 29 | |
Maxime Dupuis |
18:353fb432c03c | 30 | |
Maxime Dupuis |
18:353fb432c03c | 31 | void Target::ally_command() |
Maxime Dupuis |
18:353fb432c03c | 32 | { |
Maxime Dupuis |
18:353fb432c03c | 33 | servomotor.set_position_up(); |
Maxime Dupuis |
22:d9df16225df3 | 34 | ally_leds.turn_on(); |
Maxime Dupuis |
18:353fb432c03c | 35 | } |
Maxime Dupuis |
18:353fb432c03c | 36 | |
Maxime Dupuis |
18:353fb432c03c | 37 | void Target::enemy_command() |
Maxime Dupuis |
18:353fb432c03c | 38 | { |
Maxime Dupuis |
18:353fb432c03c | 39 | servomotor.set_position_up(); |
Maxime Dupuis |
22:d9df16225df3 | 40 | enemy_leds.turn_on(); |
Maxime Dupuis |
18:353fb432c03c | 41 | } |
Maxime Dupuis |
18:353fb432c03c | 42 | |
GaiSensei | 40:1f0a5e5f24f6 | 43 | void Target::generate_message(uint8_t target_number, Result result, uint16_t time_taken_ms, uint8_t* message) const |
Maxime Dupuis |
19:377887760869 | 44 | { |
Maxime Dupuis |
20:05e2c4941285 | 45 | const uint8_t time_taken_msb = time_taken_ms >> 8; |
Maxime Dupuis |
20:05e2c4941285 | 46 | const uint8_t time_taken_lsb = (time_taken_ms << 8) >> 8; |
Maxime Dupuis |
19:377887760869 | 47 | |
GaiSensei | 40:1f0a5e5f24f6 | 48 | message[0] = target_number; |
GaiSensei | 40:1f0a5e5f24f6 | 49 | message[1] = result; |
GaiSensei | 40:1f0a5e5f24f6 | 50 | message[2] = time_taken_msb; |
GaiSensei | 40:1f0a5e5f24f6 | 51 | message[3] = time_taken_lsb; |
Maxime Dupuis |
19:377887760869 | 52 | } |
Maxime Dupuis |
23:fb47bceb61da | 53 | |
Maxime Dupuis |
23:fb47bceb61da | 54 | void Target::timeout(const int time_taken_ms) |
Maxime Dupuis |
23:fb47bceb61da | 55 | { |
GaiSensei | 40:1f0a5e5f24f6 | 56 | uint8_t message[4]; |
GaiSensei | 40:1f0a5e5f24f6 | 57 | generate_message(target_number, Target::RESULT_MISSED, time_taken_ms, message); |
Maxime Dupuis |
41:d60c9ced57ab | 58 | xbee_transmitter.transmit(message, 4, coordinator_address); |
Maxime Dupuis |
23:fb47bceb61da | 59 | } |
Maxime Dupuis |
24:801271795da0 | 60 | |
Maxime Dupuis |
24:801271795da0 | 61 | void Target::hit(const int time_taken_ms) |
Maxime Dupuis |
24:801271795da0 | 62 | { |
GaiSensei | 40:1f0a5e5f24f6 | 63 | uint8_t message[4]; |
GaiSensei | 40:1f0a5e5f24f6 | 64 | generate_message(target_number, Target::RESULT_HIT, time_taken_ms, message); |
Maxime Dupuis |
41:d60c9ced57ab | 65 | xbee_transmitter.transmit(message, 4, coordinator_address); |
Maxime Dupuis |
24:801271795da0 | 66 | } |