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:
Sat Mar 11 13:30:23 2017 -0500
Revision:
24:801271795da0
Parent:
23:fb47bceb61da
Child:
36:f55ce07292c9
Implement hit method

Transmit message containing target number, that it was hit,
and the time taken in ms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Maxime Dupuis 18:353fb432c03c 1 #include "Target.hpp"
Maxime Dupuis 18:353fb432c03c 2
Maxime Dupuis 18:353fb432c03c 3
Maxime Dupuis 22:d9df16225df3 4 Target::Target(ServomotorInterface& servomotor,
Maxime Dupuis 22:d9df16225df3 5 LedControllerInterface& ally_leds,
Maxime Dupuis 22:d9df16225df3 6 LedControllerInterface& enemy_leds,
Maxime Dupuis 23:fb47bceb61da 7 XbeeTransmitterInterface& xbee_transmitter,
Maxime Dupuis 23:fb47bceb61da 8 const int target_number):
Maxime Dupuis 18:353fb432c03c 9 servomotor(servomotor),
Maxime Dupuis 22:d9df16225df3 10 ally_leds(ally_leds),
Maxime Dupuis 22:d9df16225df3 11 enemy_leds(enemy_leds),
Maxime Dupuis 23:fb47bceb61da 12 xbee_transmitter(xbee_transmitter),
Maxime Dupuis 23:fb47bceb61da 13 target_number(target_number)
Maxime Dupuis 18:353fb432c03c 14 {
Maxime Dupuis 18:353fb432c03c 15 }
Maxime Dupuis 18:353fb432c03c 16
Maxime Dupuis 18:353fb432c03c 17 void Target::execute_command(Mode mode, int timeout_ms)
Maxime Dupuis 18:353fb432c03c 18 {
Maxime Dupuis 18:353fb432c03c 19 if(mode == Ally)
Maxime Dupuis 18:353fb432c03c 20 {
Maxime Dupuis 18:353fb432c03c 21 ally_command();
Maxime Dupuis 18:353fb432c03c 22 }
Maxime Dupuis 18:353fb432c03c 23 else
Maxime Dupuis 18:353fb432c03c 24 {
Maxime Dupuis 18:353fb432c03c 25 enemy_command();
Maxime Dupuis 18:353fb432c03c 26 }
Maxime Dupuis 18:353fb432c03c 27 }
Maxime Dupuis 18:353fb432c03c 28
Maxime Dupuis 18:353fb432c03c 29
Maxime Dupuis 18:353fb432c03c 30 void Target::ally_command()
Maxime Dupuis 18:353fb432c03c 31 {
Maxime Dupuis 18:353fb432c03c 32 servomotor.set_position_up();
Maxime Dupuis 22:d9df16225df3 33 ally_leds.turn_on();
Maxime Dupuis 18:353fb432c03c 34 }
Maxime Dupuis 18:353fb432c03c 35
Maxime Dupuis 18:353fb432c03c 36 void Target::enemy_command()
Maxime Dupuis 18:353fb432c03c 37 {
Maxime Dupuis 18:353fb432c03c 38 servomotor.set_position_up();
Maxime Dupuis 22:d9df16225df3 39 enemy_leds.turn_on();
Maxime Dupuis 18:353fb432c03c 40 }
Maxime Dupuis 18:353fb432c03c 41
Maxime Dupuis 20:05e2c4941285 42 std::vector<uint8_t> Target::generate_message(uint8_t target_number, Result result, uint16_t time_taken_ms) const
Maxime Dupuis 19:377887760869 43 {
Maxime Dupuis 20:05e2c4941285 44 const uint8_t time_taken_msb = time_taken_ms >> 8;
Maxime Dupuis 20:05e2c4941285 45 const uint8_t time_taken_lsb = (time_taken_ms << 8) >> 8;
Maxime Dupuis 19:377887760869 46
Maxime Dupuis 19:377887760869 47 std::vector<uint8_t> message;
Maxime Dupuis 23:fb47bceb61da 48 message.push_back(target_number);
Maxime Dupuis 23:fb47bceb61da 49 message.push_back(result);
Maxime Dupuis 20:05e2c4941285 50 message.push_back(time_taken_msb);
Maxime Dupuis 20:05e2c4941285 51 message.push_back(time_taken_lsb);
Maxime Dupuis 19:377887760869 52
Maxime Dupuis 19:377887760869 53 return message;
Maxime Dupuis 19:377887760869 54 }
Maxime Dupuis 23:fb47bceb61da 55
Maxime Dupuis 23:fb47bceb61da 56 void Target::timeout(const int time_taken_ms)
Maxime Dupuis 23:fb47bceb61da 57 {
Maxime Dupuis 23:fb47bceb61da 58 const std::vector<uint8_t> message = generate_message(target_number, Target::RESULT_MISSED, time_taken_ms);
Maxime Dupuis 23:fb47bceb61da 59 xbee_transmitter.transmit(message);
Maxime Dupuis 23:fb47bceb61da 60 }
Maxime Dupuis 24:801271795da0 61
Maxime Dupuis 24:801271795da0 62 void Target::hit(const int time_taken_ms)
Maxime Dupuis 24:801271795da0 63 {
Maxime Dupuis 24:801271795da0 64 const std::vector<uint8_t> message = generate_message(target_number, Target::RESULT_HIT, time_taken_ms);
Maxime Dupuis 24:801271795da0 65 xbee_transmitter.transmit(message);
Maxime Dupuis 24:801271795da0 66 }