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:
GaiSensei
Date:
Wed Apr 05 22:24:50 2017 +0000
Revision:
36:f55ce07292c9
Parent:
24:801271795da0
Child:
40:1f0a5e5f24f6
updated Target manager with send to specefic address xbee code

Who changed what in which revision?

UserRevisionLine numberNew 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
Maxime Dupuis 20:05e2c4941285 43 std::vector<uint8_t> Target::generate_message(uint8_t target_number, Result result, uint16_t time_taken_ms) 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
Maxime Dupuis 19:377887760869 48 std::vector<uint8_t> message;
Maxime Dupuis 23:fb47bceb61da 49 message.push_back(target_number);
Maxime Dupuis 23:fb47bceb61da 50 message.push_back(result);
Maxime Dupuis 20:05e2c4941285 51 message.push_back(time_taken_msb);
Maxime Dupuis 20:05e2c4941285 52 message.push_back(time_taken_lsb);
Maxime Dupuis 19:377887760869 53
Maxime Dupuis 19:377887760869 54 return message;
Maxime Dupuis 19:377887760869 55 }
Maxime Dupuis 23:fb47bceb61da 56
Maxime Dupuis 23:fb47bceb61da 57 void Target::timeout(const int time_taken_ms)
Maxime Dupuis 23:fb47bceb61da 58 {
Maxime Dupuis 23:fb47bceb61da 59 const std::vector<uint8_t> message = generate_message(target_number, Target::RESULT_MISSED, time_taken_ms);
GaiSensei 36:f55ce07292c9 60 xbee_transmitter.transmit(message, coordinator_address);
Maxime Dupuis 23:fb47bceb61da 61 }
Maxime Dupuis 24:801271795da0 62
Maxime Dupuis 24:801271795da0 63 void Target::hit(const int time_taken_ms)
Maxime Dupuis 24:801271795da0 64 {
Maxime Dupuis 24:801271795da0 65 const std::vector<uint8_t> message = generate_message(target_number, Target::RESULT_HIT, time_taken_ms);
GaiSensei 36:f55ce07292c9 66 xbee_transmitter.transmit(message, coordinator_address);
Maxime Dupuis 24:801271795da0 67 }