Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 1 | #include "y2017/constants.h" |
| 2 | |
| 3 | #include <math.h> |
| 4 | #include <stdint.h> |
| 5 | #include <inttypes.h> |
| 6 | |
| 7 | #include <map> |
| 8 | |
| 9 | #if __has_feature(address_sanitizer) |
| 10 | #include "sanitizer/lsan_interface.h" |
| 11 | #endif |
| 12 | |
| 13 | #include "aos/common/logging/logging.h" |
| 14 | #include "aos/common/once.h" |
| 15 | #include "aos/common/network/team_number.h" |
| 16 | #include "aos/common/mutex.h" |
| 17 | |
| 18 | #ifndef M_PI |
| 19 | #define M_PI 3.14159265358979323846 |
| 20 | #endif |
| 21 | |
| 22 | namespace y2017 { |
| 23 | namespace constants { |
| 24 | |
| 25 | // ///// Mutual constants between robots. ///// |
| 26 | const int Values::kZeroingSampleSize; |
| 27 | |
| 28 | constexpr double Values::kDrivetrainEncoderRatio; |
| 29 | |
| 30 | namespace { |
| 31 | const uint16_t kCompTeamNumber = 971; |
| 32 | const uint16_t kPracticeTeamNumber = 9971; |
| 33 | |
| 34 | // ///// Dynamic constants. ///// |
| 35 | |
| 36 | const Values *DoGetValuesForTeam(uint16_t team) { |
| 37 | switch (team) { |
| 38 | case 1: // for tests |
| 39 | return new Values{ |
Diana Vandenberg | 223703d | 2017-01-28 17:39:53 -0800 | [diff] [blame] | 40 | 0.0, // down error |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 41 | }; |
| 42 | break; |
| 43 | |
| 44 | case kCompTeamNumber: |
| 45 | return new Values{ |
Diana Vandenberg | 223703d | 2017-01-28 17:39:53 -0800 | [diff] [blame] | 46 | 0.0, // down error |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 47 | }; |
| 48 | break; |
| 49 | |
| 50 | case kPracticeTeamNumber: |
| 51 | return new Values{ |
Diana Vandenberg | 223703d | 2017-01-28 17:39:53 -0800 | [diff] [blame] | 52 | 0.0, // down error |
Tyler Chatow | 6107aba | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 53 | }; |
| 54 | break; |
| 55 | |
| 56 | default: |
| 57 | LOG(FATAL, "unknown team #%" PRIu16 "\n", team); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const Values *DoGetValues() { |
| 62 | uint16_t team = ::aos::network::GetTeamNumber(); |
| 63 | LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team); |
| 64 | return DoGetValuesForTeam(team); |
| 65 | } |
| 66 | |
| 67 | } // namespace |
| 68 | |
| 69 | const Values &GetValues() { |
| 70 | static ::aos::Once<const Values> once(DoGetValues); |
| 71 | return *once.Get(); |
| 72 | } |
| 73 | |
| 74 | const Values &GetValuesForTeam(uint16_t team_number) { |
| 75 | static ::aos::Mutex mutex; |
| 76 | ::aos::MutexLocker locker(&mutex); |
| 77 | |
| 78 | // IMPORTANT: This declaration has to stay after the mutex is locked to avoid |
| 79 | // race conditions. |
| 80 | static ::std::map<uint16_t, const Values *> values; |
| 81 | |
| 82 | if (values.count(team_number) == 0) { |
| 83 | values[team_number] = DoGetValuesForTeam(team_number); |
| 84 | #if __has_feature(address_sanitizer) |
| 85 | __lsan_ignore_object(values[team_number]); |
| 86 | #endif |
| 87 | } |
| 88 | return *values[team_number]; |
| 89 | } |
| 90 | |
| 91 | } // namespace constants |
| 92 | } // namespace y2017 |