blob: 0bd75c70a96a3f3e131d157e6511cdb2dc2e6e7c [file] [log] [blame]
Tyler Chatow6107aba2017-01-22 01:39:40 +00001#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
22namespace y2017 {
23namespace constants {
24
25// ///// Mutual constants between robots. /////
26const int Values::kZeroingSampleSize;
27
28constexpr double Values::kDrivetrainEncoderRatio;
29
30namespace {
31const uint16_t kCompTeamNumber = 971;
32const uint16_t kPracticeTeamNumber = 9971;
33
34// ///// Dynamic constants. /////
35
36const Values *DoGetValuesForTeam(uint16_t team) {
37 switch (team) {
38 case 1: // for tests
39 return new Values{
Diana Vandenberg223703d2017-01-28 17:39:53 -080040 0.0, // down error
Tyler Chatow6107aba2017-01-22 01:39:40 +000041 };
42 break;
43
44 case kCompTeamNumber:
45 return new Values{
Diana Vandenberg223703d2017-01-28 17:39:53 -080046 0.0, // down error
Tyler Chatow6107aba2017-01-22 01:39:40 +000047 };
48 break;
49
50 case kPracticeTeamNumber:
51 return new Values{
Diana Vandenberg223703d2017-01-28 17:39:53 -080052 0.0, // down error
Tyler Chatow6107aba2017-01-22 01:39:40 +000053 };
54 break;
55
56 default:
57 LOG(FATAL, "unknown team #%" PRIu16 "\n", team);
58 }
59}
60
61const 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
69const Values &GetValues() {
70 static ::aos::Once<const Values> once(DoGetValues);
71 return *once.Get();
72}
73
74const 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