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:
Sun Apr 09 21:22:08 2017 +0000
Revision:
40:1f0a5e5f24f6
Parent:
36:f55ce07292c9
Child:
41:d60c9ced57ab
Fix mallocs in interrupt. Use local variables instead of vector.push_back

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
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);
GaiSensei 36:f55ce07292c9 58 xbee_transmitter.transmit(message, 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);
GaiSensei 36:f55ce07292c9 65 xbee_transmitter.transmit(message, coordinator_address);
Maxime Dupuis 24:801271795da0 66 }